2019-08-25 17:49:16 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2007-10-16 16:27:34 +08:00
|
|
|
/*
|
2007-10-16 16:26:41 +08:00
|
|
|
* Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2007-10-16 16:26:41 +08:00
|
|
|
#include <unistd.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <errno.h>
|
2006-02-08 04:58:41 +08:00
|
|
|
#include <sched.h>
|
2007-10-16 16:26:41 +08:00
|
|
|
#include <signal.h>
|
|
|
|
#include <termios.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include "chan_user.h"
|
2012-10-08 10:27:32 +08:00
|
|
|
#include <os.h>
|
|
|
|
#include <um_malloc.h>
|
2007-10-16 16:26:39 +08:00
|
|
|
|
|
|
|
void generic_close(int fd, void *unused)
|
|
|
|
{
|
2007-10-16 16:26:40 +08:00
|
|
|
close(fd);
|
2007-10-16 16:26:39 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 15:37:11 +08:00
|
|
|
int generic_read(int fd, __u8 *c_out, void *unused)
|
2007-10-16 16:26:39 +08:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
2007-10-16 16:26:40 +08:00
|
|
|
n = read(fd, c_out, sizeof(*c_out));
|
|
|
|
if (n > 0)
|
|
|
|
return n;
|
|
|
|
else if (n == 0)
|
2007-10-16 16:26:39 +08:00
|
|
|
return -EIO;
|
2020-12-08 01:19:39 +08:00
|
|
|
else if (errno == EAGAIN)
|
|
|
|
return 0;
|
2007-10-16 16:26:40 +08:00
|
|
|
return -errno;
|
2007-10-16 16:26:39 +08:00
|
|
|
}
|
|
|
|
|
2007-10-16 16:26:40 +08:00
|
|
|
/* XXX Trivial wrapper around write */
|
2007-10-16 16:26:39 +08:00
|
|
|
|
2023-12-06 15:37:11 +08:00
|
|
|
int generic_write(int fd, const __u8 *buf, size_t n, void *unused)
|
2007-10-16 16:26:39 +08:00
|
|
|
{
|
2007-10-16 16:26:42 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
err = write(fd, buf, n);
|
|
|
|
if (err > 0)
|
|
|
|
return err;
|
|
|
|
else if (errno == EAGAIN)
|
|
|
|
return 0;
|
|
|
|
else if (err == 0)
|
|
|
|
return -EIO;
|
|
|
|
return -errno;
|
2007-10-16 16:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int generic_window_size(int fd, void *unused, unsigned short *rows_out,
|
|
|
|
unsigned short *cols_out)
|
|
|
|
{
|
2007-10-16 16:26:40 +08:00
|
|
|
struct winsize size;
|
2007-10-16 16:26:39 +08:00
|
|
|
int ret;
|
|
|
|
|
2007-10-16 16:26:41 +08:00
|
|
|
if (ioctl(fd, TIOCGWINSZ, &size) < 0)
|
2007-10-16 16:26:40 +08:00
|
|
|
return -errno;
|
2007-10-16 16:26:39 +08:00
|
|
|
|
2007-10-16 16:26:40 +08:00
|
|
|
ret = ((*rows_out != size.ws_row) || (*cols_out != size.ws_col));
|
2007-10-16 16:26:39 +08:00
|
|
|
|
2007-10-16 16:26:40 +08:00
|
|
|
*rows_out = size.ws_row;
|
|
|
|
*cols_out = size.ws_col;
|
2007-10-16 16:26:39 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void generic_free(void *data)
|
|
|
|
{
|
|
|
|
kfree(data);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-11-14 08:07:11 +08:00
|
|
|
int generic_console_write(int fd, const char *buf, int n)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-12-02 04:16:30 +08:00
|
|
|
sigset_t old, no_sigio;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct termios save, new;
|
|
|
|
int err;
|
|
|
|
|
2007-10-16 16:26:41 +08:00
|
|
|
if (isatty(fd)) {
|
2007-12-02 04:16:30 +08:00
|
|
|
sigemptyset(&no_sigio);
|
|
|
|
sigaddset(&no_sigio, SIGIO);
|
|
|
|
if (sigprocmask(SIG_BLOCK, &no_sigio, &old))
|
|
|
|
goto error;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
CATCH_EINTR(err = tcgetattr(fd, &save));
|
|
|
|
if (err)
|
|
|
|
goto error;
|
|
|
|
new = save;
|
2007-10-16 16:27:34 +08:00
|
|
|
/*
|
|
|
|
* The terminal becomes a bit less raw, to handle \n also as
|
2005-04-17 06:20:36 +08:00
|
|
|
* "Carriage Return", not only as "New Line". Otherwise, the new
|
2007-10-16 16:27:34 +08:00
|
|
|
* line won't start at the first column.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
new.c_oflag |= OPOST;
|
|
|
|
CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
|
|
|
|
if (err)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
err = generic_write(fd, buf, n, NULL);
|
2007-10-16 16:27:34 +08:00
|
|
|
/*
|
|
|
|
* Restore raw mode, in any case; we *must* ignore any error apart
|
|
|
|
* EINTR, except for debug.
|
|
|
|
*/
|
2007-12-02 04:16:30 +08:00
|
|
|
if (isatty(fd)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
|
2007-12-02 04:16:30 +08:00
|
|
|
sigprocmask(SIG_SETMASK, &old, NULL);
|
|
|
|
}
|
|
|
|
|
2007-10-16 16:26:41 +08:00
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
error:
|
2007-10-16 16:26:41 +08:00
|
|
|
return -errno;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* UML SIGWINCH handling
|
|
|
|
*
|
2007-07-16 14:38:55 +08:00
|
|
|
* 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).
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
2007-07-16 14:38:55 +08:00
|
|
|
* 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).
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
2007-07-16 14:38:55 +08:00
|
|
|
* 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).
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void winch_handler(int sig)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
struct winch_data {
|
|
|
|
int pty_fd;
|
|
|
|
int pipe_fd;
|
|
|
|
};
|
|
|
|
|
2023-11-10 19:03:43 +08:00
|
|
|
static __noreturn int winch_thread(void *arg)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct winch_data *data = arg;
|
|
|
|
sigset_t sigs;
|
|
|
|
int pty_fd, pipe_fd;
|
2007-10-16 16:27:08 +08:00
|
|
|
int count;
|
2005-04-17 06:20:36 +08:00
|
|
|
char c = 1;
|
|
|
|
|
|
|
|
pty_fd = data->pty_fd;
|
|
|
|
pipe_fd = data->pipe_fd;
|
2007-10-16 16:27:08 +08:00
|
|
|
count = write(pipe_fd, &c, sizeof(c));
|
2007-10-16 16:26:41 +08:00
|
|
|
if (count != sizeof(c))
|
2023-11-10 19:03:42 +08:00
|
|
|
os_info("winch_thread : failed to write synchronization byte, err = %d\n",
|
|
|
|
-count);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-16 16:26:41 +08:00
|
|
|
/*
|
|
|
|
* We are not using SIG_IGN on purpose, so don't fix it as I thought to
|
2005-09-04 06:57:24 +08:00
|
|
|
* do! If using SIG_IGN, the sigsuspend() call below would not stop on
|
2007-10-16 16:26:41 +08:00
|
|
|
* SIGWINCH.
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
signal(SIGWINCH, winch_handler);
|
|
|
|
sigfillset(&sigs);
|
2005-09-04 06:57:24 +08:00
|
|
|
/* Block all signals possible. */
|
2007-10-16 16:26:41 +08:00
|
|
|
if (sigprocmask(SIG_SETMASK, &sigs, NULL) < 0) {
|
2023-11-10 19:03:42 +08:00
|
|
|
os_info("winch_thread : sigprocmask failed, errno = %d\n",
|
|
|
|
errno);
|
2023-11-10 19:03:43 +08:00
|
|
|
goto wait_kill;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2005-09-04 06:57:24 +08:00
|
|
|
/* In sigsuspend(), block anything else than SIGWINCH. */
|
|
|
|
sigdelset(&sigs, SIGWINCH);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-16 16:26:41 +08:00
|
|
|
if (setsid() < 0) {
|
2023-11-10 19:03:42 +08:00
|
|
|
os_info("winch_thread : setsid failed, errno = %d\n",
|
2007-10-16 16:26:41 +08:00
|
|
|
errno);
|
2023-11-10 19:03:43 +08:00
|
|
|
goto wait_kill;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-10-16 16:27:34 +08:00
|
|
|
if (ioctl(pty_fd, TIOCSCTTY, 0) < 0) {
|
2023-11-10 19:03:42 +08:00
|
|
|
os_info("winch_thread : TIOCSCTTY failed on "
|
|
|
|
"fd %d err = %d\n", pty_fd, errno);
|
2023-11-10 19:03:43 +08:00
|
|
|
goto wait_kill;
|
2007-10-16 16:27:08 +08:00
|
|
|
}
|
|
|
|
|
2007-10-16 16:27:34 +08:00
|
|
|
if (tcsetpgrp(pty_fd, os_getpid()) < 0) {
|
2023-11-10 19:03:42 +08:00
|
|
|
os_info("winch_thread : tcsetpgrp failed on fd %d err = %d\n",
|
|
|
|
pty_fd, errno);
|
2023-11-10 19:03:43 +08:00
|
|
|
goto wait_kill;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2007-10-16 16:26:41 +08:00
|
|
|
/*
|
|
|
|
* These are synchronization calls between various UML threads on the
|
2005-04-17 06:20:36 +08:00
|
|
|
* host - since they are not different kernel threads, we cannot use
|
|
|
|
* kernel semaphores. We don't use SysV semaphores because they are
|
2007-10-16 16:26:41 +08:00
|
|
|
* persistent.
|
|
|
|
*/
|
2007-10-16 16:27:08 +08:00
|
|
|
count = read(pipe_fd, &c, sizeof(c));
|
2007-10-16 16:26:41 +08:00
|
|
|
if (count != sizeof(c))
|
2023-11-10 19:03:42 +08:00
|
|
|
os_info("winch_thread : failed to read synchronization byte, err = %d\n",
|
|
|
|
errno);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-16 16:26:41 +08:00
|
|
|
while(1) {
|
|
|
|
/*
|
|
|
|
* This will be interrupted by SIGWINCH only, since
|
2007-07-16 14:38:55 +08:00
|
|
|
* other signals are blocked.
|
|
|
|
*/
|
2005-09-04 06:57:24 +08:00
|
|
|
sigsuspend(&sigs);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-10-16 16:27:08 +08:00
|
|
|
count = write(pipe_fd, &c, sizeof(c));
|
2007-10-16 16:26:41 +08:00
|
|
|
if (count != sizeof(c))
|
2023-11-10 19:03:42 +08:00
|
|
|
os_info("winch_thread : write failed, err = %d\n",
|
|
|
|
errno);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2023-11-10 19:03:43 +08:00
|
|
|
|
|
|
|
wait_kill:
|
|
|
|
c = 2;
|
|
|
|
count = write(pipe_fd, &c, sizeof(c));
|
|
|
|
while (1)
|
|
|
|
pause();
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2013-03-11 17:05:45 +08:00
|
|
|
static int winch_tramp(int fd, struct tty_port *port, int *fd_out,
|
2007-07-16 14:38:55 +08:00
|
|
|
unsigned long *stack_out)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct winch_data data;
|
2022-05-21 01:45:36 +08:00
|
|
|
int fds[2], n, err, pid;
|
2005-04-17 06:20:36 +08:00
|
|
|
char c;
|
|
|
|
|
|
|
|
err = os_pipe(fds, 1, 1);
|
2007-10-16 16:26:41 +08:00
|
|
|
if (err < 0) {
|
|
|
|
printk(UM_KERN_ERR "winch_tramp : os_pipe failed, err = %d\n",
|
|
|
|
-err);
|
2005-06-09 06:48:27 +08:00
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
data = ((struct winch_data) { .pty_fd = fd,
|
2006-02-08 04:58:41 +08:00
|
|
|
.pipe_fd = fds[1] } );
|
2007-10-16 16:26:41 +08:00
|
|
|
/*
|
|
|
|
* CLONE_FILES so this thread doesn't hold open files which are open
|
2007-07-16 14:38:55 +08:00
|
|
|
* 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.
|
2006-02-08 04:58:41 +08:00
|
|
|
*/
|
2022-05-21 01:45:36 +08:00
|
|
|
pid = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
|
|
|
|
if (pid < 0) {
|
|
|
|
err = pid;
|
2007-10-16 16:26:41 +08:00
|
|
|
printk(UM_KERN_ERR "fork of winch_thread failed - errno = %d\n",
|
|
|
|
-err);
|
2005-06-09 06:48:27 +08:00
|
|
|
goto out_close;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*fd_out = fds[0];
|
2007-10-16 16:27:08 +08:00
|
|
|
n = read(fds[0], &c, sizeof(c));
|
2007-10-16 16:26:41 +08:00
|
|
|
if (n != sizeof(c)) {
|
|
|
|
printk(UM_KERN_ERR "winch_tramp : failed to read "
|
|
|
|
"synchronization byte\n");
|
2007-10-16 16:27:08 +08:00
|
|
|
printk(UM_KERN_ERR "read failed, err = %d\n", errno);
|
2007-10-16 16:26:41 +08:00
|
|
|
printk(UM_KERN_ERR "fd %d will not support SIGWINCH\n", fd);
|
|
|
|
err = -EINVAL;
|
2006-02-08 04:58:41 +08:00
|
|
|
goto out_close;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-07-16 14:38:51 +08:00
|
|
|
|
2021-05-08 11:22:39 +08:00
|
|
|
err = os_set_fd_block(*fd_out, 0);
|
|
|
|
if (err) {
|
2007-10-16 16:26:41 +08:00
|
|
|
printk(UM_KERN_ERR "winch_tramp: failed to set thread_fd "
|
|
|
|
"non-blocking.\n");
|
2007-07-16 14:38:51 +08:00
|
|
|
goto out_close;
|
|
|
|
}
|
|
|
|
|
2022-05-21 01:45:36 +08:00
|
|
|
return pid;
|
2005-06-09 06:48:27 +08:00
|
|
|
|
|
|
|
out_close:
|
2007-10-16 16:27:08 +08:00
|
|
|
close(fds[1]);
|
|
|
|
close(fds[0]);
|
2005-06-09 06:48:27 +08:00
|
|
|
out:
|
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2013-03-11 17:05:45 +08:00
|
|
|
void register_winch(int fd, struct tty_port *port)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2007-07-16 14:38:55 +08:00
|
|
|
unsigned long stack;
|
|
|
|
int pid, thread, count, thread_fd = -1;
|
2005-04-17 06:20:36 +08:00
|
|
|
char c = 1;
|
|
|
|
|
2007-10-16 16:26:41 +08:00
|
|
|
if (!isatty(fd))
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
pid = tcgetpgrp(fd);
|
2013-03-11 17:05:45 +08:00
|
|
|
if (is_skas_winch(pid, fd, port)) {
|
|
|
|
register_winch_irq(-1, fd, -1, port, 0);
|
2011-08-19 03:08:19 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == -1) {
|
2013-03-11 17:05:45 +08:00
|
|
|
thread = winch_tramp(fd, port, &thread_fd, &stack);
|
2007-07-16 14:38:55 +08:00
|
|
|
if (thread < 0)
|
|
|
|
return;
|
|
|
|
|
2013-03-11 17:05:45 +08:00
|
|
|
register_winch_irq(thread_fd, fd, thread, port, stack);
|
2007-07-16 14:38:55 +08:00
|
|
|
|
2007-10-16 16:27:08 +08:00
|
|
|
count = write(thread_fd, &c, sizeof(c));
|
2007-10-16 16:26:41 +08:00
|
|
|
if (count != sizeof(c))
|
|
|
|
printk(UM_KERN_ERR "register_winch : failed to write "
|
2007-10-16 16:27:08 +08:00
|
|
|
"synchronization byte, err = %d\n", errno);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|