2 #include "../port/lib.h"
6 #include "../port/error.h"
8 int chandebug=0; /* toggled by sysr1 */
9 #define DBG if(chandebug)iprint
17 static struct Chanalloc
25 typedef struct Elemlist Elemlist;
29 char *aname; /* original name */
30 char *name; /* copy of name, so '/' can be overwritten */
39 #define SEP(c) ((c) == 0 || (c) == '/')
42 dumpmount(void) /* DEBUGGING */
49 print("no process for dumpmount\n");
54 print("no pgrp for dumpmount\n");
63 he = &pg->mnthash[MNTHASH];
64 for(h = pg->mnthash; h < he; h++){
65 for(f = *h; f; f = f->hash){
66 print("head: %#p: %s %#llux.%lud %C %lud -> \n", f,
67 f->from->path->s, f->from->qid.path,
68 f->from->qid.vers, devtab[f->from->type]->dc,
70 for(t = f->mount; t; t = t->next)
71 print("\t%#p: %s (umh %#p) (path %#.8llux dev %C %lud)\n", t, t->to->path->s, t->to->umh, t->to->qid.path, devtab[t->to->type]->dc, t->to->dev);
86 return "<nil path.s>";
93 return p[0]=='.' && p[1]=='.' && p[2]=='\0';
104 } while(!cmpswap(&r->ref, old, new));
116 panic("decref pc=%#p", getcallerpc(&r));
118 } while(!cmpswap(&r->ref, old, new));
123 * Rather than strncpy, which zeros the rest of the buffer, kstrcpy
124 * truncates if necessary, always zero terminates, does not zero fill,
125 * and puts ... at the end of the string if it's too long. Usually used to
126 * save a string in up->genbuf;
129 kstrcpy(char *s, char *t, int ns)
139 /* too long, truncate */
143 /* append ... if there is space */
147 /* look for first byte of UTF-8 sequence by skipping continuation bytes */
148 while(ns>0 && (s[--ns]&0xC0)==0x80)
164 * Atomically replace *p with copy of s
167 kstrdup(char **p, char *s)
173 /* if it's a user, we can wait for memory; if not, something's very wrong */
179 panic("kstrdup: no memory");
181 setmalloctag(t, getcallerpc(&p));
194 todinit(); /* avoid later reentry causing infinite recursion */
195 for(i=0; devtab[i] != nil; i++)
199 static void closeproc(void*);
206 for(i=0; devtab[i] != nil; i++)
208 kproc("closeproc", closeproc, nil);
212 chandevshutdown(void)
216 /* shutdown in reverse order */
217 for(i=0; devtab[i] != nil; i++)
219 for(i--; i >= 0; i--)
220 devtab[i]->shutdown();
231 chanalloc.free = c->next;
235 c = smalloc(sizeof(Chan));
237 c->link = chanalloc.list;
241 c->fid = ++chanalloc.fid;
244 /* if you get an error before associating with a dev,
245 close calls rootclose, a nop */
265 memset(&c->mqid, 0, sizeof(c->mqid));
279 p = smalloc(sizeof(Path));
282 p->alen = i+PATHSLOP;
283 p->s = smalloc(p->alen);
284 memmove(p->s, s, i+1);
289 * Cannot use newpath for arbitrary names because the mtpt
290 * array will not be populated correctly. The names #/ and / are
291 * allowed, but other names with / in them draw warnings.
293 if(strchr(s, '/') != nil && strcmp(s, "#/") != 0 && strcmp(s, "/") != 0)
294 print("newpath: %s from %#p\n", s, getcallerpc(&s));
297 p->malen = PATHMSLOP;
298 p->mtpt = smalloc(p->malen*sizeof p->mtpt[0]);
308 pp = smalloc(sizeof(Path));
311 DBG("copypath %s %p => %p\n", p->s, p, pp);
315 pp->s = smalloc(p->alen);
316 memmove(pp->s, p->s, p->len+1);
319 pp->malen = p->malen;
320 pp->mtpt = smalloc(p->malen*sizeof pp->mtpt[0]);
321 for(i=0; i<pp->mlen; i++){
322 pp->mtpt[i] = p->mtpt[i];
323 if(pp->mtpt[i] != nil)
338 DBG("pathclose %p %s ref=%ld =>", p, p->s, p->ref);
339 for(i=0; i<p->mlen; i++)
340 DBG(" %p", p->mtpt[i]);
347 for(i=0; i<p->mlen; i++)
348 if(p->mtpt[i] != nil)
355 * In place, rewrite name to compress multiple /, eliminate ., and process ..
356 * (Really only called to remove a trailing .. that has been added.
357 * Otherwise would need to update n->mtpt as well.)
360 fixdotdotname(Path *p)
365 r = strchr(p->s, '/');
371 * The correct name is #i rather than #i/,
372 * but the correct name of #/ is #/.
374 if(strcmp(r, "/")==0 && p->s[1] != '/')
378 p->len = strlen(p->s);
396 addelem(Path *p, char *s, Chan *from)
402 if(s[0]=='.' && s[1]=='\0')
408 if(p->len+1+i+1 > p->alen){
409 a = p->len+1+i+1 + PATHSLOP;
411 memmove(t, p->s, p->len+1);
416 /* don't insert extra slash if one is present */
417 if(p->len>0 && p->s[p->len-1]!='/' && s[0]!='/')
418 p->s[p->len++] = '/';
419 memmove(p->s+p->len, s, i+1);
423 DBG("addelem %s .. => rm %p\n", p->s, p->mtpt[p->mlen-1]);
424 if(p->mlen > 1 && (c = p->mtpt[--p->mlen]) != nil){
425 p->mtpt[p->mlen] = nil;
429 if(p->mlen >= p->malen){
430 p->malen = p->mlen+1+PATHMSLOP;
431 tt = smalloc(p->malen*sizeof tt[0]);
432 memmove(tt, p->mtpt, p->mlen*sizeof tt[0]);
436 DBG("addelem %s %s => add %p\n", p->s, s, from);
437 p->mtpt[p->mlen++] = from;
449 if(c->dirrock != nil){
476 c->next = chanalloc.free;
482 * Queue a chan to be closed by one of the clunk procs.
497 return clunkq.head != nil;
507 clunkq.tail->next = c;
523 clunkq.head = c->next;
540 tsleep(&clunkq.r, clunkwork, nil, 500);
545 if(clunkq.q.head != nil) {
552 if(clunkq.q.head == nil) {
554 kproc("closeproc", closeproc, nil);
561 devtab[c->type]->close(c);
571 if(c == nil || c->ref < 1 || c->flag&CFREE)
572 panic("cclose %#p", getcallerpc(&c));
574 DBG("cclose %p name=%s ref=%ld\n", c, chanpath(c), c->ref);
579 if(devtab[c->type]->dc == L'M')
580 if((c->flag&(CRCLOSE|CCACHE)) == CCACHE)
581 if((c->qid.type&(QTEXCL|QTMOUNT|QTAUTH)) == 0)
582 if((clunkq.nqueued - clunkq.nclosed) < 64){
588 devtab[c->type]->close(c);
597 if(c == nil || c->ref < 1 || c->flag&CFREE)
598 panic("ccloseq %#p", getcallerpc(&c));
600 DBG("ccloseq %p name=%s ref=%ld\n", c, chanpath(c), c->ref);
609 * Make sure we have the only copy of c. (Copy on write.)
628 return a.path==b.path && a.vers==b.vers;
632 eqchan(Chan *a, Chan *b, int skipvers)
634 if(a->qid.path != b->qid.path)
636 if(!skipvers && a->qid.vers!=b->qid.vers)
638 if(a->type != b->type)
646 eqchantdqid(Chan *a, int type, int dev, Qid qid, int skipvers)
648 if(a->qid.path != qid.path)
650 if(!skipvers && a->qid.vers!=qid.vers)
664 mh = smalloc(sizeof(Mhead));
672 cmount(Chan **newp, Chan *old, int flag, char *spec)
677 Mount *nm, *f, *um, **h;
680 if(QTDIR & (old->qid.type^(*newp)->qid.type))
684 print("cmount: unexpected umh, caller %#p\n", getcallerpc(&newp));
688 if((old->qid.type&QTDIR)==0 && order != MREPL)
695 * Not allowed to bind when the old directory is itself a union.
696 * (Maybe it should be allowed, but I don't see what the semantics
699 * We need to check mh->mount->next to tell unions apart from
700 * simple mount points, so that things like
705 * The check of mount->mflag allows things like
709 * This is far more complicated than it should be, but I don't
710 * see an easier way at the moment.
712 if((flag&MCREATE) && mh && mh->mount
713 && (mh->mount->next || !(mh->mount->mflag&MCREATE)))
719 l = &MOUNTH(pg, old->qid);
720 for(m = *l; m; m = m->hash){
721 if(eqchan(m->from, old, 1))
728 * nothing mounted here yet. create a mount
729 * head and add to the hash table.
735 * if this is a union mount, add the old
736 * node to the mount chain.
739 m->mount = newmount(m, old, 0, 0);
748 nm = newmount(m, new, flag, spec);
749 if(mh != nil && mh->mount != nil){
751 * copy a union when binding it onto a directory
758 for(um = um->next; um; um = um->next){
759 f = newmount(m, um->to, flg, um->spec);
765 if(m->mount && order == MREPL){
771 nm->mflag |= MCREATE;
773 if(m->mount && order == MAFTER){
774 for(f = m->mount; f->next; f = f->next)
778 for(f = nm; f->next; f = f->next)
790 cunmount(Chan *mnt, Chan *mounted)
796 if(mnt->umh) /* should not happen */
797 print("cunmount newp extra umh %p has %p\n", mnt, mnt->umh);
800 * It _can_ happen that mounted->umh is non-nil,
801 * because mounted is the result of namec(Aopen)
802 * (see sysfile.c:/^sysunmount).
803 * If we open a union directory, it will have a umh.
804 * Although surprising, this is okay, since the
805 * cclose will take care of freeing the umh.
811 l = &MOUNTH(pg, mnt->qid);
812 for(m = *l; m; m = m->hash){
813 if(eqchan(m->from, mnt, 1))
836 for(f = *p; f; f = f->next){
837 /* BUG: Needs to be 2 pass */
838 if(eqchan(f->to, mounted, 1) ||
839 (f->to->mchan && eqchan(f->to->mchan, mounted, 1))){
868 if(c == nil || c->ref < 1 || c->flag&CFREE)
869 panic("cclone: %#p", getcallerpc(&c));
870 wq = devtab[c->type]->walk(c, nil, nil, 0);
872 error("clone failed");
875 if((nc->path = c->path) != nil)
880 /* also used by sysfile.c:/^mountfix */
882 findmount(Chan **cp, Mhead **mp, int type, int dev, Qid qid)
889 for(m = MOUNTH(pg, qid); m; m = m->hash){
892 print("m %p m->from 0\n", m);
896 if(eqchantdqid(m->from, type, dev, qid, 1)){
906 incref(m->mount->to);
919 * Calls findmount but also updates path.
922 domount(Chan **cp, Mhead **mp, Path **path)
927 if(findmount(cp, mp, (*cp)->type, (*cp)->dev, (*cp)->qid) == 0)
934 print("domount: path %s has mlen==%d\n", p->s, p->mlen);
936 lc = &p->mtpt[p->mlen-1];
937 DBG("domount %p %s => add %p (was %p)\n", p, p->s, (*mp)->from, p->mtpt[p->mlen-1]);
949 * If c is the right-hand-side of a mount point, returns the left hand side.
950 * Changes name to reflect the fact that we've uncrossed the mountpoint,
951 * so name had better be ours to change!
954 undomount(Chan *c, Path *path)
958 if(path->ref != 1 || path->mlen == 0)
959 print("undomount: path %s ref %ld mlen %d caller %#p\n",
960 path->s, path->ref, path->mlen, getcallerpc(&c));
962 if(path->mlen > 0 && (nc = path->mtpt[path->mlen-1]) != nil){
963 DBG("undomount %p %s => remove %p\n", path, path->s, nc);
965 path->mtpt[path->mlen-1] = nil;
972 * Call dev walk but catch errors.
975 ewalk(Chan *c, Chan *nc, char **name, int nname)
981 wq = devtab[c->type]->walk(c, nc, name, nname);
987 * Either walks all the way or not at all. No partial results in *cp.
988 * *nerror is the number of names to display in an error message.
990 static char Edoesnotexist[] = "does not exist";
992 walk(Chan **cp, char **names, int nnames, int nomount, int *nerror)
994 int dev, didmount, dotdot, i, n, nhave, ntry, type;
1008 * While we haven't gotten all the way down the path:
1009 * 1. step through a mount point, if any
1010 * 2. send a walk request for initial dotdot or initial prefix without dotdot
1011 * 3. move to the first mountpoint along the way.
1014 * Each time through the loop:
1016 * If didmount==0, c is on the undomount side of the mount point.
1017 * If didmount==1, c is on the domount side of the mount point.
1018 * Either way, c's full path is path.
1021 for(nhave=0; nhave<nnames; nhave+=n){
1022 if((c->qid.type&QTDIR)==0){
1027 kstrcpy(up->errstr, Enotdir, ERRMAX);
1032 ntry = nnames - nhave;
1036 for(i=0; i<ntry; i++){
1037 if(isdotdot(names[nhave+i])){
1047 if(!dotdot && !nomount && !didmount)
1048 domount(&c, &mh, &path);
1053 if((wq = ewalk(c, nil, names+nhave, ntry)) == nil){
1054 /* try a union mount, if any */
1057 * mh->mount->to == c, so start at mh->mount->next
1060 if((f = mh->mount) != nil)
1062 for(; f != nil; f = f->next)
1063 if((wq = ewalk(f->to, nil, names+nhave, ntry)) != nil){
1083 assert(wq->nqid == 1);
1084 assert(wq->clone != nil);
1086 path = addelem(path, "..", nil);
1087 nc = undomount(wq->clone, path);
1094 for(i=0; i<wq->nqid && i<ntry-1; i++){
1095 if(findmount(&nc, &nmh, type, dev, wq->qid[i])){
1101 if(nc == nil){ /* no mount points along path */
1102 if(wq->clone == nil){
1105 if(wq->nqid==0 || (wq->qid[wq->nqid-1].type&QTDIR)){
1107 *nerror = nhave+wq->nqid+1;
1108 kstrcpy(up->errstr, Edoesnotexist, ERRMAX);
1111 *nerror = nhave+wq->nqid;
1112 kstrcpy(up->errstr, Enotdir, ERRMAX);
1121 }else{ /* stopped early, at a mount point */
1123 if(wq->clone != nil){
1131 if(i==n-1 && nmh!=nil)
1133 path = addelem(path, names[nhave+i], mtpt);
1147 if(c->umh != nil){ //BUG
1148 print("walk umh\n");
1164 * c is a mounted non-creatable directory. find a creatable one.
1167 createdir(Chan *c, Mhead *m)
1177 for(f = m->mount; f; f = f->next){
1178 if(f->to != nil && (f->mflag&MCREATE) != 0){
1196 growparse(Elemlist *e)
1202 if(e->nelems % Delta == 0){
1203 new = smalloc((e->nelems+Delta) * sizeof(char*));
1204 memmove(new, e->elems, e->nelems*sizeof(char*));
1207 inew = smalloc((e->nelems+Delta+1) * sizeof(int));
1208 memmove(inew, e->off, (e->nelems+1)*sizeof(int));
1215 * The name is known to be valid.
1216 * Copy the name so slashes can be overwritten.
1217 * An empty string will set nelem=0.
1218 * A path ending in / or /. or /.//./ etc. will have
1219 * e.mustbedir = 1, so that we correctly
1220 * reject, e.g., "/adm/users/." when /adm/users is a file
1221 * rather than a directory.
1224 parsename(char *aname, Elemlist *e)
1228 kstrdup(&e->name, aname);
1232 e->off = smalloc(sizeof(int));
1233 e->off[0] = skipslash(name) - name;
1235 name = skipslash(name);
1237 e->off[e->nelems] = name+strlen(name) - e->name;
1242 e->elems[e->nelems++] = name;
1243 slash = utfrune(name, '/');
1245 e->off[e->nelems] = name+strlen(name) - e->name;
1249 e->off[e->nelems] = slash - e->name;
1257 print("parsename %s:", e->name);
1258 for(i=0; i<=e->nelems; i++)
1259 print(" %d", e->off[i]);
1265 memrchr(void *va, int c, long n)
1270 for(e=a+n-1; e>a; e--)
1277 namelenerror(char *aname, int len, char *err)
1279 char *ename, *name, *next;
1283 * If the name is short enough, just use the whole thing.
1285 errlen = strlen(err);
1286 if(len < ERRMAX/3 || len+errlen < 2*ERRMAX/3)
1287 snprint(up->genbuf, sizeof up->genbuf, "%.*s",
1288 utfnlen(aname, len), aname);
1291 * Print a suffix of the name, but try to get a little info.
1297 next = memrchr(aname, '/', name-aname);
1301 }while(len < ERRMAX/3 || len + errlen < 2*ERRMAX/3);
1304 * If the name is ridiculously long, chop it.
1307 name = ename-ERRMAX/4;
1309 panic("bad math in namelenerror");
1310 /* walk out of current UTF sequence */
1311 for(i=0; (*name&0xC0)==0x80 && i<UTFmax; i++)
1314 snprint(up->genbuf, sizeof up->genbuf, "...%.*s",
1315 utfnlen(name, ename-name), name);
1317 snprint(up->errstr, ERRMAX, "%#q %s", up->genbuf, err);
1322 nameerror(char *name, char *err)
1324 namelenerror(name, strlen(name), err);
1328 * Turn a name into a channel.
1329 * &name[0] is known to be a valid address. It may be a kernel address.
1331 * Opening with amode Aopen, Acreate, Aremove, or Aaccess guarantees
1332 * that the result will be the only reference to that particular fid.
1333 * This is necessary since we might pass the result to
1334 * devtab[]->remove().
1336 * Opening Atodir or Amount does not guarantee this.
1338 * Under certain circumstances, opening Aaccess will cause
1339 * an unnecessary clone in order to get a cunique Chan so it
1340 * can attach the correct name. Sysstat and sys_stat need the
1341 * correct name so they can rewrite the stat info.
1344 namec(char *aname, int amode, int omode, ulong perm)
1346 int len, n, t, nomount;
1352 char *createerr, tmperrbuf[ERRMAX];
1355 if(aname[0] == '\0')
1356 error("empty file name");
1357 aname = validnamedup(aname, 1);
1362 DBG("namec %s %d %d\n", aname, amode, omode);
1366 * Find the starting off point (the current slash, the root of
1367 * a device tree, or the current dot) as well as the name to
1368 * evaluate starting there.
1379 up->genbuf[0] = '\0';
1381 while(*name != '\0' && (*name != '/' || n < 2)){
1382 if(n >= sizeof(up->genbuf)-1)
1384 up->genbuf[n++] = *name++;
1386 up->genbuf[n] = '\0';
1388 * noattach is sandboxing.
1390 * the OK exceptions are:
1391 * | it only gives access to pipes you create
1392 * d this process's file descriptors
1393 * e this process's environment
1394 * the iffy exceptions are:
1395 * c time and pid, but also cons and consctl
1396 * p control of your own processes (and unfortunately
1397 * any others left unprotected)
1399 n = chartorune(&r, up->genbuf+1)+1;
1400 /* actually / is caught by parsing earlier */
1401 if(utfrune("M", r) != nil)
1403 if(up->pgrp->noattach && utfrune("|decp", r)==nil)
1408 c = devtab[t]->attach(up->genbuf+n);
1418 e.prefix = name - aname;
1429 * Prepare nice error, showing first e.nerror elements of name.
1433 strcpy(tmperrbuf, up->errstr);
1434 if(e.off[e.nerror]==0)
1435 print("nerror=%d but off=%d\n",
1436 e.nerror, e.off[e.nerror]);
1438 print("showing %d+%d/%d (of %d) of %s (%d %d)\n", e.prefix, e.off[e.nerror], e.nerror, e.nelems, aname, e.off[0], e.off[1]);
1439 len = e.prefix+e.off[e.nerror];
1441 namelenerror(aname, len, tmperrbuf);
1445 * Build a list of elements in the name.
1447 parsename(name, &e);
1452 if(amode == Acreate){
1453 /* perm must have DMDIR if last element is / or /. */
1454 if(e.mustbedir && !(perm&DMDIR)){
1455 e.nerror = e.nelems;
1456 error("create without DMDIR");
1459 /* don't try to walk the last path element just yet. */
1465 if(walk(&c, e.elems, e.nelems, nomount, &e.nerror) < 0){
1466 if(e.nerror < 0 || e.nerror > e.nelems){
1467 print("namec %s walk error nerror=%d\n", aname, e.nerror);
1473 if(e.mustbedir && !(c->qid.type&QTDIR))
1474 error("not a directory");
1476 if(amode == Aopen && (omode&3) == OEXEC && (c->qid.type&QTDIR))
1477 error("cannot exec directory");
1481 /* no need to maintain path - cannot dotdot an Abind */
1484 domount(&c, &m, nil);
1494 /* save&update the name; domount might change c */
1503 domount(&c, &m, &path);
1505 /* our own copy to open or remove */
1508 /* now it's our copy anyway, we can put the name back */
1513 /* record whether c is on a mount point */
1525 print("cunique umh Open\n");
1529 /* only save the mount head if it's a multiple element union */
1530 if(m != nil && m->mount != nil && m->mount->next != nil)
1535 /* save registers else error() in open has wrong value of c saved */
1538 c = devtab[c->type]->open(c, omode&~OCEXEC);
1550 * Directories (e.g. for cd) are left before the mount point,
1551 * so one may mount on / or . and see the effect.
1553 if(!(c->qid.type & QTDIR))
1559 * When mounting on an already mounted upon directory,
1560 * one wants subsequent mounts to be attached to the
1561 * original directory, not the replacement. Don't domount.
1567 * We've already walked all but the last element.
1568 * If the last exists, try to open it OTRUNC.
1569 * If omode&OEXCL is set, just give up.
1573 if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) == 0){
1581 * The semantics of the create(2) system call are that if the
1582 * file exists and can be written, it is to be opened with truncation.
1583 * On the other hand, the create(5) message fails if the file exists.
1584 * If we get two create(2) calls happening simultaneously,
1585 * they might both get here and send create(5) messages, but only
1586 * one of the messages will succeed. To provide the expected create(2)
1587 * semantics, the call with the failed message needs to try the above
1588 * walk again, opening for truncation. This correctly solves the
1589 * create/create race, in the sense that any observable outcome can
1590 * be explained as one happening before the other.
1591 * The create/create race is quite common. For example, it happens
1592 * when two rc subshells simultaneously update the same
1593 * environment variable.
1595 * The implementation still admits a create/create/remove race:
1596 * (A) walk to file, fails
1597 * (B) walk to file, fails
1598 * (A) create file, succeeds, returns
1599 * (B) create file, fails
1600 * (A) remove file, succeeds, returns
1601 * (B) walk to file, return failure.
1603 * This is hardly as common as the create/create race, and is really
1604 * not too much worse than what might happen if (B) got a hold of a
1605 * file descriptor and then the file was removed -- either way (B) can't do
1606 * anything with the result of the create call. So we don't care about this race.
1608 * Applications that care about more fine-grained decision of the races
1609 * can use the OEXCL flag to get at the underlying create(5) semantics;
1610 * by default we provide the common case.
1612 * We need to stay behind the mount point in case we
1613 * need to do the first walk again (should the create fail).
1615 * We also need to cross the mount point and find the directory
1616 * in the union in which we should be creating.
1618 * The channel staying behind is c, the one moving forward is cnew.
1621 cnew = nil; /* is this assignment necessary? */
1622 if(!waserror()){ /* try create */
1623 if(!nomount && findmount(&cnew, &m, c->type, c->dev, c->qid))
1624 cnew = createdir(cnew, m);
1631 * We need our own copy of the Chan because we're
1632 * about to send a create, which will move it. Once we have
1633 * our own copy, we can fix the name, which might be wrong
1634 * if findmount gave us a new Chan.
1636 cnew = cunique(cnew);
1637 pathclose(cnew->path);
1638 cnew->path = c->path;
1641 cnew = devtab[cnew->type]->create(cnew, e.elems[e.nelems-1], omode&~(OEXCL|OCEXEC), perm);
1644 cnew->flag |= CCEXEC;
1646 cnew->flag |= CRCLOSE;
1651 c->path = addelem(c->path, e.elems[e.nelems-1], nil);
1662 createerr = up->errstr;
1663 up->errstr = tmperrbuf;
1664 /* note: we depend that walk does not error */
1665 if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) < 0){
1666 up->errstr = createerr;
1667 error(createerr); /* report true error */
1669 up->errstr = createerr;
1674 panic("unknown namec access %d", amode);
1677 /* place final element in genbuf for e.g. exec */
1679 kstrcpy(up->genbuf, e.elems[e.nelems-1], sizeof up->genbuf);
1681 kstrcpy(up->genbuf, ".", sizeof up->genbuf);
1685 poperror(); /* e c */
1687 poperror(); /* aname */
1693 * name is valid. skip leading / and ./ as much as possible
1696 skipslash(char *name)
1698 while(name[0]=='/' || (name[0]=='.' && (name[1]==0 || name[1]=='/')))
1704 /*NUL*/ 1, 1, 1, 1, 1, 1, 1, 1,
1705 /*BKS*/ 1, 1, 1, 1, 1, 1, 1, 1,
1706 /*DLE*/ 1, 1, 1, 1, 1, 1, 1, 1,
1707 /*CAN*/ 1, 1, 1, 1, 1, 1, 1, 1,
1713 * Check that the name
1714 * a) is in valid memory.
1715 * b) is shorter than 2^16 bytes, so it can fit in a 9P string field.
1716 * c) contains no frogs.
1717 * The first byte is known to be addressible by the requester, so the
1718 * routine works for kernel and user memory both.
1719 * The parameter slashok flags whether a slash character is an error
1720 * or a valid character.
1722 * The parameter dup flags whether the string should be copied
1723 * out of user space before being scanned the second time.
1724 * (Otherwise a malicious thread could remove the NUL, causing us
1725 * to access unchecked addresses.)
1728 validname0(char *aname, int slashok, int dup, uintptr pc)
1730 char *ename, *name, *s;
1735 if((uintptr)name < KZERO){
1737 print("warning: validname called from %#p with user pointer", pc);
1738 ename = vmemchr(name, 0, (1<<16));
1740 ename = memchr(name, 0, (1<<16));
1742 if(ename==nil || ename-name>=(1<<16))
1743 error("name too long");
1749 memmove(s, name, n);
1753 setmalloctag(s, pc);
1757 /* all characters above '~' are ok */
1760 name += chartorune(&r, name);
1763 if(!slashok || c!='/'){
1764 snprint(up->genbuf, sizeof(up->genbuf), "%s: %q", Ebadchar, aname);
1775 validname(char *aname, int slashok)
1777 validname0(aname, slashok, 0, getcallerpc(&aname));
1781 validnamedup(char *aname, int slashok)
1783 return validname0(aname, slashok, 1, getcallerpc(&aname));
1789 if(c->qid.type & QTDIR)
1795 * This is necessary because there are many
1796 * pointers to the top of a given mount list:
1798 * - the mhead in the namespace hash table
1799 * - the mhead in chans returned from findmount:
1800 * used in namec and then by unionread.
1801 * - the mhead in chans returned from createdir:
1802 * used in the open/create race protect, which is gone.
1804 * The RWlock in the Mhead protects the mount list it contains.
1805 * The mount list is deleted when we cunmount.
1806 * The RWlock ensures that nothing is using the mount list at that time.
1808 * It is okay to replace c->mh with whatever you want as
1809 * long as you are sure you have a unique reference to it.
1811 * This comment might belong somewhere else.
1816 if(m != nil && decref(m) == 0){
1817 m->mount = (Mount*)0xCafeBeef;