chan_user.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 ttys and
  51. * relay them inside UML to whatever might be running on the console and cares
  52. * about the window size (since SIGWINCH notifies about terminal size changes).
  53. *
  54. * So, we have a separate thread for each host tty attached to a UML device
  55. * (side-issue - I'm annoyed that one thread can't have multiple controlling
  56. * ttys for purposed of handling SIGWINCH, but I imagine there are other reasons
  57. * that doesn't make any sense).
  58. *
  59. * SIGWINCH can't be received synchronously, so you have to set up to receive it
  60. * as a signal. That being the case, if you are going to wait for it, it is
  61. * convenient to sit in sigsuspend() and wait for the signal to bounce you out of
  62. * it (see below for how we make sure to exit only on SIGWINCH).
  63. */
  64. static void winch_handler(int sig)
  65. {
  66. }
  67. struct winch_data {
  68. int pty_fd;
  69. int pipe_fd;
  70. };
  71. static int winch_thread(void *arg)
  72. {
  73. struct winch_data *data = arg;
  74. sigset_t sigs;
  75. int pty_fd, pipe_fd;
  76. int count, err;
  77. char c = 1;
  78. pty_fd = data->pty_fd;
  79. pipe_fd = data->pipe_fd;
  80. count = os_write_file(pipe_fd, &c, sizeof(c));
  81. if(count != sizeof(c))
  82. printk("winch_thread : failed to write synchronization "
  83. "byte, err = %d\n", -count);
  84. /* We are not using SIG_IGN on purpose, so don't fix it as I thought to
  85. * do! If using SIG_IGN, the sigsuspend() call below would not stop on
  86. * SIGWINCH. */
  87. signal(SIGWINCH, winch_handler);
  88. sigfillset(&sigs);
  89. /* Block all signals possible. */
  90. if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){
  91. printk("winch_thread : sigprocmask failed, errno = %d\n",
  92. errno);
  93. exit(1);
  94. }
  95. /* In sigsuspend(), block anything else than SIGWINCH. */
  96. sigdelset(&sigs, SIGWINCH);
  97. if(setsid() < 0){
  98. printk("winch_thread : setsid failed, errno = %d\n", errno);
  99. exit(1);
  100. }
  101. err = os_new_tty_pgrp(pty_fd, os_getpid());
  102. if(err < 0){
  103. printk("winch_thread : new_tty_pgrp failed, err = %d\n", -err);
  104. exit(1);
  105. }
  106. /* These are synchronization calls between various UML threads on the
  107. * host - since they are not different kernel threads, we cannot use
  108. * kernel semaphores. We don't use SysV semaphores because they are
  109. * persistent. */
  110. count = os_read_file(pipe_fd, &c, sizeof(c));
  111. if(count != sizeof(c))
  112. printk("winch_thread : failed to read synchronization byte, "
  113. "err = %d\n", -count);
  114. while(1){
  115. /* This will be interrupted by SIGWINCH only, since other signals
  116. * are blocked.*/
  117. sigsuspend(&sigs);
  118. count = os_write_file(pipe_fd, &c, sizeof(c));
  119. if(count != sizeof(c))
  120. printk("winch_thread : write failed, err = %d\n",
  121. -count);
  122. }
  123. }
  124. static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out)
  125. {
  126. struct winch_data data;
  127. unsigned long stack;
  128. int fds[2], n, err;
  129. char c;
  130. err = os_pipe(fds, 1, 1);
  131. if(err < 0){
  132. printk("winch_tramp : os_pipe failed, err = %d\n", -err);
  133. goto out;
  134. }
  135. data = ((struct winch_data) { .pty_fd = fd,
  136. .pipe_fd = fds[1] } );
  137. /* CLONE_FILES so this thread doesn't hold open files which are open
  138. * now, but later closed. This is a problem with /dev/net/tun.
  139. */
  140. err = run_helper_thread(winch_thread, &data, CLONE_FILES, &stack, 0);
  141. if(err < 0){
  142. printk("fork of winch_thread failed - errno = %d\n", -err);
  143. goto out_close;
  144. }
  145. *fd_out = fds[0];
  146. n = os_read_file(fds[0], &c, sizeof(c));
  147. if(n != sizeof(c)){
  148. printk("winch_tramp : failed to read synchronization byte\n");
  149. printk("read failed, err = %d\n", -n);
  150. printk("fd %d will not support SIGWINCH\n", fd);
  151. err = -EINVAL;
  152. goto out_close;
  153. }
  154. return err ;
  155. out_close:
  156. os_close_file(fds[1]);
  157. os_close_file(fds[0]);
  158. out:
  159. return err;
  160. }
  161. void register_winch(int fd, struct tty_struct *tty)
  162. {
  163. int pid, thread, thread_fd = -1;
  164. int count;
  165. char c = 1;
  166. if(!isatty(fd))
  167. return;
  168. pid = tcgetpgrp(fd);
  169. if(!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd,
  170. tty) && (pid == -1)){
  171. thread = winch_tramp(fd, tty, &thread_fd);
  172. if(thread > 0){
  173. register_winch_irq(thread_fd, fd, thread, tty);
  174. count = os_write_file(thread_fd, &c, sizeof(c));
  175. if(count != sizeof(c))
  176. printk("register_winch : failed to write "
  177. "synchronization byte, err = %d\n",
  178. -count);
  179. }
  180. }
  181. }