chan_user.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
  3. * Licensed under the GPL
  4. */
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <errno.h>
  8. #include <termios.h>
  9. #include <string.h>
  10. #include <signal.h>
  11. #include <sched.h>
  12. #include <sys/stat.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/socket.h>
  15. #include "kern_util.h"
  16. #include "chan_user.h"
  17. #include "user.h"
  18. #include "os.h"
  19. #include "choose-mode.h"
  20. #include "mode.h"
  21. int generic_console_write(int fd, const char *buf, int n)
  22. {
  23. struct termios save, new;
  24. int err;
  25. if(isatty(fd)){
  26. CATCH_EINTR(err = tcgetattr(fd, &save));
  27. if (err)
  28. goto error;
  29. new = save;
  30. /* The terminal becomes a bit less raw, to handle \n also as
  31. * "Carriage Return", not only as "New Line". Otherwise, the new
  32. * line won't start at the first column.*/
  33. new.c_oflag |= OPOST;
  34. CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
  35. if (err)
  36. goto error;
  37. }
  38. err = generic_write(fd, buf, n, NULL);
  39. /* Restore raw mode, in any case; we *must* ignore any error apart
  40. * EINTR, except for debug.*/
  41. if(isatty(fd))
  42. CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
  43. return(err);
  44. error:
  45. return(-errno);
  46. }
  47. /*
  48. * UML SIGWINCH handling
  49. *
  50. * The point of this is to handle SIGWINCH on consoles which have host
  51. * ttys and relay them inside UML to whatever might be running on the
  52. * console and cares about the window size (since SIGWINCH notifies
  53. * about terminal size changes).
  54. *
  55. * So, we have a separate thread for each host tty attached to a UML
  56. * device (side-issue - I'm annoyed that one thread can't have
  57. * multiple controlling ttys for the purpose of handling SIGWINCH, but
  58. * I imagine there are other reasons that doesn't make any sense).
  59. *
  60. * SIGWINCH can't be received synchronously, so you have to set up to
  61. * receive it as a signal. That being the case, if you are going to
  62. * wait for it, it is convenient to sit in sigsuspend() and wait for
  63. * the signal to bounce you out of it (see below for how we make sure
  64. * to exit only on SIGWINCH).
  65. */
  66. static void winch_handler(int sig)
  67. {
  68. }
  69. struct winch_data {
  70. int pty_fd;
  71. int pipe_fd;
  72. };
  73. static int winch_thread(void *arg)
  74. {
  75. struct winch_data *data = arg;
  76. sigset_t sigs;
  77. int pty_fd, pipe_fd;
  78. int count, err;
  79. char c = 1;
  80. pty_fd = data->pty_fd;
  81. pipe_fd = data->pipe_fd;
  82. count = os_write_file(pipe_fd, &c, sizeof(c));
  83. if(count != sizeof(c))
  84. printk("winch_thread : failed to write synchronization "
  85. "byte, err = %d\n", -count);
  86. /* We are not using SIG_IGN on purpose, so don't fix it as I thought to
  87. * do! If using SIG_IGN, the sigsuspend() call below would not stop on
  88. * SIGWINCH. */
  89. signal(SIGWINCH, winch_handler);
  90. sigfillset(&sigs);
  91. /* Block all signals possible. */
  92. if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){
  93. printk("winch_thread : sigprocmask failed, errno = %d\n",
  94. errno);
  95. exit(1);
  96. }
  97. /* In sigsuspend(), block anything else than SIGWINCH. */
  98. sigdelset(&sigs, SIGWINCH);
  99. if(setsid() < 0){
  100. printk("winch_thread : setsid failed, errno = %d\n", errno);
  101. exit(1);
  102. }
  103. err = os_new_tty_pgrp(pty_fd, os_getpid());
  104. if(err < 0){
  105. printk("winch_thread : new_tty_pgrp failed on fd %d, "
  106. "err = %d\n", pty_fd, -err);
  107. exit(1);
  108. }
  109. /* These are synchronization calls between various UML threads on the
  110. * host - since they are not different kernel threads, we cannot use
  111. * kernel semaphores. We don't use SysV semaphores because they are
  112. * persistent. */
  113. count = os_read_file(pipe_fd, &c, sizeof(c));
  114. if(count != sizeof(c))
  115. printk("winch_thread : failed to read synchronization byte, "
  116. "err = %d\n", -count);
  117. while(1){
  118. /* This will be interrupted by SIGWINCH only, since
  119. * other signals are blocked.
  120. */
  121. sigsuspend(&sigs);
  122. count = os_write_file(pipe_fd, &c, sizeof(c));
  123. if(count != sizeof(c))
  124. printk("winch_thread : write failed, err = %d\n",
  125. -count);
  126. }
  127. }
  128. static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out,
  129. unsigned long *stack_out)
  130. {
  131. struct winch_data data;
  132. int fds[2], n, err;
  133. char c;
  134. err = os_pipe(fds, 1, 1);
  135. if(err < 0){
  136. printk("winch_tramp : os_pipe failed, err = %d\n", -err);
  137. goto out;
  138. }
  139. data = ((struct winch_data) { .pty_fd = fd,
  140. .pipe_fd = fds[1] } );
  141. /* CLONE_FILES so this thread doesn't hold open files which are open
  142. * now, but later closed in a different thread. This is a
  143. * problem with /dev/net/tun, which if held open by this
  144. * thread, prevents the TUN/TAP device from being reused.
  145. */
  146. err = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
  147. if(err < 0){
  148. printk("fork of winch_thread failed - errno = %d\n", -err);
  149. goto out_close;
  150. }
  151. *fd_out = fds[0];
  152. n = os_read_file(fds[0], &c, sizeof(c));
  153. if(n != sizeof(c)){
  154. printk("winch_tramp : failed to read synchronization byte\n");
  155. printk("read failed, err = %d\n", -n);
  156. printk("fd %d will not support SIGWINCH\n", fd);
  157. err = -EINVAL;
  158. goto out_close;
  159. }
  160. if (os_set_fd_block(*fd_out, 0)) {
  161. printk("winch_tramp: failed to set thread_fd non-blocking.\n");
  162. goto out_close;
  163. }
  164. return err;
  165. out_close:
  166. os_close_file(fds[1]);
  167. os_close_file(fds[0]);
  168. out:
  169. return err;
  170. }
  171. void register_winch(int fd, struct tty_struct *tty)
  172. {
  173. unsigned long stack;
  174. int pid, thread, count, thread_fd = -1;
  175. char c = 1;
  176. if(!isatty(fd))
  177. return;
  178. pid = tcgetpgrp(fd);
  179. if (!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd, tty) &&
  180. (pid == -1)) {
  181. thread = winch_tramp(fd, tty, &thread_fd, &stack);
  182. if (thread < 0)
  183. return;
  184. register_winch_irq(thread_fd, fd, thread, tty, stack);
  185. count = os_write_file(thread_fd, &c, sizeof(c));
  186. if(count != sizeof(c))
  187. printk("register_winch : failed to write "
  188. "synchronization byte, err = %d\n", -count);
  189. }
  190. }