blob: 3fd7c3efdb18df6b0f8f1dfd68e3883052b329c1 [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"
Al Viro37185b32012-10-08 03:27:32 +010014#include <os.h>
15#include <um_malloc.h>
Jeff Dike89fe6472007-10-16 01:26:39 -070016
17void generic_close(int fd, void *unused)
18{
Jeff Dike8e2d10e2007-10-16 01:26:40 -070019 close(fd);
Jeff Dike89fe6472007-10-16 01:26:39 -070020}
21
22int generic_read(int fd, char *c_out, void *unused)
23{
24 int n;
25
Jeff Dike8e2d10e2007-10-16 01:26:40 -070026 n = read(fd, c_out, sizeof(*c_out));
27 if (n > 0)
28 return n;
29 else if (errno == EAGAIN)
Jeff Dike89fe6472007-10-16 01:26:39 -070030 return 0;
Jeff Dike8e2d10e2007-10-16 01:26:40 -070031 else if (n == 0)
Jeff Dike89fe6472007-10-16 01:26:39 -070032 return -EIO;
Jeff Dike8e2d10e2007-10-16 01:26:40 -070033 return -errno;
Jeff Dike89fe6472007-10-16 01:26:39 -070034}
35
Jeff Dike8e2d10e2007-10-16 01:26:40 -070036/* XXX Trivial wrapper around write */
Jeff Dike89fe6472007-10-16 01:26:39 -070037
38int generic_write(int fd, const char *buf, int n, void *unused)
39{
Jeff Dikec59dbca2007-10-16 01:26:42 -070040 int err;
41
42 err = write(fd, buf, n);
43 if (err > 0)
44 return err;
45 else if (errno == EAGAIN)
46 return 0;
47 else if (err == 0)
48 return -EIO;
49 return -errno;
Jeff Dike89fe6472007-10-16 01:26:39 -070050}
51
52int generic_window_size(int fd, void *unused, unsigned short *rows_out,
53 unsigned short *cols_out)
54{
Jeff Dike8e2d10e2007-10-16 01:26:40 -070055 struct winsize size;
Jeff Dike89fe6472007-10-16 01:26:39 -070056 int ret;
57
Jeff Dikee99525f2007-10-16 01:26:41 -070058 if (ioctl(fd, TIOCGWINSZ, &size) < 0)
Jeff Dike8e2d10e2007-10-16 01:26:40 -070059 return -errno;
Jeff Dike89fe6472007-10-16 01:26:39 -070060
Jeff Dike8e2d10e2007-10-16 01:26:40 -070061 ret = ((*rows_out != size.ws_row) || (*cols_out != size.ws_col));
Jeff Dike89fe6472007-10-16 01:26:39 -070062
Jeff Dike8e2d10e2007-10-16 01:26:40 -070063 *rows_out = size.ws_row;
64 *cols_out = size.ws_col;
Jeff Dike89fe6472007-10-16 01:26:39 -070065
66 return ret;
67}
68
69void generic_free(void *data)
70{
71 kfree(data);
72}
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Paolo 'Blaisorblade' Giarrusso55c033c2005-11-13 16:07:11 -080074int generic_console_write(int fd, const char *buf, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Jeff Dikece3b6422007-12-01 12:16:30 -080076 sigset_t old, no_sigio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct termios save, new;
78 int err;
79
Jeff Dikee99525f2007-10-16 01:26:41 -070080 if (isatty(fd)) {
Jeff Dikece3b6422007-12-01 12:16:30 -080081 sigemptyset(&no_sigio);
82 sigaddset(&no_sigio, SIGIO);
83 if (sigprocmask(SIG_BLOCK, &no_sigio, &old))
84 goto error;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 CATCH_EINTR(err = tcgetattr(fd, &save));
87 if (err)
88 goto error;
89 new = save;
Jeff Dikecb8fa612007-10-16 01:27:34 -070090 /*
91 * The terminal becomes a bit less raw, to handle \n also as
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * "Carriage Return", not only as "New Line". Otherwise, the new
Jeff Dikecb8fa612007-10-16 01:27:34 -070093 * line won't start at the first column.
94 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 new.c_oflag |= OPOST;
96 CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
97 if (err)
98 goto error;
99 }
100 err = generic_write(fd, buf, n, NULL);
Jeff Dikecb8fa612007-10-16 01:27:34 -0700101 /*
102 * Restore raw mode, in any case; we *must* ignore any error apart
103 * EINTR, except for debug.
104 */
Jeff Dikece3b6422007-12-01 12:16:30 -0800105 if (isatty(fd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
Jeff Dikece3b6422007-12-01 12:16:30 -0800107 sigprocmask(SIG_SETMASK, &old, NULL);
108 }
109
Jeff Dikee99525f2007-10-16 01:26:41 -0700110 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111error:
Jeff Dikee99525f2007-10-16 01:26:41 -0700112 return -errno;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115/*
116 * UML SIGWINCH handling
117 *
Jeff Dike42a359e2007-07-15 23:38:55 -0700118 * The point of this is to handle SIGWINCH on consoles which have host
119 * ttys and relay them inside UML to whatever might be running on the
120 * console and cares about the window size (since SIGWINCH notifies
121 * about terminal size changes).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 *
Jeff Dike42a359e2007-07-15 23:38:55 -0700123 * So, we have a separate thread for each host tty attached to a UML
124 * device (side-issue - I'm annoyed that one thread can't have
125 * multiple controlling ttys for the purpose of handling SIGWINCH, but
126 * I imagine there are other reasons that doesn't make any sense).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 *
Jeff Dike42a359e2007-07-15 23:38:55 -0700128 * SIGWINCH can't be received synchronously, so you have to set up to
129 * receive it as a signal. That being the case, if you are going to
130 * wait for it, it is convenient to sit in sigsuspend() and wait for
131 * the signal to bounce you out of it (see below for how we make sure
132 * to exit only on SIGWINCH).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134
135static void winch_handler(int sig)
136{
137}
138
139struct winch_data {
140 int pty_fd;
141 int pipe_fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
144static int winch_thread(void *arg)
145{
146 struct winch_data *data = arg;
147 sigset_t sigs;
148 int pty_fd, pipe_fd;
Jeff Dike8ca842c2007-10-16 01:27:08 -0700149 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 char c = 1;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 pty_fd = data->pty_fd;
153 pipe_fd = data->pipe_fd;
Jeff Dike8ca842c2007-10-16 01:27:08 -0700154 count = write(pipe_fd, &c, sizeof(c));
Jeff Dikee99525f2007-10-16 01:26:41 -0700155 if (count != sizeof(c))
156 printk(UM_KERN_ERR "winch_thread : failed to write "
157 "synchronization byte, err = %d\n", -count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jeff Dikee99525f2007-10-16 01:26:41 -0700159 /*
160 * We are not using SIG_IGN on purpose, so don't fix it as I thought to
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700161 * do! If using SIG_IGN, the sigsuspend() call below would not stop on
Jeff Dikee99525f2007-10-16 01:26:41 -0700162 * SIGWINCH.
163 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 signal(SIGWINCH, winch_handler);
166 sigfillset(&sigs);
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700167 /* Block all signals possible. */
Jeff Dikee99525f2007-10-16 01:26:41 -0700168 if (sigprocmask(SIG_SETMASK, &sigs, NULL) < 0) {
169 printk(UM_KERN_ERR "winch_thread : sigprocmask failed, "
170 "errno = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 exit(1);
172 }
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700173 /* In sigsuspend(), block anything else than SIGWINCH. */
174 sigdelset(&sigs, SIGWINCH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jeff Dikee99525f2007-10-16 01:26:41 -0700176 if (setsid() < 0) {
177 printk(UM_KERN_ERR "winch_thread : setsid failed, errno = %d\n",
178 errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 exit(1);
180 }
181
Jeff Dikecb8fa612007-10-16 01:27:34 -0700182 if (ioctl(pty_fd, TIOCSCTTY, 0) < 0) {
Jeff Dike8ca842c2007-10-16 01:27:08 -0700183 printk(UM_KERN_ERR "winch_thread : TIOCSCTTY failed on "
184 "fd %d err = %d\n", pty_fd, errno);
185 exit(1);
186 }
187
Jeff Dikecb8fa612007-10-16 01:27:34 -0700188 if (tcsetpgrp(pty_fd, os_getpid()) < 0) {
Jeff Dike8ca842c2007-10-16 01:27:08 -0700189 printk(UM_KERN_ERR "winch_thread : tcsetpgrp failed on "
190 "fd %d err = %d\n", pty_fd, errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 exit(1);
192 }
193
Jeff Dikee99525f2007-10-16 01:26:41 -0700194 /*
195 * These are synchronization calls between various UML threads on the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * host - since they are not different kernel threads, we cannot use
197 * kernel semaphores. We don't use SysV semaphores because they are
Jeff Dikee99525f2007-10-16 01:26:41 -0700198 * persistent.
199 */
Jeff Dike8ca842c2007-10-16 01:27:08 -0700200 count = read(pipe_fd, &c, sizeof(c));
Jeff Dikee99525f2007-10-16 01:26:41 -0700201 if (count != sizeof(c))
202 printk(UM_KERN_ERR "winch_thread : failed to read "
Jeff Dike8ca842c2007-10-16 01:27:08 -0700203 "synchronization byte, err = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Jeff Dikee99525f2007-10-16 01:26:41 -0700205 while(1) {
206 /*
207 * This will be interrupted by SIGWINCH only, since
Jeff Dike42a359e2007-07-15 23:38:55 -0700208 * other signals are blocked.
209 */
Bodo Stroessered1b58d2005-09-03 15:57:24 -0700210 sigsuspend(&sigs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Jeff Dike8ca842c2007-10-16 01:27:08 -0700212 count = write(pipe_fd, &c, sizeof(c));
Jeff Dikee99525f2007-10-16 01:26:41 -0700213 if (count != sizeof(c))
214 printk(UM_KERN_ERR "winch_thread : write failed, "
Jeff Dike8ca842c2007-10-16 01:27:08 -0700215 "err = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217}
218
Richard Weinberger2116bda2013-03-11 10:05:45 +0100219static int winch_tramp(int fd, struct tty_port *port, int *fd_out,
Jeff Dike42a359e2007-07-15 23:38:55 -0700220 unsigned long *stack_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 struct winch_data data;
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700223 int fds[2], n, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 char c;
225
226 err = os_pipe(fds, 1, 1);
Jeff Dikee99525f2007-10-16 01:26:41 -0700227 if (err < 0) {
228 printk(UM_KERN_ERR "winch_tramp : os_pipe failed, err = %d\n",
229 -err);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700230 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
233 data = ((struct winch_data) { .pty_fd = fd,
Jeff Dike1d2ddcf2006-02-07 12:58:41 -0800234 .pipe_fd = fds[1] } );
Jeff Dikee99525f2007-10-16 01:26:41 -0700235 /*
236 * CLONE_FILES so this thread doesn't hold open files which are open
Jeff Dike42a359e2007-07-15 23:38:55 -0700237 * now, but later closed in a different thread. This is a
238 * problem with /dev/net/tun, which if held open by this
239 * thread, prevents the TUN/TAP device from being reused.
Jeff Dike1d2ddcf2006-02-07 12:58:41 -0800240 */
Jeff Dikec4399012007-07-15 23:38:56 -0700241 err = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
Jeff Dikee99525f2007-10-16 01:26:41 -0700242 if (err < 0) {
243 printk(UM_KERN_ERR "fork of winch_thread failed - errno = %d\n",
244 -err);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700245 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 *fd_out = fds[0];
Jeff Dike8ca842c2007-10-16 01:27:08 -0700249 n = read(fds[0], &c, sizeof(c));
Jeff Dikee99525f2007-10-16 01:26:41 -0700250 if (n != sizeof(c)) {
251 printk(UM_KERN_ERR "winch_tramp : failed to read "
252 "synchronization byte\n");
Jeff Dike8ca842c2007-10-16 01:27:08 -0700253 printk(UM_KERN_ERR "read failed, err = %d\n", errno);
Jeff Dikee99525f2007-10-16 01:26:41 -0700254 printk(UM_KERN_ERR "fd %d will not support SIGWINCH\n", fd);
255 err = -EINVAL;
Jeff Dike1d2ddcf2006-02-07 12:58:41 -0800256 goto out_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Eduard-Gabriel Munteanu89df6bf2007-07-15 23:38:51 -0700258
259 if (os_set_fd_block(*fd_out, 0)) {
Jeff Dikee99525f2007-10-16 01:26:41 -0700260 printk(UM_KERN_ERR "winch_tramp: failed to set thread_fd "
261 "non-blocking.\n");
Eduard-Gabriel Munteanu89df6bf2007-07-15 23:38:51 -0700262 goto out_close;
263 }
264
265 return err;
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700266
267 out_close:
Jeff Dike8ca842c2007-10-16 01:27:08 -0700268 close(fds[1]);
269 close(fds[0]);
Jeff Dike1f96ddb2005-06-08 15:48:27 -0700270 out:
271 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Richard Weinberger2116bda2013-03-11 10:05:45 +0100274void register_winch(int fd, struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Jeff Dike42a359e2007-07-15 23:38:55 -0700276 unsigned long stack;
277 int pid, thread, count, thread_fd = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 char c = 1;
279
Jeff Dikee99525f2007-10-16 01:26:41 -0700280 if (!isatty(fd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return;
282
283 pid = tcgetpgrp(fd);
Richard Weinberger2116bda2013-03-11 10:05:45 +0100284 if (is_skas_winch(pid, fd, port)) {
285 register_winch_irq(-1, fd, -1, port, 0);
Al Viro17e05202011-08-18 20:08:19 +0100286 return;
287 }
288
289 if (pid == -1) {
Richard Weinberger2116bda2013-03-11 10:05:45 +0100290 thread = winch_tramp(fd, port, &thread_fd, &stack);
Jeff Dike42a359e2007-07-15 23:38:55 -0700291 if (thread < 0)
292 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Richard Weinberger2116bda2013-03-11 10:05:45 +0100294 register_winch_irq(thread_fd, fd, thread, port, stack);
Jeff Dike42a359e2007-07-15 23:38:55 -0700295
Jeff Dike8ca842c2007-10-16 01:27:08 -0700296 count = write(thread_fd, &c, sizeof(c));
Jeff Dikee99525f2007-10-16 01:26:41 -0700297 if (count != sizeof(c))
298 printk(UM_KERN_ERR "register_winch : failed to write "
Jeff Dike8ca842c2007-10-16 01:27:08 -0700299 "synchronization byte, err = %d\n", errno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301}