chan_user.c 6.5 KB

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