chan_user.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
  4. */
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <sched.h>
  9. #include <signal.h>
  10. #include <termios.h>
  11. #include <sys/ioctl.h>
  12. #include "chan_user.h"
  13. #include <os.h>
  14. #include <um_malloc.h>
  15. void generic_close(int fd, void *unused)
  16. {
  17. close(fd);
  18. }
  19. int generic_read(int fd, __u8 *c_out, void *unused)
  20. {
  21. int n;
  22. CATCH_EINTR(n = read(fd, c_out, sizeof(*c_out)));
  23. if (n > 0)
  24. return n;
  25. else if (n == 0)
  26. return -EIO;
  27. else if (errno == EAGAIN)
  28. return 0;
  29. return -errno;
  30. }
  31. /* XXX Trivial wrapper around write */
  32. int generic_write(int fd, const __u8 *buf, size_t n, void *unused)
  33. {
  34. int written = 0;
  35. int err;
  36. /* The FD may be in blocking mode, as such, need to retry short writes,
  37. * they may have been interrupted by a signal.
  38. */
  39. do {
  40. errno = 0;
  41. err = write(fd, buf + written, n - written);
  42. if (err > 0) {
  43. written += err;
  44. continue;
  45. }
  46. } while (err < 0 && errno == EINTR);
  47. if (written > 0)
  48. return written;
  49. else if (errno == EAGAIN)
  50. return 0;
  51. else if (err == 0)
  52. return -EIO;
  53. return -errno;
  54. }
  55. int generic_window_size(int fd, void *unused, unsigned short *rows_out,
  56. unsigned short *cols_out)
  57. {
  58. struct winsize size;
  59. int ret;
  60. if (ioctl(fd, TIOCGWINSZ, &size) < 0)
  61. return -errno;
  62. ret = ((*rows_out != size.ws_row) || (*cols_out != size.ws_col));
  63. *rows_out = size.ws_row;
  64. *cols_out = size.ws_col;
  65. return ret;
  66. }
  67. void generic_free(void *data)
  68. {
  69. kfree(data);
  70. }
  71. int generic_console_write(int fd, const char *buf, int n)
  72. {
  73. sigset_t old, no_sigio;
  74. struct termios save, new;
  75. int err;
  76. if (isatty(fd)) {
  77. sigemptyset(&no_sigio);
  78. sigaddset(&no_sigio, SIGIO);
  79. if (sigprocmask(SIG_BLOCK, &no_sigio, &old))
  80. goto error;
  81. CATCH_EINTR(err = tcgetattr(fd, &save));
  82. if (err)
  83. goto error;
  84. new = save;
  85. /*
  86. * The terminal becomes a bit less raw, to handle \n also as
  87. * "Carriage Return", not only as "New Line". Otherwise, the new
  88. * line won't start at the first column.
  89. */
  90. new.c_oflag |= OPOST;
  91. CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
  92. if (err)
  93. goto error;
  94. }
  95. err = generic_write(fd, buf, n, NULL);
  96. /*
  97. * Restore raw mode, in any case; we *must* ignore any error apart
  98. * EINTR, except for debug.
  99. */
  100. if (isatty(fd)) {
  101. CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
  102. sigprocmask(SIG_SETMASK, &old, NULL);
  103. }
  104. return err;
  105. error:
  106. return -errno;
  107. }
  108. /*
  109. * UML SIGWINCH handling
  110. *
  111. * The point of this is to handle SIGWINCH on consoles which have host
  112. * ttys and relay them inside UML to whatever might be running on the
  113. * console and cares about the window size (since SIGWINCH notifies
  114. * about terminal size changes).
  115. *
  116. * So, we have a separate thread for each host tty attached to a UML
  117. * device (side-issue - I'm annoyed that one thread can't have
  118. * multiple controlling ttys for the purpose of handling SIGWINCH, but
  119. * I imagine there are other reasons that doesn't make any sense).
  120. *
  121. * SIGWINCH can't be received synchronously, so you have to set up to
  122. * receive it as a signal. That being the case, if you are going to
  123. * wait for it, it is convenient to sit in sigsuspend() and wait for
  124. * the signal to bounce you out of it (see below for how we make sure
  125. * to exit only on SIGWINCH).
  126. */
  127. static void winch_handler(int sig)
  128. {
  129. }
  130. struct winch_data {
  131. int pty_fd;
  132. int pipe_fd;
  133. };
  134. static __noreturn int winch_thread(void *arg)
  135. {
  136. struct winch_data *data = arg;
  137. sigset_t sigs;
  138. int pty_fd, pipe_fd;
  139. int count;
  140. char c = 1;
  141. os_set_pdeathsig();
  142. pty_fd = data->pty_fd;
  143. pipe_fd = data->pipe_fd;
  144. count = write(pipe_fd, &c, sizeof(c));
  145. if (count != sizeof(c))
  146. os_info("winch_thread : failed to write synchronization byte, err = %d\n",
  147. -count);
  148. /*
  149. * We are not using SIG_IGN on purpose, so don't fix it as I thought to
  150. * do! If using SIG_IGN, the sigsuspend() call below would not stop on
  151. * SIGWINCH.
  152. */
  153. signal(SIGWINCH, winch_handler);
  154. sigfillset(&sigs);
  155. /* Block all signals possible. */
  156. if (sigprocmask(SIG_SETMASK, &sigs, NULL) < 0) {
  157. os_info("winch_thread : sigprocmask failed, errno = %d\n",
  158. errno);
  159. goto wait_kill;
  160. }
  161. /* In sigsuspend(), block anything else than SIGWINCH. */
  162. sigdelset(&sigs, SIGWINCH);
  163. if (setsid() < 0) {
  164. os_info("winch_thread : setsid failed, errno = %d\n",
  165. errno);
  166. goto wait_kill;
  167. }
  168. if (ioctl(pty_fd, TIOCSCTTY, 0) < 0) {
  169. os_info("winch_thread : TIOCSCTTY failed on "
  170. "fd %d err = %d\n", pty_fd, errno);
  171. goto wait_kill;
  172. }
  173. if (tcsetpgrp(pty_fd, os_getpid()) < 0) {
  174. os_info("winch_thread : tcsetpgrp failed on fd %d err = %d\n",
  175. pty_fd, errno);
  176. goto wait_kill;
  177. }
  178. /*
  179. * These are synchronization calls between various UML threads on the
  180. * host - since they are not different kernel threads, we cannot use
  181. * kernel semaphores. We don't use SysV semaphores because they are
  182. * persistent.
  183. */
  184. count = read(pipe_fd, &c, sizeof(c));
  185. if (count != sizeof(c))
  186. os_info("winch_thread : failed to read synchronization byte, err = %d\n",
  187. errno);
  188. while(1) {
  189. /*
  190. * This will be interrupted by SIGWINCH only, since
  191. * other signals are blocked.
  192. */
  193. sigsuspend(&sigs);
  194. count = write(pipe_fd, &c, sizeof(c));
  195. if (count != sizeof(c))
  196. os_info("winch_thread : write failed, err = %d\n",
  197. errno);
  198. }
  199. wait_kill:
  200. c = 2;
  201. count = write(pipe_fd, &c, sizeof(c));
  202. while (1)
  203. pause();
  204. }
  205. static int winch_tramp(int fd, struct tty_port *port, int *fd_out,
  206. unsigned long *stack_out)
  207. {
  208. struct winch_data data;
  209. int fds[2], n, err, pid;
  210. char c;
  211. err = os_pipe(fds, 1, 1);
  212. if (err < 0) {
  213. printk(UM_KERN_ERR "winch_tramp : os_pipe failed, err = %d\n",
  214. -err);
  215. goto out;
  216. }
  217. data = ((struct winch_data) { .pty_fd = fd,
  218. .pipe_fd = fds[1] } );
  219. /*
  220. * CLONE_FILES so this thread doesn't hold open files which are open
  221. * now, but later closed in a different thread. This is a
  222. * problem with /dev/net/tun, which if held open by this
  223. * thread, prevents the TUN/TAP device from being reused.
  224. */
  225. pid = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
  226. if (pid < 0) {
  227. err = pid;
  228. printk(UM_KERN_ERR "fork of winch_thread failed - errno = %d\n",
  229. -err);
  230. goto out_close;
  231. }
  232. *fd_out = fds[0];
  233. n = read(fds[0], &c, sizeof(c));
  234. if (n != sizeof(c)) {
  235. printk(UM_KERN_ERR "winch_tramp : failed to read "
  236. "synchronization byte\n");
  237. printk(UM_KERN_ERR "read failed, err = %d\n", errno);
  238. printk(UM_KERN_ERR "fd %d will not support SIGWINCH\n", fd);
  239. err = -EINVAL;
  240. goto out_close;
  241. }
  242. err = os_set_fd_block(*fd_out, 0);
  243. if (err) {
  244. printk(UM_KERN_ERR "winch_tramp: failed to set thread_fd "
  245. "non-blocking.\n");
  246. goto out_close;
  247. }
  248. return pid;
  249. out_close:
  250. close(fds[1]);
  251. close(fds[0]);
  252. out:
  253. return err;
  254. }
  255. void register_winch(int fd, struct tty_port *port)
  256. {
  257. unsigned long stack;
  258. int pid, thread, count, thread_fd = -1;
  259. char c = 1;
  260. if (!isatty(fd))
  261. return;
  262. pid = tcgetpgrp(fd);
  263. if (is_skas_winch(pid, fd, port)) {
  264. register_winch_irq(-1, fd, -1, port, 0);
  265. return;
  266. }
  267. if (pid == -1) {
  268. thread = winch_tramp(fd, port, &thread_fd, &stack);
  269. if (thread < 0)
  270. return;
  271. register_winch_irq(thread_fd, fd, thread, port, stack);
  272. count = write(thread_fd, &c, sizeof(c));
  273. if (count != sizeof(c))
  274. printk(UM_KERN_ERR "register_winch : failed to write "
  275. "synchronization byte, err = %d\n", errno);
  276. }
  277. }