chan_user.c 6.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/* 
J
Jeff Dike 已提交
2
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
L
Linus Torvalds 已提交
3 4 5 6
 * Licensed under the GPL
 */

#include <stdlib.h>
J
Jeff Dike 已提交
7
#include <unistd.h>
L
Linus Torvalds 已提交
8
#include <errno.h>
9
#include <sched.h>
J
Jeff Dike 已提交
10 11
#include <signal.h>
#include <termios.h>
L
Linus Torvalds 已提交
12 13 14
#include <sys/ioctl.h>
#include "chan_user.h"
#include "os.h"
15
#include "um_malloc.h"
J
Jeff Dike 已提交
16
#include "user.h"
17 18 19

void generic_close(int fd, void *unused)
{
J
Jeff Dike 已提交
20
	close(fd);
21 22 23 24 25 26
}

int generic_read(int fd, char *c_out, void *unused)
{
	int n;

J
Jeff Dike 已提交
27 28 29 30
	n = read(fd, c_out, sizeof(*c_out));
	if (n > 0)
		return n;
	else if (errno == EAGAIN)
31
		return 0;
J
Jeff Dike 已提交
32
	else if (n == 0)
33
		return -EIO;
J
Jeff Dike 已提交
34
	return -errno;
35 36
}

J
Jeff Dike 已提交
37
/* XXX Trivial wrapper around write */
38 39 40

int generic_write(int fd, const char *buf, int n, void *unused)
{
J
Jeff Dike 已提交
41
	return write(fd, buf, n);
42 43 44 45 46
}

int generic_window_size(int fd, void *unused, unsigned short *rows_out,
			unsigned short *cols_out)
{
J
Jeff Dike 已提交
47
	struct winsize size;
48 49
	int ret;

J
Jeff Dike 已提交
50
	if (ioctl(fd, TIOCGWINSZ, &size) < 0)
J
Jeff Dike 已提交
51
		return -errno;
52

J
Jeff Dike 已提交
53
	ret = ((*rows_out != size.ws_row) || (*cols_out != size.ws_col));
54

J
Jeff Dike 已提交
55 56
	*rows_out = size.ws_row;
	*cols_out = size.ws_col;
57 58 59 60 61 62 63 64

	return ret;
}

void generic_free(void *data)
{
	kfree(data);
}
L
Linus Torvalds 已提交
65

66
int generic_console_write(int fd, const char *buf, int n)
L
Linus Torvalds 已提交
67 68 69 70
{
	struct termios save, new;
	int err;

J
Jeff Dike 已提交
71
	if (isatty(fd)) {
L
Linus Torvalds 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
		CATCH_EINTR(err = tcgetattr(fd, &save));
		if (err)
			goto error;
		new = save;
		/* The terminal becomes a bit less raw, to handle \n also as
		 * "Carriage Return", not only as "New Line". Otherwise, the new
		 * line won't start at the first column.*/
		new.c_oflag |= OPOST;
		CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
		if (err)
			goto error;
	}
	err = generic_write(fd, buf, n, NULL);
	/* Restore raw mode, in any case; we *must* ignore any error apart
	 * EINTR, except for debug.*/
J
Jeff Dike 已提交
87
	if (isatty(fd))
L
Linus Torvalds 已提交
88
		CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
J
Jeff Dike 已提交
89
	return err;
L
Linus Torvalds 已提交
90
error:
J
Jeff Dike 已提交
91
	return -errno;
L
Linus Torvalds 已提交
92 93 94 95 96
}

/*
 * UML SIGWINCH handling
 *
J
Jeff Dike 已提交
97 98 99 100
 * The point of this is to handle SIGWINCH on consoles which have host
 * ttys and relay them inside UML to whatever might be running on the
 * console and cares about the window size (since SIGWINCH notifies
 * about terminal size changes).
L
Linus Torvalds 已提交
101
 *
J
Jeff Dike 已提交
102 103 104 105
 * So, we have a separate thread for each host tty attached to a UML
 * device (side-issue - I'm annoyed that one thread can't have
 * multiple controlling ttys for the purpose of handling SIGWINCH, but
 * I imagine there are other reasons that doesn't make any sense).
L
Linus Torvalds 已提交
106
 *
J
Jeff Dike 已提交
107 108 109 110 111
 * SIGWINCH can't be received synchronously, so you have to set up to
 * receive it as a signal.  That being the case, if you are going to
 * wait for it, it is convenient to sit in sigsuspend() and wait for
 * the signal to bounce you out of it (see below for how we make sure
 * to exit only on SIGWINCH).
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
 */

static void winch_handler(int sig)
{
}

struct winch_data {
	int pty_fd;
	int pipe_fd;
};

static int winch_thread(void *arg)
{
	struct winch_data *data = arg;
	sigset_t sigs;
	int pty_fd, pipe_fd;
	int count, err;
	char c = 1;

	pty_fd = data->pty_fd;
	pipe_fd = data->pipe_fd;
133
	count = os_write_file(pipe_fd, &c, sizeof(c));
J
Jeff Dike 已提交
134 135 136
	if (count != sizeof(c))
		printk(UM_KERN_ERR "winch_thread : failed to write "
		       "synchronization byte, err = %d\n", -count);
L
Linus Torvalds 已提交
137

J
Jeff Dike 已提交
138 139
	/*
	 * We are not using SIG_IGN on purpose, so don't fix it as I thought to
140
	 * do! If using SIG_IGN, the sigsuspend() call below would not stop on
J
Jeff Dike 已提交
141 142
	 * SIGWINCH.
	 */
L
Linus Torvalds 已提交
143 144 145

	signal(SIGWINCH, winch_handler);
	sigfillset(&sigs);
146
	/* Block all signals possible. */
J
Jeff Dike 已提交
147 148 149
	if (sigprocmask(SIG_SETMASK, &sigs, NULL) < 0) {
		printk(UM_KERN_ERR "winch_thread : sigprocmask failed, "
		       "errno = %d\n", errno);
L
Linus Torvalds 已提交
150 151
		exit(1);
	}
152 153
	/* In sigsuspend(), block anything else than SIGWINCH. */
	sigdelset(&sigs, SIGWINCH);
L
Linus Torvalds 已提交
154

J
Jeff Dike 已提交
155 156 157
	if (setsid() < 0) {
		printk(UM_KERN_ERR "winch_thread : setsid failed, errno = %d\n",
		       errno);
L
Linus Torvalds 已提交
158 159 160 161
		exit(1);
	}

	err = os_new_tty_pgrp(pty_fd, os_getpid());
J
Jeff Dike 已提交
162 163 164
	if (err < 0) {
		printk(UM_KERN_ERR "winch_thread : new_tty_pgrp failed on "
		       "fd %d err = %d\n", pty_fd, -err);
L
Linus Torvalds 已提交
165 166 167
		exit(1);
	}

J
Jeff Dike 已提交
168 169
	/*
	 * These are synchronization calls between various UML threads on the
L
Linus Torvalds 已提交
170 171
	 * host - since they are not different kernel threads, we cannot use
	 * kernel semaphores. We don't use SysV semaphores because they are
J
Jeff Dike 已提交
172 173
	 * persistent.
	 */
174
	count = os_read_file(pipe_fd, &c, sizeof(c));
J
Jeff Dike 已提交
175 176 177
	if (count != sizeof(c))
		printk(UM_KERN_ERR "winch_thread : failed to read "
		       "synchronization byte, err = %d\n", -count);
L
Linus Torvalds 已提交
178

J
Jeff Dike 已提交
179 180 181
	while(1) {
		/*
		 * This will be interrupted by SIGWINCH only, since
J
Jeff Dike 已提交
182 183
		 * other signals are blocked.
		 */
184
		sigsuspend(&sigs);
L
Linus Torvalds 已提交
185

186
		count = os_write_file(pipe_fd, &c, sizeof(c));
J
Jeff Dike 已提交
187 188 189
		if (count != sizeof(c))
			printk(UM_KERN_ERR "winch_thread : write failed, "
			       "err = %d\n", -count);
L
Linus Torvalds 已提交
190 191 192
	}
}

J
Jeff Dike 已提交
193 194
static int winch_tramp(int fd, struct tty_struct *tty, int *fd_out,
		       unsigned long *stack_out)
L
Linus Torvalds 已提交
195 196
{
	struct winch_data data;
J
Jeff Dike 已提交
197
	int fds[2], n, err;
L
Linus Torvalds 已提交
198 199 200
	char c;

	err = os_pipe(fds, 1, 1);
J
Jeff Dike 已提交
201 202 203
	if (err < 0) {
		printk(UM_KERN_ERR "winch_tramp : os_pipe failed, err = %d\n",
		       -err);
J
Jeff Dike 已提交
204
		goto out;
L
Linus Torvalds 已提交
205 206 207
	}

	data = ((struct winch_data) { .pty_fd 		= fd,
208
				      .pipe_fd 		= fds[1] } );
J
Jeff Dike 已提交
209 210
	/*
	 * CLONE_FILES so this thread doesn't hold open files which are open
J
Jeff Dike 已提交
211 212 213
	 * now, but later closed in a different thread.  This is a
	 * problem with /dev/net/tun, which if held open by this
	 * thread, prevents the TUN/TAP device from being reused.
214
	 */
J
Jeff Dike 已提交
215
	err = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
J
Jeff Dike 已提交
216 217 218
	if (err < 0) {
		printk(UM_KERN_ERR "fork of winch_thread failed - errno = %d\n",
		       -err);
J
Jeff Dike 已提交
219
		goto out_close;
L
Linus Torvalds 已提交
220 221 222
	}

	*fd_out = fds[0];
223
	n = os_read_file(fds[0], &c, sizeof(c));
J
Jeff Dike 已提交
224 225 226 227 228 229
	if (n != sizeof(c)) {
		printk(UM_KERN_ERR "winch_tramp : failed to read "
		       "synchronization byte\n");
		printk(UM_KERN_ERR "read failed, err = %d\n", -n);
		printk(UM_KERN_ERR "fd %d will not support SIGWINCH\n", fd);
		err = -EINVAL;
230
		goto out_close;
L
Linus Torvalds 已提交
231
	}
E
Eduard-Gabriel Munteanu 已提交
232 233

	if (os_set_fd_block(*fd_out, 0)) {
J
Jeff Dike 已提交
234 235
		printk(UM_KERN_ERR "winch_tramp: failed to set thread_fd "
		       "non-blocking.\n");
E
Eduard-Gabriel Munteanu 已提交
236 237 238 239
		goto out_close;
	}

	return err;
J
Jeff Dike 已提交
240 241 242 243 244 245

 out_close:
	os_close_file(fds[1]);
	os_close_file(fds[0]);
 out:
	return err;
L
Linus Torvalds 已提交
246 247 248 249
}

void register_winch(int fd, struct tty_struct *tty)
{
J
Jeff Dike 已提交
250 251
	unsigned long stack;
	int pid, thread, count, thread_fd = -1;
L
Linus Torvalds 已提交
252 253
	char c = 1;

J
Jeff Dike 已提交
254
	if (!isatty(fd))
L
Linus Torvalds 已提交
255 256 257
		return;

	pid = tcgetpgrp(fd);
J
Jeff Dike 已提交
258 259 260 261 262 263 264 265 266
	if (!CHOOSE_MODE_PROC(is_tracer_winch, is_skas_winch, pid, fd, tty) &&
	    (pid == -1)) {
		thread = winch_tramp(fd, tty, &thread_fd, &stack);
		if (thread < 0)
			return;

		register_winch_irq(thread_fd, fd, thread, tty, stack);

		count = os_write_file(thread_fd, &c, sizeof(c));
J
Jeff Dike 已提交
267 268
		if (count != sizeof(c))
			printk(UM_KERN_ERR "register_winch : failed to write "
J
Jeff Dike 已提交
269
			       "synchronization byte, err = %d\n", -count);
L
Linus Torvalds 已提交
270 271
	}
}