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