tty.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 16 KB of state; allocate them from the frame heap instead of
  18. * BSS to keep the kernel's low-memory footprint small. */
  19. static struct tty *ttys;
  20. static int tty_ready;
  21. static int tty_init(void)
  22. {
  23. unsigned int base, i;
  24. unsigned char *p;
  25. if (tty_ready)
  26. return 1;
  27. base = mm_alloc_pages(TTY_MAX * (TTY_STRIDE / PAGE_SIZE));
  28. if (!base)
  29. return 0;
  30. p = (unsigned char *)base;
  31. for (i = 0; i < TTY_MAX * TTY_STRIDE; i++)
  32. p[i] = 0;
  33. ttys = (struct tty *)base;
  34. tty_ready = 1;
  35. return 1;
  36. }
  37. static struct tty *tty_slot(unsigned int i)
  38. {
  39. return (struct tty *)((unsigned char *)ttys + i * TTY_STRIDE);
  40. }
  41. static struct tty *tty_get(int id)
  42. {
  43. if (!ttys || id <= 0 || id > TTY_MAX)
  44. return 0;
  45. return tty_slot((unsigned int)(id - 1));
  46. }
  47. int tty_valid(int id)
  48. {
  49. struct tty *t = tty_get(id);
  50. return t && t->used;
  51. }
  52. int tty_free_count(void)
  53. {
  54. unsigned int i, n = 0;
  55. if (!tty_init())
  56. return 0;
  57. for (i = 0; i < TTY_MAX; i++)
  58. if (!tty_slot(i)->used)
  59. n++;
  60. return (int)n;
  61. }
  62. int tty_create(void)
  63. {
  64. struct process *p = current_proc();
  65. unsigned int i;
  66. if (!p || !tty_init())
  67. return -1;
  68. for (i = 0; i < TTY_MAX; i++) {
  69. struct tty *t = tty_slot(i);
  70. if (!t->used) {
  71. t->used = 1;
  72. t->owner = p->pid;
  73. t->head = t->tail = 0;
  74. p->tty = (int)(i + 1);
  75. return (int)(i + 1);
  76. }
  77. }
  78. return -1;
  79. }
  80. void tty_close(int id)
  81. {
  82. struct tty *t = tty_get(id);
  83. if (t) {
  84. t->used = 0;
  85. t->head = t->tail = 0;
  86. }
  87. }
  88. int tty_write(int id, const char *buf, unsigned int len)
  89. {
  90. struct tty *t;
  91. unsigned int i = 0;
  92. if (!(t = tty_get(id)) || !t->used)
  93. return -1;
  94. while (i < len) {
  95. unsigned int next = (t->head + 1) % TTY_BUF;
  96. if (next == t->tail) {
  97. struct process *cur = current_proc();
  98. if (!process_is_alive(t->owner))
  99. return -1;
  100. /* The owner is the only reader: sleeping on its own
  101. * full ring can never make progress. Drop instead
  102. * (the browser rebinds itself to its helpers' tty). */
  103. if (cur && cur->pid == t->owner)
  104. return -1;
  105. process_sleep_ms(1);
  106. continue;
  107. }
  108. t->buf[t->head] = (unsigned char)buf[i++];
  109. t->head = next;
  110. /* Mirror command output (including ring-3 terminal commands) to
  111. * the network console, like screen_putchar does for the kernel
  112. * terminal. */
  113. netlog_char(buf[i - 1]);
  114. }
  115. return 0;
  116. }
  117. int tty_write_str(int id, const char *s)
  118. {
  119. unsigned int n = 0;
  120. while (s[n])
  121. n++;
  122. return tty_write(id, s, n);
  123. }
  124. int tty_read(int id, char *buf, unsigned int max)
  125. {
  126. struct tty *t;
  127. unsigned int n = 0;
  128. if (!(t = tty_get(id)) || !t->used)
  129. return -1;
  130. while (n < max && t->tail != t->head) {
  131. buf[n++] = (char)t->buf[t->tail];
  132. t->tail = (t->tail + 1) % TTY_BUF;
  133. }
  134. return (int)n;
  135. }
  136. void tty_reap(unsigned int pid)
  137. {
  138. unsigned int i;
  139. if (!ttys)
  140. return;
  141. for (i = 0; i < TTY_MAX; i++) {
  142. struct tty *t = tty_slot(i);
  143. if (t->used && t->owner == pid)
  144. t->used = 0;
  145. }
  146. }