blob: 2f880cb167a582f5f10be75d4647facd1f82e274 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000 - 2003 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
4 */
5
6#include <unistd.h>
7#include <stdlib.h>
8#include <errno.h>
9#include <termios.h>
10#include <string.h>
11#include <signal.h>
Jeff Dike1d2ddcf2006-02-07 12:58:41 -080012#include <sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <sys/stat.h>
14#include <sys/ioctl.h>
15#include <sys/socket.h>
16#include "kern_util.h"
17#include "user_util.h"
18#include "chan_user.h"
19#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "os.h"
21#include "choose-mode.h"
22#include "mode.h"
23
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -080024int generic_console_write(int fd, const char *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 struct termios save, new;
27 int err;
28
29 if(isatty(fd)){
30 CATCH_EINTR(err = tcgetattr(fd, &save));
31 if (err)
32 goto error;
33 new = save;
34 /* The terminal becomes a bit less raw, to handle \n also as
35 * "Carriage Return", not only as "New Line". Otherwise, the new
36 * line won't start at the first column.*/
37 new.c_oflag |= OPOST;
38 CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
39 if (err)
40 goto error;
41 }
42 err = generic_write(fd, buf, n, NULL);
43 /* Restore raw mode, in any case; we *must* ignore any error apart
44 * EINTR, except for debug.*/
45 if(isatty(fd))
46 CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
47 return(err);
48error:
49 return(-errno);
50}
51
52/*
53 * UML SIGWINCH handling
54 *
55 * The point of this is to handle SIGWINCH on consoles which have host ttys and
56 * relay them inside UML to whatever might be running on the console and cares
57 * about the window size (since SIGWINCH notifies about terminal size changes).
58 *
59 * So, we have a separate thread for each host tty attached to a UML device
60 * (side-issue - I'm annoyed that one thread can't have multiple controlling
61 * ttys for purposed of handling SIGWINCH, but I imagine there are other reasons
62 * that doesn't make any sense).
63 *
64 * SIGWINCH can't be received synchronously, so you have to set up to receive it
65 * as a signal. That being the case, if you are going to wait for it, it is
Bodo Stroessered1b58d2005-09-03 15:57:24 -070066 * convenient to sit in sigsuspend() and wait for the signal to bounce you out of
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 * it (see below for how we make sure to exit only on SIGWINCH).
68 */
69
70static void winch_handler(int sig)
71{
72}
73
74struct winch_data {
75 int pty_fd;
76 int pipe_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
79static int winch_thread(void *arg)
80{
81 struct winch_data *data = arg;
82 sigset_t sigs;
83 int pty_fd, pipe_fd;
84 int count, err;
85 char c = 1;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 pty_fd = data->pty_fd;
88 pipe_fd = data->pipe_fd;
89 count = os_write_file(pipe_fd, &c, sizeof(c));
90 if(count != sizeof(c))
91 printk("winch_thread : failed to write synchronization "
92 "byte, err = %d\n", -count);
93
94 /* We are not using SIG_IGN on purpose, so don't fix it as I thought to
Bodo Stroessered1b58d2005-09-03 15:57:24 -070095 * do! If using SIG_IGN, the sigsuspend() call below would not stop on
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * SIGWINCH. */
97
98 signal(SIGWINCH, winch_handler);
99 sigfillset(&sigs);
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700100 /* Block all signals possible. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){
102 printk("winch_thread : sigprocmask failed, errno = %d\n",
103 errno);
104 exit(1);
105 }
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700106 /* In sigsuspend(), block anything else than SIGWINCH. */
107 sigdelset(&sigs, SIGWINCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 if(setsid() < 0){
110 printk("winch_thread : setsid failed, errno = %d\n", errno);
111 exit(1);
112 }
113
114 err = os_new_tty_pgrp(pty_fd, os_getpid());
115 if(err < 0){
116 printk("winch_thread : new_tty_pgrp failed, err = %d\n", -err);
117 exit(1);
118 }
119
120 /* These are synchronization calls between various UML threads on the
121 * host - since they are not different kernel threads, we cannot use
122 * kernel semaphores. We don't use SysV semaphores because they are
123 * persistant. */
124 count = os_read_file(pipe_fd, &c, sizeof(c));
125 if(count != sizeof(c))
126 printk("winch_thread : failed to read synchronization byte, "
127 "err = %d\n", -count);
128
129 while(1){
130 /* This will be interrupted by SIGWINCH only, since other signals
131 * are blocked.*/
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700132 sigsuspend(&sigs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 count = os_write_file(pipe_fd, &c, sizeof(c));
135 if(count != sizeof(c))
136 printk("winch_thread : write failed, err = %d\n",
137 -count);
138 }
139}
140
141static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out)
142{
143 struct winch_data data;
144 unsigned long stack;
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700145 int fds[2], n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 char c;
147
148 err = os_pipe(fds, 1, 1);
149 if(err < 0){
150 printk("winch_tramp : os_pipe failed, err = %d\n", -err);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700151 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153
154 data = ((struct winch_data) { .pty_fd = fd,
Jeff Dike1d2ddcf2006-02-07 12:58:41 -0800155 .pipe_fd = fds[1] } );
156 /* CLONE_FILES so this thread doesn't hold open files which are open
157 * now, but later closed. This is a problem with /dev/net/tun.
158 */
159 err = run_helper_thread(winch_thread, &data, CLONE_FILES, &stack, 0);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700160 if(err < 0){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 printk("fork of winch_thread failed - errno = %d\n", errno);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700162 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 *fd_out = fds[0];
166 n = os_read_file(fds[0], &c, sizeof(c));
167 if(n != sizeof(c)){
168 printk("winch_tramp : failed to read synchronization byte\n");
169 printk("read failed, err = %d\n", -n);
170 printk("fd %d will not support SIGWINCH\n", fd);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700171 err = -EINVAL;
Jeff Dike1d2ddcf2006-02-07 12:58:41 -0800172 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700174 return err ;
175
176 out_close:
177 os_close_file(fds[1]);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700178 os_close_file(fds[0]);
179 out:
180 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183void register_winch(int fd, struct tty_struct *tty)
184{
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700185 int pid, thread, thread_fd = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int count;
187 char c = 1;
188
189 if(!isatty(fd))
190 return;
191
192 pid = tcgetpgrp(fd);
193 if(!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd,
194 tty) && (pid == -1)){
195 thread = winch_tramp(fd, tty, &thread_fd);
Jeff Dikeda00d9a2005-06-08 15:48:01 -0700196 if(thread > 0){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 register_winch_irq(thread_fd, fd, thread, tty);
198
199 count = os_write_file(thread_fd, &c, sizeof(c));
200 if(count != sizeof(c))
201 printk("register_winch : failed to write "
202 "synchronization byte, err = %d\n",
203 -count);
204 }
205 }
206}
207
208/*
209 * Overrides for Emacs so that we follow Linus's tabbing style.
210 * Emacs will notice this stuff at the end of the file and automatically
211 * adjust the settings for this buffer only. This must remain at the end
212 * of the file.
213 * ---------------------------------------------------------------------------
214 * Local variables:
215 * c-file-style: "linux"
216 * End:
217 */