tty.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* TTY output rings. Commands write bytes; the terminal process drains and
  2. * renders them. A full ring blocks the writer (a terminal that is not
  3. * scheduled will drain shortly), so `cat`/helpers cannot silently lose output. */
  4. #include "tty.h"
  5. #include "mm.h"
  6. #include "netlog.h"
  7. #include "process.h"
  8. #define TTY_STRIDE (2 * PAGE_SIZE)
  9. struct tty {
  10. int used;
  11. unsigned int owner;
  12. unsigned char buf[TTY_BUF];
  13. unsigned int head, tail;
  14. };
  15. _Static_assert(sizeof(struct tty) <= TTY_STRIDE,
  16. "tty state no longer fits its two-page slot");
  17. /* The rings are 8 KB of state each (128 KB at TTY_MAX 16); allocate them from
  18. * the frame heap instead of BSS to keep the kernel's low-memory footprint
  19. * small. */
  20. static struct tty *ttys;
  21. static int tty_ready;
  22. static int tty_init(void)
  23. {
  24. unsigned int base, i;
  25. unsigned char *p;
  26. if (tty_ready)
  27. return 1;
  28. base = mm_alloc_pages(TTY_MAX * (TTY_STRIDE / PAGE_SIZE));
  29. if (!base)
  30. return 0;
  31. p = (unsigned char *)base;
  32. for (i = 0; i < TTY_MAX * TTY_STRIDE; i++)
  33. p[i] = 0;
  34. ttys = (struct tty *)base;
  35. tty_ready = 1;
  36. return 1;
  37. }
  38. static struct tty *tty_slot(unsigned int i)
  39. {
  40. return (struct tty *)((unsigned char *)ttys + i * TTY_STRIDE);
  41. }
  42. static struct tty *tty_get(int id)
  43. {
  44. if (!ttys || id <= 0 || id > TTY_MAX)
  45. return 0;
  46. return tty_slot((unsigned int)(id - 1));
  47. }
  48. int tty_valid(int id)
  49. {
  50. struct tty *t = tty_get(id);
  51. return t && t->used;
  52. }
  53. int tty_free_count(void)
  54. {
  55. unsigned int i, n = 0;
  56. if (!tty_init())
  57. return 0;
  58. for (i = 0; i < TTY_MAX; i++)
  59. if (!tty_slot(i)->used)
  60. n++;
  61. return (int)n;
  62. }
  63. int tty_alloc(void)
  64. {
  65. struct process *p = current_proc();
  66. unsigned int i;
  67. if (!p || !tty_init())
  68. return -1;
  69. for (i = 0; i < TTY_MAX; i++) {
  70. struct tty *t = tty_slot(i);
  71. if (!t->used) {
  72. t->used = 1;
  73. t->owner = p->pid;
  74. t->head = t->tail = 0;
  75. return (int)(i + 1);
  76. }
  77. }
  78. return -1;
  79. }
  80. int tty_create(void)
  81. {
  82. struct process *p = current_proc();
  83. int id = tty_alloc();
  84. if (id > 0 && p)
  85. p->tty = id;
  86. return id;
  87. }
  88. void tty_close(int id)
  89. {
  90. struct tty *t = tty_get(id);
  91. if (t) {
  92. t->used = 0;
  93. t->head = t->tail = 0;
  94. }
  95. }
  96. int tty_close_owned(int id, unsigned int pid)
  97. {
  98. struct tty *t = tty_get(id);
  99. if (!t || !t->used || t->owner != pid)
  100. return -1;
  101. t->used = 0;
  102. t->head = t->tail = 0;
  103. return 0;
  104. }
  105. int tty_write(int id, const char *buf, unsigned int len)
  106. {
  107. struct tty *t;
  108. unsigned int i = 0;
  109. if (!(t = tty_get(id)) || !t->used)
  110. return -1;
  111. while (i < len) {
  112. unsigned int next = (t->head + 1) % TTY_BUF;
  113. if (next == t->tail) {
  114. struct process *cur = current_proc();
  115. if (!process_is_alive(t->owner))
  116. return -1;
  117. /* The owner is the only reader: sleeping on its own
  118. * full ring can never make progress. Drop instead
  119. * (the browser rebinds itself to its helpers' tty). */
  120. if (cur && cur->pid == t->owner)
  121. return -1;
  122. process_sleep_ms(1);
  123. continue;
  124. }
  125. t->buf[t->head] = (unsigned char)buf[i++];
  126. t->head = next;
  127. /* Mirror command output (including ring-3 terminal commands) to
  128. * the network console, like screen_putchar does for the kernel
  129. * terminal. */
  130. netlog_char(buf[i - 1]);
  131. }
  132. return 0;
  133. }
  134. int tty_try_write(int id, const char *buf, unsigned int len)
  135. {
  136. struct tty *t;
  137. unsigned int i, used, free;
  138. if (!(t = tty_get(id)) || !t->used)
  139. return -1;
  140. used = (t->head + TTY_BUF - t->tail) % TTY_BUF;
  141. free = TTY_BUF - 1 - used;
  142. if (len > free)
  143. return -1; /* no partial control messages */
  144. for (i = 0; i < len; i++) {
  145. t->buf[t->head] = (unsigned char)buf[i];
  146. t->head = (t->head + 1) % TTY_BUF;
  147. netlog_char(buf[i]);
  148. }
  149. return 0;
  150. }
  151. int tty_write_str(int id, const char *s)
  152. {
  153. unsigned int n = 0;
  154. while (s[n])
  155. n++;
  156. return tty_write(id, s, n);
  157. }
  158. int tty_read(int id, char *buf, unsigned int max)
  159. {
  160. struct tty *t;
  161. unsigned int n = 0;
  162. if (!(t = tty_get(id)) || !t->used)
  163. return -1;
  164. while (n < max && t->tail != t->head) {
  165. buf[n++] = (char)t->buf[t->tail];
  166. t->tail = (t->tail + 1) % TTY_BUF;
  167. }
  168. return (int)n;
  169. }
  170. void tty_reap(unsigned int pid)
  171. {
  172. unsigned int i;
  173. if (!ttys)
  174. return;
  175. for (i = 0; i < TTY_MAX; i++) {
  176. struct tty *t = tty_slot(i);
  177. if (t->used && t->owner == pid)
  178. t->used = 0;
  179. }
  180. }