devcons.c 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "u.h"
  10. #include "lib.h"
  11. #include "dat.h"
  12. #include "fns.h"
  13. #include "error.h"
  14. #include "keyboard.h"
  15. void (*consdebug)(void) = 0;
  16. void (*screenputs)(char*, int) = 0;
  17. Queue* kbdq; /* unprocessed console input */
  18. Queue* lineq; /* processed console input */
  19. Queue* serialoq; /* serial console output */
  20. Queue* kprintoq; /* console output, for /dev/kprint */
  21. int32_t kprintinuse; /* test and set whether /dev/kprint is open */
  22. Lock kprintlock;
  23. int iprintscreenputs = 0;
  24. int panicking;
  25. struct
  26. {
  27. int exiting;
  28. int machs;
  29. } active;
  30. static struct
  31. {
  32. QLock lk;
  33. int raw; /* true if we shouldn't process input */
  34. int ctl; /* number of opens to the control file */
  35. int x; /* index into line */
  36. char line[1024]; /* current input line */
  37. int count;
  38. int ctlpoff;
  39. /* a place to save up characters at interrupt time before dumping them in the queue */
  40. Lock lockputc;
  41. char istage[1024];
  42. char *iw;
  43. char *ir;
  44. char *ie;
  45. } kbd = {
  46. { 0 },
  47. 0,
  48. 0,
  49. 0,
  50. { 0 },
  51. 0,
  52. 0,
  53. { 0 },
  54. { 0 },
  55. kbd.istage,
  56. kbd.istage,
  57. kbd.istage + sizeof(kbd.istage),
  58. };
  59. char *sysname;
  60. int64_t fasthz;
  61. static int readtime(uint32_t, char*, int);
  62. static int readbintime(char*, int);
  63. static int writetime(char*, int);
  64. static int writebintime(char*, int);
  65. enum
  66. {
  67. CMreboot,
  68. CMpanic,
  69. };
  70. Cmdtab rebootmsg[] =
  71. {
  72. CMreboot, "reboot", 0,
  73. CMpanic, "panic", 0,
  74. };
  75. int
  76. return0(void *v)
  77. {
  78. return 0;
  79. }
  80. void
  81. printinit(void)
  82. {
  83. lineq = qopen(2*1024, 0, 0, nil);
  84. if(lineq == nil)
  85. panic("printinit");
  86. qnoblock(lineq, 1);
  87. kbdq = qopen(4*1024, 0, 0, 0);
  88. if(kbdq == nil)
  89. panic("kbdinit");
  90. qnoblock(kbdq, 1);
  91. }
  92. int
  93. consactive(void)
  94. {
  95. if(serialoq)
  96. return qlen(serialoq) > 0;
  97. return 0;
  98. }
  99. void
  100. prflush(void)
  101. {
  102. /*
  103. ulong now;
  104. now = m->ticks;
  105. while(consactive())
  106. if(m->ticks - now >= HZ)
  107. break;
  108. */
  109. }
  110. /*
  111. * Print a string on the console. Convert \n to \r\n for serial
  112. * line consoles. Locking of the queues is left up to the screen
  113. * or uart code. Multi-line messages to serial consoles may get
  114. * interspersed with other messages.
  115. */
  116. static void
  117. putstrn0(char *str, int n, int usewrite)
  118. {
  119. /*
  120. * if someone is reading /dev/kprint,
  121. * put the message there.
  122. * if not and there's an attached bit mapped display,
  123. * put the message there.
  124. *
  125. * if there's a serial line being used as a console,
  126. * put the message there.
  127. */
  128. if(kprintoq != nil && !qisclosed(kprintoq)){
  129. if(usewrite)
  130. qwrite(kprintoq, str, n);
  131. else
  132. qiwrite(kprintoq, str, n);
  133. }else if(screenputs != 0)
  134. screenputs(str, n);
  135. }
  136. void
  137. putstrn(char *str, int n)
  138. {
  139. putstrn0(str, n, 0);
  140. }
  141. int noprint;
  142. int
  143. print(char *fmt, ...)
  144. {
  145. int n;
  146. va_list arg;
  147. char buf[PRINTSIZE];
  148. if(noprint)
  149. return -1;
  150. va_start(arg, fmt);
  151. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  152. va_end(arg);
  153. putstrn(buf, n);
  154. return n;
  155. }
  156. void
  157. panic(char *fmt, ...)
  158. {
  159. int n;
  160. va_list arg;
  161. char buf[PRINTSIZE];
  162. kprintoq = nil; /* don't try to write to /dev/kprint */
  163. if(panicking)
  164. for(;;);
  165. panicking = 1;
  166. splhi();
  167. strcpy(buf, "panic: ");
  168. va_start(arg, fmt);
  169. n = vseprint(buf+strlen(buf), buf+sizeof(buf), fmt, arg) - buf;
  170. va_end(arg);
  171. buf[n] = '\n';
  172. uartputs(buf, n+1);
  173. if(consdebug)
  174. (*consdebug)();
  175. spllo();
  176. prflush();
  177. putstrn(buf, n+1);
  178. dumpstack();
  179. exit(1);
  180. }
  181. int
  182. pprint(char *fmt, ...)
  183. {
  184. int n;
  185. Chan *c;
  186. va_list arg;
  187. char buf[2*PRINTSIZE];
  188. if(up == nil || up->fgrp == nil)
  189. return 0;
  190. c = up->fgrp->fd[2];
  191. if(c==0 || (c->mode!=OWRITE && c->mode!=ORDWR))
  192. return 0;
  193. n = sprint(buf, "%s %lud: ", up->text, up->pid);
  194. va_start(arg, fmt);
  195. n = vseprint(buf+n, buf+sizeof(buf), fmt, arg) - buf;
  196. va_end(arg);
  197. if(waserror())
  198. return 0;
  199. devtab[c->type]->write(c, buf, n, c->offset);
  200. poperror();
  201. lock(&c->ref.lk);
  202. c->offset += n;
  203. unlock(&c->ref.lk);
  204. return n;
  205. }
  206. static void
  207. echoscreen(char *buf, int n)
  208. {
  209. char *e, *p;
  210. char ebuf[128];
  211. int x;
  212. p = ebuf;
  213. e = ebuf + sizeof(ebuf) - 4;
  214. while(n-- > 0){
  215. if(p >= e){
  216. screenputs(ebuf, p - ebuf);
  217. p = ebuf;
  218. }
  219. x = *buf++;
  220. if(x == 0x15){
  221. *p++ = '^';
  222. *p++ = 'U';
  223. *p++ = '\n';
  224. } else
  225. *p++ = x;
  226. }
  227. if(p != ebuf)
  228. screenputs(ebuf, p - ebuf);
  229. }
  230. static void
  231. echoserialoq(char *buf, int n)
  232. {
  233. char *e, *p;
  234. char ebuf[128];
  235. int x;
  236. p = ebuf;
  237. e = ebuf + sizeof(ebuf) - 4;
  238. while(n-- > 0){
  239. if(p >= e){
  240. qiwrite(serialoq, ebuf, p - ebuf);
  241. p = ebuf;
  242. }
  243. x = *buf++;
  244. if(x == '\n'){
  245. *p++ = '\r';
  246. *p++ = '\n';
  247. } else if(x == 0x15){
  248. *p++ = '^';
  249. *p++ = 'U';
  250. *p++ = '\n';
  251. } else
  252. *p++ = x;
  253. }
  254. if(p != ebuf)
  255. qiwrite(serialoq, ebuf, p - ebuf);
  256. }
  257. static void
  258. echo(char *buf, int n)
  259. {
  260. static int ctrlt;
  261. int x;
  262. char *e, *p;
  263. e = buf+n;
  264. for(p = buf; p < e; p++){
  265. switch(*p){
  266. case 0x10: /* ^P */
  267. if(cpuserver && !kbd.ctlpoff){
  268. active.exiting = 1;
  269. return;
  270. }
  271. break;
  272. case 0x14: /* ^T */
  273. ctrlt++;
  274. if(ctrlt > 2)
  275. ctrlt = 2;
  276. continue;
  277. }
  278. if(ctrlt != 2)
  279. continue;
  280. /* ^T escapes */
  281. ctrlt = 0;
  282. switch(*p){
  283. case 'S':
  284. x = splhi();
  285. dumpstack();
  286. procdump();
  287. splx(x);
  288. return;
  289. case 's':
  290. dumpstack();
  291. return;
  292. case 'x':
  293. xsummary();
  294. ixsummary();
  295. mallocsummary();
  296. pagersummary();
  297. return;
  298. case 'd':
  299. if(consdebug == 0)
  300. consdebug = rdb;
  301. else
  302. consdebug = 0;
  303. print("consdebug now 0x%p\n", consdebug);
  304. return;
  305. case 'D':
  306. if(consdebug == 0)
  307. consdebug = rdb;
  308. consdebug();
  309. return;
  310. case 'p':
  311. x = spllo();
  312. procdump();
  313. splx(x);
  314. return;
  315. case 'q':
  316. scheddump();
  317. return;
  318. case 'k':
  319. killbig();
  320. return;
  321. case 'r':
  322. exit(0);
  323. return;
  324. }
  325. }
  326. qproduce(kbdq, buf, n);
  327. if(kbd.raw)
  328. return;
  329. if(screenputs != 0)
  330. echoscreen(buf, n);
  331. if(serialoq)
  332. echoserialoq(buf, n);
  333. }
  334. /*
  335. * Called by a uart interrupt for console input.
  336. *
  337. * turn '\r' into '\n' before putting it into the queue.
  338. */
  339. int
  340. kbdcr2nl(Queue *q, int ch)
  341. {
  342. char *next;
  343. USED(q);
  344. ilock(&kbd.lockputc); /* just a mutex */
  345. if(ch == '\r' && !kbd.raw)
  346. ch = '\n';
  347. next = kbd.iw+1;
  348. if(next >= kbd.ie)
  349. next = kbd.istage;
  350. if(next != kbd.ir){
  351. *kbd.iw = ch;
  352. kbd.iw = next;
  353. }
  354. iunlock(&kbd.lockputc);
  355. return 0;
  356. }
  357. static
  358. void
  359. _kbdputc(int c)
  360. {
  361. Rune r;
  362. char buf[UTFmax];
  363. int n;
  364. r = c;
  365. n = runetochar(buf, &r);
  366. if(n == 0)
  367. return;
  368. echo(buf, n);
  369. // kbd.c = r;
  370. // qproduce(kbdq, buf, n);
  371. }
  372. /* _kbdputc, but with compose translation */
  373. int
  374. kbdputc(Queue *q, int c)
  375. {
  376. int i;
  377. static int collecting, nk;
  378. static Rune kc[5];
  379. if(c == Kalt){
  380. collecting = 1;
  381. nk = 0;
  382. return 0;
  383. }
  384. if(!collecting){
  385. _kbdputc(c);
  386. return 0;
  387. }
  388. kc[nk++] = c;
  389. c = latin1(kc, nk);
  390. if(c < -1) /* need more keystrokes */
  391. return 0;
  392. if(c != -1) /* valid sequence */
  393. _kbdputc(c);
  394. else
  395. for(i=0; i<nk; i++)
  396. _kbdputc(kc[i]);
  397. nk = 0;
  398. collecting = 0;
  399. return 0;
  400. }
  401. enum{
  402. Qdir,
  403. Qbintime,
  404. Qcons,
  405. Qconsctl,
  406. Qcpunote,
  407. Qcputime,
  408. Qdrivers,
  409. Qkprint,
  410. Qhostdomain,
  411. Qhostowner,
  412. Qnull,
  413. Qosversion,
  414. Qpgrpid,
  415. Qpid,
  416. Qppid,
  417. Qrandom,
  418. Qreboot,
  419. Qsecstore,
  420. Qshowfile,
  421. Qsnarf,
  422. Qswap,
  423. Qsysname,
  424. Qsysstat,
  425. Qtime,
  426. Quser,
  427. Qzero,
  428. };
  429. enum
  430. {
  431. VLNUMSIZE= 22,
  432. };
  433. static Dirtab consdir[]={
  434. ".", {Qdir, 0, QTDIR}, 0, DMDIR|0555,
  435. "bintime", {Qbintime}, 24, 0664,
  436. "cons", {Qcons}, 0, 0660,
  437. "consctl", {Qconsctl}, 0, 0220,
  438. "cpunote", {Qcpunote}, 0, 0444,
  439. "cputime", {Qcputime}, 6*NUMSIZE, 0444,
  440. "drivers", {Qdrivers}, 0, 0444,
  441. "hostdomain", {Qhostdomain}, DOMLEN, 0664,
  442. "hostowner", {Qhostowner}, 0, 0664,
  443. "kprint", {Qkprint, 0, QTEXCL}, 0, DMEXCL|0440,
  444. "null", {Qnull}, 0, 0666,
  445. "osversion", {Qosversion}, 0, 0444,
  446. "pgrpid", {Qpgrpid}, NUMSIZE, 0444,
  447. "pid", {Qpid}, NUMSIZE, 0444,
  448. "ppid", {Qppid}, NUMSIZE, 0444,
  449. "random", {Qrandom}, 0, 0444,
  450. "reboot", {Qreboot}, 0, 0664,
  451. "secstore", {Qsecstore}, 0, 0666,
  452. "showfile", {Qshowfile}, 0, 0220,
  453. "snarf", {Qsnarf}, 0, 0666,
  454. "swap", {Qswap}, 0, 0664,
  455. "sysname", {Qsysname}, 0, 0664,
  456. "sysstat", {Qsysstat}, 0, 0666,
  457. "time", {Qtime}, NUMSIZE+3*VLNUMSIZE, 0664,
  458. "user", {Quser}, 0, 0666,
  459. "zero", {Qzero}, 0, 0444,
  460. };
  461. char secstorebuf[65536];
  462. Dirtab *secstoretab = &consdir[Qsecstore];
  463. Dirtab *snarftab = &consdir[Qsnarf];
  464. int
  465. readnum(uint32_t off, char *buf, uint32_t n, uint32_t val, int size)
  466. {
  467. char tmp[64];
  468. snprint(tmp, sizeof(tmp), "%*.0lud", size-1, val);
  469. tmp[size-1] = ' ';
  470. if(off >= size)
  471. return 0;
  472. if(off+n > size)
  473. n = size-off;
  474. memmove(buf, tmp+off, n);
  475. return n;
  476. }
  477. int
  478. readstr(uint32_t off, char *buf, uint32_t n, char *str)
  479. {
  480. int size;
  481. size = strlen(str);
  482. if(off >= size)
  483. return 0;
  484. if(off+n > size)
  485. n = size-off;
  486. memmove(buf, str+off, n);
  487. return n;
  488. }
  489. static void
  490. consinit(void)
  491. {
  492. todinit();
  493. randominit();
  494. /*
  495. * at 115200 baud, the 1024 char buffer takes 56 ms to process,
  496. * processing it every 22 ms should be fine
  497. */
  498. /* addclock0link(kbdputcclock, 22); */
  499. }
  500. static Chan*
  501. consattach(char *spec)
  502. {
  503. return devattach('c', spec);
  504. }
  505. static Walkqid*
  506. conswalk(Chan *c, Chan *nc, char **name, int nname)
  507. {
  508. return devwalk(c, nc, name,nname, consdir, nelem(consdir), devgen);
  509. }
  510. static int
  511. consstat(Chan *c, uint8_t *dp, int n)
  512. {
  513. return devstat(c, dp, n, consdir, nelem(consdir), devgen);
  514. }
  515. static Chan*
  516. consopen(Chan *c, int omode)
  517. {
  518. c->aux = nil;
  519. c = devopen(c, omode, consdir, nelem(consdir), devgen);
  520. switch((uint32_t)c->qid.path){
  521. case Qconsctl:
  522. qlock(&kbd.lk);
  523. kbd.ctl++;
  524. qunlock(&kbd.lk);
  525. break;
  526. case Qkprint:
  527. lock(&kprintlock);
  528. if(kprintinuse != 0){
  529. c->flag &= ~COPEN;
  530. unlock(&kprintlock);
  531. error(Einuse);
  532. }
  533. kprintinuse = 1;
  534. unlock(&kprintlock);
  535. if(kprintoq == nil){
  536. kprintoq = qopen(8*1024, Qcoalesce, 0, 0);
  537. if(kprintoq == nil){
  538. c->flag &= ~COPEN;
  539. error(Enomem);
  540. }
  541. qnoblock(kprintoq, 1);
  542. }else
  543. qreopen(kprintoq);
  544. c->iounit = qiomaxatomic;
  545. break;
  546. case Qsecstore:
  547. if(omode == ORDWR)
  548. error(Eperm);
  549. if(omode != OREAD)
  550. memset(secstorebuf, 0, sizeof secstorebuf);
  551. break;
  552. case Qsnarf:
  553. if(omode == ORDWR)
  554. error(Eperm);
  555. if(omode == OREAD)
  556. c->aux = strdup("");
  557. else
  558. c->aux = mallocz(SnarfSize, 1);
  559. break;
  560. }
  561. return c;
  562. }
  563. static void
  564. consclose(Chan *c)
  565. {
  566. switch((uint32_t)c->qid.path){
  567. /* last close of control file turns off raw */
  568. case Qconsctl:
  569. if(c->flag&COPEN){
  570. qlock(&kbd.lk);
  571. if(--kbd.ctl == 0)
  572. kbd.raw = 0;
  573. qunlock(&kbd.lk);
  574. }
  575. break;
  576. /* close of kprint allows other opens */
  577. case Qkprint:
  578. if(c->flag & COPEN){
  579. kprintinuse = 0;
  580. qhangup(kprintoq, nil);
  581. }
  582. break;
  583. case Qsnarf:
  584. if(c->mode == OWRITE)
  585. clipwrite(c->aux);
  586. free(c->aux);
  587. break;
  588. }
  589. }
  590. static int32_t
  591. consread(Chan *c, void *buf, int32_t n, int64_t off)
  592. {
  593. char *b;
  594. char tmp[128]; /* must be >= 6*NUMSIZE */
  595. char *cbuf = buf;
  596. int ch, i, eol;
  597. int64_t offset = off;
  598. if(n <= 0)
  599. return n;
  600. switch((uint32_t)c->qid.path){
  601. case Qdir:
  602. return devdirread(c, buf, n, consdir, nelem(consdir), devgen);
  603. case Qcons:
  604. qlock(&kbd.lk);
  605. if(waserror()) {
  606. qunlock(&kbd.lk);
  607. nexterror();
  608. }
  609. if(kbd.raw) {
  610. if(qcanread(lineq))
  611. n = qread(lineq, buf, n);
  612. else {
  613. /* read as much as possible */
  614. do {
  615. i = qread(kbdq, cbuf, n);
  616. cbuf += i;
  617. n -= i;
  618. } while (n>0 && qcanread(kbdq));
  619. n = cbuf - (char*)buf;
  620. }
  621. } else {
  622. while(!qcanread(lineq)) {
  623. qread(kbdq, &kbd.line[kbd.x], 1);
  624. ch = kbd.line[kbd.x];
  625. eol = 0;
  626. switch(ch){
  627. case '\b':
  628. if(kbd.x)
  629. kbd.x--;
  630. break;
  631. case 0x15:
  632. kbd.x = 0;
  633. break;
  634. case '\n':
  635. case 0x04:
  636. eol = 1;
  637. default:
  638. kbd.line[kbd.x++] = ch;
  639. break;
  640. }
  641. if(kbd.x == sizeof(kbd.line) || eol){
  642. if(ch == 0x04)
  643. kbd.x--;
  644. qwrite(lineq, kbd.line, kbd.x);
  645. kbd.x = 0;
  646. }
  647. }
  648. n = qread(lineq, buf, n);
  649. }
  650. qunlock(&kbd.lk);
  651. poperror();
  652. return n;
  653. case Qcpunote:
  654. sleep(&up->sleep, return0, nil);
  655. case Qcputime:
  656. return 0;
  657. case Qkprint:
  658. return qread(kprintoq, buf, n);
  659. case Qpgrpid:
  660. return readnum((uint32_t)offset, buf, n, up->pgrp->pgrpid,
  661. NUMSIZE);
  662. case Qpid:
  663. return readnum((uint32_t)offset, buf, n, up->pid, NUMSIZE);
  664. case Qppid:
  665. return readnum((uint32_t)offset, buf, n, up->parentpid,
  666. NUMSIZE);
  667. case Qtime:
  668. return readtime((uint32_t)offset, buf, n);
  669. case Qbintime:
  670. return readbintime(buf, n);
  671. case Qhostowner:
  672. return readstr((uint32_t)offset, buf, n, eve);
  673. case Qhostdomain:
  674. return readstr((uint32_t)offset, buf, n, hostdomain);
  675. case Quser:
  676. return readstr((uint32_t)offset, buf, n, up->user);
  677. case Qnull:
  678. return 0;
  679. case Qsnarf:
  680. if(offset == 0){
  681. free(c->aux);
  682. c->aux = clipread();
  683. }
  684. if(c->aux == nil)
  685. return 0;
  686. return readstr(offset, buf, n, c->aux);
  687. case Qsecstore:
  688. return readstr(offset, buf, n, secstorebuf);
  689. case Qsysstat:
  690. return 0;
  691. case Qswap:
  692. return 0;
  693. case Qsysname:
  694. if(sysname == nil)
  695. return 0;
  696. return readstr((uint32_t)offset, buf, n, sysname);
  697. case Qrandom:
  698. return randomread(buf, n);
  699. case Qdrivers:
  700. b = malloc(READSTR);
  701. if(b == nil)
  702. error(Enomem);
  703. n = 0;
  704. for(i = 0; devtab[i] != nil; i++)
  705. n += snprint(b+n, READSTR-n, "#%C %s\n", devtab[i]->dc, devtab[i]->name);
  706. if(waserror()){
  707. free(b);
  708. nexterror();
  709. }
  710. n = readstr((uint32_t)offset, buf, n, b);
  711. free(b);
  712. poperror();
  713. return n;
  714. case Qzero:
  715. memset(buf, 0, n);
  716. return n;
  717. case Qosversion:
  718. snprint(tmp, sizeof tmp, "2000");
  719. n = readstr((uint32_t)offset, buf, n, tmp);
  720. return n;
  721. default:
  722. print("consread 0x%llux\n", c->qid.path);
  723. error(Egreg);
  724. }
  725. return -1; /* never reached */
  726. }
  727. static int32_t
  728. conswrite(Chan *c, void *va, int32_t n, int64_t off)
  729. {
  730. char buf[256];
  731. int32_t l, bp;
  732. char *a = va;
  733. int fd;
  734. Chan *swc;
  735. uint32_t offset = off;
  736. Cmdbuf *cb;
  737. Cmdtab *ct;
  738. switch((uint32_t)c->qid.path){
  739. case Qcons:
  740. /*
  741. * Can't page fault in putstrn, so copy the data locally.
  742. */
  743. l = n;
  744. while(l > 0){
  745. bp = l;
  746. if(bp > sizeof buf)
  747. bp = sizeof buf;
  748. memmove(buf, a, bp);
  749. putstrn0(buf, bp, 1);
  750. a += bp;
  751. l -= bp;
  752. }
  753. break;
  754. case Qconsctl:
  755. if(n >= sizeof(buf))
  756. n = sizeof(buf)-1;
  757. strncpy(buf, a, n);
  758. buf[n] = 0;
  759. for(a = buf; a;){
  760. if(strncmp(a, "rawon", 5) == 0){
  761. qlock(&kbd.lk);
  762. if(kbd.x){
  763. qwrite(kbdq, kbd.line, kbd.x);
  764. kbd.x = 0;
  765. }
  766. kbd.raw = 1;
  767. qunlock(&kbd.lk);
  768. } else if(strncmp(a, "rawoff", 6) == 0){
  769. qlock(&kbd.lk);
  770. kbd.raw = 0;
  771. kbd.x = 0;
  772. qunlock(&kbd.lk);
  773. } else if(strncmp(a, "ctlpon", 6) == 0){
  774. kbd.ctlpoff = 0;
  775. } else if(strncmp(a, "ctlpoff", 7) == 0){
  776. kbd.ctlpoff = 1;
  777. }
  778. if((a = strchr(a, ' ')))
  779. a++;
  780. }
  781. break;
  782. case Qtime:
  783. if(!iseve())
  784. error(Eperm);
  785. return writetime(a, n);
  786. case Qbintime:
  787. if(!iseve())
  788. error(Eperm);
  789. return writebintime(a, n);
  790. case Qhostowner:
  791. return hostownerwrite(a, n);
  792. case Qhostdomain:
  793. return hostdomainwrite(a, n);
  794. case Quser:
  795. return userwrite(a, n);
  796. case Qnull:
  797. break;
  798. case Qreboot:
  799. if(!iseve())
  800. error(Eperm);
  801. cb = parsecmd(a, n);
  802. if(waserror()) {
  803. free(cb);
  804. nexterror();
  805. }
  806. ct = lookupcmd(cb, rebootmsg, nelem(rebootmsg));
  807. switch(ct->index) {
  808. case CMreboot:
  809. rebootcmd(cb->nf-1, cb->f+1);
  810. break;
  811. case CMpanic:
  812. panic("/dev/reboot");
  813. }
  814. poperror();
  815. free(cb);
  816. break;
  817. case Qsecstore:
  818. if(offset >= sizeof secstorebuf || offset+n+1 >= sizeof secstorebuf)
  819. error(Etoobig);
  820. secstoretab->qid.vers++;
  821. memmove(secstorebuf+offset, va, n);
  822. return n;
  823. case Qshowfile:
  824. return showfilewrite(a, n);
  825. case Qsnarf:
  826. if(offset >= SnarfSize || offset+n >= SnarfSize)
  827. error(Etoobig);
  828. snarftab->qid.vers++;
  829. memmove((uint8_t*)c->aux+offset, va, n);
  830. return n;
  831. case Qsysstat:
  832. n = 0;
  833. break;
  834. case Qswap:
  835. if(n >= sizeof buf)
  836. error(Egreg);
  837. memmove(buf, va, n); /* so we can NUL-terminate */
  838. buf[n] = 0;
  839. /* start a pager if not already started */
  840. if(strncmp(buf, "start", 5) == 0){
  841. kickpager();
  842. break;
  843. }
  844. if(cpuserver && !iseve())
  845. error(Eperm);
  846. if(buf[0]<'0' || '9'<buf[0])
  847. error(Ebadarg);
  848. fd = strtoul(buf, 0, 0);
  849. swc = fdtochan(fd, -1, 1, 1);
  850. setswapchan(swc);
  851. break;
  852. case Qsysname:
  853. if(offset != 0)
  854. error(Ebadarg);
  855. if(n <= 0 || n >= sizeof buf)
  856. error(Ebadarg);
  857. strncpy(buf, a, n);
  858. buf[n] = 0;
  859. if(buf[n-1] == '\n')
  860. buf[n-1] = 0;
  861. kstrdup(&sysname, buf);
  862. break;
  863. default:
  864. print("conswrite: 0x%llux\n", c->qid.path);
  865. error(Egreg);
  866. }
  867. return n;
  868. }
  869. Dev consdevtab = {
  870. 'c',
  871. "cons",
  872. devreset,
  873. consinit,
  874. devshutdown,
  875. consattach,
  876. conswalk,
  877. consstat,
  878. consopen,
  879. devcreate,
  880. consclose,
  881. consread,
  882. devbread,
  883. conswrite,
  884. devbwrite,
  885. devremove,
  886. devwstat,
  887. };
  888. static uint64_t uvorder = (uint64_t) 0x0001020304050607ULL;
  889. static uint8_t*
  890. le2vlong(int64_t *to, uint8_t *f)
  891. {
  892. uint8_t *t, *o;
  893. int i;
  894. t = (uint8_t*)to;
  895. o = (uint8_t*)&uvorder;
  896. for(i = 0; i < sizeof(int64_t); i++)
  897. t[o[i]] = f[i];
  898. return f+sizeof(int64_t);
  899. }
  900. static uint8_t*
  901. vlong2le(uint8_t *t, int64_t from)
  902. {
  903. uint8_t *f, *o;
  904. int i;
  905. f = (uint8_t*)&from;
  906. o = (uint8_t*)&uvorder;
  907. for(i = 0; i < sizeof(int64_t); i++)
  908. t[i] = f[o[i]];
  909. return t+sizeof(int64_t);
  910. }
  911. static int32_t order = 0x00010203;
  912. static uint8_t*
  913. le2long(int32_t *to, uint8_t *f)
  914. {
  915. uint8_t *t, *o;
  916. int i;
  917. t = (uint8_t*)to;
  918. o = (uint8_t*)&order;
  919. for(i = 0; i < sizeof(int32_t); i++)
  920. t[o[i]] = f[i];
  921. return f+sizeof(int32_t);
  922. }
  923. /*
  924. static uchar*
  925. long2le(uchar *t, long from)
  926. {
  927. uchar *f, *o;
  928. int i;
  929. f = (uchar*)&from;
  930. o = (uchar*)&order;
  931. for(i = 0; i < sizeof(long); i++)
  932. t[i] = f[o[i]];
  933. return t+sizeof(long);
  934. }
  935. */
  936. char *Ebadtimectl = "bad time control";
  937. /*
  938. * like the old #c/time but with added info. Return
  939. *
  940. * secs nanosecs fastticks fasthz
  941. */
  942. static int
  943. readtime(uint32_t off, char *buf, int n)
  944. {
  945. int64_t nsec, ticks;
  946. int32_t sec;
  947. char str[7*NUMSIZE];
  948. nsec = todget(&ticks);
  949. if(fasthz == (int64_t)0)
  950. fastticks((uint64_t*)&fasthz);
  951. sec = nsec/((uint64_t) 1000000000);
  952. snprint(str, sizeof(str), "%*.0lud %*.0llud %*.0llud %*.0llud ",
  953. NUMSIZE-1, sec,
  954. VLNUMSIZE-1, nsec,
  955. VLNUMSIZE-1, ticks,
  956. VLNUMSIZE-1, fasthz);
  957. return readstr(off, buf, n, str);
  958. }
  959. /*
  960. * set the time in seconds
  961. */
  962. static int
  963. writetime(char *buf, int n)
  964. {
  965. char b[13];
  966. int32_t i;
  967. int64_t now;
  968. if(n >= sizeof(b))
  969. error(Ebadtimectl);
  970. strncpy(b, buf, n);
  971. b[n] = 0;
  972. i = strtol(b, 0, 0);
  973. if(i <= 0)
  974. error(Ebadtimectl);
  975. now = i*((int64_t) 1000000000);
  976. todset(now, 0, 0);
  977. return n;
  978. }
  979. /*
  980. * read binary time info. all numbers are little endian.
  981. * ticks and nsec are syncronized.
  982. */
  983. static int
  984. readbintime(char *buf, int n)
  985. {
  986. int i;
  987. int64_t nsec, ticks;
  988. uint8_t *b = (uint8_t*)buf;
  989. i = 0;
  990. if(fasthz == (int64_t)0)
  991. fastticks((uint64_t*)&fasthz);
  992. nsec = todget(&ticks);
  993. if(n >= 3*sizeof(uint64_t)){
  994. vlong2le(b+2*sizeof(uint64_t), fasthz);
  995. i += sizeof(uint64_t);
  996. }
  997. if(n >= 2*sizeof(uint64_t)){
  998. vlong2le(b+sizeof(uint64_t), ticks);
  999. i += sizeof(uint64_t);
  1000. }
  1001. if(n >= 8){
  1002. vlong2le(b, nsec);
  1003. i += sizeof(int64_t);
  1004. }
  1005. return i;
  1006. }
  1007. /*
  1008. * set any of the following
  1009. * - time in nsec
  1010. * - nsec trim applied over some seconds
  1011. * - clock frequency
  1012. */
  1013. static int
  1014. writebintime(char *buf, int n)
  1015. {
  1016. uint8_t *p;
  1017. int64_t delta;
  1018. int32_t period;
  1019. n--;
  1020. p = (uint8_t*)buf + 1;
  1021. switch(*buf){
  1022. case 'n':
  1023. if(n < sizeof(int64_t))
  1024. error(Ebadtimectl);
  1025. le2vlong(&delta, p);
  1026. todset(delta, 0, 0);
  1027. break;
  1028. case 'd':
  1029. if(n < sizeof(int64_t)+sizeof(int32_t))
  1030. error(Ebadtimectl);
  1031. p = le2vlong(&delta, p);
  1032. le2long(&period, p);
  1033. todset(-1, delta, period);
  1034. break;
  1035. case 'f':
  1036. if(n < sizeof(uint64_t))
  1037. error(Ebadtimectl);
  1038. le2vlong(&fasthz, p);
  1039. todsetfreq(fasthz);
  1040. break;
  1041. }
  1042. return n;
  1043. }
  1044. int
  1045. iprint(char *fmt, ...)
  1046. {
  1047. int n, s;
  1048. va_list arg;
  1049. char buf[PRINTSIZE];
  1050. s = splhi();
  1051. va_start(arg, fmt);
  1052. n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
  1053. va_end(arg);
  1054. if(screenputs != 0 && iprintscreenputs)
  1055. screenputs(buf, n);
  1056. #undef write
  1057. write(2, buf, n);
  1058. splx(s);
  1059. return n;
  1060. }