blob: b88e93b3a39f055a5b8a4a4d72b54ae8fe3ed5ee [file] [log] [blame]
Jeff Dikecb8fa612007-10-16 01:27:34 -07001/*
Jeff Dikee99525f2007-10-16 01:26:41 -07002 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <stdlib.h>
Jeff Dikee99525f2007-10-16 01:26:41 -07007#include <unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <errno.h>
Jeff Dike1d2ddcf2006-02-07 12:58:41 -08009#include <sched.h>
Jeff Dikee99525f2007-10-16 01:26:41 -070010#include <signal.h>
11#include <termios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <sys/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "chan_user.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "os.h"
Jeff Dike89fe6472007-10-16 01:26:39 -070015#include "um_malloc.h"
Jeff Dikee99525f2007-10-16 01:26:41 -070016#include "user.h"
Jeff Dike89fe6472007-10-16 01:26:39 -070017
18void generic_close(int fd, void *unused)
19{
Jeff Dike8e2d10e2007-10-16 01:26:40 -070020 close(fd);
Jeff Dike89fe6472007-10-16 01:26:39 -070021}
22
23int generic_read(int fd, char *c_out, void *unused)
24{
25 int n;
26
Jeff Dike8e2d10e2007-10-16 01:26:40 -070027 n = read(fd, c_out, sizeof(*c_out));
28 if (n > 0)
29 return n;
30 else if (errno == EAGAIN)
Jeff Dike89fe6472007-10-16 01:26:39 -070031 return 0;
Jeff Dike8e2d10e2007-10-16 01:26:40 -070032 else if (n == 0)
Jeff Dike89fe6472007-10-16 01:26:39 -070033 return -EIO;
Jeff Dike8e2d10e2007-10-16 01:26:40 -070034 return -errno;
Jeff Dike89fe6472007-10-16 01:26:39 -070035}
36
Jeff Dike8e2d10e2007-10-16 01:26:40 -070037/* XXX Trivial wrapper around write */
Jeff Dike89fe6472007-10-16 01:26:39 -070038
39int generic_write(int fd, const char *buf, int n, void *unused)
40{
Jeff Dikec59dbca2007-10-16 01:26:42 -070041 int err;
42
43 err = write(fd, buf, n);
44 if (err > 0)
45 return err;
46 else if (errno == EAGAIN)
47 return 0;
48 else if (err == 0)
49 return -EIO;
50 return -errno;
Jeff Dike89fe6472007-10-16 01:26:39 -070051}
52
53int generic_window_size(int fd, void *unused, unsigned short *rows_out,
54 unsigned short *cols_out)
55{
Jeff Dike8e2d10e2007-10-16 01:26:40 -070056 struct winsize size;
Jeff Dike89fe6472007-10-16 01:26:39 -070057 int ret;
58
Jeff Dikee99525f2007-10-16 01:26:41 -070059 if (ioctl(fd, TIOCGWINSZ, &size) < 0)
Jeff Dike8e2d10e2007-10-16 01:26:40 -070060 return -errno;
Jeff Dike89fe6472007-10-16 01:26:39 -070061
Jeff Dike8e2d10e2007-10-16 01:26:40 -070062 ret = ((*rows_out != size.ws_row) || (*cols_out != size.ws_col));
Jeff Dike89fe6472007-10-16 01:26:39 -070063
Jeff Dike8e2d10e2007-10-16 01:26:40 -070064 *rows_out = size.ws_row;
65 *cols_out = size.ws_col;
Jeff Dike89fe6472007-10-16 01:26:39 -070066
67 return ret;
68}
69
70void generic_free(void *data)
71{
72 kfree(data);
73}
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -080075int generic_console_write(int fd, const char *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 struct termios save, new;
78 int err;
79
Jeff Dikee99525f2007-10-16 01:26:41 -070080 if (isatty(fd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 CATCH_EINTR(err = tcgetattr(fd, &save));
82 if (err)
83 goto error;
84 new = save;
Jeff Dikecb8fa612007-10-16 01:27:34 -070085 /*
86 * The terminal becomes a bit less raw, to handle \n also as
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * "Carriage Return", not only as "New Line". Otherwise, the new
Jeff Dikecb8fa612007-10-16 01:27:34 -070088 * line won't start at the first column.
89 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 new.c_oflag |= OPOST;
91 CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
92 if (err)
93 goto error;
94 }
95 err = generic_write(fd, buf, n, NULL);
Jeff Dikecb8fa612007-10-16 01:27:34 -070096 /*
97 * Restore raw mode, in any case; we *must* ignore any error apart
98 * EINTR, except for debug.
99 */
Jeff Dikee99525f2007-10-16 01:26:41 -0700100 if (isatty(fd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
Jeff Dikee99525f2007-10-16 01:26:41 -0700102 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103error:
Jeff Dikee99525f2007-10-16 01:26:41 -0700104 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107/*
108 * UML SIGWINCH handling
109 *
Jeff Dike42a359e2007-07-15 23:38:55 -0700110 * The point of this is to handle SIGWINCH on consoles which have host
111 * ttys and relay them inside UML to whatever might be running on the
112 * console and cares about the window size (since SIGWINCH notifies
113 * about terminal size changes).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *
Jeff Dike42a359e2007-07-15 23:38:55 -0700115 * So, we have a separate thread for each host tty attached to a UML
116 * device (side-issue - I'm annoyed that one thread can't have
117 * multiple controlling ttys for the purpose of handling SIGWINCH, but
118 * I imagine there are other reasons that doesn't make any sense).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 *
Jeff Dike42a359e2007-07-15 23:38:55 -0700120 * SIGWINCH can't be received synchronously, so you have to set up to
121 * receive it as a signal. That being the case, if you are going to
122 * wait for it, it is convenient to sit in sigsuspend() and wait for
123 * the signal to bounce you out of it (see below for how we make sure
124 * to exit only on SIGWINCH).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 */
126
127static void winch_handler(int sig)
128{
129}
130
131struct winch_data {
132 int pty_fd;
133 int pipe_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136static int winch_thread(void *arg)
137{
138 struct winch_data *data = arg;
139 sigset_t sigs;
140 int pty_fd, pipe_fd;
Jeff Dike8ca842c2007-10-16 01:27:08 -0700141 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 char c = 1;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 pty_fd = data->pty_fd;
145 pipe_fd = data->pipe_fd;
Jeff Dike8ca842c2007-10-16 01:27:08 -0700146 count = write(pipe_fd, &c, sizeof(c));
Jeff Dikee99525f2007-10-16 01:26:41 -0700147 if (count != sizeof(c))
148 printk(UM_KERN_ERR "winch_thread : failed to write "
149 "synchronization byte, err = %d\n", -count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Jeff Dikee99525f2007-10-16 01:26:41 -0700151 /*
152 * We are not using SIG_IGN on purpose, so don't fix it as I thought to
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700153 * do! If using SIG_IGN, the sigsuspend() call below would not stop on
Jeff Dikee99525f2007-10-16 01:26:41 -0700154 * SIGWINCH.
155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 signal(SIGWINCH, winch_handler);
158 sigfillset(&sigs);
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700159 /* Block all signals possible. */
Jeff Dikee99525f2007-10-16 01:26:41 -0700160 if (sigprocmask(SIG_SETMASK, &sigs, NULL) < 0) {
161 printk(UM_KERN_ERR "winch_thread : sigprocmask failed, "
162 "errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 exit(1);
164 }
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700165 /* In sigsuspend(), block anything else than SIGWINCH. */
166 sigdelset(&sigs, SIGWINCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jeff Dikee99525f2007-10-16 01:26:41 -0700168 if (setsid() < 0) {
169 printk(UM_KERN_ERR "winch_thread : setsid failed, errno = %d\n",
170 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 exit(1);
172 }
173
Jeff Dikecb8fa612007-10-16 01:27:34 -0700174 if (ioctl(pty_fd, TIOCSCTTY, 0) < 0) {
Jeff Dike8ca842c2007-10-16 01:27:08 -0700175 printk(UM_KERN_ERR "winch_thread : TIOCSCTTY failed on "
176 "fd %d err = %d\n", pty_fd, errno);
177 exit(1);
178 }
179
Jeff Dikecb8fa612007-10-16 01:27:34 -0700180 if (tcsetpgrp(pty_fd, os_getpid()) < 0) {
Jeff Dike8ca842c2007-10-16 01:27:08 -0700181 printk(UM_KERN_ERR "winch_thread : tcsetpgrp failed on "
182 "fd %d err = %d\n", pty_fd, errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 exit(1);
184 }
185
Jeff Dikee99525f2007-10-16 01:26:41 -0700186 /*
187 * These are synchronization calls between various UML threads on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * host - since they are not different kernel threads, we cannot use
189 * kernel semaphores. We don't use SysV semaphores because they are
Jeff Dikee99525f2007-10-16 01:26:41 -0700190 * persistent.
191 */
Jeff Dike8ca842c2007-10-16 01:27:08 -0700192 count = read(pipe_fd, &c, sizeof(c));
Jeff Dikee99525f2007-10-16 01:26:41 -0700193 if (count != sizeof(c))
194 printk(UM_KERN_ERR "winch_thread : failed to read "
Jeff Dike8ca842c2007-10-16 01:27:08 -0700195 "synchronization byte, err = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Jeff Dikee99525f2007-10-16 01:26:41 -0700197 while(1) {
198 /*
199 * This will be interrupted by SIGWINCH only, since
Jeff Dike42a359e2007-07-15 23:38:55 -0700200 * other signals are blocked.
201 */
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700202 sigsuspend(&sigs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Jeff Dike8ca842c2007-10-16 01:27:08 -0700204 count = write(pipe_fd, &c, sizeof(c));
Jeff Dikee99525f2007-10-16 01:26:41 -0700205 if (count != sizeof(c))
206 printk(UM_KERN_ERR "winch_thread : write failed, "
Jeff Dike8ca842c2007-10-16 01:27:08 -0700207 "err = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209}
210
Jeff Dike42a359e2007-07-15 23:38:55 -0700211static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out,
212 unsigned long *stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct winch_data data;
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700215 int fds[2], n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 char c;
217
218 err = os_pipe(fds, 1, 1);
Jeff Dikee99525f2007-10-16 01:26:41 -0700219 if (err < 0) {
220 printk(UM_KERN_ERR "winch_tramp : os_pipe failed, err = %d\n",
221 -err);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700222 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224
225 data = ((struct winch_data) { .pty_fd = fd,
Jeff Dike1d2ddcf2006-02-07 12:58:41 -0800226 .pipe_fd = fds[1] } );
Jeff Dikee99525f2007-10-16 01:26:41 -0700227 /*
228 * CLONE_FILES so this thread doesn't hold open files which are open
Jeff Dike42a359e2007-07-15 23:38:55 -0700229 * now, but later closed in a different thread. This is a
230 * problem with /dev/net/tun, which if held open by this
231 * thread, prevents the TUN/TAP device from being reused.
Jeff Dike1d2ddcf2006-02-07 12:58:41 -0800232 */
Jeff Dikec4399012007-07-15 23:38:56 -0700233 err = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700234 if (err < 0) {
235 printk(UM_KERN_ERR "fork of winch_thread failed - errno = %d\n",
236 -err);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700237 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 *fd_out = fds[0];
Jeff Dike8ca842c2007-10-16 01:27:08 -0700241 n = read(fds[0], &c, sizeof(c));
Jeff Dikee99525f2007-10-16 01:26:41 -0700242 if (n != sizeof(c)) {
243 printk(UM_KERN_ERR "winch_tramp : failed to read "
244 "synchronization byte\n");
Jeff Dike8ca842c2007-10-16 01:27:08 -0700245 printk(UM_KERN_ERR "read failed, err = %d\n", errno);
Jeff Dikee99525f2007-10-16 01:26:41 -0700246 printk(UM_KERN_ERR "fd %d will not support SIGWINCH\n", fd);
247 err = -EINVAL;
Jeff Dike1d2ddcf2006-02-07 12:58:41 -0800248 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Eduard-Gabriel Munteanu89df6bf2007-07-15 23:38:51 -0700250
251 if (os_set_fd_block(*fd_out, 0)) {
Jeff Dikee99525f2007-10-16 01:26:41 -0700252 printk(UM_KERN_ERR "winch_tramp: failed to set thread_fd "
253 "non-blocking.\n");
Eduard-Gabriel Munteanu89df6bf2007-07-15 23:38:51 -0700254 goto out_close;
255 }
256
257 return err;
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700258
259 out_close:
Jeff Dike8ca842c2007-10-16 01:27:08 -0700260 close(fds[1]);
261 close(fds[0]);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700262 out:
263 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266void register_winch(int fd, struct tty_struct *tty)
267{
Jeff Dike42a359e2007-07-15 23:38:55 -0700268 unsigned long stack;
269 int pid, thread, count, thread_fd = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 char c = 1;
271
Jeff Dikee99525f2007-10-16 01:26:41 -0700272 if (!isatty(fd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return;
274
275 pid = tcgetpgrp(fd);
Jeff Dike6aa802c2007-10-16 01:26:56 -0700276 if (!is_skas_winch(pid, fd, tty) && (pid == -1)) {
Jeff Dike42a359e2007-07-15 23:38:55 -0700277 thread = winch_tramp(fd, tty, &thread_fd, &stack);
278 if (thread < 0)
279 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Jeff Dike42a359e2007-07-15 23:38:55 -0700281 register_winch_irq(thread_fd, fd, thread, tty, stack);
282
Jeff Dike8ca842c2007-10-16 01:27:08 -0700283 count = write(thread_fd, &c, sizeof(c));
Jeff Dikee99525f2007-10-16 01:26:41 -0700284 if (count != sizeof(c))
285 printk(UM_KERN_ERR "register_winch : failed to write "
Jeff Dike8ca842c2007-10-16 01:27:08 -0700286 "synchronization byte, err = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288}