chan_user.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <sys/stat.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/socket.h>
  14. #include "kern_util.h"
  15. #include "user_util.h"
  16. #include "chan_user.h"
  17. #include "user.h"
  18. #include "helper.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, void *unused)
  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. int close_me;
  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. os_close_file(data->close_me);
  81. pty_fd = data->pty_fd;
  82. pipe_fd = data->pipe_fd;
  83. count = os_write_file(pipe_fd, &c, sizeof(c));
  84. if(count != sizeof(c))
  85. printk("winch_thread : failed to write synchronization "
  86. "byte, err = %d\n", -count);
  87. /* We are not using SIG_IGN on purpose, so don't fix it as I thought to
  88. * do! If using SIG_IGN, the sigsuspend() call below would not stop on
  89. * SIGWINCH. */
  90. signal(SIGWINCH, winch_handler);
  91. sigfillset(&sigs);
  92. /* Block all signals possible. */
  93. if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){
  94. printk("winch_thread : sigprocmask failed, errno = %d\n",
  95. errno);
  96. exit(1);
  97. }
  98. /* In sigsuspend(), block anything else than SIGWINCH. */
  99. sigdelset(&sigs, SIGWINCH);
  100. if(setsid() < 0){
  101. printk("winch_thread : setsid failed, errno = %d\n", errno);
  102. exit(1);
  103. }
  104. err = os_new_tty_pgrp(pty_fd, os_getpid());
  105. if(err < 0){
  106. printk("winch_thread : new_tty_pgrp failed, err = %d\n", -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. * persistant. */
  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 other signals
  119. * are blocked.*/
  120. sigsuspend(&sigs);
  121. count = os_write_file(pipe_fd, &c, sizeof(c));
  122. if(count != sizeof(c))
  123. printk("winch_thread : write failed, err = %d\n",
  124. -count);
  125. }
  126. }
  127. static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out)
  128. {
  129. struct winch_data data;
  130. unsigned long stack;
  131. int fds[2], n, err;
  132. char c;
  133. err = os_pipe(fds, 1, 1);
  134. if(err < 0){
  135. printk("winch_tramp : os_pipe failed, err = %d\n", -err);
  136. goto out;
  137. }
  138. data = ((struct winch_data) { .pty_fd = fd,
  139. .pipe_fd = fds[1],
  140. .close_me = fds[0] } );
  141. err = run_helper_thread(winch_thread, &data, 0, &stack, 0);
  142. if(err < 0){
  143. printk("fork of winch_thread failed - errno = %d\n", errno);
  144. goto out_close;
  145. }
  146. os_close_file(fds[1]);
  147. *fd_out = fds[0];
  148. n = os_read_file(fds[0], &c, sizeof(c));
  149. if(n != sizeof(c)){
  150. printk("winch_tramp : failed to read synchronization byte\n");
  151. printk("read failed, err = %d\n", -n);
  152. printk("fd %d will not support SIGWINCH\n", fd);
  153. err = -EINVAL;
  154. goto out_close1;
  155. }
  156. return err ;
  157. out_close:
  158. os_close_file(fds[1]);
  159. out_close1:
  160. os_close_file(fds[0]);
  161. out:
  162. return err;
  163. }
  164. void register_winch(int fd, struct tty_struct *tty)
  165. {
  166. int pid, thread, thread_fd = -1;
  167. int count;
  168. char c = 1;
  169. if(!isatty(fd))
  170. return;
  171. pid = tcgetpgrp(fd);
  172. if(!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd,
  173. tty) && (pid == -1)){
  174. thread = winch_tramp(fd, tty, &thread_fd);
  175. if(thread > 0){
  176. register_winch_irq(thread_fd, fd, thread, tty);
  177. count = os_write_file(thread_fd, &c, sizeof(c));
  178. if(count != sizeof(c))
  179. printk("register_winch : failed to write "
  180. "synchronization byte, err = %d\n",
  181. -count);
  182. }
  183. }
  184. }
  185. /*
  186. * Overrides for Emacs so that we follow Linus's tabbing style.
  187. * Emacs will notice this stuff at the end of the file and automatically
  188. * adjust the settings for this buffer only. This must remain at the end
  189. * of the file.
  190. * ---------------------------------------------------------------------------
  191. * Local variables:
  192. * c-file-style: "linux"
  193. * End:
  194. */