chan_user.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. pty_fd = data->pty_fd;
  142. pipe_fd = data->pipe_fd;
  143. count = write(pipe_fd, &c, sizeof(c));
  144. if (count != sizeof(c))
  145. os_info("winch_thread : failed to write synchronization byte, err = %d\n",
  146. -count);
  147. /*
  148. * We are not using SIG_IGN on purpose, so don't fix it as I thought to
  149. * do! If using SIG_IGN, the sigsuspend() call below would not stop on
  150. * SIGWINCH.
  151. */
  152. signal(SIGWINCH, winch_handler);
  153. sigfillset(&sigs);
  154. /* Block all signals possible. */
  155. if (sigprocmask(SIG_SETMASK, &sigs, NULL) < 0) {
  156. os_info("winch_thread : sigprocmask failed, errno = %d\n",
  157. errno);
  158. goto wait_kill;
  159. }
  160. /* In sigsuspend(), block anything else than SIGWINCH. */
  161. sigdelset(&sigs, SIGWINCH);
  162. if (setsid() < 0) {
  163. os_info("winch_thread : setsid failed, errno = %d\n",
  164. errno);
  165. goto wait_kill;
  166. }
  167. if (ioctl(pty_fd, TIOCSCTTY, 0) < 0) {
  168. os_info("winch_thread : TIOCSCTTY failed on "
  169. "fd %d err = %d\n", pty_fd, errno);
  170. goto wait_kill;
  171. }
  172. if (tcsetpgrp(pty_fd, os_getpid()) < 0) {
  173. os_info("winch_thread : tcsetpgrp failed on fd %d err = %d\n",
  174. pty_fd, errno);
  175. goto wait_kill;
  176. }
  177. /*
  178. * These are synchronization calls between various UML threads on the
  179. * host - since they are not different kernel threads, we cannot use
  180. * kernel semaphores. We don't use SysV semaphores because they are
  181. * persistent.
  182. */
  183. count = read(pipe_fd, &c, sizeof(c));
  184. if (count != sizeof(c))
  185. os_info("winch_thread : failed to read synchronization byte, err = %d\n",
  186. errno);
  187. while(1) {
  188. /*
  189. * This will be interrupted by SIGWINCH only, since
  190. * other signals are blocked.
  191. */
  192. sigsuspend(&sigs);
  193. count = write(pipe_fd, &c, sizeof(c));
  194. if (count != sizeof(c))
  195. os_info("winch_thread : write failed, err = %d\n",
  196. errno);
  197. }
  198. wait_kill:
  199. c = 2;
  200. count = write(pipe_fd, &c, sizeof(c));
  201. while (1)
  202. pause();
  203. }
  204. static int winch_tramp(int fd, struct tty_port *port, int *fd_out,
  205. unsigned long *stack_out)
  206. {
  207. struct winch_data data;
  208. int fds[2], n, err, pid;
  209. char c;
  210. err = os_pipe(fds, 1, 1);
  211. if (err < 0) {
  212. printk(UM_KERN_ERR "winch_tramp : os_pipe failed, err = %d\n",
  213. -err);
  214. goto out;
  215. }
  216. data = ((struct winch_data) { .pty_fd = fd,
  217. .pipe_fd = fds[1] } );
  218. /*
  219. * CLONE_FILES so this thread doesn't hold open files which are open
  220. * now, but later closed in a different thread. This is a
  221. * problem with /dev/net/tun, which if held open by this
  222. * thread, prevents the TUN/TAP device from being reused.
  223. */
  224. pid = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
  225. if (pid < 0) {
  226. err = pid;
  227. printk(UM_KERN_ERR "fork of winch_thread failed - errno = %d\n",
  228. -err);
  229. goto out_close;
  230. }
  231. *fd_out = fds[0];
  232. n = read(fds[0], &c, sizeof(c));
  233. if (n != sizeof(c)) {
  234. printk(UM_KERN_ERR "winch_tramp : failed to read "
  235. "synchronization byte\n");
  236. printk(UM_KERN_ERR "read failed, err = %d\n", errno);
  237. printk(UM_KERN_ERR "fd %d will not support SIGWINCH\n", fd);
  238. err = -EINVAL;
  239. goto out_close;
  240. }
  241. err = os_set_fd_block(*fd_out, 0);
  242. if (err) {
  243. printk(UM_KERN_ERR "winch_tramp: failed to set thread_fd "
  244. "non-blocking.\n");
  245. goto out_close;
  246. }
  247. return pid;
  248. out_close:
  249. close(fds[1]);
  250. close(fds[0]);
  251. out:
  252. return err;
  253. }
  254. void register_winch(int fd, struct tty_port *port)
  255. {
  256. unsigned long stack;
  257. int pid, thread, count, thread_fd = -1;
  258. char c = 1;
  259. if (!isatty(fd))
  260. return;
  261. pid = tcgetpgrp(fd);
  262. if (is_skas_winch(pid, fd, port)) {
  263. register_winch_irq(-1, fd, -1, port, 0);
  264. return;
  265. }
  266. if (pid == -1) {
  267. thread = winch_tramp(fd, port, &thread_fd, &stack);
  268. if (thread < 0)
  269. return;
  270. register_winch_irq(thread_fd, fd, thread, port, stack);
  271. count = write(thread_fd, &c, sizeof(c));
  272. if (count != sizeof(c))
  273. printk(UM_KERN_ERR "register_winch : failed to write "
  274. "synchronization byte, err = %d\n", errno);
  275. }
  276. }