chan_user.c 6.9 KB

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