blob: 40271afa3ee28f3d38059e57cb5a5630fbbcb2fe [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "chan_user.h"
18#include "user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "os.h"
20#include "choose-mode.h"
21#include "mode.h"
Jeff Dike89fe6472007-10-16 01:26:39 -070022#include "um_malloc.h"
23
24void generic_close(int fd, void *unused)
25{
Jeff Dike8e2d10e2007-10-16 01:26:40 -070026 close(fd);
Jeff Dike89fe6472007-10-16 01:26:39 -070027}
28
29int generic_read(int fd, char *c_out, void *unused)
30{
31 int n;
32
Jeff Dike8e2d10e2007-10-16 01:26:40 -070033 n = read(fd, c_out, sizeof(*c_out));
34 if (n > 0)
35 return n;
36 else if (errno == EAGAIN)
Jeff Dike89fe6472007-10-16 01:26:39 -070037 return 0;
Jeff Dike8e2d10e2007-10-16 01:26:40 -070038 else if (n == 0)
Jeff Dike89fe6472007-10-16 01:26:39 -070039 return -EIO;
Jeff Dike8e2d10e2007-10-16 01:26:40 -070040 return -errno;
Jeff Dike89fe6472007-10-16 01:26:39 -070041}
42
Jeff Dike8e2d10e2007-10-16 01:26:40 -070043/* XXX Trivial wrapper around write */
Jeff Dike89fe6472007-10-16 01:26:39 -070044
45int generic_write(int fd, const char *buf, int n, void *unused)
46{
Jeff Dike8e2d10e2007-10-16 01:26:40 -070047 return write(fd, buf, n);
Jeff Dike89fe6472007-10-16 01:26:39 -070048}
49
50int generic_window_size(int fd, void *unused, unsigned short *rows_out,
51 unsigned short *cols_out)
52{
Jeff Dike8e2d10e2007-10-16 01:26:40 -070053 struct winsize size;
Jeff Dike89fe6472007-10-16 01:26:39 -070054 int ret;
55
Jeff Dike8e2d10e2007-10-16 01:26:40 -070056 if(ioctl(fd, TIOCGWINSZ, &size) < 0)
57 return -errno;
Jeff Dike89fe6472007-10-16 01:26:39 -070058
Jeff Dike8e2d10e2007-10-16 01:26:40 -070059 ret = ((*rows_out != size.ws_row) || (*cols_out != size.ws_col));
Jeff Dike89fe6472007-10-16 01:26:39 -070060
Jeff Dike8e2d10e2007-10-16 01:26:40 -070061 *rows_out = size.ws_row;
62 *cols_out = size.ws_col;
Jeff Dike89fe6472007-10-16 01:26:39 -070063
64 return ret;
65}
66
67void generic_free(void *data)
68{
69 kfree(data);
70}
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -080072int generic_console_write(int fd, const char *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 struct termios save, new;
75 int err;
76
77 if(isatty(fd)){
78 CATCH_EINTR(err = tcgetattr(fd, &save));
79 if (err)
80 goto error;
81 new = save;
82 /* The terminal becomes a bit less raw, to handle \n also as
83 * "Carriage Return", not only as "New Line". Otherwise, the new
84 * line won't start at the first column.*/
85 new.c_oflag |= OPOST;
86 CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
87 if (err)
88 goto error;
89 }
90 err = generic_write(fd, buf, n, NULL);
91 /* Restore raw mode, in any case; we *must* ignore any error apart
92 * EINTR, except for debug.*/
93 if(isatty(fd))
94 CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
95 return(err);
96error:
97 return(-errno);
98}
99
100/*
101 * UML SIGWINCH handling
102 *
Jeff Dike42a359e2007-07-15 23:38:55 -0700103 * The point of this is to handle SIGWINCH on consoles which have host
104 * ttys and relay them inside UML to whatever might be running on the
105 * console and cares about the window size (since SIGWINCH notifies
106 * about terminal size changes).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Jeff Dike42a359e2007-07-15 23:38:55 -0700108 * So, we have a separate thread for each host tty attached to a UML
109 * device (side-issue - I'm annoyed that one thread can't have
110 * multiple controlling ttys for the purpose of handling SIGWINCH, but
111 * I imagine there are other reasons that doesn't make any sense).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *
Jeff Dike42a359e2007-07-15 23:38:55 -0700113 * SIGWINCH can't be received synchronously, so you have to set up to
114 * receive it as a signal. That being the case, if you are going to
115 * wait for it, it is convenient to sit in sigsuspend() and wait for
116 * the signal to bounce you out of it (see below for how we make sure
117 * to exit only on SIGWINCH).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119
120static void winch_handler(int sig)
121{
122}
123
124struct winch_data {
125 int pty_fd;
126 int pipe_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129static int winch_thread(void *arg)
130{
131 struct winch_data *data = arg;
132 sigset_t sigs;
133 int pty_fd, pipe_fd;
134 int count, err;
135 char c = 1;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 pty_fd = data->pty_fd;
138 pipe_fd = data->pipe_fd;
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700139 count = os_write_file(pipe_fd, &c, sizeof(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if(count != sizeof(c))
141 printk("winch_thread : failed to write synchronization "
142 "byte, err = %d\n", -count);
143
144 /* We are not using SIG_IGN on purpose, so don't fix it as I thought to
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700145 * do! If using SIG_IGN, the sigsuspend() call below would not stop on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * SIGWINCH. */
147
148 signal(SIGWINCH, winch_handler);
149 sigfillset(&sigs);
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700150 /* Block all signals possible. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if(sigprocmask(SIG_SETMASK, &sigs, NULL) < 0){
152 printk("winch_thread : sigprocmask failed, errno = %d\n",
153 errno);
154 exit(1);
155 }
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700156 /* In sigsuspend(), block anything else than SIGWINCH. */
157 sigdelset(&sigs, SIGWINCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if(setsid() < 0){
160 printk("winch_thread : setsid failed, errno = %d\n", errno);
161 exit(1);
162 }
163
164 err = os_new_tty_pgrp(pty_fd, os_getpid());
165 if(err < 0){
Jeff Dike42a359e2007-07-15 23:38:55 -0700166 printk("winch_thread : new_tty_pgrp failed on fd %d, "
167 "err = %d\n", pty_fd, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 exit(1);
169 }
170
171 /* These are synchronization calls between various UML threads on the
172 * host - since they are not different kernel threads, we cannot use
173 * kernel semaphores. We don't use SysV semaphores because they are
Jan Engelhardt03a67a42006-11-30 05:32:19 +0100174 * persistent. */
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700175 count = os_read_file(pipe_fd, &c, sizeof(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if(count != sizeof(c))
177 printk("winch_thread : failed to read synchronization byte, "
178 "err = %d\n", -count);
179
180 while(1){
Jeff Dike42a359e2007-07-15 23:38:55 -0700181 /* This will be interrupted by SIGWINCH only, since
182 * other signals are blocked.
183 */
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700184 sigsuspend(&sigs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700186 count = os_write_file(pipe_fd, &c, sizeof(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if(count != sizeof(c))
188 printk("winch_thread : write failed, err = %d\n",
189 -count);
190 }
191}
192
Jeff Dike42a359e2007-07-15 23:38:55 -0700193static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out,
194 unsigned long *stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 struct winch_data data;
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700197 int fds[2], n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 char c;
199
200 err = os_pipe(fds, 1, 1);
201 if(err < 0){
202 printk("winch_tramp : os_pipe failed, err = %d\n", -err);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 data = ((struct winch_data) { .pty_fd = fd,
Jeff Dike1d2ddcf2006-02-07 12:58:41 -0800207 .pipe_fd = fds[1] } );
208 /* CLONE_FILES so this thread doesn't hold open files which are open
Jeff Dike42a359e2007-07-15 23:38:55 -0700209 * now, but later closed in a different thread. This is a
210 * problem with /dev/net/tun, which if held open by this
211 * thread, prevents the TUN/TAP device from being reused.
Jeff Dike1d2ddcf2006-02-07 12:58:41 -0800212 */
Jeff Dikec4399012007-07-15 23:38:56 -0700213 err = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700214 if(err < 0){
Andrew Mortonb16895b2007-05-06 14:51:03 -0700215 printk("fork of winch_thread failed - errno = %d\n", -err);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700216 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 *fd_out = fds[0];
Jeff Dikea6ea4cc2007-05-06 14:51:43 -0700220 n = os_read_file(fds[0], &c, sizeof(c));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if(n != sizeof(c)){
222 printk("winch_tramp : failed to read synchronization byte\n");
223 printk("read failed, err = %d\n", -n);
224 printk("fd %d will not support SIGWINCH\n", fd);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700225 err = -EINVAL;
Jeff Dike1d2ddcf2006-02-07 12:58:41 -0800226 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
Eduard-Gabriel Munteanu89df6bf2007-07-15 23:38:51 -0700228
229 if (os_set_fd_block(*fd_out, 0)) {
230 printk("winch_tramp: failed to set thread_fd non-blocking.\n");
231 goto out_close;
232 }
233
234 return err;
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700235
236 out_close:
237 os_close_file(fds[1]);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700238 os_close_file(fds[0]);
239 out:
240 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243void register_winch(int fd, struct tty_struct *tty)
244{
Jeff Dike42a359e2007-07-15 23:38:55 -0700245 unsigned long stack;
246 int pid, thread, count, thread_fd = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 char c = 1;
248
249 if(!isatty(fd))
250 return;
251
252 pid = tcgetpgrp(fd);
Jeff Dike42a359e2007-07-15 23:38:55 -0700253 if (!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd, tty) &&
254 (pid == -1)) {
255 thread = winch_tramp(fd, tty, &thread_fd, &stack);
256 if (thread < 0)
257 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Jeff Dike42a359e2007-07-15 23:38:55 -0700259 register_winch_irq(thread_fd, fd, thread, tty, stack);
260
261 count = os_write_file(thread_fd, &c, sizeof(c));
262 if(count != sizeof(c))
263 printk("register_winch : failed to write "
264 "synchronization byte, err = %d\n", -count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266}