12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244 |
- #include "u.h"
- #include "lib.h"
- #include "dat.h"
- #include "fns.h"
- #include "error.h"
- #include "user.h"
- #undef open
- #undef mount
- #undef read
- #undef write
- #undef seek
- #undef stat
- #undef wstat
- #undef remove
- #undef close
- #undef fstat
- #undef fwstat
- /*
- * The sys*() routines needn't poperror() as they return directly to syscall().
- */
- static void
- unlockfgrp(Fgrp *f)
- {
- int ex;
- ex = f->exceed;
- f->exceed = 0;
- unlock(&f->ref.lk);
- if(ex)
- pprint("warning: process exceeds %d file descriptors\n", ex);
- }
- int
- growfd(Fgrp *f, int fd) /* fd is always >= 0 */
- {
- Chan **newfd, **oldfd;
- if(fd < f->nfd)
- return 0;
- if(fd >= f->nfd+DELTAFD)
- return -1; /* out of range */
- /*
- * Unbounded allocation is unwise; besides, there are only 16 bits
- * of fid in 9P
- */
- if(f->nfd >= 5000){
- Exhausted:
- print("no free file descriptors\n");
- return -1;
- }
- newfd = malloc((f->nfd+DELTAFD)*sizeof(Chan*));
- if(newfd == 0)
- goto Exhausted;
- oldfd = f->fd;
- memmove(newfd, oldfd, f->nfd*sizeof(Chan*));
- f->fd = newfd;
- free(oldfd);
- f->nfd += DELTAFD;
- if(fd > f->maxfd){
- if(fd/100 > f->maxfd/100)
- f->exceed = (fd/100)*100;
- f->maxfd = fd;
- }
- return 1;
- }
- /*
- * this assumes that the fgrp is locked
- */
- int
- findfreefd(Fgrp *f, int start)
- {
- int fd;
- for(fd=start; fd<f->nfd; fd++)
- if(f->fd[fd] == 0)
- break;
- if(fd >= f->nfd && growfd(f, fd) < 0)
- return -1;
- return fd;
- }
- int
- newfd(Chan *c)
- {
- int fd;
- Fgrp *f;
- f = up->fgrp;
- lock(&f->ref.lk);
- fd = findfreefd(f, 0);
- if(fd < 0){
- unlockfgrp(f);
- return -1;
- }
- if(fd > f->maxfd)
- f->maxfd = fd;
- f->fd[fd] = c;
- unlockfgrp(f);
- return fd;
- }
- int
- newfd2(int fd[2], Chan *c[2])
- {
- Fgrp *f;
- f = up->fgrp;
- lock(&f->ref.lk);
- fd[0] = findfreefd(f, 0);
- if(fd[0] < 0){
- unlockfgrp(f);
- return -1;
- }
- fd[1] = findfreefd(f, fd[0]+1);
- if(fd[1] < 0){
- unlockfgrp(f);
- return -1;
- }
- if(fd[1] > f->maxfd)
- f->maxfd = fd[1];
- f->fd[fd[0]] = c[0];
- f->fd[fd[1]] = c[1];
- unlockfgrp(f);
- return 0;
- }
- Chan*
- fdtochan(int fd, int mode, int chkmnt, int iref)
- {
- Chan *c;
- Fgrp *f;
- c = 0;
- f = up->fgrp;
- lock(&f->ref.lk);
- if(fd<0 || f->nfd<=fd || (c = f->fd[fd])==0) {
- unlock(&f->ref.lk);
- error(Ebadfd);
- }
- if(iref)
- incref(&c->ref);
- unlock(&f->ref.lk);
- if(chkmnt && (c->flag&CMSG)) {
- if(iref)
- cclose(c);
- error(Ebadusefd);
- }
- if(mode<0 || c->mode==ORDWR)
- return c;
- if((mode&OTRUNC) && c->mode==OREAD) {
- if(iref)
- cclose(c);
- error(Ebadusefd);
- }
- if((mode&~OTRUNC) != c->mode) {
- if(iref)
- cclose(c);
- error(Ebadusefd);
- }
- return c;
- }
- int
- openmode(ulong o)
- {
- o &= ~(OTRUNC|OCEXEC|ORCLOSE);
- if(o > OEXEC)
- error(Ebadarg);
- if(o == OEXEC)
- return OREAD;
- return o;
- }
- long
- _sysfd2path(int fd, char *buf, uint nbuf)
- {
- Chan *c;
- c = fdtochan(fd, -1, 0, 1);
- if(c->name == nil)
- snprint(buf, nbuf, "<null>");
- else
- snprint(buf, nbuf, "%s", c->name->s);
- cclose(c);
- return 0;
- }
- long
- _syspipe(int fd[2])
- {
- Chan *c[2];
- Dev *d;
- static char *datastr[] = {"data", "data1"};
- d = devtab[devno('|', 0)];
- c[0] = namec("#|", Atodir, 0, 0);
- c[1] = 0;
- fd[0] = -1;
- fd[1] = -1;
- if(waserror()){
- cclose(c[0]);
- if(c[1])
- cclose(c[1]);
- nexterror();
- }
- c[1] = cclone(c[0]);
- if(walk(&c[0], datastr+0, 1, 1, nil) < 0)
- error(Egreg);
- if(walk(&c[1], datastr+1, 1, 1, nil) < 0)
- error(Egreg);
- c[0] = d->open(c[0], ORDWR);
- c[1] = d->open(c[1], ORDWR);
- if(newfd2(fd, c) < 0)
- error(Enofd);
- poperror();
- return 0;
- }
- long
- _sysdup(int fd0, int fd1)
- {
- int fd;
- Chan *c, *oc;
- Fgrp *f = up->fgrp;
- /*
- * Close after dup'ing, so date > #d/1 works
- */
- c = fdtochan(fd0, -1, 0, 1);
- fd = fd1;
- if(fd != -1){
- lock(&f->ref.lk);
- if(fd<0 || growfd(f, fd)<0) {
- unlockfgrp(f);
- cclose(c);
- error(Ebadfd);
- }
- if(fd > f->maxfd)
- f->maxfd = fd;
- oc = f->fd[fd];
- f->fd[fd] = c;
- unlockfgrp(f);
- if(oc)
- cclose(oc);
- }else{
- if(waserror()) {
- cclose(c);
- nexterror();
- }
- fd = newfd(c);
- if(fd < 0)
- error(Enofd);
- poperror();
- }
- return fd;
- }
- long
- _sysopen(char *name, int mode)
- {
- int fd;
- Chan *c = 0;
- openmode(mode); /* error check only */
- if(waserror()){
- if(c)
- cclose(c);
- nexterror();
- }
- c = namec(name, Aopen, mode, 0);
- fd = newfd(c);
- if(fd < 0)
- error(Enofd);
- poperror();
- return fd;
- }
- void
- fdclose(int fd, int flag)
- {
- int i;
- Chan *c;
- Fgrp *f = up->fgrp;
- lock(&f->ref.lk);
- c = f->fd[fd];
- if(c == 0){
- /* can happen for users with shared fd tables */
- unlock(&f->ref.lk);
- return;
- }
- if(flag){
- if(c==0 || !(c->flag&flag)){
- unlock(&f->ref.lk);
- return;
- }
- }
- f->fd[fd] = 0;
- if(fd == f->maxfd)
- for(i=fd; --i>=0 && f->fd[i]==0; )
- f->maxfd = i;
- unlock(&f->ref.lk);
- cclose(c);
- }
- long
- _sysclose(int fd)
- {
- fdtochan(fd, -1, 0, 0);
- fdclose(fd, 0);
- return 0;
- }
- long
- unionread(Chan *c, void *va, long n)
- {
- int i;
- long nr;
- Mhead *m;
- Mount *mount;
- qlock(&c->umqlock);
- m = c->umh;
- rlock(&m->lock);
- mount = m->mount;
- /* bring mount in sync with c->uri and c->umc */
- for(i = 0; mount != nil && i < c->uri; i++)
- mount = mount->next;
- nr = 0;
- while(mount != nil) {
- /* Error causes component of union to be skipped */
- if(mount->to && !waserror()) {
- if(c->umc == nil){
- c->umc = cclone(mount->to);
- c->umc = devtab[c->umc->type]->open(c->umc, OREAD);
- }
-
- nr = devtab[c->umc->type]->read(c->umc, va, n, c->umc->offset);
- c->umc->offset += nr;
- poperror();
- }
- if(nr > 0)
- break;
- /* Advance to next element */
- c->uri++;
- if(c->umc) {
- cclose(c->umc);
- c->umc = nil;
- }
- mount = mount->next;
- }
- runlock(&m->lock);
- qunlock(&c->umqlock);
- return nr;
- }
- static long
- kread(int fd, void *buf, long n, vlong *offp)
- {
- int dir;
- Chan *c;
- vlong off;
- c = fdtochan(fd, OREAD, 1, 1);
- if(waserror()) {
- cclose(c);
- nexterror();
- }
- dir = c->qid.type&QTDIR;
- /*
- * The offset is passed through on directories, normally. sysseek complains but
- * pread is used by servers and e.g. exportfs that shouldn't need to worry about this issue.
- */
- if(offp == nil) /* use and maintain channel's offset */
- off = c->offset;
- else
- off = *offp;
- if(off < 0)
- error(Enegoff);
- if(dir && c->umh)
- n = unionread(c, buf, n);
- else
- n = devtab[c->type]->read(c, buf, n, off);
- if(offp == nil){
- lock(&c->ref.lk);
- c->offset += n;
- unlock(&c->ref.lk);
- }
- poperror();
- cclose(c);
- return n;
- }
- /* name conflicts with netbsd
- long
- _sys_read(int fd, void *buf, long n)
- {
- return kread(fd, buf, n, nil);
- }
- */
- long
- _syspread(int fd, void *buf, long n, vlong off)
- {
- if(off == ((uvlong) ~0))
- return kread(fd, buf, n, nil);
- return kread(fd, buf, n, &off);
- }
- static long
- kwrite(int fd, void *buf, long nn, vlong *offp)
- {
- Chan *c;
- long m, n;
- vlong off;
- n = 0;
- c = fdtochan(fd, OWRITE, 1, 1);
- if(waserror()) {
- if(offp == nil){
- lock(&c->ref.lk);
- c->offset -= n;
- unlock(&c->ref.lk);
- }
- cclose(c);
- nexterror();
- }
- if(c->qid.type & QTDIR)
- error(Eisdir);
- n = nn;
- if(offp == nil){ /* use and maintain channel's offset */
- lock(&c->ref.lk);
- off = c->offset;
- c->offset += n;
- unlock(&c->ref.lk);
- }else
- off = *offp;
- if(off < 0)
- error(Enegoff);
- m = devtab[c->type]->write(c, buf, n, off);
- if(offp == nil && m < n){
- lock(&c->ref.lk);
- c->offset -= n - m;
- unlock(&c->ref.lk);
- }
- poperror();
- cclose(c);
- return m;
- }
- long
- sys_write(int fd, void *buf, long n)
- {
- return kwrite(fd, buf, n, nil);
- }
- long
- _syspwrite(int fd, void *buf, long n, vlong off)
- {
- if(off == ((uvlong) ~0))
- return kwrite(fd, buf, n, nil);
- return kwrite(fd, buf, n, &off);
- }
- static vlong
- _sysseek(int fd, vlong off, int whence)
- {
- Chan *c;
- uchar buf[sizeof(Dir)+100];
- Dir dir;
- int n;
- c = fdtochan(fd, -1, 1, 1);
- if(waserror()){
- cclose(c);
- nexterror();
- }
- if(devtab[c->type]->dc == '|')
- error(Eisstream);
- switch(whence){
- case 0:
- if((c->qid.type & QTDIR) && off != 0)
- error(Eisdir);
- if(off < 0)
- error(Enegoff);
- c->offset = off;
- break;
- case 1:
- if(c->qid.type & QTDIR)
- error(Eisdir);
- lock(&c->ref.lk); /* lock for read/write update */
- off = off + c->offset;
- if(off < 0)
- error(Enegoff);
- c->offset = off;
- unlock(&c->ref.lk);
- break;
- case 2:
- if(c->qid.type & QTDIR)
- error(Eisdir);
- n = devtab[c->type]->stat(c, buf, sizeof buf);
- if(convM2D(buf, n, &dir, nil) == 0)
- error("internal error: stat error in seek");
- off = dir.length + off;
- if(off < 0)
- error(Enegoff);
- c->offset = off;
- break;
- default:
- error(Ebadarg);
- }
- c->uri = 0;
- c->dri = 0;
- cclose(c);
- poperror();
- return off;
- }
- void
- validstat(uchar *s, int n)
- {
- int m;
- char buf[64];
- if(statcheck(s, n) < 0)
- error(Ebadstat);
- /* verify that name entry is acceptable */
- s += STATFIXLEN - 4*BIT16SZ; /* location of first string */
- /*
- * s now points at count for first string.
- * if it's too long, let the server decide; this is
- * only for his protection anyway. otherwise
- * we'd have to allocate and waserror.
- */
- m = GBIT16(s);
- s += BIT16SZ;
- if(m+1 > sizeof buf)
- return;
- memmove(buf, s, m);
- buf[m] = '\0';
- /* name could be '/' */
- if(strcmp(buf, "/") != 0)
- validname(buf, 0);
- }
- long
- _sysfstat(int fd, void *buf, long n)
- {
- Chan *c;
- uint l;
- l = n;
- validaddr(buf, l, 1);
- c = fdtochan(fd, -1, 0, 1);
- if(waserror()) {
- cclose(c);
- nexterror();
- }
- l = devtab[c->type]->stat(c, buf, l);
- poperror();
- cclose(c);
- return l;
- }
- long
- _sysstat(char *name, void *buf, long n)
- {
- Chan *c;
- uint l;
- l = n;
- validaddr(buf, l, 1);
- validaddr(name, 1, 0);
- c = namec(name, Aaccess, 0, 0);
- if(waserror()){
- cclose(c);
- nexterror();
- }
- l = devtab[c->type]->stat(c, buf, l);
- poperror();
- cclose(c);
- return l;
- }
- long
- _syschdir(char *name)
- {
- Chan *c;
- validaddr(name, 1, 0);
- c = namec(name, Atodir, 0, 0);
- cclose(up->dot);
- up->dot = c;
- return 0;
- }
- long
- bindmount(int ismount, int fd, int afd, char* arg0, char* arg1, ulong flag, char* spec)
- {
- int ret;
- Chan *c0, *c1, *ac, *bc;
- struct{
- Chan *chan;
- Chan *authchan;
- char *spec;
- int flags;
- }bogus;
- if((flag&~MMASK) || (flag&MORDER)==(MBEFORE|MAFTER))
- error(Ebadarg);
- bogus.flags = flag & MCACHE;
- if(ismount){
- if(up->pgrp->noattach)
- error(Enoattach);
- ac = nil;
- bc = fdtochan(fd, ORDWR, 0, 1);
- if(waserror()) {
- if(ac)
- cclose(ac);
- cclose(bc);
- nexterror();
- }
- if(afd >= 0)
- ac = fdtochan(afd, ORDWR, 0, 1);
- bogus.chan = bc;
- bogus.authchan = ac;
- validaddr((ulong)spec, 1, 0);
- bogus.spec = spec;
- if(waserror())
- error(Ebadspec);
- validname(spec, 1);
- poperror();
- ret = devno('M', 0);
- c0 = devtab[ret]->attach((char*)&bogus);
- poperror();
- if(ac)
- cclose(ac);
- cclose(bc);
- }else{
- bogus.spec = 0;
- validaddr((ulong)arg0, 1, 0);
- c0 = namec(arg0, Abind, 0, 0);
- }
- if(waserror()){
- cclose(c0);
- nexterror();
- }
- validaddr((ulong)arg1, 1, 0);
- c1 = namec(arg1, Amount, 0, 0);
- if(waserror()){
- cclose(c1);
- nexterror();
- }
- ret = cmount(&c0, c1, flag, bogus.spec);
- poperror();
- cclose(c1);
- poperror();
- cclose(c0);
- if(ismount)
- fdclose(fd, 0);
- return ret;
- }
- long
- _sysbind(char *old, char *new, int flag)
- {
- return bindmount(0, -1, -1, old, new, flag, nil);
- }
- long
- _sysmount(int fd, int afd, char *new, int flag, char *spec)
- {
- return bindmount(1, fd, afd, nil, new, flag, spec);
- }
- long
- _sysunmount(char *old, char *new)
- {
- Chan *cmount, *cmounted;
- cmounted = 0;
- cmount = namec(new, Amount, 0, 0);
- if(old) {
- if(waserror()) {
- cclose(cmount);
- nexterror();
- }
- validaddr(old, 1, 0);
- /*
- * This has to be namec(..., Aopen, ...) because
- * if arg[0] is something like /srv/cs or /fd/0,
- * opening it is the only way to get at the real
- * Chan underneath.
- */
- cmounted = namec(old, Aopen, OREAD, 0);
- poperror();
- }
- if(waserror()) {
- cclose(cmount);
- if(cmounted)
- cclose(cmounted);
- nexterror();
- }
- cunmount(cmount, cmounted);
- cclose(cmount);
- if(cmounted)
- cclose(cmounted);
- poperror();
- return 0;
- }
- long
- _syscreate(char *name, int mode, ulong perm)
- {
- int fd;
- Chan *c = 0;
- openmode(mode&~OEXCL); /* error check only; OEXCL okay here */
- if(waserror()) {
- if(c)
- cclose(c);
- nexterror();
- }
- validaddr(name, 1, 0);
- c = namec(name, Acreate, mode, perm);
- fd = newfd(c);
- if(fd < 0)
- error(Enofd);
- poperror();
- return fd;
- }
- long
- _sysremove(char *name)
- {
- Chan *c;
- c = namec(name, Aremove, 0, 0);
- if(waserror()){
- c->type = 0; /* see below */
- cclose(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;
- poperror();
- cclose(c);
- return 0;
- }
- long
- _syswstat(char *name, void *buf, long n)
- {
- Chan *c;
- uint l;
- l = n;
- validstat(buf, l);
- validaddr(name, 1, 0);
- c = namec(name, Aaccess, 0, 0);
- if(waserror()){
- cclose(c);
- nexterror();
- }
- l = devtab[c->type]->wstat(c, buf, l);
- poperror();
- cclose(c);
- return l;
- }
- long
- _sysfwstat(int fd, void *buf, long n)
- {
- Chan *c;
- uint l;
- l = n;
- validaddr(buf, l, 0);
- validstat(buf, l);
- c = fdtochan(fd, -1, 1, 1);
- if(waserror()) {
- cclose(c);
- nexterror();
- }
- l = devtab[c->type]->wstat(c, buf, l);
- poperror();
- cclose(c);
- return l;
- }
- static void
- starterror(void)
- {
- assert(up->nerrlab == 0);
- }
- static void
- _syserror(void)
- {
- char *p;
- p = up->syserrstr;
- up->syserrstr = up->errstr;
- up->errstr = p;
- }
- static void
- enderror(void)
- {
- assert(up->nerrlab == 1);
- poperror();
- }
- int
- sysbind(char *old, char *new, int flag)
- {
- int n;
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _sysbind(old, new, flag);
- enderror();
- return n;
- }
- int
- syschdir(char *path)
- {
- int n;
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _syschdir(path);
- enderror();
- return n;
- }
- int
- sysclose(int fd)
- {
- int n;
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _sysclose(fd);
- enderror();
- return n;
- }
- int
- syscreate(char *name, int mode, ulong perm)
- {
- int n;
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _syscreate(name, mode, perm);
- enderror();
- return n;
- }
- int
- sysdup(int fd0, int fd1)
- {
- int n;
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _sysdup(fd0, fd1);
- enderror();
- return n;
- }
- int
- sysfstat(int fd, uchar *buf, int n)
- {
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _sysfstat(fd, buf, n);
- enderror();
- return n;
- }
- int
- sysfwstat(int fd, uchar *buf, int n)
- {
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _sysfwstat(fd, buf, n);
- enderror();
- return n;
- }
- int
- sysmount(int fd, int afd, char *new, int flag, char *spec)
- {
- int n;
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _sysmount(fd, afd, new, flag, spec);
- enderror();
- return n;
- }
- int
- sysunmount(char *old, char *new)
- {
- int n;
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _sysunmount(old, new);
- enderror();
- return n;
- }
- int
- sysopen(char *name, int mode)
- {
- int n;
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _sysopen(name, mode);
- enderror();
- return n;
- }
- int
- syspipe(int *fd)
- {
- int n;
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _syspipe(fd);
- enderror();
- return n;
- }
- long
- syspread(int fd, void *buf, long n, vlong off)
- {
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _syspread(fd, buf, n, off);
- enderror();
- return n;
- }
- long
- syspwrite(int fd, void *buf, long n, vlong off)
- {
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _syspwrite(fd, buf, n, off);
- enderror();
- return n;
- }
- long
- sysread(int fd, void *buf, long n)
- {
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _syspread(fd, buf, n, (uvlong) ~0);
- enderror();
- return n;
- }
- int
- sysremove(char *path)
- {
- int n;
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _sysremove(path);
- enderror();
- return n;
- }
- vlong
- sysseek(int fd, vlong off, int whence)
- {
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- off = _sysseek(fd, off, whence);
- enderror();
- return off;
- }
- int
- sysstat(char *name, uchar *buf, int n)
- {
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _sysstat(name, buf, n);
- enderror();
- return n;
- }
- long
- syswrite(int fd, void *buf, long n)
- {
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _syspwrite(fd, buf, n, (uvlong) ~0);
- enderror();
- return n;
- }
- int
- syswstat(char *name, uchar *buf, int n)
- {
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _syswstat(name, buf, n);
- enderror();
- return n;
- }
- void
- werrstr(char *f, ...)
- {
- char buf[ERRMAX];
- va_list arg;
- va_start(arg, f);
- vsnprint(buf, sizeof buf, f, arg);
- va_end(arg);
- if(up->nerrlab)
- strecpy(up->errstr, up->errstr+ERRMAX, buf);
- else
- strecpy(up->syserrstr, up->syserrstr+ERRMAX, buf);
- }
- int
- __errfmt(Fmt *fmt)
- {
- if(up->nerrlab)
- return fmtstrcpy(fmt, up->errstr);
- else
- return fmtstrcpy(fmt, up->syserrstr);
- }
- int
- errstr(char *buf, uint n)
- {
- char tmp[ERRMAX];
- char *p;
- p = up->nerrlab ? up->errstr : up->syserrstr;
- memmove(tmp, p, ERRMAX);
- utfecpy(p, p+ERRMAX, buf);
- utfecpy(buf, buf+n, tmp);
- return strlen(buf);
- }
- int
- rerrstr(char *buf, uint n)
- {
- char *p;
- p = up->nerrlab ? up->errstr : up->syserrstr;
- utfecpy(buf, buf+n, p);
- return strlen(buf);
- }
- ulong
- _sysrendezvous(ulong arg0, ulong arg1)
- {
- ulong tag, val;
- Proc *p, **l;
- tag = arg0;
- l = &REND(up->rgrp, tag);
- up->rendval = ~0UL;
- lock(&up->rgrp->ref.lk);
- for(p = *l; p; p = p->rendhash) {
- if(p->rendtag == tag) {
- *l = p->rendhash;
- val = p->rendval;
- p->rendval = arg1;
- while(p->mach != 0)
- ;
- procwakeup(p);
- unlock(&up->rgrp->ref.lk);
- return val;
- }
- l = &p->rendhash;
- }
- /* Going to sleep here */
- up->rendtag = tag;
- up->rendval = arg1;
- up->rendhash = *l;
- *l = up;
- up->state = Rendezvous;
- unlock(&up->rgrp->ref.lk);
- procsleep();
- return up->rendval;
- }
- ulong
- sysrendezvous(ulong tag, ulong val)
- {
- ulong n;
- starterror();
- if(waserror()){
- _syserror();
- return -1;
- }
- n = _sysrendezvous(tag, val);
- enderror();
- return n;
- }
|