sysfile.c 18 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  1. #include "u.h"
  2. #include "lib.h"
  3. #include "dat.h"
  4. #include "fns.h"
  5. #include "error.h"
  6. #include "user.h"
  7. #undef open
  8. #undef mount
  9. #undef read
  10. #undef write
  11. #undef seek
  12. #undef stat
  13. #undef wstat
  14. #undef remove
  15. #undef close
  16. #undef fstat
  17. #undef fwstat
  18. /*
  19. * The sys*() routines needn't poperror() as they return directly to syscall().
  20. */
  21. static void
  22. unlockfgrp(Fgrp *f)
  23. {
  24. int ex;
  25. ex = f->exceed;
  26. f->exceed = 0;
  27. unlock(&f->ref.lk);
  28. if(ex)
  29. pprint("warning: process exceeds %d file descriptors\n", ex);
  30. }
  31. int
  32. growfd(Fgrp *f, int fd) /* fd is always >= 0 */
  33. {
  34. Chan **newfd, **oldfd;
  35. if(fd < f->nfd)
  36. return 0;
  37. if(fd >= f->nfd+DELTAFD)
  38. return -1; /* out of range */
  39. /*
  40. * Unbounded allocation is unwise; besides, there are only 16 bits
  41. * of fid in 9P
  42. */
  43. if(f->nfd >= 5000){
  44. Exhausted:
  45. print("no free file descriptors\n");
  46. return -1;
  47. }
  48. newfd = malloc((f->nfd+DELTAFD)*sizeof(Chan*));
  49. if(newfd == 0)
  50. goto Exhausted;
  51. oldfd = f->fd;
  52. memmove(newfd, oldfd, f->nfd*sizeof(Chan*));
  53. f->fd = newfd;
  54. free(oldfd);
  55. f->nfd += DELTAFD;
  56. if(fd > f->maxfd){
  57. if(fd/100 > f->maxfd/100)
  58. f->exceed = (fd/100)*100;
  59. f->maxfd = fd;
  60. }
  61. return 1;
  62. }
  63. /*
  64. * this assumes that the fgrp is locked
  65. */
  66. int
  67. findfreefd(Fgrp *f, int start)
  68. {
  69. int fd;
  70. for(fd=start; fd<f->nfd; fd++)
  71. if(f->fd[fd] == 0)
  72. break;
  73. if(fd >= f->nfd && growfd(f, fd) < 0)
  74. return -1;
  75. return fd;
  76. }
  77. int
  78. newfd(Chan *c)
  79. {
  80. int fd;
  81. Fgrp *f;
  82. f = up->fgrp;
  83. lock(&f->ref.lk);
  84. fd = findfreefd(f, 0);
  85. if(fd < 0){
  86. unlockfgrp(f);
  87. return -1;
  88. }
  89. if(fd > f->maxfd)
  90. f->maxfd = fd;
  91. f->fd[fd] = c;
  92. unlockfgrp(f);
  93. return fd;
  94. }
  95. int
  96. newfd2(int fd[2], Chan *c[2])
  97. {
  98. Fgrp *f;
  99. f = up->fgrp;
  100. lock(&f->ref.lk);
  101. fd[0] = findfreefd(f, 0);
  102. if(fd[0] < 0){
  103. unlockfgrp(f);
  104. return -1;
  105. }
  106. fd[1] = findfreefd(f, fd[0]+1);
  107. if(fd[1] < 0){
  108. unlockfgrp(f);
  109. return -1;
  110. }
  111. if(fd[1] > f->maxfd)
  112. f->maxfd = fd[1];
  113. f->fd[fd[0]] = c[0];
  114. f->fd[fd[1]] = c[1];
  115. unlockfgrp(f);
  116. return 0;
  117. }
  118. Chan*
  119. fdtochan(int fd, int mode, int chkmnt, int iref)
  120. {
  121. Chan *c;
  122. Fgrp *f;
  123. c = 0;
  124. f = up->fgrp;
  125. lock(&f->ref.lk);
  126. if(fd<0 || f->nfd<=fd || (c = f->fd[fd])==0) {
  127. unlock(&f->ref.lk);
  128. error(Ebadfd);
  129. }
  130. if(iref)
  131. incref(&c->ref);
  132. unlock(&f->ref.lk);
  133. if(chkmnt && (c->flag&CMSG)) {
  134. if(iref)
  135. cclose(c);
  136. error(Ebadusefd);
  137. }
  138. if(mode<0 || c->mode==ORDWR)
  139. return c;
  140. if((mode&OTRUNC) && c->mode==OREAD) {
  141. if(iref)
  142. cclose(c);
  143. error(Ebadusefd);
  144. }
  145. if((mode&~OTRUNC) != c->mode) {
  146. if(iref)
  147. cclose(c);
  148. error(Ebadusefd);
  149. }
  150. return c;
  151. }
  152. int
  153. openmode(ulong o)
  154. {
  155. o &= ~(OTRUNC|OCEXEC|ORCLOSE);
  156. if(o > OEXEC)
  157. error(Ebadarg);
  158. if(o == OEXEC)
  159. return OREAD;
  160. return o;
  161. }
  162. long
  163. _sysfd2path(int fd, char *buf, uint nbuf)
  164. {
  165. Chan *c;
  166. c = fdtochan(fd, -1, 0, 1);
  167. if(c->name == nil)
  168. snprint(buf, nbuf, "<null>");
  169. else
  170. snprint(buf, nbuf, "%s", c->name->s);
  171. cclose(c);
  172. return 0;
  173. }
  174. long
  175. _syspipe(int fd[2])
  176. {
  177. Chan *c[2];
  178. Dev *d;
  179. static char *datastr[] = {"data", "data1"};
  180. d = devtab[devno('|', 0)];
  181. c[0] = namec("#|", Atodir, 0, 0);
  182. c[1] = 0;
  183. fd[0] = -1;
  184. fd[1] = -1;
  185. if(waserror()){
  186. cclose(c[0]);
  187. if(c[1])
  188. cclose(c[1]);
  189. nexterror();
  190. }
  191. c[1] = cclone(c[0]);
  192. if(walk(&c[0], datastr+0, 1, 1, nil) < 0)
  193. error(Egreg);
  194. if(walk(&c[1], datastr+1, 1, 1, nil) < 0)
  195. error(Egreg);
  196. c[0] = d->open(c[0], ORDWR);
  197. c[1] = d->open(c[1], ORDWR);
  198. if(newfd2(fd, c) < 0)
  199. error(Enofd);
  200. poperror();
  201. return 0;
  202. }
  203. long
  204. _sysdup(int fd0, int fd1)
  205. {
  206. int fd;
  207. Chan *c, *oc;
  208. Fgrp *f = up->fgrp;
  209. /*
  210. * Close after dup'ing, so date > #d/1 works
  211. */
  212. c = fdtochan(fd0, -1, 0, 1);
  213. fd = fd1;
  214. if(fd != -1){
  215. lock(&f->ref.lk);
  216. if(fd<0 || growfd(f, fd)<0) {
  217. unlockfgrp(f);
  218. cclose(c);
  219. error(Ebadfd);
  220. }
  221. if(fd > f->maxfd)
  222. f->maxfd = fd;
  223. oc = f->fd[fd];
  224. f->fd[fd] = c;
  225. unlockfgrp(f);
  226. if(oc)
  227. cclose(oc);
  228. }else{
  229. if(waserror()) {
  230. cclose(c);
  231. nexterror();
  232. }
  233. fd = newfd(c);
  234. if(fd < 0)
  235. error(Enofd);
  236. poperror();
  237. }
  238. return fd;
  239. }
  240. long
  241. _sysopen(char *name, int mode)
  242. {
  243. int fd;
  244. Chan *c = 0;
  245. openmode(mode); /* error check only */
  246. if(waserror()){
  247. if(c)
  248. cclose(c);
  249. nexterror();
  250. }
  251. c = namec(name, Aopen, mode, 0);
  252. fd = newfd(c);
  253. if(fd < 0)
  254. error(Enofd);
  255. poperror();
  256. return fd;
  257. }
  258. void
  259. fdclose(int fd, int flag)
  260. {
  261. int i;
  262. Chan *c;
  263. Fgrp *f = up->fgrp;
  264. lock(&f->ref.lk);
  265. c = f->fd[fd];
  266. if(c == 0){
  267. /* can happen for users with shared fd tables */
  268. unlock(&f->ref.lk);
  269. return;
  270. }
  271. if(flag){
  272. if(c==0 || !(c->flag&flag)){
  273. unlock(&f->ref.lk);
  274. return;
  275. }
  276. }
  277. f->fd[fd] = 0;
  278. if(fd == f->maxfd)
  279. for(i=fd; --i>=0 && f->fd[i]==0; )
  280. f->maxfd = i;
  281. unlock(&f->ref.lk);
  282. cclose(c);
  283. }
  284. long
  285. _sysclose(int fd)
  286. {
  287. fdtochan(fd, -1, 0, 0);
  288. fdclose(fd, 0);
  289. return 0;
  290. }
  291. long
  292. unionread(Chan *c, void *va, long n)
  293. {
  294. int i;
  295. long nr;
  296. Mhead *m;
  297. Mount *mount;
  298. qlock(&c->umqlock);
  299. m = c->umh;
  300. rlock(&m->lock);
  301. mount = m->mount;
  302. /* bring mount in sync with c->uri and c->umc */
  303. for(i = 0; mount != nil && i < c->uri; i++)
  304. mount = mount->next;
  305. nr = 0;
  306. while(mount != nil) {
  307. /* Error causes component of union to be skipped */
  308. if(mount->to && !waserror()) {
  309. if(c->umc == nil){
  310. c->umc = cclone(mount->to);
  311. c->umc = devtab[c->umc->type]->open(c->umc, OREAD);
  312. }
  313. nr = devtab[c->umc->type]->read(c->umc, va, n, c->umc->offset);
  314. c->umc->offset += nr;
  315. poperror();
  316. }
  317. if(nr > 0)
  318. break;
  319. /* Advance to next element */
  320. c->uri++;
  321. if(c->umc) {
  322. cclose(c->umc);
  323. c->umc = nil;
  324. }
  325. mount = mount->next;
  326. }
  327. runlock(&m->lock);
  328. qunlock(&c->umqlock);
  329. return nr;
  330. }
  331. static long
  332. kread(int fd, void *buf, long n, vlong *offp)
  333. {
  334. int dir;
  335. Chan *c;
  336. vlong off;
  337. c = fdtochan(fd, OREAD, 1, 1);
  338. if(waserror()) {
  339. cclose(c);
  340. nexterror();
  341. }
  342. dir = c->qid.type&QTDIR;
  343. /*
  344. * The offset is passed through on directories, normally. sysseek complains but
  345. * pread is used by servers and e.g. exportfs that shouldn't need to worry about this issue.
  346. */
  347. if(offp == nil) /* use and maintain channel's offset */
  348. off = c->offset;
  349. else
  350. off = *offp;
  351. if(off < 0)
  352. error(Enegoff);
  353. if(dir && c->umh)
  354. n = unionread(c, buf, n);
  355. else
  356. n = devtab[c->type]->read(c, buf, n, off);
  357. if(offp == nil){
  358. lock(&c->ref.lk);
  359. c->offset += n;
  360. unlock(&c->ref.lk);
  361. }
  362. poperror();
  363. cclose(c);
  364. return n;
  365. }
  366. /* name conflicts with netbsd
  367. long
  368. _sys_read(int fd, void *buf, long n)
  369. {
  370. return kread(fd, buf, n, nil);
  371. }
  372. */
  373. long
  374. _syspread(int fd, void *buf, long n, vlong off)
  375. {
  376. if(off == ((uvlong) ~0))
  377. return kread(fd, buf, n, nil);
  378. return kread(fd, buf, n, &off);
  379. }
  380. static long
  381. kwrite(int fd, void *buf, long nn, vlong *offp)
  382. {
  383. Chan *c;
  384. long m, n;
  385. vlong off;
  386. n = 0;
  387. c = fdtochan(fd, OWRITE, 1, 1);
  388. if(waserror()) {
  389. if(offp == nil){
  390. lock(&c->ref.lk);
  391. c->offset -= n;
  392. unlock(&c->ref.lk);
  393. }
  394. cclose(c);
  395. nexterror();
  396. }
  397. if(c->qid.type & QTDIR)
  398. error(Eisdir);
  399. n = nn;
  400. if(offp == nil){ /* use and maintain channel's offset */
  401. lock(&c->ref.lk);
  402. off = c->offset;
  403. c->offset += n;
  404. unlock(&c->ref.lk);
  405. }else
  406. off = *offp;
  407. if(off < 0)
  408. error(Enegoff);
  409. m = devtab[c->type]->write(c, buf, n, off);
  410. if(offp == nil && m < n){
  411. lock(&c->ref.lk);
  412. c->offset -= n - m;
  413. unlock(&c->ref.lk);
  414. }
  415. poperror();
  416. cclose(c);
  417. return m;
  418. }
  419. long
  420. sys_write(int fd, void *buf, long n)
  421. {
  422. return kwrite(fd, buf, n, nil);
  423. }
  424. long
  425. _syspwrite(int fd, void *buf, long n, vlong off)
  426. {
  427. if(off == ((uvlong) ~0))
  428. return kwrite(fd, buf, n, nil);
  429. return kwrite(fd, buf, n, &off);
  430. }
  431. static vlong
  432. _sysseek(int fd, vlong off, int whence)
  433. {
  434. Chan *c;
  435. uchar buf[sizeof(Dir)+100];
  436. Dir dir;
  437. int n;
  438. c = fdtochan(fd, -1, 1, 1);
  439. if(waserror()){
  440. cclose(c);
  441. nexterror();
  442. }
  443. if(devtab[c->type]->dc == '|')
  444. error(Eisstream);
  445. switch(whence){
  446. case 0:
  447. if((c->qid.type & QTDIR) && off != 0)
  448. error(Eisdir);
  449. if(off < 0)
  450. error(Enegoff);
  451. c->offset = off;
  452. break;
  453. case 1:
  454. if(c->qid.type & QTDIR)
  455. error(Eisdir);
  456. lock(&c->ref.lk); /* lock for read/write update */
  457. off = off + c->offset;
  458. if(off < 0)
  459. error(Enegoff);
  460. c->offset = off;
  461. unlock(&c->ref.lk);
  462. break;
  463. case 2:
  464. if(c->qid.type & QTDIR)
  465. error(Eisdir);
  466. n = devtab[c->type]->stat(c, buf, sizeof buf);
  467. if(convM2D(buf, n, &dir, nil) == 0)
  468. error("internal error: stat error in seek");
  469. off = dir.length + off;
  470. if(off < 0)
  471. error(Enegoff);
  472. c->offset = off;
  473. break;
  474. default:
  475. error(Ebadarg);
  476. }
  477. c->uri = 0;
  478. c->dri = 0;
  479. cclose(c);
  480. poperror();
  481. return off;
  482. }
  483. void
  484. validstat(uchar *s, int n)
  485. {
  486. int m;
  487. char buf[64];
  488. if(statcheck(s, n) < 0)
  489. error(Ebadstat);
  490. /* verify that name entry is acceptable */
  491. s += STATFIXLEN - 4*BIT16SZ; /* location of first string */
  492. /*
  493. * s now points at count for first string.
  494. * if it's too long, let the server decide; this is
  495. * only for his protection anyway. otherwise
  496. * we'd have to allocate and waserror.
  497. */
  498. m = GBIT16(s);
  499. s += BIT16SZ;
  500. if(m+1 > sizeof buf)
  501. return;
  502. memmove(buf, s, m);
  503. buf[m] = '\0';
  504. /* name could be '/' */
  505. if(strcmp(buf, "/") != 0)
  506. validname(buf, 0);
  507. }
  508. long
  509. _sysfstat(int fd, void *buf, long n)
  510. {
  511. Chan *c;
  512. uint l;
  513. l = n;
  514. validaddr(buf, l, 1);
  515. c = fdtochan(fd, -1, 0, 1);
  516. if(waserror()) {
  517. cclose(c);
  518. nexterror();
  519. }
  520. l = devtab[c->type]->stat(c, buf, l);
  521. poperror();
  522. cclose(c);
  523. return l;
  524. }
  525. long
  526. _sysstat(char *name, void *buf, long n)
  527. {
  528. Chan *c;
  529. uint l;
  530. l = n;
  531. validaddr(buf, l, 1);
  532. validaddr(name, 1, 0);
  533. c = namec(name, Aaccess, 0, 0);
  534. if(waserror()){
  535. cclose(c);
  536. nexterror();
  537. }
  538. l = devtab[c->type]->stat(c, buf, l);
  539. poperror();
  540. cclose(c);
  541. return l;
  542. }
  543. long
  544. _syschdir(char *name)
  545. {
  546. Chan *c;
  547. validaddr(name, 1, 0);
  548. c = namec(name, Atodir, 0, 0);
  549. cclose(up->dot);
  550. up->dot = c;
  551. return 0;
  552. }
  553. long
  554. bindmount(int ismount, int fd, int afd, char* arg0, char* arg1, ulong flag, char* spec)
  555. {
  556. int ret;
  557. Chan *c0, *c1, *ac, *bc;
  558. struct{
  559. Chan *chan;
  560. Chan *authchan;
  561. char *spec;
  562. int flags;
  563. }bogus;
  564. if((flag&~MMASK) || (flag&MORDER)==(MBEFORE|MAFTER))
  565. error(Ebadarg);
  566. bogus.flags = flag & MCACHE;
  567. if(ismount){
  568. if(up->pgrp->noattach)
  569. error(Enoattach);
  570. ac = nil;
  571. bc = fdtochan(fd, ORDWR, 0, 1);
  572. if(waserror()) {
  573. if(ac)
  574. cclose(ac);
  575. cclose(bc);
  576. nexterror();
  577. }
  578. if(afd >= 0)
  579. ac = fdtochan(afd, ORDWR, 0, 1);
  580. bogus.chan = bc;
  581. bogus.authchan = ac;
  582. validaddr((ulong)spec, 1, 0);
  583. bogus.spec = spec;
  584. if(waserror())
  585. error(Ebadspec);
  586. validname(spec, 1);
  587. poperror();
  588. ret = devno('M', 0);
  589. c0 = devtab[ret]->attach((char*)&bogus);
  590. poperror();
  591. if(ac)
  592. cclose(ac);
  593. cclose(bc);
  594. }else{
  595. bogus.spec = 0;
  596. validaddr((ulong)arg0, 1, 0);
  597. c0 = namec(arg0, Abind, 0, 0);
  598. }
  599. if(waserror()){
  600. cclose(c0);
  601. nexterror();
  602. }
  603. validaddr((ulong)arg1, 1, 0);
  604. c1 = namec(arg1, Amount, 0, 0);
  605. if(waserror()){
  606. cclose(c1);
  607. nexterror();
  608. }
  609. ret = cmount(&c0, c1, flag, bogus.spec);
  610. poperror();
  611. cclose(c1);
  612. poperror();
  613. cclose(c0);
  614. if(ismount)
  615. fdclose(fd, 0);
  616. return ret;
  617. }
  618. long
  619. _sysbind(char *old, char *new, int flag)
  620. {
  621. return bindmount(0, -1, -1, old, new, flag, nil);
  622. }
  623. long
  624. _sysmount(int fd, int afd, char *new, int flag, char *spec)
  625. {
  626. return bindmount(1, fd, afd, nil, new, flag, spec);
  627. }
  628. long
  629. _sysunmount(char *old, char *new)
  630. {
  631. Chan *cmount, *cmounted;
  632. cmounted = 0;
  633. cmount = namec(new, Amount, 0, 0);
  634. if(old) {
  635. if(waserror()) {
  636. cclose(cmount);
  637. nexterror();
  638. }
  639. validaddr(old, 1, 0);
  640. /*
  641. * This has to be namec(..., Aopen, ...) because
  642. * if arg[0] is something like /srv/cs or /fd/0,
  643. * opening it is the only way to get at the real
  644. * Chan underneath.
  645. */
  646. cmounted = namec(old, Aopen, OREAD, 0);
  647. poperror();
  648. }
  649. if(waserror()) {
  650. cclose(cmount);
  651. if(cmounted)
  652. cclose(cmounted);
  653. nexterror();
  654. }
  655. cunmount(cmount, cmounted);
  656. cclose(cmount);
  657. if(cmounted)
  658. cclose(cmounted);
  659. poperror();
  660. return 0;
  661. }
  662. long
  663. _syscreate(char *name, int mode, ulong perm)
  664. {
  665. int fd;
  666. Chan *c = 0;
  667. openmode(mode&~OEXCL); /* error check only; OEXCL okay here */
  668. if(waserror()) {
  669. if(c)
  670. cclose(c);
  671. nexterror();
  672. }
  673. validaddr(name, 1, 0);
  674. c = namec(name, Acreate, mode, perm);
  675. fd = newfd(c);
  676. if(fd < 0)
  677. error(Enofd);
  678. poperror();
  679. return fd;
  680. }
  681. long
  682. _sysremove(char *name)
  683. {
  684. Chan *c;
  685. c = namec(name, Aremove, 0, 0);
  686. if(waserror()){
  687. c->type = 0; /* see below */
  688. cclose(c);
  689. nexterror();
  690. }
  691. devtab[c->type]->remove(c);
  692. /*
  693. * Remove clunks the fid, but we need to recover the Chan
  694. * so fake it up. rootclose() is known to be a nop.
  695. */
  696. c->type = 0;
  697. poperror();
  698. cclose(c);
  699. return 0;
  700. }
  701. long
  702. _syswstat(char *name, void *buf, long n)
  703. {
  704. Chan *c;
  705. uint l;
  706. l = n;
  707. validstat(buf, l);
  708. validaddr(name, 1, 0);
  709. c = namec(name, Aaccess, 0, 0);
  710. if(waserror()){
  711. cclose(c);
  712. nexterror();
  713. }
  714. l = devtab[c->type]->wstat(c, buf, l);
  715. poperror();
  716. cclose(c);
  717. return l;
  718. }
  719. long
  720. _sysfwstat(int fd, void *buf, long n)
  721. {
  722. Chan *c;
  723. uint l;
  724. l = n;
  725. validaddr(buf, l, 0);
  726. validstat(buf, l);
  727. c = fdtochan(fd, -1, 1, 1);
  728. if(waserror()) {
  729. cclose(c);
  730. nexterror();
  731. }
  732. l = devtab[c->type]->wstat(c, buf, l);
  733. poperror();
  734. cclose(c);
  735. return l;
  736. }
  737. static void
  738. starterror(void)
  739. {
  740. assert(up->nerrlab == 0);
  741. }
  742. static void
  743. _syserror(void)
  744. {
  745. char *p;
  746. p = up->syserrstr;
  747. up->syserrstr = up->errstr;
  748. up->errstr = p;
  749. }
  750. static void
  751. enderror(void)
  752. {
  753. assert(up->nerrlab == 1);
  754. poperror();
  755. }
  756. int
  757. sysbind(char *old, char *new, int flag)
  758. {
  759. int n;
  760. starterror();
  761. if(waserror()){
  762. _syserror();
  763. return -1;
  764. }
  765. n = _sysbind(old, new, flag);
  766. enderror();
  767. return n;
  768. }
  769. int
  770. syschdir(char *path)
  771. {
  772. int n;
  773. starterror();
  774. if(waserror()){
  775. _syserror();
  776. return -1;
  777. }
  778. n = _syschdir(path);
  779. enderror();
  780. return n;
  781. }
  782. int
  783. sysclose(int fd)
  784. {
  785. int n;
  786. starterror();
  787. if(waserror()){
  788. _syserror();
  789. return -1;
  790. }
  791. n = _sysclose(fd);
  792. enderror();
  793. return n;
  794. }
  795. int
  796. syscreate(char *name, int mode, ulong perm)
  797. {
  798. int n;
  799. starterror();
  800. if(waserror()){
  801. _syserror();
  802. return -1;
  803. }
  804. n = _syscreate(name, mode, perm);
  805. enderror();
  806. return n;
  807. }
  808. int
  809. sysdup(int fd0, int fd1)
  810. {
  811. int n;
  812. starterror();
  813. if(waserror()){
  814. _syserror();
  815. return -1;
  816. }
  817. n = _sysdup(fd0, fd1);
  818. enderror();
  819. return n;
  820. }
  821. int
  822. sysfstat(int fd, uchar *buf, int n)
  823. {
  824. starterror();
  825. if(waserror()){
  826. _syserror();
  827. return -1;
  828. }
  829. n = _sysfstat(fd, buf, n);
  830. enderror();
  831. return n;
  832. }
  833. int
  834. sysfwstat(int fd, uchar *buf, int n)
  835. {
  836. starterror();
  837. if(waserror()){
  838. _syserror();
  839. return -1;
  840. }
  841. n = _sysfwstat(fd, buf, n);
  842. enderror();
  843. return n;
  844. }
  845. int
  846. sysmount(int fd, int afd, char *new, int flag, char *spec)
  847. {
  848. int n;
  849. starterror();
  850. if(waserror()){
  851. _syserror();
  852. return -1;
  853. }
  854. n = _sysmount(fd, afd, new, flag, spec);
  855. enderror();
  856. return n;
  857. }
  858. int
  859. sysunmount(char *old, char *new)
  860. {
  861. int n;
  862. starterror();
  863. if(waserror()){
  864. _syserror();
  865. return -1;
  866. }
  867. n = _sysunmount(old, new);
  868. enderror();
  869. return n;
  870. }
  871. int
  872. sysopen(char *name, int mode)
  873. {
  874. int n;
  875. starterror();
  876. if(waserror()){
  877. _syserror();
  878. return -1;
  879. }
  880. n = _sysopen(name, mode);
  881. enderror();
  882. return n;
  883. }
  884. int
  885. syspipe(int *fd)
  886. {
  887. int n;
  888. starterror();
  889. if(waserror()){
  890. _syserror();
  891. return -1;
  892. }
  893. n = _syspipe(fd);
  894. enderror();
  895. return n;
  896. }
  897. long
  898. syspread(int fd, void *buf, long n, vlong off)
  899. {
  900. starterror();
  901. if(waserror()){
  902. _syserror();
  903. return -1;
  904. }
  905. n = _syspread(fd, buf, n, off);
  906. enderror();
  907. return n;
  908. }
  909. long
  910. syspwrite(int fd, void *buf, long n, vlong off)
  911. {
  912. starterror();
  913. if(waserror()){
  914. _syserror();
  915. return -1;
  916. }
  917. n = _syspwrite(fd, buf, n, off);
  918. enderror();
  919. return n;
  920. }
  921. long
  922. sysread(int fd, void *buf, long n)
  923. {
  924. starterror();
  925. if(waserror()){
  926. _syserror();
  927. return -1;
  928. }
  929. n = _syspread(fd, buf, n, (uvlong) ~0);
  930. enderror();
  931. return n;
  932. }
  933. int
  934. sysremove(char *path)
  935. {
  936. int n;
  937. starterror();
  938. if(waserror()){
  939. _syserror();
  940. return -1;
  941. }
  942. n = _sysremove(path);
  943. enderror();
  944. return n;
  945. }
  946. vlong
  947. sysseek(int fd, vlong off, int whence)
  948. {
  949. starterror();
  950. if(waserror()){
  951. _syserror();
  952. return -1;
  953. }
  954. off = _sysseek(fd, off, whence);
  955. enderror();
  956. return off;
  957. }
  958. int
  959. sysstat(char *name, uchar *buf, int n)
  960. {
  961. starterror();
  962. if(waserror()){
  963. _syserror();
  964. return -1;
  965. }
  966. n = _sysstat(name, buf, n);
  967. enderror();
  968. return n;
  969. }
  970. long
  971. syswrite(int fd, void *buf, long n)
  972. {
  973. starterror();
  974. if(waserror()){
  975. _syserror();
  976. return -1;
  977. }
  978. n = _syspwrite(fd, buf, n, (uvlong) ~0);
  979. enderror();
  980. return n;
  981. }
  982. int
  983. syswstat(char *name, uchar *buf, int n)
  984. {
  985. starterror();
  986. if(waserror()){
  987. _syserror();
  988. return -1;
  989. }
  990. n = _syswstat(name, buf, n);
  991. enderror();
  992. return n;
  993. }
  994. void
  995. werrstr(char *f, ...)
  996. {
  997. char buf[ERRMAX];
  998. va_list arg;
  999. va_start(arg, f);
  1000. vsnprint(buf, sizeof buf, f, arg);
  1001. va_end(arg);
  1002. if(up->nerrlab)
  1003. strecpy(up->errstr, up->errstr+ERRMAX, buf);
  1004. else
  1005. strecpy(up->syserrstr, up->syserrstr+ERRMAX, buf);
  1006. }
  1007. int
  1008. __errfmt(Fmt *fmt)
  1009. {
  1010. if(up->nerrlab)
  1011. return fmtstrcpy(fmt, up->errstr);
  1012. else
  1013. return fmtstrcpy(fmt, up->syserrstr);
  1014. }
  1015. int
  1016. errstr(char *buf, uint n)
  1017. {
  1018. char tmp[ERRMAX];
  1019. char *p;
  1020. p = up->nerrlab ? up->errstr : up->syserrstr;
  1021. memmove(tmp, p, ERRMAX);
  1022. utfecpy(p, p+ERRMAX, buf);
  1023. utfecpy(buf, buf+n, tmp);
  1024. return strlen(buf);
  1025. }
  1026. int
  1027. rerrstr(char *buf, uint n)
  1028. {
  1029. char *p;
  1030. p = up->nerrlab ? up->errstr : up->syserrstr;
  1031. utfecpy(buf, buf+n, p);
  1032. return strlen(buf);
  1033. }
  1034. ulong
  1035. _sysrendezvous(ulong arg0, ulong arg1)
  1036. {
  1037. ulong tag, val;
  1038. Proc *p, **l;
  1039. tag = arg0;
  1040. l = &REND(up->rgrp, tag);
  1041. up->rendval = ~0UL;
  1042. lock(&up->rgrp->ref.lk);
  1043. for(p = *l; p; p = p->rendhash) {
  1044. if(p->rendtag == tag) {
  1045. *l = p->rendhash;
  1046. val = p->rendval;
  1047. p->rendval = arg1;
  1048. while(p->mach != 0)
  1049. ;
  1050. procwakeup(p);
  1051. unlock(&up->rgrp->ref.lk);
  1052. return val;
  1053. }
  1054. l = &p->rendhash;
  1055. }
  1056. /* Going to sleep here */
  1057. up->rendtag = tag;
  1058. up->rendval = arg1;
  1059. up->rendhash = *l;
  1060. *l = up;
  1061. up->state = Rendezvous;
  1062. unlock(&up->rgrp->ref.lk);
  1063. procsleep();
  1064. return up->rendval;
  1065. }
  1066. ulong
  1067. sysrendezvous(ulong tag, ulong val)
  1068. {
  1069. ulong n;
  1070. starterror();
  1071. if(waserror()){
  1072. _syserror();
  1073. return -1;
  1074. }
  1075. n = _sysrendezvous(tag, val);
  1076. enderror();
  1077. return n;
  1078. }