PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/sys/src/9/port/chan.c

https://bitbucket.org/rminnich/sysfromiso
C | 1792 lines | 1316 code | 189 blank | 287 comment | 351 complexity | 9dd6d61288995151426b3a09ecd722dd MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, Unlicense
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "../port/error.h"
  7. int chandebug=0; /* toggled by sysr1 */
  8. #define DBG if(chandebug)iprint
  9. enum
  10. {
  11. PATHSLOP = 20,
  12. PATHMSLOP = 20,
  13. };
  14. struct
  15. {
  16. Lock;
  17. int fid;
  18. Chan *free;
  19. Chan *list;
  20. }chanalloc;
  21. typedef struct Elemlist Elemlist;
  22. struct Elemlist
  23. {
  24. char *aname; /* original name */
  25. char *name; /* copy of name, so '/' can be overwritten */
  26. int nelems;
  27. char **elems;
  28. int *off;
  29. int mustbedir;
  30. int nerror;
  31. int prefix;
  32. };
  33. #define SEP(c) ((c) == 0 || (c) == '/')
  34. static void
  35. dumpmount(void) /* DEBUGGING */
  36. {
  37. Pgrp *pg;
  38. Mount *t;
  39. Mhead **h, **he, *f;
  40. if(up == nil){
  41. print("no process for dumpmount\n");
  42. return;
  43. }
  44. pg = up->pgrp;
  45. if(pg == nil){
  46. print("no pgrp for dumpmount\n");
  47. return;
  48. }
  49. rlock(&pg->ns);
  50. if(waserror()){
  51. runlock(&pg->ns);
  52. nexterror();
  53. }
  54. he = &pg->mnthash[MNTHASH];
  55. for(h = pg->mnthash; h < he; h++){
  56. for(f = *h; f; f = f->hash){
  57. print("head: %#p: %s %#llux.%lud %C %lud -> \n", f,
  58. f->from->path->s, f->from->qid.path,
  59. f->from->qid.vers, devtab[f->from->type]->dc,
  60. f->from->dev);
  61. for(t = f->mount; t; t = t->next)
  62. 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);
  63. }
  64. }
  65. poperror();
  66. runlock(&pg->ns);
  67. }
  68. char*
  69. chanpath(Chan *c)
  70. {
  71. if(c == nil)
  72. return "<nil chan>";
  73. if(c->path == nil)
  74. return "<nil path>";
  75. if(c->path->s == nil)
  76. return "<nil path.s>";
  77. return c->path->s;
  78. }
  79. int
  80. isdotdot(char *p)
  81. {
  82. return p[0]=='.' && p[1]=='.' && p[2]=='\0';
  83. }
  84. long
  85. incref(Ref *r)
  86. {
  87. long x;
  88. lock(r);
  89. x = ++r->ref;
  90. unlock(r);
  91. return x;
  92. }
  93. long
  94. decref(Ref *r)
  95. {
  96. long x;
  97. lock(r);
  98. x = --r->ref;
  99. unlock(r);
  100. if(x < 0)
  101. panic("decref pc=%#p", getcallerpc(&r));
  102. return x;
  103. }
  104. /*
  105. * Rather than strncpy, which zeros the rest of the buffer, kstrcpy
  106. * truncates if necessary, always zero terminates, does not zero fill,
  107. * and puts ... at the end of the string if it's too long. Usually used to
  108. * save a string in up->genbuf;
  109. */
  110. void
  111. kstrcpy(char *s, char *t, int ns)
  112. {
  113. int nt;
  114. nt = strlen(t);
  115. if(nt+1 <= ns){
  116. memmove(s, t, nt+1);
  117. return;
  118. }
  119. /* too long */
  120. if(ns < 4){
  121. /* but very short! */
  122. strncpy(s, t, ns);
  123. return;
  124. }
  125. /* truncate with ... at character boundary (very rare case) */
  126. memmove(s, t, ns-4);
  127. ns -= 4;
  128. s[ns] = '\0';
  129. /* look for first byte of UTF-8 sequence by skipping continuation bytes */
  130. while(ns>0 && (s[--ns]&0xC0)==0x80)
  131. ;
  132. strcpy(s+ns, "...");
  133. }
  134. int
  135. emptystr(char *s)
  136. {
  137. if(s == nil)
  138. return 1;
  139. if(s[0] == '\0')
  140. return 1;
  141. return 0;
  142. }
  143. /*
  144. * Atomically replace *p with copy of s
  145. */
  146. void
  147. kstrdup(char **p, char *s)
  148. {
  149. int n;
  150. char *t, *prev;
  151. n = strlen(s)+1;
  152. /* if it's a user, we can wait for memory; if not, something's very wrong */
  153. if(up){
  154. t = smalloc(n);
  155. setmalloctag(t, getcallerpc(&p));
  156. }else{
  157. t = malloc(n);
  158. if(t == nil)
  159. panic("kstrdup: no memory");
  160. }
  161. memmove(t, s, n);
  162. prev = *p;
  163. *p = t;
  164. free(prev);
  165. }
  166. static int debugstart;
  167. void
  168. chandevreset(void)
  169. {
  170. int i;
  171. todinit(); /* avoid later reentry causing infinite recursion */
  172. debugstart = getconf("*debugstart") != nil;
  173. if(debugstart)
  174. iprint("reset:");
  175. for(i=0; devtab[i] != nil; i++) {
  176. if(debugstart)
  177. iprint(" %s", devtab[i]->name);
  178. devtab[i]->reset();
  179. }
  180. if(debugstart)
  181. iprint("\n");
  182. }
  183. void
  184. chandevinit(void)
  185. {
  186. int i;
  187. if(debugstart)
  188. iprint("init:");
  189. for(i=0; devtab[i] != nil; i++) {
  190. if(debugstart)
  191. iprint(" %s", devtab[i]->name);
  192. devtab[i]->init();
  193. }
  194. if(debugstart)
  195. iprint("\n");
  196. }
  197. void
  198. chandevshutdown(void)
  199. {
  200. int i;
  201. /* shutdown in reverse order */
  202. for(i=0; devtab[i] != nil; i++)
  203. ;
  204. for(i--; i >= 0; i--)
  205. devtab[i]->shutdown();
  206. }
  207. Chan*
  208. newchan(void)
  209. {
  210. Chan *c;
  211. lock(&chanalloc);
  212. c = chanalloc.free;
  213. if(c != 0)
  214. chanalloc.free = c->next;
  215. unlock(&chanalloc);
  216. if(c == nil){
  217. c = smalloc(sizeof(Chan));
  218. lock(&chanalloc);
  219. c->fid = ++chanalloc.fid;
  220. c->link = chanalloc.list;
  221. chanalloc.list = c;
  222. unlock(&chanalloc);
  223. }
  224. /* if you get an error before associating with a dev,
  225. close calls rootclose, a nop */
  226. c->type = 0;
  227. c->flag = 0;
  228. c->ref = 1;
  229. c->dev = 0;
  230. c->offset = 0;
  231. c->devoffset = 0;
  232. c->iounit = 0;
  233. c->umh = 0;
  234. c->uri = 0;
  235. c->dri = 0;
  236. c->aux = 0;
  237. c->mchan = 0;
  238. c->mcp = 0;
  239. c->mux = 0;
  240. memset(&c->mqid, 0, sizeof(c->mqid));
  241. c->path = 0;
  242. c->ismtpt = 0;
  243. return c;
  244. }
  245. Ref npath;
  246. Path*
  247. newpath(char *s)
  248. {
  249. int i;
  250. Path *p;
  251. p = smalloc(sizeof(Path));
  252. i = strlen(s);
  253. p->len = i;
  254. p->alen = i+PATHSLOP;
  255. p->s = smalloc(p->alen);
  256. memmove(p->s, s, i+1);
  257. p->ref = 1;
  258. incref(&npath);
  259. /*
  260. * Cannot use newpath for arbitrary names because the mtpt
  261. * array will not be populated correctly. The names #/ and / are
  262. * allowed, but other names with / in them draw warnings.
  263. */
  264. if(strchr(s, '/') && strcmp(s, "#/") != 0 && strcmp(s, "/") != 0)
  265. print("newpath: %s from %#p\n", s, getcallerpc(&s));
  266. p->mlen = 1;
  267. p->malen = PATHMSLOP;
  268. p->mtpt = smalloc(p->malen*sizeof p->mtpt[0]);
  269. return p;
  270. }
  271. static Path*
  272. copypath(Path *p)
  273. {
  274. int i;
  275. Path *pp;
  276. pp = smalloc(sizeof(Path));
  277. pp->ref = 1;
  278. incref(&npath);
  279. DBG("copypath %s %p => %p\n", p->s, p, pp);
  280. pp->len = p->len;
  281. pp->alen = p->alen;
  282. pp->s = smalloc(p->alen);
  283. memmove(pp->s, p->s, p->len+1);
  284. pp->mlen = p->mlen;
  285. pp->malen = p->malen;
  286. pp->mtpt = smalloc(p->malen*sizeof pp->mtpt[0]);
  287. for(i=0; i<pp->mlen; i++){
  288. pp->mtpt[i] = p->mtpt[i];
  289. if(pp->mtpt[i])
  290. incref(pp->mtpt[i]);
  291. }
  292. return pp;
  293. }
  294. void
  295. pathclose(Path *p)
  296. {
  297. int i;
  298. if(p == nil)
  299. return;
  300. //XXX
  301. DBG("pathclose %p %s ref=%ld =>", p, p->s, p->ref);
  302. for(i=0; i<p->mlen; i++)
  303. DBG(" %p", p->mtpt[i]);
  304. DBG("\n");
  305. if(decref(p))
  306. return;
  307. decref(&npath);
  308. free(p->s);
  309. for(i=0; i<p->mlen; i++)
  310. if(p->mtpt[i])
  311. cclose(p->mtpt[i]);
  312. free(p->mtpt);
  313. free(p);
  314. }
  315. /*
  316. * In place, rewrite name to compress multiple /, eliminate ., and process ..
  317. * (Really only called to remove a trailing .. that has been added.
  318. * Otherwise would need to update n->mtpt as well.)
  319. */
  320. static void
  321. fixdotdotname(Path *p)
  322. {
  323. char *r;
  324. if(p->s[0] == '#'){
  325. r = strchr(p->s, '/');
  326. if(r == nil)
  327. return;
  328. cleanname(r);
  329. /*
  330. * The correct name is #i rather than #i/,
  331. * but the correct name of #/ is #/.
  332. */
  333. if(strcmp(r, "/")==0 && p->s[1] != '/')
  334. *r = '\0';
  335. }else
  336. cleanname(p->s);
  337. p->len = strlen(p->s);
  338. }
  339. static Path*
  340. uniquepath(Path *p)
  341. {
  342. Path *new;
  343. if(p->ref > 1){
  344. /* copy on write */
  345. new = copypath(p);
  346. pathclose(p);
  347. p = new;
  348. }
  349. return p;
  350. }
  351. static Path*
  352. addelem(Path *p, char *s, Chan *from)
  353. {
  354. char *t;
  355. int a, i;
  356. Chan *c, **tt;
  357. if(s[0]=='.' && s[1]=='\0')
  358. return p;
  359. p = uniquepath(p);
  360. i = strlen(s);
  361. if(p->len+1+i+1 > p->alen){
  362. a = p->len+1+i+1 + PATHSLOP;
  363. t = smalloc(a);
  364. memmove(t, p->s, p->len+1);
  365. free(p->s);
  366. p->s = t;
  367. p->alen = a;
  368. }
  369. /* don't insert extra slash if one is present */
  370. if(p->len>0 && p->s[p->len-1]!='/' && s[0]!='/')
  371. p->s[p->len++] = '/';
  372. memmove(p->s+p->len, s, i+1);
  373. p->len += i;
  374. if(isdotdot(s)){
  375. fixdotdotname(p);
  376. DBG("addelem %s .. => rm %p\n", p->s, p->mtpt[p->mlen-1]);
  377. if(p->mlen>1 && (c = p->mtpt[--p->mlen])){
  378. p->mtpt[p->mlen] = nil;
  379. cclose(c);
  380. }
  381. }else{
  382. if(p->mlen >= p->malen){
  383. p->malen = p->mlen+1+PATHMSLOP;
  384. tt = smalloc(p->malen*sizeof tt[0]);
  385. memmove(tt, p->mtpt, p->mlen*sizeof tt[0]);
  386. free(p->mtpt);
  387. p->mtpt = tt;
  388. }
  389. DBG("addelem %s %s => add %p\n", p->s, s, from);
  390. p->mtpt[p->mlen++] = from;
  391. if(from)
  392. incref(from);
  393. }
  394. return p;
  395. }
  396. void
  397. chanfree(Chan *c)
  398. {
  399. c->flag = CFREE;
  400. if(c->dirrock != nil){
  401. free(c->dirrock);
  402. c->dirrock = 0;
  403. c->nrock = 0;
  404. c->mrock = 0;
  405. }
  406. if(c->umh != nil){
  407. putmhead(c->umh);
  408. c->umh = nil;
  409. }
  410. if(c->umc != nil){
  411. cclose(c->umc);
  412. c->umc = nil;
  413. }
  414. if(c->mux != nil){
  415. muxclose(c->mux);
  416. c->mux = nil;
  417. }
  418. if(c->mchan != nil){
  419. cclose(c->mchan);
  420. c->mchan = nil;
  421. }
  422. pathclose(c->path);
  423. c->path = nil;
  424. lock(&chanalloc);
  425. c->next = chanalloc.free;
  426. chanalloc.free = c;
  427. unlock(&chanalloc);
  428. }
  429. void
  430. cclose(Chan *c)
  431. {
  432. if(c->flag&CFREE)
  433. panic("cclose %#p", getcallerpc(&c));
  434. DBG("cclose %p name=%s ref=%ld\n", c, c->path->s, c->ref);
  435. if(decref(c))
  436. return;
  437. if(!waserror()){
  438. devtab[c->type]->close(c);
  439. poperror();
  440. }
  441. chanfree(c);
  442. }
  443. /*
  444. * Queue a chan to be closed by one of the clunk procs.
  445. */
  446. struct {
  447. Chan *head;
  448. Chan *tail;
  449. int nqueued;
  450. int nclosed;
  451. Lock l;
  452. QLock q;
  453. Rendez r;
  454. } clunkq;
  455. void closeproc(void*);
  456. void
  457. ccloseq(Chan *c)
  458. {
  459. if(c->flag&CFREE)
  460. panic("cclose %#p", getcallerpc(&c));
  461. DBG("ccloseq %p name=%s ref=%ld\n", c, c->path->s, c->ref);
  462. if(decref(c))
  463. return;
  464. lock(&clunkq.l);
  465. clunkq.nqueued++;
  466. c->next = nil;
  467. if(clunkq.head)
  468. clunkq.tail->next = c;
  469. else
  470. clunkq.head = c;
  471. clunkq.tail = c;
  472. unlock(&clunkq.l);
  473. if(!wakeup(&clunkq.r))
  474. kproc("closeproc", closeproc, nil);
  475. }
  476. static int
  477. clunkwork(void*)
  478. {
  479. return clunkq.head != nil;
  480. }
  481. void
  482. closeproc(void*)
  483. {
  484. Chan *c;
  485. for(;;){
  486. qlock(&clunkq.q);
  487. if(clunkq.head == nil){
  488. if(!waserror()){
  489. tsleep(&clunkq.r, clunkwork, nil, 5000);
  490. poperror();
  491. }
  492. if(clunkq.head == nil){
  493. qunlock(&clunkq.q);
  494. pexit("no work", 1);
  495. }
  496. }
  497. lock(&clunkq.l);
  498. c = clunkq.head;
  499. clunkq.head = c->next;
  500. clunkq.nclosed++;
  501. unlock(&clunkq.l);
  502. qunlock(&clunkq.q);
  503. if(!waserror()){
  504. devtab[c->type]->close(c);
  505. poperror();
  506. }
  507. chanfree(c);
  508. }
  509. }
  510. /*
  511. * Make sure we have the only copy of c. (Copy on write.)
  512. */
  513. Chan*
  514. cunique(Chan *c)
  515. {
  516. Chan *nc;
  517. if(c->ref != 1){
  518. nc = cclone(c);
  519. cclose(c);
  520. c = nc;
  521. }
  522. return c;
  523. }
  524. int
  525. eqqid(Qid a, Qid b)
  526. {
  527. return a.path==b.path && a.vers==b.vers;
  528. }
  529. int
  530. eqchan(Chan *a, Chan *b, int skipvers)
  531. {
  532. if(a->qid.path != b->qid.path)
  533. return 0;
  534. if(!skipvers && a->qid.vers!=b->qid.vers)
  535. return 0;
  536. if(a->type != b->type)
  537. return 0;
  538. if(a->dev != b->dev)
  539. return 0;
  540. return 1;
  541. }
  542. int
  543. eqchantdqid(Chan *a, int type, int dev, Qid qid, int skipvers)
  544. {
  545. if(a->qid.path != qid.path)
  546. return 0;
  547. if(!skipvers && a->qid.vers!=qid.vers)
  548. return 0;
  549. if(a->type != type)
  550. return 0;
  551. if(a->dev != dev)
  552. return 0;
  553. return 1;
  554. }
  555. Mhead*
  556. newmhead(Chan *from)
  557. {
  558. Mhead *mh;
  559. mh = smalloc(sizeof(Mhead));
  560. mh->ref = 1;
  561. mh->from = from;
  562. incref(from);
  563. return mh;
  564. }
  565. int
  566. cmount(Chan **newp, Chan *old, int flag, char *spec)
  567. {
  568. int order, flg;
  569. Chan *new;
  570. Mhead *m, **l, *mh;
  571. Mount *nm, *f, *um, **h;
  572. Pgrp *pg;
  573. if(QTDIR & (old->qid.type^(*newp)->qid.type))
  574. error(Emount);
  575. if(old->umh)
  576. print("cmount: unexpected umh, caller %#p\n", getcallerpc(&newp));
  577. order = flag&MORDER;
  578. if((old->qid.type&QTDIR)==0 && order != MREPL)
  579. error(Emount);
  580. new = *newp;
  581. mh = new->umh;
  582. /*
  583. * Not allowed to bind when the old directory is itself a union.
  584. * (Maybe it should be allowed, but I don't see what the semantics
  585. * would be.)
  586. *
  587. * We need to check mh->mount->next to tell unions apart from
  588. * simple mount points, so that things like
  589. * mount -c fd /root
  590. * bind -c /root /
  591. * work.
  592. *
  593. * The check of mount->mflag allows things like
  594. * mount fd /root
  595. * bind -c /root /
  596. *
  597. * This is far more complicated than it should be, but I don't
  598. * see an easier way at the moment.
  599. */
  600. if((flag&MCREATE) && mh && mh->mount
  601. && (mh->mount->next || !(mh->mount->mflag&MCREATE)))
  602. error(Emount);
  603. pg = up->pgrp;
  604. wlock(&pg->ns);
  605. l = &MOUNTH(pg, old->qid);
  606. for(m = *l; m; m = m->hash){
  607. if(eqchan(m->from, old, 1))
  608. break;
  609. l = &m->hash;
  610. }
  611. if(m == nil){
  612. /*
  613. * nothing mounted here yet. create a mount
  614. * head and add to the hash table.
  615. */
  616. m = newmhead(old);
  617. *l = m;
  618. /*
  619. * if this is a union mount, add the old
  620. * node to the mount chain.
  621. */
  622. if(order != MREPL)
  623. m->mount = newmount(m, old, 0, 0);
  624. }
  625. wlock(&m->lock);
  626. if(waserror()){
  627. wunlock(&m->lock);
  628. nexterror();
  629. }
  630. wunlock(&pg->ns);
  631. nm = newmount(m, new, flag, spec);
  632. if(mh != nil && mh->mount != nil){
  633. /*
  634. * copy a union when binding it onto a directory
  635. */
  636. flg = order;
  637. if(order == MREPL)
  638. flg = MAFTER;
  639. h = &nm->next;
  640. um = mh->mount;
  641. for(um = um->next; um; um = um->next){
  642. f = newmount(m, um->to, flg, um->spec);
  643. *h = f;
  644. h = &f->next;
  645. }
  646. }
  647. if(m->mount && order == MREPL){
  648. mountfree(m->mount);
  649. m->mount = 0;
  650. }
  651. if(flag & MCREATE)
  652. nm->mflag |= MCREATE;
  653. if(m->mount && order == MAFTER){
  654. for(f = m->mount; f->next; f = f->next)
  655. ;
  656. f->next = nm;
  657. }else{
  658. for(f = nm; f->next; f = f->next)
  659. ;
  660. f->next = m->mount;
  661. m->mount = nm;
  662. }
  663. wunlock(&m->lock);
  664. poperror();
  665. return nm->mountid;
  666. }
  667. void
  668. cunmount(Chan *mnt, Chan *mounted)
  669. {
  670. Pgrp *pg;
  671. Mhead *m, **l;
  672. Mount *f, **p;
  673. if(mnt->umh) /* should not happen */
  674. print("cunmount newp extra umh %p has %p\n", mnt, mnt->umh);
  675. /*
  676. * It _can_ happen that mounted->umh is non-nil,
  677. * because mounted is the result of namec(Aopen)
  678. * (see sysfile.c:/^sysunmount).
  679. * If we open a union directory, it will have a umh.
  680. * Although surprising, this is okay, since the
  681. * cclose will take care of freeing the umh.
  682. */
  683. pg = up->pgrp;
  684. wlock(&pg->ns);
  685. l = &MOUNTH(pg, mnt->qid);
  686. for(m = *l; m; m = m->hash){
  687. if(eqchan(m->from, mnt, 1))
  688. break;
  689. l = &m->hash;
  690. }
  691. if(m == 0){
  692. wunlock(&pg->ns);
  693. error(Eunmount);
  694. }
  695. wlock(&m->lock);
  696. if(mounted == 0){
  697. *l = m->hash;
  698. wunlock(&pg->ns);
  699. mountfree(m->mount);
  700. m->mount = nil;
  701. cclose(m->from);
  702. wunlock(&m->lock);
  703. putmhead(m);
  704. return;
  705. }
  706. p = &m->mount;
  707. for(f = *p; f; f = f->next){
  708. /* BUG: Needs to be 2 pass */
  709. if(eqchan(f->to, mounted, 1) ||
  710. (f->to->mchan && eqchan(f->to->mchan, mounted, 1))){
  711. *p = f->next;
  712. f->next = 0;
  713. mountfree(f);
  714. if(m->mount == nil){
  715. *l = m->hash;
  716. cclose(m->from);
  717. wunlock(&m->lock);
  718. wunlock(&pg->ns);
  719. putmhead(m);
  720. return;
  721. }
  722. wunlock(&m->lock);
  723. wunlock(&pg->ns);
  724. return;
  725. }
  726. p = &f->next;
  727. }
  728. wunlock(&m->lock);
  729. wunlock(&pg->ns);
  730. error(Eunion);
  731. }
  732. Chan*
  733. cclone(Chan *c)
  734. {
  735. Chan *nc;
  736. Walkqid *wq;
  737. wq = devtab[c->type]->walk(c, nil, nil, 0);
  738. if(wq == nil)
  739. error("clone failed");
  740. nc = wq->clone;
  741. free(wq);
  742. nc->path = c->path;
  743. if(c->path)
  744. incref(c->path);
  745. return nc;
  746. }
  747. /* also used by sysfile.c:/^mountfix */
  748. int
  749. findmount(Chan **cp, Mhead **mp, int type, int dev, Qid qid)
  750. {
  751. Pgrp *pg;
  752. Mhead *m;
  753. pg = up->pgrp;
  754. rlock(&pg->ns);
  755. for(m = MOUNTH(pg, qid); m; m = m->hash){
  756. rlock(&m->lock);
  757. if(m->from == nil){
  758. print("m %p m->from 0\n", m);
  759. runlock(&m->lock);
  760. continue;
  761. }
  762. if(eqchantdqid(m->from, type, dev, qid, 1)){
  763. runlock(&pg->ns);
  764. if(mp != nil){
  765. incref(m);
  766. if(*mp != nil)
  767. putmhead(*mp);
  768. *mp = m;
  769. }
  770. if(*cp != nil)
  771. cclose(*cp);
  772. incref(m->mount->to);
  773. *cp = m->mount->to;
  774. runlock(&m->lock);
  775. return 1;
  776. }
  777. runlock(&m->lock);
  778. }
  779. runlock(&pg->ns);
  780. return 0;
  781. }
  782. /*
  783. * Calls findmount but also updates path.
  784. */
  785. static int
  786. domount(Chan **cp, Mhead **mp, Path **path)
  787. {
  788. Chan **lc;
  789. Path *p;
  790. if(findmount(cp, mp, (*cp)->type, (*cp)->dev, (*cp)->qid) == 0)
  791. return 0;
  792. if(path){
  793. p = *path;
  794. p = uniquepath(p);
  795. if(p->mlen <= 0)
  796. print("domount: path %s has mlen==%d\n", p->s, p->mlen);
  797. else{
  798. lc = &p->mtpt[p->mlen-1];
  799. DBG("domount %p %s => add %p (was %p)\n", p, p->s, (*mp)->from, p->mtpt[p->mlen-1]);
  800. incref((*mp)->from);
  801. if(*lc)
  802. cclose(*lc);
  803. *lc = (*mp)->from;
  804. }
  805. *path = p;
  806. }
  807. return 1;
  808. }
  809. /*
  810. * If c is the right-hand-side of a mount point, returns the left hand side.
  811. * Changes name to reflect the fact that we've uncrossed the mountpoint,
  812. * so name had better be ours to change!
  813. */
  814. static Chan*
  815. undomount(Chan *c, Path *path)
  816. {
  817. Chan *nc;
  818. if(path->ref != 1 || path->mlen == 0)
  819. print("undomount: path %s ref %ld mlen %d caller %#p\n",
  820. path->s, path->ref, path->mlen, getcallerpc(&c));
  821. if(path->mlen>0 && (nc=path->mtpt[path->mlen-1]) != nil){
  822. DBG("undomount %p %s => remove %p\n", path, path->s, nc);
  823. cclose(c);
  824. path->mtpt[path->mlen-1] = nil;
  825. c = nc;
  826. }
  827. return c;
  828. }
  829. /*
  830. * Call dev walk but catch errors.
  831. */
  832. static Walkqid*
  833. ewalk(Chan *c, Chan *nc, char **name, int nname)
  834. {
  835. Walkqid *wq;
  836. if(waserror())
  837. return nil;
  838. wq = devtab[c->type]->walk(c, nc, name, nname);
  839. poperror();
  840. return wq;
  841. }
  842. /*
  843. * Either walks all the way or not at all. No partial results in *cp.
  844. * *nerror is the number of names to display in an error message.
  845. */
  846. static char Edoesnotexist[] = "does not exist";
  847. int
  848. walk(Chan **cp, char **names, int nnames, int nomount, int *nerror)
  849. {
  850. int dev, didmount, dotdot, i, n, nhave, ntry, type;
  851. Chan *c, *nc, *mtpt;
  852. Path *path;
  853. Mhead *mh, *nmh;
  854. Mount *f;
  855. Walkqid *wq;
  856. c = *cp;
  857. incref(c);
  858. path = c->path;
  859. incref(path);
  860. mh = nil;
  861. /*
  862. * While we haven't gotten all the way down the path:
  863. * 1. step through a mount point, if any
  864. * 2. send a walk request for initial dotdot or initial prefix without dotdot
  865. * 3. move to the first mountpoint along the way.
  866. * 4. repeat.
  867. *
  868. * Each time through the loop:
  869. *
  870. * If didmount==0, c is on the undomount side of the mount point.
  871. * If didmount==1, c is on the domount side of the mount point.
  872. * Either way, c's full path is path.
  873. */
  874. didmount = 0;
  875. for(nhave=0; nhave<nnames; nhave+=n){
  876. if((c->qid.type&QTDIR)==0){
  877. if(nerror)
  878. *nerror = nhave;
  879. pathclose(path);
  880. cclose(c);
  881. strcpy(up->errstr, Enotdir);
  882. if(mh != nil)
  883. putmhead(mh);
  884. return -1;
  885. }
  886. ntry = nnames - nhave;
  887. if(ntry > MAXWELEM)
  888. ntry = MAXWELEM;
  889. dotdot = 0;
  890. for(i=0; i<ntry; i++){
  891. if(isdotdot(names[nhave+i])){
  892. if(i==0){
  893. dotdot = 1;
  894. ntry = 1;
  895. }else
  896. ntry = i;
  897. break;
  898. }
  899. }
  900. if(!dotdot && !nomount && !didmount)
  901. domount(&c, &mh, &path);
  902. type = c->type;
  903. dev = c->dev;
  904. if((wq = ewalk(c, nil, names+nhave, ntry)) == nil){
  905. /* try a union mount, if any */
  906. if(mh && !nomount){
  907. /*
  908. * mh->mount->to == c, so start at mh->mount->next
  909. */
  910. rlock(&mh->lock);
  911. f = mh->mount;
  912. for(f = (f? f->next: f); f; f = f->next)
  913. if((wq = ewalk(f->to, nil, names+nhave, ntry)) != nil)
  914. break;
  915. runlock(&mh->lock);
  916. if(f != nil){
  917. type = f->to->type;
  918. dev = f->to->dev;
  919. }
  920. }
  921. if(wq == nil){
  922. cclose(c);
  923. pathclose(path);
  924. if(nerror)
  925. *nerror = nhave+1;
  926. if(mh != nil)
  927. putmhead(mh);
  928. return -1;
  929. }
  930. }
  931. didmount = 0;
  932. if(dotdot){
  933. assert(wq->nqid == 1);
  934. assert(wq->clone != nil);
  935. path = addelem(path, "..", nil);
  936. nc = undomount(wq->clone, path);
  937. nmh = nil;
  938. n = 1;
  939. }else{
  940. nc = nil;
  941. nmh = nil;
  942. if(!nomount){
  943. for(i=0; i<wq->nqid && i<ntry-1; i++){
  944. if(findmount(&nc, &nmh, type, dev, wq->qid[i])){
  945. didmount = 1;
  946. break;
  947. }
  948. }
  949. }
  950. if(nc == nil){ /* no mount points along path */
  951. if(wq->clone == nil){
  952. cclose(c);
  953. pathclose(path);
  954. if(wq->nqid==0 || (wq->qid[wq->nqid-1].type&QTDIR)){
  955. if(nerror)
  956. *nerror = nhave+wq->nqid+1;
  957. strcpy(up->errstr, Edoesnotexist);
  958. }else{
  959. if(nerror)
  960. *nerror = nhave+wq->nqid;
  961. strcpy(up->errstr, Enotdir);
  962. }
  963. free(wq);
  964. if(mh != nil)
  965. putmhead(mh);
  966. return -1;
  967. }
  968. n = wq->nqid;
  969. nc = wq->clone;
  970. }else{ /* stopped early, at a mount point */
  971. didmount = 1;
  972. if(wq->clone != nil){
  973. cclose(wq->clone);
  974. wq->clone = nil;
  975. }
  976. n = i+1;
  977. }
  978. for(i=0; i<n; i++){
  979. mtpt = nil;
  980. if(i==n-1 && nmh)
  981. mtpt = nmh->from;
  982. path = addelem(path, names[nhave+i], mtpt);
  983. }
  984. }
  985. cclose(c);
  986. c = nc;
  987. putmhead(mh);
  988. mh = nmh;
  989. free(wq);
  990. }
  991. putmhead(mh);
  992. c = cunique(c);
  993. if(c->umh != nil){ //BUG
  994. print("walk umh\n");
  995. putmhead(c->umh);
  996. c->umh = nil;
  997. }
  998. pathclose(c->path);
  999. c->path = path;
  1000. cclose(*cp);
  1001. *cp = c;
  1002. if(nerror)
  1003. *nerror = nhave;
  1004. return 0;
  1005. }
  1006. /*
  1007. * c is a mounted non-creatable directory. find a creatable one.
  1008. */
  1009. Chan*
  1010. createdir(Chan *c, Mhead *m)
  1011. {
  1012. Chan *nc;
  1013. Mount *f;
  1014. rlock(&m->lock);
  1015. if(waserror()){
  1016. runlock(&m->lock);
  1017. nexterror();
  1018. }
  1019. for(f = m->mount; f; f = f->next){
  1020. if(f->mflag&MCREATE){
  1021. nc = cclone(f->to);
  1022. runlock(&m->lock);
  1023. poperror();
  1024. cclose(c);
  1025. return nc;
  1026. }
  1027. }
  1028. error(Enocreate);
  1029. return 0;
  1030. }
  1031. void
  1032. saveregisters(void)
  1033. {
  1034. }
  1035. static void
  1036. growparse(Elemlist *e)
  1037. {
  1038. char **new;
  1039. int *inew;
  1040. enum { Delta = 8 };
  1041. if(e->nelems % Delta == 0){
  1042. new = smalloc((e->nelems+Delta) * sizeof(char*));
  1043. memmove(new, e->elems, e->nelems*sizeof(char*));
  1044. free(e->elems);
  1045. e->elems = new;
  1046. inew = smalloc((e->nelems+Delta+1) * sizeof(int));
  1047. memmove(inew, e->off, (e->nelems+1)*sizeof(int));
  1048. free(e->off);
  1049. e->off = inew;
  1050. }
  1051. }
  1052. /*
  1053. * The name is known to be valid.
  1054. * Copy the name so slashes can be overwritten.
  1055. * An empty string will set nelem=0.
  1056. * A path ending in / or /. or /.//./ etc. will have
  1057. * e.mustbedir = 1, so that we correctly
  1058. * reject, e.g., "/adm/users/." when /adm/users is a file
  1059. * rather than a directory.
  1060. */
  1061. static void
  1062. parsename(char *aname, Elemlist *e)
  1063. {
  1064. char *name, *slash;
  1065. kstrdup(&e->name, aname);
  1066. name = e->name;
  1067. e->nelems = 0;
  1068. e->elems = nil;
  1069. e->off = smalloc(sizeof(int));
  1070. e->off[0] = skipslash(name) - name;
  1071. for(;;){
  1072. name = skipslash(name);
  1073. if(*name == '\0'){
  1074. e->off[e->nelems] = name+strlen(name) - e->name;
  1075. e->mustbedir = 1;
  1076. break;
  1077. }
  1078. growparse(e);
  1079. e->elems[e->nelems++] = name;
  1080. slash = utfrune(name, '/');
  1081. if(slash == nil){
  1082. e->off[e->nelems] = name+strlen(name) - e->name;
  1083. e->mustbedir = 0;
  1084. break;
  1085. }
  1086. e->off[e->nelems] = slash - e->name;
  1087. *slash++ = '\0';
  1088. name = slash;
  1089. }
  1090. if(0 && chandebug){
  1091. int i;
  1092. print("parsename %s:", e->name);
  1093. for(i=0; i<=e->nelems; i++)
  1094. print(" %d", e->off[i]);
  1095. print("\n");
  1096. }
  1097. }
  1098. void*
  1099. memrchr(void *va, int c, long n)
  1100. {
  1101. uchar *a, *e;
  1102. a = va;
  1103. for(e=a+n-1; e>a; e--)
  1104. if(*e == c)
  1105. return e;
  1106. return nil;
  1107. }
  1108. void
  1109. namelenerror(char *aname, int len, char *err)
  1110. {
  1111. char *ename, *name, *next;
  1112. int i, errlen;
  1113. /*
  1114. * If the name is short enough, just use the whole thing.
  1115. */
  1116. errlen = strlen(err);
  1117. if(len < ERRMAX/3 || len+errlen < 2*ERRMAX/3)
  1118. snprint(up->genbuf, sizeof up->genbuf, "%.*s",
  1119. utfnlen(aname, len), aname);
  1120. else{
  1121. /*
  1122. * Print a suffix of the name, but try to get a little info.
  1123. */
  1124. ename = aname+len;
  1125. next = ename;
  1126. do{
  1127. name = next;
  1128. next = memrchr(aname, '/', name-aname);
  1129. if(next == nil)
  1130. next = aname;
  1131. len = ename-next;
  1132. }while(len < ERRMAX/3 || len + errlen < 2*ERRMAX/3);
  1133. /*
  1134. * If the name is ridiculously long, chop it.
  1135. */
  1136. if(name == ename){
  1137. name = ename-ERRMAX/4;
  1138. if(name <= aname)
  1139. panic("bad math in namelenerror");
  1140. /* walk out of current UTF sequence */
  1141. for(i=0; (*name&0xC0)==0x80 && i<UTFmax; i++)
  1142. name++;
  1143. }
  1144. snprint(up->genbuf, sizeof up->genbuf, "...%.*s",
  1145. utfnlen(name, ename-name), name);
  1146. }
  1147. snprint(up->errstr, ERRMAX, "%#q %s", up->genbuf, err);
  1148. nexterror();
  1149. }
  1150. void
  1151. nameerror(char *name, char *err)
  1152. {
  1153. namelenerror(name, strlen(name), err);
  1154. }
  1155. /*
  1156. * Turn a name into a channel.
  1157. * &name[0] is known to be a valid address. It may be a kernel address.
  1158. *
  1159. * Opening with amode Aopen, Acreate, Aremove, or Aaccess guarantees
  1160. * that the result will be the only reference to that particular fid.
  1161. * This is necessary since we might pass the result to
  1162. * devtab[]->remove().
  1163. *
  1164. * Opening Atodir or Amount does not guarantee this.
  1165. *
  1166. * Under certain circumstances, opening Aaccess will cause
  1167. * an unnecessary clone in order to get a cunique Chan so it
  1168. * can attach the correct name. Sysstat and sys_stat need the
  1169. * correct name so they can rewrite the stat info.
  1170. */
  1171. Chan*
  1172. namec(char *aname, int amode, int omode, ulong perm)
  1173. {
  1174. int len, n, t, nomount;
  1175. Chan *c, *cnew;
  1176. Path *path;
  1177. Elemlist e;
  1178. Rune r;
  1179. Mhead *m;
  1180. char *createerr, tmperrbuf[ERRMAX];
  1181. char *name;
  1182. if(aname[0] == '\0')
  1183. error("empty file name");
  1184. aname = validnamedup(aname, 1);
  1185. if(waserror()){
  1186. free(aname);
  1187. nexterror();
  1188. }
  1189. DBG("namec %s %d %d\n", aname, amode, omode);
  1190. name = aname;
  1191. /*
  1192. * Find the starting off point (the current slash, the root of
  1193. * a device tree, or the current dot) as well as the name to
  1194. * evaluate starting there.
  1195. */
  1196. nomount = 0;
  1197. switch(name[0]){
  1198. case '/':
  1199. c = up->slash;
  1200. incref(c);
  1201. break;
  1202. case '#':
  1203. nomount = 1;
  1204. up->genbuf[0] = '\0';
  1205. n = 0;
  1206. while(*name != '\0' && (*name != '/' || n < 2)){
  1207. if(n >= sizeof(up->genbuf)-1)
  1208. error(Efilename);
  1209. up->genbuf[n++] = *name++;
  1210. }
  1211. up->genbuf[n] = '\0';
  1212. /*
  1213. * noattach is sandboxing.
  1214. *
  1215. * the OK exceptions are:
  1216. * | it only gives access to pipes you create
  1217. * d this process's file descriptors
  1218. * e this process's environment
  1219. * the iffy exceptions are:
  1220. * c time and pid, but also cons and consctl
  1221. * p control of your own processes (and unfortunately
  1222. * any others left unprotected)
  1223. */
  1224. n = chartorune(&r, up->genbuf+1)+1;
  1225. /* actually / is caught by parsing earlier */
  1226. if(utfrune("M", r))
  1227. error(Enoattach);
  1228. if(up->pgrp->noattach && utfrune("|decp", r)==nil)
  1229. error(Enoattach);
  1230. t = devno(r, 1);
  1231. if(t == -1)
  1232. error(Ebadsharp);
  1233. c = devtab[t]->attach(up->genbuf+n);
  1234. break;
  1235. default:
  1236. c = up->dot;
  1237. incref(c);
  1238. break;
  1239. }
  1240. e.aname = aname;
  1241. e.prefix = name - aname;
  1242. e.name = nil;
  1243. e.elems = nil;
  1244. e.off = nil;
  1245. e.nelems = 0;
  1246. e.nerror = 0;
  1247. if(waserror()){
  1248. cclose(c);
  1249. free(e.name);
  1250. free(e.elems);
  1251. /*
  1252. * Prepare nice error, showing first e.nerror elements of name.
  1253. */
  1254. if(e.nerror == 0)
  1255. nexterror();
  1256. strcpy(tmperrbuf, up->errstr);
  1257. if(e.off[e.nerror]==0)
  1258. print("nerror=%d but off=%d\n",
  1259. e.nerror, e.off[e.nerror]);
  1260. if(0 && chandebug)
  1261. 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]);
  1262. len = e.prefix+e.off[e.nerror];
  1263. free(e.off);
  1264. namelenerror(aname, len, tmperrbuf);
  1265. }
  1266. /*
  1267. * Build a list of elements in the name.
  1268. */
  1269. parsename(name, &e);
  1270. /*
  1271. * On create, ....
  1272. */
  1273. if(amode == Acreate){
  1274. /* perm must have DMDIR if last element is / or /. */
  1275. if(e.mustbedir && !(perm&DMDIR)){
  1276. e.nerror = e.nelems;
  1277. error("create without DMDIR");
  1278. }
  1279. /* don't try to walk the last path element just yet. */
  1280. if(e.nelems == 0)
  1281. error(Eexist);
  1282. e.nelems--;
  1283. }
  1284. if(walk(&c, e.elems, e.nelems, nomount, &e.nerror) < 0){
  1285. if(e.nerror < 0 || e.nerror > e.nelems){
  1286. print("namec %s walk error nerror=%d\n", aname, e.nerror);
  1287. e.nerror = 0;
  1288. }
  1289. nexterror();
  1290. }
  1291. if(e.mustbedir && !(c->qid.type&QTDIR))
  1292. error("not a directory");
  1293. if(amode == Aopen && (omode&3) == OEXEC && (c->qid.type&QTDIR))
  1294. error("cannot exec directory");
  1295. switch(amode){
  1296. case Abind:
  1297. /* no need to maintain path - cannot dotdot an Abind */
  1298. m = nil;
  1299. if(!nomount)
  1300. domount(&c, &m, nil);
  1301. if(c->umh != nil)
  1302. putmhead(c->umh);
  1303. c->umh = m;
  1304. break;
  1305. case Aaccess:
  1306. case Aremove:
  1307. case Aopen:
  1308. Open:
  1309. /* save&update the name; domount might change c */
  1310. path = c->path;
  1311. incref(path);
  1312. m = nil;
  1313. if(!nomount)
  1314. domount(&c, &m, &path);
  1315. /* our own copy to open or remove */
  1316. c = cunique(c);
  1317. /* now it's our copy anyway, we can put the name back */
  1318. pathclose(c->path);
  1319. c->path = path;
  1320. /* record whether c is on a mount point */
  1321. c->ismtpt = m!=nil;
  1322. switch(amode){
  1323. case Aaccess:
  1324. case Aremove:
  1325. putmhead(m);
  1326. break;
  1327. case Aopen:
  1328. case Acreate:
  1329. if(c->umh != nil){
  1330. print("cunique umh Open\n");
  1331. putmhead(c->umh);
  1332. c->umh = nil;
  1333. }
  1334. /* only save the mount head if it's a multiple element union */
  1335. if(m && m->mount && m->mount->next)
  1336. c->umh = m;
  1337. else
  1338. putmhead(m);
  1339. /* save registers else error() in open has wrong value of c saved */
  1340. saveregisters();
  1341. if(omode == OEXEC)
  1342. c->flag &= ~CCACHE;
  1343. c = devtab[c->type]->open(c, omode&~OCEXEC);
  1344. if(omode & OCEXEC)
  1345. c->flag |= CCEXEC;
  1346. if(omode & ORCLOSE)
  1347. c->flag |= CRCLOSE;
  1348. break;
  1349. }
  1350. break;
  1351. case Atodir:
  1352. /*
  1353. * Directories (e.g. for cd) are left before the mount point,
  1354. * so one may mount on / or . and see the effect.
  1355. */
  1356. if(!(c->qid.type & QTDIR))
  1357. error(Enotdir);
  1358. break;
  1359. case Amount:
  1360. /*
  1361. * When mounting on an already mounted upon directory,
  1362. * one wants subsequent mounts to be attached to the
  1363. * original directory, not the replacement. Don't domount.
  1364. */
  1365. break;
  1366. case Acreate:
  1367. /*
  1368. * We've already walked all but the last element.
  1369. * If the last exists, try to open it OTRUNC.
  1370. * If omode&OEXCL is set, just give up.
  1371. */
  1372. e.nelems++;
  1373. e.nerror++;
  1374. if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) == 0){
  1375. if(omode&OEXCL)
  1376. error(Eexist);
  1377. omode |= OTRUNC;
  1378. goto Open;
  1379. }
  1380. /*
  1381. * The semantics of the create(2) system call are that if the
  1382. * file exists and can be written, it is to be opened with truncation.
  1383. * On the other hand, the create(5) message fails if the file exists.
  1384. * If we get two create(2) calls happening simultaneously,
  1385. * they might both get here and send create(5) messages, but only
  1386. * one of the messages will succeed. To provide the expected create(2)
  1387. * semantics, the call with the failed message needs to try the above
  1388. * walk again, opening for truncation. This correctly solves the
  1389. * create/create race, in the sense that any observable outcome can
  1390. * be explained as one happening before the other.
  1391. * The create/create race is quite common. For example, it happens
  1392. * when two rc subshells simultaneously update the same
  1393. * environment variable.
  1394. *
  1395. * The implementation still admits a create/create/remove race:
  1396. * (A) walk to file, fails
  1397. * (B) walk to file, fails
  1398. * (A) create file, succeeds, returns
  1399. * (B) create file, fails
  1400. * (A) remove file, succeeds, returns
  1401. * (B) walk to file, return failure.
  1402. *
  1403. * This is hardly as common as the create/create race, and is really
  1404. * not too much worse than what might happen if (B) got a hold of a
  1405. * file descriptor and then the file was removed -- either way (B) can't do
  1406. * anything with the result of the create call. So we don't care about this race.
  1407. *
  1408. * Applications that care about more fine-grained decision of the races
  1409. * can use the OEXCL flag to get at the underlying create(5) semantics;
  1410. * by default we provide the common case.
  1411. *
  1412. * We need to stay behind the mount point in case we
  1413. * need to do the first walk again (should the create fail).
  1414. *
  1415. * We also need to cross the mount point and find the directory
  1416. * in the union in which we should be creating.
  1417. *
  1418. * The channel staying behind is c, the one moving forward is cnew.
  1419. */
  1420. m = nil;
  1421. cnew = nil; /* is this assignment necessary? */
  1422. if(!waserror()){ /* try create */
  1423. if(!nomount && findmount(&cnew, &m, c->type, c->dev, c->qid))
  1424. cnew = createdir(cnew, m);
  1425. else{
  1426. cnew = c;
  1427. incref(cnew);
  1428. }
  1429. /*
  1430. * We need our own copy of the Chan because we're
  1431. * about to send a create, which will move it. Once we have
  1432. * our own copy, we can fix the name, which might be wrong
  1433. * if findmount gave us a new Chan.
  1434. */
  1435. cnew = cunique(cnew);
  1436. pathclose(cnew->path);
  1437. cnew->path = c->path;
  1438. incref(cnew->path);
  1439. devtab[cnew->type]->create(cnew, e.elems[e.nelems-1], omode&~(OEXCL|OCEXEC), perm);
  1440. poperror();
  1441. if(omode & OCEXEC)
  1442. cnew->flag |= CCEXEC;
  1443. if(omode & ORCLOSE)
  1444. cnew->flag |= CRCLOSE;
  1445. if(m)
  1446. putmhead(m);
  1447. cclose(c);
  1448. c = cnew;
  1449. c->path = addelem(c->path, e.elems[e.nelems-1], nil);
  1450. break;
  1451. }
  1452. /* create failed */
  1453. cclose(cnew);
  1454. if(m)
  1455. putmhead(m);
  1456. if(omode & OEXCL)
  1457. nexterror();
  1458. /* save error */
  1459. createerr = up->errstr;
  1460. up->errstr = tmperrbuf;
  1461. /* note: we depend that walk does not error */
  1462. if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) < 0){
  1463. up->errstr = createerr;
  1464. error(createerr); /* report true error */
  1465. }
  1466. up->errstr = createerr;
  1467. omode |= OTRUNC;
  1468. goto Open;
  1469. default:
  1470. panic("unknown namec access %d\n", amode);
  1471. }
  1472. /* place final element in genbuf for e.g. exec */
  1473. if(e.nelems > 0)
  1474. kstrcpy(up->genbuf, e.elems[e.nelems-1], sizeof up->genbuf);
  1475. else
  1476. kstrcpy(up->genbuf, ".", sizeof up->genbuf);
  1477. free(e.name);
  1478. free(e.elems);
  1479. free(e.off);
  1480. poperror(); /* e c */
  1481. free(aname);
  1482. poperror(); /* aname */
  1483. return c;
  1484. }
  1485. /*
  1486. * name is valid. skip leading / and ./ as much as possible
  1487. */
  1488. char*
  1489. skipslash(char *name)
  1490. {
  1491. while(name[0]=='/' || (name[0]=='.' && (name[1]==0 || name[1]=='/')))
  1492. name++;
  1493. return name;
  1494. }
  1495. char isfrog[256]={
  1496. /*NUL*/ 1, 1, 1, 1, 1, 1, 1, 1,
  1497. /*BKS*/ 1, 1, 1, 1, 1, 1, 1, 1,
  1498. /*DLE*/ 1, 1, 1, 1, 1, 1, 1, 1,
  1499. /*CAN*/ 1, 1, 1, 1, 1, 1, 1, 1,
  1500. ['/'] 1,
  1501. [0x7f] 1,
  1502. };
  1503. /*
  1504. * Check that the name
  1505. * a) is in valid memory.
  1506. * b) is shorter than 2^16 bytes, so it can fit in a 9P string field.
  1507. * c) contains no frogs.
  1508. * The first byte is known to be addressible by the requester, so the
  1509. * routine works for kernel and user memory both.
  1510. * The parameter slashok flags whether a slash character is an error
  1511. * or a valid character.
  1512. *
  1513. * The parameter dup flags whether the string should be copied
  1514. * out of user space before being scanned the second time.
  1515. * (Otherwise a malicious thread could remove the NUL, causing us
  1516. * to access unchecked addresses.)
  1517. */
  1518. static char*
  1519. validname0(char *aname, int slashok, int dup, ulong pc)
  1520. {
  1521. char *ename, *name, *s;
  1522. int c, n;
  1523. Rune r;
  1524. name = aname;
  1525. if((ulong)name < KZERO){
  1526. if(!dup)
  1527. print("warning: validname called from %#p with user pointer", pc);
  1528. ename = vmemchr(name, 0, (1<<16));
  1529. }else
  1530. ename = memchr(name, 0, (1<<16));
  1531. if(ename==nil || ename-name>=(1<<16))
  1532. error("name too long");
  1533. s = nil;
  1534. if(dup){
  1535. n = ename-name;
  1536. s = smalloc(n+1);
  1537. memmove(s, name, n);
  1538. s[n] = 0;
  1539. aname = s;
  1540. name = s;
  1541. setmalloctag(s, pc);
  1542. }
  1543. while(*name){
  1544. /* all characters above '~' are ok */
  1545. c = *(uchar*)name;
  1546. if(c >= Runeself)
  1547. name += chartorune(&r, name);
  1548. else{
  1549. if(isfrog[c])
  1550. if(!slashok || c!='/'){
  1551. snprint(up->genbuf, sizeof(up->genbuf), "%s: %q", Ebadchar, aname);
  1552. free(s);
  1553. error(up->genbuf);
  1554. }
  1555. name++;
  1556. }
  1557. }
  1558. return s;
  1559. }
  1560. void
  1561. validname(char *aname, int slashok)
  1562. {
  1563. validname0(aname, slashok, 0, getcallerpc(&aname));
  1564. }
  1565. char*
  1566. validnamedup(char *aname, int slashok)
  1567. {
  1568. return validname0(aname, slashok, 1, getcallerpc(&aname));
  1569. }
  1570. void
  1571. isdir(Chan *c)
  1572. {
  1573. if(c->qid.type & QTDIR)
  1574. return;
  1575. error(Enotdir);
  1576. }
  1577. /*
  1578. * This is necessary because there are many
  1579. * pointers to the top of a given mount list:
  1580. *
  1581. * - the mhead in the namespace hash table
  1582. * - the mhead in chans returned from findmount:
  1583. * used in namec and then by unionread.
  1584. * - the mhead in chans returned from createdir:
  1585. * used in the open/create race protect, which is gone.
  1586. *
  1587. * The RWlock in the Mhead protects the mount list it contains.
  1588. * The mount list is deleted when we cunmount.
  1589. * The RWlock ensures that nothing is using the mount list at that time.
  1590. *
  1591. * It is okay to replace c->mh with whatever you want as
  1592. * long as you are sure you have a unique reference to it.
  1593. *
  1594. * This comment might belong somewhere else.
  1595. */
  1596. void
  1597. putmhead(Mhead *m)
  1598. {
  1599. if(m && decref(m) == 0){
  1600. m->mount = (Mount*)0xCafeBeef;
  1601. free(m);
  1602. }
  1603. }