| plan 9 kernel history: overview | file list | diff list |
1992/0817/port/sysfile.c (diff list | history)
| port/sysfile.c on 1990/0227 | ||
| 1990/0227 | #include "u.h" | |
| 1992/0321 | #include "../port/lib.h" | |
| 1990/0227 | #include "mem.h" #include "dat.h" #include "fns.h" | |
| 1992/0111 | #include "../port/error.h" | |
| 1990/0227 | /* * The sys*() routines needn't poperror() as they return directly to syscall(). */ int | |
| 1991/0705 | newfd(Chan *c) | |
| 1990/0227 | { | |
| 1991/0705 | Fgrp *f = u->p->fgrp; | |
| 1990/0227 | int i; | |
| 1991/0705 | lock(f); | |
| 1990/0227 | for(i=0; i<NFD; i++) | |
| 1991/0705 | if(f->fd[i] == 0){ if(i > f->maxfd) f->maxfd = i; f->fd[i] = c; unlock(f); | |
| 1990/0227 | return i; } | |
| 1991/0705 | unlock(f); | |
| 1992/0114 | exhausted("file descriptors"); | |
| 1992/0720 | return 0; | |
| 1990/0227 | } Chan* | |
| 1991/1011 | fdtochan(int fd, int mode, int chkmnt) | |
| 1990/0227 | { Chan *c; | |
| 1991/0904 | c = 0; | |
| 1991/0705 | if(fd<0 || NFD<=fd || (c = u->p->fgrp->fd[fd])==0) | |
| 1990/11211 | error(Ebadfd); | |
| 1991/1011 | if(chkmnt && (c->flag&CMSG)) | |
| 1992/0114 | error(Ebadusefd); | |
| 1990/1009 | if(mode<0 || c->mode==ORDWR) | |
| 1990/0227 | return c; | |
| 1990/1009 | if((mode&OTRUNC) && c->mode==OREAD) | |
| 1990/11211 | error(Ebadusefd); | |
| 1990/1009 | if((mode&~OTRUNC) != c->mode) | |
| 1991/1011 | error(Ebadusefd); | |
| 1990/0227 | return c; } int openmode(ulong o) { | |
| 1990/08141 | if(o >= (OTRUNC|OCEXEC|ORCLOSE|OEXEC)) | |
| 1990/11211 | error(Ebadarg); | |
| 1990/08141 | o &= ~(OTRUNC|OCEXEC|ORCLOSE); | |
| 1990/0227 | if(o > OEXEC) | |
| 1991/1018 | error(Ebadarg); | |
| 1990/0227 | if(o == OEXEC) return OREAD; return o; } long | |
| 1990/1009 | syspipe(ulong *arg) { int fd[2]; Chan *c[2]; Dev *d; | |
| 1991/0705 | Fgrp *f = u->p->fgrp; | |
| 1990/1009 | validaddr(arg[0], 2*BY2WD, 1); evenaddr(arg[0]); d = &devtab[devno('|', 0)]; c[0] = (*d->attach)(0); c[1] = 0; fd[0] = -1; fd[1] = -1; if(waserror()){ close(c[0]); if(c[1]) close(c[1]); if(fd[0] >= 0) | |
| 1991/0705 | f->fd[fd[0]]=0; | |
| 1990/1009 | if(fd[1] >= 0) | |
| 1991/0705 | f->fd[fd[1]]=0; | |
| 1990/1009 | nexterror(); } c[1] = (*d->clone)(c[0], 0); (*d->walk)(c[0], "data"); (*d->walk)(c[1], "data1"); c[0] = (*d->open)(c[0], ORDWR); c[1] = (*d->open)(c[1], ORDWR); | |
| 1991/0705 | fd[0] = newfd(c[0]); fd[1] = newfd(c[1]); | |
| 1990/1009 | ((long*)arg[0])[0] = fd[0]; ((long*)arg[0])[1] = fd[1]; poperror(); return 0; } long | |
| 1990/0227 | sysdup(ulong *arg) { int fd; Chan *c, *oc; | |
| 1991/0705 | Fgrp *f = u->p->fgrp; | |
| 1990/0227 | /* * Close after dup'ing, so date > #d/1 works */ | |
| 1991/1011 | c = fdtochan(arg[0], -1, 0); | |
| 1990/0227 | fd = arg[1]; | |
| 1991/0221 | if(fd != -1){ if(fd<0 || NFD<=fd) error(Ebadfd); | |
| 1991/0705 | lock(f); if(fd > f->maxfd) f->maxfd = fd; incref(c); oc = f->fd[fd]; f->fd[fd] = c; unlock(f); if(oc) close(oc); | |
| 1991/0706 | }else{ | |
| 1991/0705 | if(waserror()) { close(c); nexterror(); } incref(c); fd = newfd(c); poperror(); | |
| 1990/0227 | } | |
| 1991/0705 | ||
| 1990/0227 | return fd; } long sysopen(ulong *arg) { int fd; | |
| 1991/0705 | Chan *c = 0; | |
| 1990/0227 | openmode(arg[1]); /* error check only */ | |
| 1991/0706 | if(waserror()){ | |
| 1991/0705 | if(c) close(c); nexterror(); } | |
| 1990/0227 | validaddr(arg[0], 1, 0); c = namec((char*)arg[0], Aopen, arg[1], 0); | |
| 1991/0705 | fd = newfd(c); poperror(); | |
| 1990/0227 | return fd; } void | |
| 1991/0705 | fdclose(int fd, int flag) | |
| 1990/0227 | { int i; | |
| 1991/0705 | Chan *c; Fgrp *f = u->p->fgrp; | |
| 1990/0227 | ||
| 1991/0705 | lock(f); c = f->fd[fd]; | |
| 1992/0817 | if(c == 0){ /* can happen for users with shared fd tables */ unlock(f); return; } | |
| 1991/0705 | if(flag){ if(c==0 || !(c->flag&flag)){ unlock(f); return; } } f->fd[fd] = 0; if(fd == f->maxfd) for(i=fd; --i>=0 && f->fd[i]==0; ) f->maxfd = i; unlock(f); close(c); | |
| 1990/0227 | } long sysclose(ulong *arg) { | |
| 1991/1011 | fdtochan(arg[0], -1, 0); | |
| 1991/0705 | fdclose(arg[0], 0); | |
| 1990/0227 | return 0; } long unionread(Chan *c, void *va, long n) { long nr; | |
| 1992/0720 | Chan *nc; Pgrp *pg; | |
| 1990/0227 | ||
| 1992/0720 | pg = u->p->pgrp; | |
| 1991/1011 | rlock(&pg->ns); for(;;) { if(waserror()) { runlock(&pg->ns); nexterror(); } nc = clone(c->mnt->to, 0); poperror(); | |
| 1992/0720 | if(c->mountid != c->mnt->mountid) { | |
| 1991/1011 | pprint("unionread: changed underfoot?\n"); runlock(&pg->ns); close(nc); return 0; } | |
| 1992/0720 | /* Error causes component of union to be skipped */ if(waserror()) { | |
| 1991/1011 | close(nc); | |
| 1992/0720 | goto next; | |
| 1991/1011 | } nc = (*devtab[nc->type].open)(nc, OREAD); nc->offset = c->offset; nr = (*devtab[nc->type].read)(nc, va, n, nc->offset); /* devdirread e.g. changes it */ c->offset = nc->offset; poperror(); | |
| 1990/0227 | close(nc); | |
| 1991/1011 | if(nr > 0) { runlock(&pg->ns); return nr; } | |
| 1992/0720 | /* Advance to next element */ next: | |
| 1991/1011 | c->mnt = c->mnt->next; if(c->mnt == 0) break; c->mountid = c->mnt->mountid; c->offset = 0; | |
| 1990/0227 | } | |
| 1991/1011 | runlock(&pg->ns); return 0; | |
| 1990/0227 | } long sysread(ulong *arg) { Chan *c; long n; | |
| 1991/1011 | c = fdtochan(arg[0], OREAD, 1); | |
| 1990/1004 | validaddr(arg[1], arg[2], 1); | |
| 1991/0411 | qlock(&c->rdl); | |
| 1991/0501 | ||
| 1990/0227 | if(waserror()){ | |
| 1991/0411 | qunlock(&c->rdl); | |
| 1990/0227 | nexterror(); } n = arg[2]; | |
| 1990/11211 | if(c->qid.path & CHDIR){ | |
| 1990/0227 | n -= n%DIRLEN; if(c->offset%DIRLEN || n==0) | |
| 1992/0114 | error(Etoosmall); | |
| 1990/0227 | } | |
| 1991/1011 | if((c->qid.path&CHDIR) && c->mnt) | |
| 1990/0227 | n = unionread(c, (void*)arg[1], n); else | |
| 1991/0411 | n = (*devtab[c->type].read)(c, (void*)arg[1], n, c->offset); qunlock(&c->rdl); | |
| 1991/1101 | poperror(); lock(&c->offl); c->offset += n; unlock(&c->offl); | |
| 1990/0227 | return n; } long syswrite(ulong *arg) { Chan *c; long n; | |
| 1991/1011 | c = fdtochan(arg[0], OWRITE, 1); | |
| 1990/0227 | validaddr(arg[1], arg[2], 0); | |
| 1991/0411 | qlock(&c->wrl); | |
| 1991/0501 | ||
| 1990/0227 | if(waserror()){ | |
| 1991/0411 | qunlock(&c->wrl); | |
| 1990/0227 | nexterror(); } | |
| 1990/11211 | if(c->qid.path & CHDIR) error(Eisdir); | |
| 1991/0411 | n = (*devtab[c->type].write)(c, (void*)arg[1], arg[2], c->offset); qunlock(&c->wrl); | |
| 1991/1101 | poperror(); lock(&c->offl); c->offset += n; unlock(&c->offl); | |
| 1990/0227 | return n; } long sysseek(ulong *arg) { Chan *c; char buf[DIRLEN]; Dir dir; long off; | |
| 1991/1011 | c = fdtochan(arg[0], -1, 1); | |
| 1990/11211 | if(c->qid.path & CHDIR) error(Eisdir); | |
| 1991/0319 | if(devchar[c->type] == '|') error(Eisstream); | |
| 1991/1101 | off = 0; | |
| 1990/0227 | switch(arg[2]){ case 0: | |
| 1991/1101 | off = c->offset = arg[1]; | |
| 1990/0227 | break; case 1: | |
| 1991/1101 | lock(&c->offl); /* lock for read/write update */ | |
| 1990/0227 | c->offset += (long)arg[1]; | |
| 1991/1101 | off = c->offset; unlock(&c->offl); | |
| 1990/0227 | break; case 2: (*devtab[c->type].stat)(c, buf); convM2D(buf, &dir); c->offset = dir.length + (long)arg[1]; | |
| 1991/1101 | off = c->offset; | |
| 1990/0227 | break; } return off; } long sysfstat(ulong *arg) { Chan *c; validaddr(arg[1], DIRLEN, 1); evenaddr(arg[1]); | |
| 1991/1011 | c = fdtochan(arg[0], -1, 0); | |
| 1990/0227 | (*devtab[c->type].stat)(c, (char*)arg[1]); return 0; } long sysstat(ulong *arg) { Chan *c; validaddr(arg[1], DIRLEN, 1); evenaddr(arg[1]); validaddr(arg[0], 1, 0); c = namec((char*)arg[0], Aaccess, 0, 0); if(waserror()){ close(c); nexterror(); } (*devtab[c->type].stat)(c, (char*)arg[1]); poperror(); close(c); return 0; } long syschdir(ulong *arg) { Chan *c; validaddr(arg[0], 1, 0); c = namec((char*)arg[0], Atodir, 0, 0); close(u->dot); u->dot = c; return 0; } long bindmount(ulong *arg, int ismount) { Chan *c0, *c1; ulong flag; long ret; | |
| 1992/0711 | int fd; | |
| 1990/0227 | struct{ Chan *chan; char *spec; | |
| 1992/0312 | char *serv; | |
| 1990/0227 | }bogus; flag = arg[2]; | |
| 1992/0607 | fd = arg[0]; | |
| 1990/0227 | if(flag>MMASK || (flag&MORDER)==(MBEFORE|MAFTER)) | |
| 1990/11211 | error(Ebadarg); | |
| 1990/0227 | if(ismount){ | |
| 1992/0607 | bogus.chan = fdtochan(fd, 2, 0); | |
| 1990/11211 | validaddr(arg[3], 1, 0); | |
| 1991/0615 | if(vmemchr((char*)arg[3], '\0', NAMELEN) == 0) error(Ebadarg); | |
| 1990/0227 | bogus.spec = (char*)arg[3]; | |
| 1990/11211 | validaddr(arg[4], 1, 0); | |
| 1991/0615 | if(vmemchr((char*)arg[4], '\0', NAMELEN) == 0) error(Ebadarg); | |
| 1992/0312 | bogus.serv = (char*)arg[4]; | |
| 1990/0227 | ret = devno('M', 0); c0 = (*devtab[ret].attach)((char*)&bogus); }else{ validaddr(arg[0], 1, 0); c0 = namec((char*)arg[0], Aaccess, 0, 0); } if(waserror()){ close(c0); nexterror(); } validaddr(arg[1], 1, 0); c1 = namec((char*)arg[1], Amount, 0, 0); if(waserror()){ close(c1); nexterror(); } | |
| 1990/11211 | if((c0->qid.path^c1->qid.path) & CHDIR) | |
| 1992/0114 | error(Emount); | |
| 1990/11211 | if(flag && !(c0->qid.path&CHDIR)) | |
| 1992/0114 | error(Emount); | |
| 1990/0227 | ret = mount(c0, c1, flag); | |
| 1991/0614 | poperror(); | |
| 1990/0227 | close(c1); | |
| 1991/0614 | poperror(); close(c0); | |
| 1991/0705 | if(ismount) | |
| 1992/0607 | fdclose(fd, 0); | |
| 1990/0227 | return ret; } long sysbind(ulong *arg) { return bindmount(arg, 0); } long sysmount(ulong *arg) { return bindmount(arg, 1); } long | |
| 1991/1011 | sysunmount(ulong *arg) { Chan *cmount, *cmounted; cmounted = 0; validaddr(arg[1], 1, 0); cmount = namec((char *)arg[1], Amount, 0, 0); if(arg[0]) { if(waserror()) { close(cmount); nexterror(); } validaddr(arg[0], 1, 0); cmounted = namec((char*)arg[0], Aaccess, 0, 0); poperror(); } if(waserror()) { close(cmount); if(cmounted) close(cmounted); nexterror(); } unmount(cmount, cmounted); close(cmount); if(cmounted) close(cmounted); poperror(); return 0; } long | |
| 1990/0227 | syscreate(ulong *arg) { int fd; | |
| 1991/0705 | Chan *c = 0; | |
| 1990/0227 | openmode(arg[1]); /* error check only */ | |
| 1991/0705 | if(waserror()) { if(c) close(c); nexterror(); } | |
| 1990/0227 | validaddr(arg[0], 1, 0); c = namec((char*)arg[0], Acreate, arg[1], arg[2]); | |
| 1991/0705 | fd = newfd(c); poperror(); | |
| 1990/0227 | return fd; } long sysremove(ulong *arg) { Chan *c; validaddr(arg[0], 1, 0); c = namec((char*)arg[0], Aaccess, 0, 0); if(waserror()){ | |
| 1990/0703 | c->type = 0; /* see below */ | |
| 1990/0227 | close(c); nexterror(); } (*devtab[c->type].remove)(c); /* * Remove clunks the fid, but we need to recover the Chan * so fake it up. rootclose() is known to be a nop. */ c->type = 0; | |
| 1991/0614 | poperror(); | |
| 1990/0227 | close(c); return 0; } long syswstat(ulong *arg) { Chan *c; validaddr(arg[1], DIRLEN, 0); | |
| 1991/1107 | nameok((char*)arg[1]); | |
| 1990/0227 | validaddr(arg[0], 1, 0); c = namec((char*)arg[0], Aaccess, 0, 0); if(waserror()){ close(c); nexterror(); } (*devtab[c->type].wstat)(c, (char*)arg[1]); poperror(); close(c); return 0; } long sysfwstat(ulong *arg) { Chan *c; validaddr(arg[1], DIRLEN, 0); | |
| 1991/1107 | nameok((char*)arg[1]); | |
| 1991/1011 | c = fdtochan(arg[0], -1, 1); | |
| 1990/0227 | (*devtab[c->type].wstat)(c, (char*)arg[1]); return 0; } | |