chan_user.c 6.9 KB

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