PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

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

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