chan_user.c 6.9 KB

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