plan9.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * Plan 9 versions of system-specific functions
  3. * By convention, exported routines herein have names beginning with an
  4. * upper case letter.
  5. */
  6. #include "rc.h"
  7. #include "exec.h"
  8. #include "io.h"
  9. #include "fns.h"
  10. #include "getflags.h"
  11. char *Signame[] = {
  12. "sigexit", "sighup", "sigint", "sigquit",
  13. "sigalrm", "sigkill", "sigfpe", "sigterm",
  14. 0
  15. };
  16. char *syssigname[] = {
  17. "exit", /* can't happen */
  18. "hangup",
  19. "interrupt",
  20. "quit", /* can't happen */
  21. "alarm",
  22. "kill",
  23. "sys: fp: ",
  24. "term",
  25. 0
  26. };
  27. char Rcmain[]="/rc/lib/rcmain";
  28. char Fdprefix[]="/fd/";
  29. void execfinit(void);
  30. void execbind(void);
  31. void execmount(void);
  32. void execnewpgrp(void);
  33. builtin Builtin[] = {
  34. "cd", execcd,
  35. "whatis", execwhatis,
  36. "eval", execeval,
  37. "exec", execexec, /* but with popword first */
  38. "exit", execexit,
  39. "shift", execshift,
  40. "wait", execwait,
  41. ".", execdot,
  42. "finit", execfinit,
  43. "flag", execflag,
  44. "rfork", execnewpgrp,
  45. 0
  46. };
  47. void
  48. execnewpgrp(void)
  49. {
  50. int arg;
  51. char *s;
  52. switch(count(runq->argv->words)){
  53. case 1:
  54. arg = RFENVG|RFNAMEG|RFNOTEG;
  55. break;
  56. case 2:
  57. arg = 0;
  58. for(s = runq->argv->words->next->word;*s;s++) switch(*s){
  59. default:
  60. goto Usage;
  61. case 'n':
  62. arg|=RFNAMEG; break;
  63. case 'N':
  64. arg|=RFCNAMEG;
  65. break;
  66. case 'm':
  67. arg|=RFNOMNT; break;
  68. case 'e':
  69. arg|=RFENVG; break;
  70. case 'E':
  71. arg|=RFCENVG; break;
  72. case 's':
  73. arg|=RFNOTEG; break;
  74. case 'f':
  75. arg|=RFFDG; break;
  76. case 'F':
  77. arg|=RFCFDG; break;
  78. }
  79. break;
  80. default:
  81. Usage:
  82. pfmt(err, "Usage: %s [fnesFNEm]\n", runq->argv->words->word);
  83. setstatus("rfork usage");
  84. poplist();
  85. return;
  86. }
  87. if(rfork(arg)==-1){
  88. pfmt(err, "rc: %s failed\n", runq->argv->words->word);
  89. setstatus("rfork failed");
  90. }
  91. else
  92. setstatus("");
  93. poplist();
  94. }
  95. void
  96. Vinit(void)
  97. {
  98. int dir, f, len;
  99. word *val;
  100. char *buf, *s;
  101. Dir *ent;
  102. int i, nent;
  103. char envname[256];
  104. dir = open("/env", OREAD);
  105. if(dir<0){
  106. pfmt(err, "rc: can't open /env: %r\n");
  107. return;
  108. }
  109. ent = nil;
  110. for(;;){
  111. nent = dirread(dir, &ent);
  112. if(nent <= 0)
  113. break;
  114. for(i = 0; i<nent; i++){
  115. len = ent[i].length;
  116. if(len && strncmp(ent[i].name, "fn#", 3)!=0){
  117. snprint(envname, sizeof envname, "/env/%s", ent[i].name);
  118. if((f = open(envname, 0))>=0){
  119. buf = emalloc((int)len+1);
  120. read(f, buf, (long)len);
  121. val = 0;
  122. /* Charitably add a 0 at the end if need be */
  123. if(buf[len-1])
  124. buf[len++]='\0';
  125. s = buf+len-1;
  126. for(;;){
  127. while(s!=buf && s[-1]!='\0') --s;
  128. val = newword(s, val);
  129. if(s==buf)
  130. break;
  131. --s;
  132. }
  133. setvar(ent[i].name, val);
  134. vlook(ent[i].name)->changed = 0;
  135. close(f);
  136. efree(buf);
  137. }
  138. }
  139. }
  140. free(ent);
  141. }
  142. close(dir);
  143. }
  144. int envdir;
  145. void
  146. Xrdfn(void)
  147. {
  148. int f, len;
  149. static Dir *ent, *allocent;
  150. static int nent;
  151. Dir *e;
  152. char envname[256];
  153. for(;;){
  154. if(nent == 0){
  155. free(allocent);
  156. nent = dirread(envdir, &allocent);
  157. ent = allocent;
  158. }
  159. if(nent <= 0)
  160. break;
  161. while(nent){
  162. e = ent++;
  163. nent--;
  164. len = e->length;
  165. if(len && strncmp(e->name, "fn#", 3)==0){
  166. snprint(envname, sizeof envname, "/env/%s", e->name);
  167. if((f = open(envname, 0))>=0){
  168. execcmds(openfd(f));
  169. return;
  170. }
  171. }
  172. }
  173. }
  174. close(envdir);
  175. Xreturn();
  176. }
  177. union code rdfns[4];
  178. void
  179. execfinit(void)
  180. {
  181. static int first = 1;
  182. if(first){
  183. rdfns[0].i = 1;
  184. rdfns[1].f = Xrdfn;
  185. rdfns[2].f = Xjump;
  186. rdfns[3].i = 1;
  187. first = 0;
  188. }
  189. Xpopm();
  190. envdir = open("/env", 0);
  191. if(envdir<0){
  192. pfmt(err, "rc: can't open /env: %r\n");
  193. return;
  194. }
  195. start(rdfns, 1, runq->local);
  196. }
  197. int
  198. Waitfor(int pid, int)
  199. {
  200. thread *p;
  201. Waitmsg *w;
  202. char errbuf[ERRMAX];
  203. while((w = wait()) != nil){
  204. if(w->pid==pid){
  205. setstatus(w->msg);
  206. free(w);
  207. return 0;
  208. }
  209. for(p = runq->ret;p;p = p->ret)
  210. if(p->pid==w->pid){
  211. p->pid=-1;
  212. strcpy(p->status, w->msg);
  213. }
  214. free(w);
  215. }
  216. errstr(errbuf, sizeof errbuf);
  217. if(strcmp(errbuf, "interrupted")==0) return -1;
  218. return 0;
  219. }
  220. char*
  221. *mkargv(word *a)
  222. {
  223. char **argv = (char **)emalloc((count(a)+2)*sizeof(char *));
  224. char **argp = argv+1; /* leave one at front for runcoms */
  225. for(;a;a = a->next) *argp++=a->word;
  226. *argp = 0;
  227. return argv;
  228. }
  229. void
  230. addenv(var *v)
  231. {
  232. char envname[256];
  233. word *w;
  234. int f;
  235. io *fd;
  236. if(v->changed){
  237. v->changed = 0;
  238. snprint(envname, sizeof envname, "/env/%s", v->name);
  239. if((f = Creat(envname))<0)
  240. pfmt(err, "rc: can't open %s: %r\n", envname);
  241. else{
  242. for(w = v->val;w;w = w->next)
  243. write(f, w->word, strlen(w->word)+1L);
  244. close(f);
  245. }
  246. }
  247. if(v->fnchanged){
  248. v->fnchanged = 0;
  249. snprint(envname, sizeof envname, "/env/fn#%s", v->name);
  250. if((f = Creat(envname))<0)
  251. pfmt(err, "rc: can't open %s: %r\n", envname);
  252. else{
  253. if(v->fn){
  254. fd = openfd(f);
  255. pfmt(fd, "fn %s %s\n", v->name, v->fn[v->pc-1].s);
  256. closeio(fd);
  257. }
  258. close(f);
  259. }
  260. }
  261. }
  262. void
  263. updenvlocal(var *v)
  264. {
  265. if(v){
  266. updenvlocal(v->next);
  267. addenv(v);
  268. }
  269. }
  270. void
  271. Updenv(void)
  272. {
  273. var *v, **h;
  274. for(h = gvar;h!=&gvar[NVAR];h++)
  275. for(v=*h;v;v = v->next)
  276. addenv(v);
  277. if(runq)
  278. updenvlocal(runq->local);
  279. }
  280. int
  281. ForkExecute(char *file, char **argv, int sin, int sout, int serr)
  282. {
  283. int pid;
  284. if(access(file, 1) != 0)
  285. return -1;
  286. switch(pid = fork()){
  287. case -1:
  288. return -1;
  289. case 0:
  290. if(sin >= 0)
  291. dup(sin, 0);
  292. else
  293. close(0);
  294. if(sout >= 0)
  295. dup(sout, 1);
  296. else
  297. close(1);
  298. if(serr >= 0)
  299. dup(serr, 2);
  300. else
  301. close(2);
  302. exec(file, argv);
  303. exits(file);
  304. }
  305. return pid;
  306. }
  307. void
  308. Execute(word *args, word *path)
  309. {
  310. char **argv = mkargv(args);
  311. char file[1024];
  312. int nc;
  313. Updenv();
  314. for(;path;path = path->next){
  315. nc = strlen(path->word);
  316. if(nc<1024){
  317. strcpy(file, path->word);
  318. if(file[0]){
  319. strcat(file, "/");
  320. nc++;
  321. }
  322. if(nc+strlen(argv[1])<1024){
  323. strcat(file, argv[1]);
  324. exec(file, argv+1);
  325. }
  326. else werrstr("command name too long");
  327. }
  328. }
  329. rerrstr(file, sizeof file);
  330. pfmt(err, "%s: %s\n", argv[1], file);
  331. efree((char *)argv);
  332. }
  333. #define NDIR 256 /* shoud be a better way */
  334. int
  335. Globsize(char *p)
  336. {
  337. ulong isglob = 0, globlen = NDIR+1;
  338. for(;*p;p++){
  339. if(*p==GLOB){
  340. p++;
  341. if(*p!=GLOB)
  342. isglob++;
  343. globlen+=*p=='*'?NDIR:1;
  344. }
  345. else
  346. globlen++;
  347. }
  348. return isglob?globlen:0;
  349. }
  350. #define NFD 50
  351. #define NDBUF 32
  352. struct{
  353. Dir *dbuf;
  354. int i;
  355. int n;
  356. }dir[NFD];
  357. int
  358. Opendir(char *name)
  359. {
  360. Dir *db;
  361. int f;
  362. f = open(name, 0);
  363. if(f==-1)
  364. return f;
  365. db = dirfstat(f);
  366. if(db!=nil && (db->mode&DMDIR)){
  367. if(f<NFD){
  368. dir[f].i = 0;
  369. dir[f].n = 0;
  370. }
  371. free(db);
  372. return f;
  373. }
  374. free(db);
  375. close(f);
  376. return -1;
  377. }
  378. static int
  379. trimdirs(Dir *d, int nd)
  380. {
  381. int r, w;
  382. for(r=w=0; r<nd; r++)
  383. if(d[r].mode&DMDIR)
  384. d[w++] = d[r];
  385. return w;
  386. }
  387. /*
  388. * onlydirs is advisory -- it means you only
  389. * need to return the directories. it's okay to
  390. * return files too (e.g., on unix where you can't
  391. * tell during the readdir), but that just makes
  392. * the globber work harder.
  393. */
  394. int
  395. Readdir(int f, char *p, int onlydirs)
  396. {
  397. int n;
  398. if(f<0 || f>=NFD)
  399. return 0;
  400. Again:
  401. if(dir[f].i==dir[f].n){ /* read */
  402. free(dir[f].dbuf);
  403. dir[f].dbuf = 0;
  404. n = dirread(f, &dir[f].dbuf);
  405. if(n>0){
  406. if(onlydirs){
  407. n = trimdirs(dir[f].dbuf, n);
  408. if(n == 0)
  409. goto Again;
  410. }
  411. dir[f].n = n;
  412. }else
  413. dir[f].n = 0;
  414. dir[f].i = 0;
  415. }
  416. if(dir[f].i == dir[f].n)
  417. return 0;
  418. strcpy(p, dir[f].dbuf[dir[f].i].name);
  419. dir[f].i++;
  420. return 1;
  421. }
  422. void
  423. Closedir(int f)
  424. {
  425. if(f>=0 && f<NFD){
  426. free(dir[f].dbuf);
  427. dir[f].i = 0;
  428. dir[f].n = 0;
  429. dir[f].dbuf = 0;
  430. }
  431. close(f);
  432. }
  433. int interrupted = 0;
  434. void
  435. notifyf(void*, char *s)
  436. {
  437. int i;
  438. for(i = 0;syssigname[i];i++) if(strncmp(s, syssigname[i], strlen(syssigname[i]))==0){
  439. if(strncmp(s, "sys: ", 5)!=0) interrupted = 1;
  440. goto Out;
  441. }
  442. pfmt(err, "rc: note: %s\n", s);
  443. noted(NDFLT);
  444. return;
  445. Out:
  446. if(strcmp(s, "interrupt")!=0 || trap[i]==0){
  447. trap[i]++;
  448. ntrap++;
  449. }
  450. if(ntrap>=32){ /* rc is probably in a trap loop */
  451. pfmt(err, "rc: Too many traps (trap %s), aborting\n", s);
  452. abort();
  453. }
  454. noted(NCONT);
  455. }
  456. void
  457. Trapinit(void)
  458. {
  459. notify(notifyf);
  460. }
  461. void
  462. Unlink(char *name)
  463. {
  464. remove(name);
  465. }
  466. long
  467. Write(int fd, char *buf, long cnt)
  468. {
  469. return write(fd, buf, (long)cnt);
  470. }
  471. long
  472. Read(int fd, char *buf, long cnt)
  473. {
  474. return read(fd, buf, cnt);
  475. }
  476. long
  477. Seek(int fd, long cnt, long whence)
  478. {
  479. return seek(fd, cnt, whence);
  480. }
  481. int
  482. Executable(char *file)
  483. {
  484. Dir *statbuf;
  485. int ret;
  486. statbuf = dirstat(file);
  487. if(statbuf == nil)
  488. return 0;
  489. ret = ((statbuf->mode&0111)!=0 && (statbuf->mode&DMDIR)==0);
  490. free(statbuf);
  491. return ret;
  492. }
  493. int
  494. Creat(char *file)
  495. {
  496. return create(file, 1, 0666L);
  497. }
  498. int
  499. Dup(int a, int b)
  500. {
  501. return dup(a, b);
  502. }
  503. int
  504. Dup1(int)
  505. {
  506. return -1;
  507. }
  508. void
  509. Exit(char *stat)
  510. {
  511. Updenv();
  512. setstatus(stat);
  513. exits(truestatus()?"":getstatus());
  514. }
  515. int
  516. Eintr(void)
  517. {
  518. return interrupted;
  519. }
  520. void
  521. Noerror(void)
  522. {
  523. interrupted = 0;
  524. }
  525. int
  526. Isatty(int fd)
  527. {
  528. char buf[64];
  529. if(fd2path(fd, buf, sizeof buf) != 0)
  530. return 0;
  531. /* might be #c/cons during boot - fixed 22 april 2005, remove this later */
  532. if(strcmp(buf, "#c/cons") == 0)
  533. return 1;
  534. /* might be /mnt/term/dev/cons */
  535. return strlen(buf) >= 9 && strcmp(buf+strlen(buf)-9, "/dev/cons") == 0;
  536. }
  537. void
  538. Abort(void)
  539. {
  540. pfmt(err, "aborting\n");
  541. flush(err);
  542. Exit("aborting");
  543. }
  544. void
  545. Memcpy(char *a, char *b, long n)
  546. {
  547. memmove(a, b, (long)n);
  548. }
  549. void*
  550. Malloc(ulong n)
  551. {
  552. return malloc(n);
  553. }