devcons.c 19 KB

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