chan_user.c 6.7 KB

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