commit 7e254b64801e42ff89392a157f1f1a822099ce1c
parent a169e5e8b958395249861cc6d64b191ea3164ef9
Author: Roberto E. Vargas Caballero <roberto.vargas@midokura.com>
Date: Sat, 26 Nov 2022 00:31:04 +0100
os9: Remove direct access to errno
As we are moving to be a kernel we cannot use an errno variable.
We have to allocate a field in the Task structure that will be used
later in the syscall implementation to return the errno value.
Diffstat:
9 files changed, 66 insertions(+), 55 deletions(-)
diff --git a/include/os9/os9.h b/include/os9/os9.h
@@ -224,6 +224,8 @@ struct task {
void *entry;
+ int errno;
+
int prio;
int baseprio;
int retainprio;
@@ -274,6 +276,7 @@ extern void locktask(Task *);
extern void unlocktask(Task *);
extern void sleep(Rendez *, int (*cond)(void *), void *);
extern void wakeup(Rendez *);
+extern void seterror(int);
/* alloc.c */
extern void *alloc(size_t);
diff --git a/src/os9/alloc.c b/src/os9/alloc.c
@@ -46,7 +46,7 @@ allocb(void)
lock(&bufpool.m);
if (!bufpool.list) {
- errno = ENOMEM;
+ seterror(ENOMEM);
} else {
bp = bufpool.list;
bufpool.list = bp->next;
@@ -65,7 +65,7 @@ alloc(size_t size)
void *bp;
if (size == 0) {
- errno = EINVAL;
+ seterror(EINVAL);
return NULL;
}
@@ -75,7 +75,7 @@ alloc(size_t size)
if (used > PAGESIZE*HEAPSIZ - size) {
bp = NULL;
- errno = ENOMEM;
+ seterror(ENOMEM);
} else {
bp = (char *) heap + used;
used += size;
diff --git a/src/os9/dev/dev.c b/src/os9/dev/dev.c
@@ -53,7 +53,7 @@ newchan(unsigned char type)
return c;
err1:
- errno = ENOMEM;
+ seterror(ENOMEM);
err2:
unlock(&fds->m);
return NULL;
@@ -198,7 +198,7 @@ fd2chan(int fd)
return c;
err:
- errno = EBADF;
+ seterror(EBADF);
return NULL;
}
@@ -214,7 +214,7 @@ validmode(int mode)
return 1;
err:
- errno = EINVAL;
+ seterror(EINVAL);
return 0;
}
@@ -232,7 +232,7 @@ next(char *s, char *elem)
if (*s != '\0') {
while (*t != '/' && *t != '\0') {
if (n == NAMELEN) {
- errno = EINVAL;
+ seterror(EINVAL);
return NULL;
}
elem[n++] = *t++;
@@ -252,7 +252,7 @@ devtype(int c)
for (i = 0, dp = devtab; *dp && (*dp)->id != c; ++dp)
i++;
if (*dp == NULL) {
- errno = ENODEV;
+ seterror(ENODEV);
return -1;
}
return i;
@@ -381,7 +381,7 @@ namec(char *name, int mode)
return c;
noent:
- errno = ENOENT;
+ seterror(ENOENT);
err:
if (c)
delchan(c);
@@ -422,7 +422,7 @@ devwalk(Chan *c, char *name, Dirtab *tab, int ntab, Devgen *gen)
case 0:
continue;
case -1:
- errno = ENOENT;
+ seterror(ENOENT);
return -1;
case 1:
if (strcmp(name, dir.name))
@@ -454,7 +454,7 @@ dirread(Chan *c,
c->offset += DIRLEN;
n = dirtop9(&dir, buf + cnt, nbytes);
if (n < 0) {
- errno = EINVAL;
+ seterror(EINVAL);
return (cnt > 0) ? cnt : -1;
}
nbytes -= n;
@@ -562,7 +562,7 @@ devstat(Chan *dirc, char *file,
for (i = 0; ; i++) {
switch ((*gen)(dirc, tab, ntab, i, &dir)) {
case 0:
- errno = ENOENT;
+ seterror(ENOENT);
case -1:
r = -1;
goto leave;
@@ -593,13 +593,13 @@ stat(char *path, void *buf, int n)
char *p, dirname[PATHLEN];
if (n < DIRLEN) {
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
len = strlen(path);
if (len + 1 > sizeof(dirname)) {
- errno = ENAMETOOLONG;
+ seterror(ENAMETOOLONG);
return -1;
}
memcpy(dirname, path, len);
@@ -610,7 +610,7 @@ stat(char *path, void *buf, int n)
p = memrchr(dirname, '/', p - dirname);
if (!p) {
- errno = ENOENT;
+ seterror(ENOENT);
return -1;
}
dirname[p - dirname + 1] = '\0';
@@ -631,7 +631,7 @@ chanread(Chan *c, void *buf, int n)
if (c->qid.type == CHDIR && n < DIRLEN) {
r = -1;
- errno = EINVAL;
+ seterror(EINVAL);
} else {
r = devtab[c->type]->read(c, buf, n);
}
@@ -660,7 +660,7 @@ chanwrite(Chan *c, void *buf, int n)
if (c->qid.type == CHDIR) {
r = -1;
- errno = EISDIR;
+ seterror(EISDIR);
} else {
r = devtab[c->type]->write(c, buf, n);
}
@@ -692,7 +692,7 @@ seek(int fd, long off, int whence)
return -1;
if (c->qid.type == CHDIR)
- errno = EISDIR;
+ seterror(EISDIR);
else
r = devtab[c->type]->seek(c, off, whence);
@@ -710,7 +710,7 @@ fsync(int fd)
return -1;
if (c->qid.type == CHDIR)
- errno = EISDIR;
+ seterror(EISDIR);
else
r = devtab[c->type]->sync(c, SYNCDEV);
@@ -734,20 +734,20 @@ sync(void)
Chan *
deverrmount(Chan *c, char *spec)
{
- errno = EINVAL;
+ seterror(EINVAL);
return NULL;
}
int
deverrwrite(Chan *c, void *buf, int n)
{
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
int deverrseek(Chan *c, long off, int whence)
{
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
@@ -790,7 +790,7 @@ addmntpoint(Chan *c, char *new)
goto err0;
if (cn->qid.type != CHDIR) {
- errno = ENOTDIR;
+ seterror(ENOTDIR);
goto err1;
}
@@ -808,7 +808,7 @@ addmntpoint(Chan *c, char *new)
return 0;
err2:
- errno = ENOMEM;
+ seterror(ENOMEM);
unlock(&ns->m);
err1:
delchan(cn);
diff --git a/src/os9/dev/devar.c b/src/os9/dev/devar.c
@@ -44,7 +44,7 @@ gethdr(Chan *c, struct ar_hdr *hdr)
return n;
if (n != sizeof(*hdr) || strncmp(hdr->ar_fmag, ARFMAG, 2) != 0) {
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
@@ -83,7 +83,7 @@ argen(Chan *c, Dirtab *tab, int ntab, int n, Dir *dir)
}
if ((len = strlen(hdr.ar_name)) >= NAMELEN) {
- errno = ENAMETOOLONG;
+ seterror(ENAMETOOLONG);
return -1;
}
@@ -133,7 +133,7 @@ arread(Chan *c, void *buf, int n)
return 0;
if (n < 0) {
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
if (n > size - c->offset)
@@ -161,7 +161,7 @@ armount(Chan *c, char *spec)
struct arfile *ar;
if (nars == NR_ARS) {
- errno = ENOMEM;
+ seterror(ENOMEM);
return NULL;
}
ar = &archives[nars];
@@ -177,7 +177,7 @@ armount(Chan *c, char *spec)
if (r < 0)
goto err;
if (r != SARMAG || strncmp(buf, ARMAG, SARMAG)) {
- errno = EINVAL;
+ seterror(EINVAL);
goto err;
}
@@ -201,7 +201,7 @@ armount(Chan *c, char *spec)
break;
}
}
- errno = ENOMEM;
+ seterror(ENOMEM);
err:
chanclose(cspec);
diff --git a/src/os9/dev/devblk.c b/src/os9/dev/devblk.c
@@ -75,7 +75,7 @@ repeat:
if (bp->flags & BDIRTY) {
dev = blks[bp->devno];
if (dev->phy->bwrite(dev, bp->block, bp->ptr) < 0) {
- errno = EIO;
+ seterror(EIO);
bp->flags |= BERROR;
goto return_buffer;
}
@@ -118,7 +118,7 @@ brelse(Buffer *bp)
if ((bp->flags & BDIRTY) != 0) {
dev = blks[bp->devno];
if (dev->phy->bwrite(dev, bp->block, bp->ptr) < 0) {
- errno = EIO;
+ seterror(EIO);
bp->flags |= BERROR;
r = -1;
}
@@ -194,7 +194,7 @@ rawwrite(Chan *c, void *buf, int nbytes)
bp = getblk(c->dev, blk);
switch (bp->flags & (BVALID | BERROR)) {
case 0:
- errno = EFBIG;
+ seterror(EFBIG);
case BVALID|BERROR:
case BERROR:
brelse(bp);
@@ -242,7 +242,7 @@ blksync(Chan *c, int what)
dev = blks[bp->devno];
if (dev->phy->bwrite(dev, bp->block, bp->ptr) < 0) {
r = -1;
- errno = EIO;
+ seterror(EIO);
bp->flags |= BERROR;
} else {
bp->flags &= BDIRTY;
diff --git a/src/os9/dev/devcons.c b/src/os9/dev/devcons.c
@@ -96,7 +96,7 @@ flushraw(char *buf, int n)
if (w < 0) {
r = -1;
} else if (w != n) {
- errno = EIO;
+ seterror(EIO);
r = -1;
}
}
@@ -135,7 +135,7 @@ static int
consaddin(struct conscmd *cmd, char *s)
{
if (in) {
- errno = ENOMEM;
+ seterror(ENOMEM);
return -1;
}
@@ -156,7 +156,7 @@ consaddout(struct conscmd *cmd, char *s)
return 0;
}
}
- errno = ENOMEM;
+ seterror(ENOMEM);
return -1;
}
@@ -164,7 +164,7 @@ static int
consdelin(struct conscmd *cmd, char *s)
{
if (!in || strcmp(s, inname)) {
- errno = ENOENT;
+ seterror(ENOENT);
return -1;
}
@@ -187,7 +187,7 @@ consdelout(struct conscmd *cmd, char *s)
return 0;
}
}
- errno = ENOENT;
+ seterror(ENOENT);
return -1;
}
@@ -201,7 +201,7 @@ consmode(struct conscmd *cmd, char *s)
} else if(!strcmp(s, "off")) {
mode = 0;
} else {
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
@@ -210,7 +210,7 @@ consmode(struct conscmd *cmd, char *s)
} else if (!strcmp(cmd->name, "echo")) {
echof = mode;
} else {
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
@@ -228,7 +228,7 @@ conswrite(Chan *c, void *buf, int n)
return conswriteraw(buf, n);
case Qctl:
if (tokenize(buf, n, tokens, 2) != 2) {
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
@@ -237,7 +237,7 @@ conswrite(Chan *c, void *buf, int n)
return (*cmd->fn)(cmd, tokens[1]);
}
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
default:
panic("conswrite");
@@ -342,7 +342,7 @@ fillbuffer(void)
break;
}
if (ch == EOF && head == 0) {
- errno = EIO;
+ seterror(EIO);
return -1;
}
@@ -374,7 +374,7 @@ consread(Chan *c, void *buf, int n)
return dirread(c, buf, n, dirtab, NELEM(dirtab), devgen);
case Qraw:
if (!in) {
- errno = ENOENT;
+ seterror(ENOENT);
return -1;
}
if (head == 0) {
diff --git a/src/os9/dev/devfip.c b/src/os9/dev/devfip.c
@@ -53,7 +53,7 @@ getntry(Chan *c, struct fipntry *ntry)
return n;
if (n != sizeof(buf)) {
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
@@ -73,7 +73,7 @@ getntry(Chan *c, struct fipntry *ntry)
LLONG(ntry->flags, buf, n, sizeof(buf));
if (ntry->size > LONG_MAX || ntry->offset_address > LONG_MAX) {
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
@@ -221,7 +221,7 @@ fipread(Chan *c, void *buf, int n)
return 0;
if (n < 0) {
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
if (n > size - c->offset)
@@ -249,7 +249,7 @@ fipmount(Chan *c, char *spec)
struct fipfile *fip;
if (nfips == NR_FIPS) {
- errno = ENOMEM;
+ seterror(ENOMEM);
return NULL;
}
fip = &archives[nfips];
@@ -267,7 +267,7 @@ fipmount(Chan *c, char *spec)
n = 0;
LONG(name, buf, n, sizeof(buf));
if (r != STOC_HEADER_NAME || name != TOC_HEADER_NAME) {
- errno = EINVAL;
+ seterror(EINVAL);
goto err;
}
@@ -286,7 +286,7 @@ fipmount(Chan *c, char *spec)
break;
}
}
- errno = ENOMEM;
+ seterror(ENOMEM);
err:
chanclose(cspec);
diff --git a/src/os9/dev/devproc.c b/src/os9/dev/devproc.c
@@ -98,6 +98,7 @@ taskattrread(Chan *c, void *buf, int n)
sizeof(tmp),
"pid: %d\n"
"entry: %p\n"
+ "errno: %d\n"
"stack size: %zd\n"
"base prio: %d\n"
"period: %ld\n"
@@ -105,6 +106,7 @@ taskattrread(Chan *c, void *buf, int n)
"deadline: %ld\n",
tp->pid,
tp->entry,
+ tp->errno,
tp->stack,
tp->prio,
tp->period,
diff --git a/src/os9/proc.c b/src/os9/proc.c
@@ -32,7 +32,7 @@ getntask(int n, Task **tpp)
Task *tp;
if (n >= NR_TASKS) {
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
tp = &tasktab[n];
@@ -78,7 +78,7 @@ newslot(void)
}
if (tp == &tasktab[NR_TASKS]) {
- errno = ENOMEM;
+ seterror(ENOMEM);
return NULL;
}
@@ -125,7 +125,7 @@ newtask(void)
return tp;
err:
unlock(&m);
- errno = ENOMEM;
+ seterror(ENOMEM);
return NULL;
}
@@ -211,7 +211,7 @@ rfork(int flags)
if ((flags & (RFMEM|RFPROC)) == 0
|| (flags & (RFNAMEG|RFCNAMEG)) == (RFNAMEG|RFCNAMEG)
|| (flags & (RFFDG|RFCFDG)) == (RFFDG|RFCFDG)) {
- errno = EINVAL;
+ seterror(EINVAL);
return -1;
}
@@ -404,6 +404,12 @@ inittask(void)
return tp;
}
+void
+seterror(int n)
+{
+ proc->errno = n;
+}
+
/*
* sleep() and wakeup() are highly inspired in
* the paper 'Process Sleep and Wakeup on a