chan_user.c 6.7 KB

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