chan_user.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 a pause() 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 pause() call below would not stop on
  89. * SIGWINCH. */
  90. signal(SIGWINCH, winch_handler);
  91. sigfillset(&sigs);
  92. sigdelset(&sigs, SIGWINCH);
  93. /* Block anything else than SIGWINCH. */
  94. if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){
  95. printk("winch_thread : sigprocmask failed, errno = %d\n",
  96. errno);
  97. exit(1);
  98. }
  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, err = %d\n", -err);
  106. exit(1);
  107. }
  108. /* These are synchronization calls between various UML threads on the
  109. * host - since they are not different kernel threads, we cannot use
  110. * kernel semaphores. We don't use SysV semaphores because they are
  111. * persistant. */
  112. count = os_read_file(pipe_fd, &c, sizeof(c));
  113. if(count != sizeof(c))
  114. printk("winch_thread : failed to read synchronization byte, "
  115. "err = %d\n", -count);
  116. while(1){
  117. /* This will be interrupted by SIGWINCH only, since other signals
  118. * are blocked.*/
  119. pause();
  120. count = os_write_file(pipe_fd, &c, sizeof(c));
  121. if(count != sizeof(c))
  122. printk("winch_thread : write failed, err = %d\n",
  123. -count);
  124. }
  125. }
  126. static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out)
  127. {
  128. struct winch_data data;
  129. unsigned long stack;
  130. int fds[2], n, err;
  131. char c;
  132. err = os_pipe(fds, 1, 1);
  133. if(err < 0){
  134. printk("winch_tramp : os_pipe failed, err = %d\n", -err);
  135. goto out;
  136. }
  137. data = ((struct winch_data) { .pty_fd = fd,
  138. .pipe_fd = fds[1],
  139. .close_me = fds[0] } );
  140. err = run_helper_thread(winch_thread, &data, 0, &stack, 0);
  141. if(err < 0){
  142. printk("fork of winch_thread failed - errno = %d\n", errno);
  143. goto out_close;
  144. }
  145. os_close_file(fds[1]);
  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_close1;
  154. }
  155. return err ;
  156. out_close:
  157. os_close_file(fds[1]);
  158. out_close1:
  159. os_close_file(fds[0]);
  160. out:
  161. return err;
  162. }
  163. void register_winch(int fd, struct tty_struct *tty)
  164. {
  165. int pid, thread, thread_fd = -1;
  166. int count;
  167. char c = 1;
  168. if(!isatty(fd))
  169. return;
  170. pid = tcgetpgrp(fd);
  171. if(!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd,
  172. tty) && (pid == -1)){
  173. thread = winch_tramp(fd, tty, &thread_fd);
  174. if(thread > 0){
  175. register_winch_irq(thread_fd, fd, thread, tty);
  176. count = os_write_file(thread_fd, &c, sizeof(c));
  177. if(count != sizeof(c))
  178. printk("register_winch : failed to write "
  179. "synchronization byte, err = %d\n",
  180. -count);
  181. }
  182. }
  183. }
  184. /*
  185. * Overrides for Emacs so that we follow Linus's tabbing style.
  186. * Emacs will notice this stuff at the end of the file and automatically
  187. * adjust the settings for this buffer only. This must remain at the end
  188. * of the file.
  189. * ---------------------------------------------------------------------------
  190. * Local variables:
  191. * c-file-style: "linux"
  192. * End:
  193. */