xref: /openbmc/linux/arch/um/drivers/chan_user.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1dbddf429SAlex Dewar // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3e99525f9SJeff Dike  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <stdlib.h>
7e99525f9SJeff Dike #include <unistd.h>
81da177e4SLinus Torvalds #include <errno.h>
91d2ddcfbSJeff Dike #include <sched.h>
10e99525f9SJeff Dike #include <signal.h>
11e99525f9SJeff Dike #include <termios.h>
121da177e4SLinus Torvalds #include <sys/ioctl.h>
131da177e4SLinus Torvalds #include "chan_user.h"
1437185b33SAl Viro #include <os.h>
1537185b33SAl Viro #include <um_malloc.h>
1689fe6476SJeff Dike 
generic_close(int fd,void * unused)1789fe6476SJeff Dike void generic_close(int fd, void *unused)
1889fe6476SJeff Dike {
198e2d10e1SJeff Dike 	close(fd);
2089fe6476SJeff Dike }
2189fe6476SJeff Dike 
generic_read(int fd,char * c_out,void * unused)2289fe6476SJeff Dike int generic_read(int fd, char *c_out, void *unused)
2389fe6476SJeff Dike {
2489fe6476SJeff Dike 	int n;
2589fe6476SJeff Dike 
268e2d10e1SJeff Dike 	n = read(fd, c_out, sizeof(*c_out));
278e2d10e1SJeff Dike 	if (n > 0)
288e2d10e1SJeff Dike 		return n;
2989fe6476SJeff Dike 	else if (n == 0)
3089fe6476SJeff Dike 		return -EIO;
319b1c0c0eSAnton Ivanov 	else if (errno == EAGAIN)
329b1c0c0eSAnton Ivanov 		return 0;
338e2d10e1SJeff Dike 	return -errno;
3489fe6476SJeff Dike }
3589fe6476SJeff Dike 
368e2d10e1SJeff Dike /* XXX Trivial wrapper around write */
3789fe6476SJeff Dike 
generic_write(int fd,const char * buf,int n,void * unused)3889fe6476SJeff Dike int generic_write(int fd, const char *buf, int n, void *unused)
3989fe6476SJeff Dike {
40c59dbcadSJeff Dike 	int err;
41c59dbcadSJeff Dike 
42c59dbcadSJeff Dike 	err = write(fd, buf, n);
43c59dbcadSJeff Dike 	if (err > 0)
44c59dbcadSJeff Dike 		return err;
45c59dbcadSJeff Dike 	else if (errno == EAGAIN)
46c59dbcadSJeff Dike 		return 0;
47c59dbcadSJeff Dike 	else if (err == 0)
48c59dbcadSJeff Dike 		return -EIO;
49c59dbcadSJeff Dike 	return -errno;
5089fe6476SJeff Dike }
5189fe6476SJeff Dike 
generic_window_size(int fd,void * unused,unsigned short * rows_out,unsigned short * cols_out)5289fe6476SJeff Dike int generic_window_size(int fd, void *unused, unsigned short *rows_out,
5389fe6476SJeff Dike 			unsigned short *cols_out)
5489fe6476SJeff Dike {
558e2d10e1SJeff Dike 	struct winsize size;
5689fe6476SJeff Dike 	int ret;
5789fe6476SJeff Dike 
588e2d10e1SJeff Dike 	if (ioctl(fd, TIOCGWINSZ, &size) < 0)
598e2d10e1SJeff Dike 		return -errno;
6089fe6476SJeff Dike 
618e2d10e1SJeff Dike 	ret = ((*rows_out != size.ws_row) || (*cols_out != size.ws_col));
6289fe6476SJeff Dike 
638e2d10e1SJeff Dike 	*rows_out = size.ws_row;
648e2d10e1SJeff Dike 	*cols_out = size.ws_col;
6589fe6476SJeff Dike 
6689fe6476SJeff Dike 	return ret;
6789fe6476SJeff Dike }
6889fe6476SJeff Dike 
generic_free(void * data)6989fe6476SJeff Dike void generic_free(void *data)
7089fe6476SJeff Dike {
7189fe6476SJeff Dike 	kfree(data);
7289fe6476SJeff Dike }
731da177e4SLinus Torvalds 
generic_console_write(int fd,const char * buf,int n)7455c033c1SPaolo 'Blaisorblade' Giarrusso int generic_console_write(int fd, const char *buf, int n)
751da177e4SLinus Torvalds {
76ce3b642dSJeff Dike 	sigset_t old, no_sigio;
771da177e4SLinus Torvalds 	struct termios save, new;
781da177e4SLinus Torvalds 	int err;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	if (isatty(fd)) {
81ce3b642dSJeff Dike 		sigemptyset(&no_sigio);
82ce3b642dSJeff Dike 		sigaddset(&no_sigio, SIGIO);
83ce3b642dSJeff Dike 		if (sigprocmask(SIG_BLOCK, &no_sigio, &old))
84ce3b642dSJeff Dike 			goto error;
85ce3b642dSJeff Dike 
861da177e4SLinus Torvalds 		CATCH_EINTR(err = tcgetattr(fd, &save));
871da177e4SLinus Torvalds 		if (err)
881da177e4SLinus Torvalds 			goto error;
891da177e4SLinus Torvalds 		new = save;
90cb8fa61cSJeff Dike 		/*
91cb8fa61cSJeff Dike 		 * The terminal becomes a bit less raw, to handle \n also as
921da177e4SLinus Torvalds 		 * "Carriage Return", not only as "New Line". Otherwise, the new
93cb8fa61cSJeff Dike 		 * line won't start at the first column.
94cb8fa61cSJeff Dike 		 */
951da177e4SLinus Torvalds 		new.c_oflag |= OPOST;
961da177e4SLinus Torvalds 		CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
971da177e4SLinus Torvalds 		if (err)
981da177e4SLinus Torvalds 			goto error;
991da177e4SLinus Torvalds 	}
1001da177e4SLinus Torvalds 	err = generic_write(fd, buf, n, NULL);
101cb8fa61cSJeff Dike 	/*
102cb8fa61cSJeff Dike 	 * Restore raw mode, in any case; we *must* ignore any error apart
103cb8fa61cSJeff Dike 	 * EINTR, except for debug.
104cb8fa61cSJeff Dike 	 */
105ce3b642dSJeff Dike 	if (isatty(fd)) {
1061da177e4SLinus Torvalds 		CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
107ce3b642dSJeff Dike 		sigprocmask(SIG_SETMASK, &old, NULL);
108ce3b642dSJeff Dike 	}
109ce3b642dSJeff Dike 
110e99525f9SJeff Dike 	return err;
1111da177e4SLinus Torvalds error:
112e99525f9SJeff Dike 	return -errno;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /*
1161da177e4SLinus Torvalds  * UML SIGWINCH handling
1171da177e4SLinus Torvalds  *
11842a359e3SJeff Dike  * The point of this is to handle SIGWINCH on consoles which have host
11942a359e3SJeff Dike  * ttys and relay them inside UML to whatever might be running on the
12042a359e3SJeff Dike  * console and cares about the window size (since SIGWINCH notifies
12142a359e3SJeff Dike  * about terminal size changes).
1221da177e4SLinus Torvalds  *
12342a359e3SJeff Dike  * So, we have a separate thread for each host tty attached to a UML
12442a359e3SJeff Dike  * device (side-issue - I'm annoyed that one thread can't have
12542a359e3SJeff Dike  * multiple controlling ttys for the purpose of handling SIGWINCH, but
12642a359e3SJeff Dike  * I imagine there are other reasons that doesn't make any sense).
1271da177e4SLinus Torvalds  *
12842a359e3SJeff Dike  * SIGWINCH can't be received synchronously, so you have to set up to
12942a359e3SJeff Dike  * receive it as a signal.  That being the case, if you are going to
13042a359e3SJeff Dike  * wait for it, it is convenient to sit in sigsuspend() and wait for
13142a359e3SJeff Dike  * the signal to bounce you out of it (see below for how we make sure
13242a359e3SJeff Dike  * to exit only on SIGWINCH).
1331da177e4SLinus Torvalds  */
1341da177e4SLinus Torvalds 
winch_handler(int sig)1351da177e4SLinus Torvalds static void winch_handler(int sig)
1361da177e4SLinus Torvalds {
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds struct winch_data {
1401da177e4SLinus Torvalds 	int pty_fd;
1411da177e4SLinus Torvalds 	int pipe_fd;
1421da177e4SLinus Torvalds };
1431da177e4SLinus Torvalds 
winch_thread(void * arg)1441da177e4SLinus Torvalds static int winch_thread(void *arg)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	struct winch_data *data = arg;
1471da177e4SLinus Torvalds 	sigset_t sigs;
1481da177e4SLinus Torvalds 	int pty_fd, pipe_fd;
1498ca842c4SJeff Dike 	int count;
1501da177e4SLinus Torvalds 	char c = 1;
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 	pty_fd = data->pty_fd;
1531da177e4SLinus Torvalds 	pipe_fd = data->pipe_fd;
1548ca842c4SJeff Dike 	count = write(pipe_fd, &c, sizeof(c));
1551da177e4SLinus Torvalds 	if (count != sizeof(c))
156e99525f9SJeff Dike 		printk(UM_KERN_ERR "winch_thread : failed to write "
157e99525f9SJeff Dike 		       "synchronization byte, err = %d\n", -count);
1581da177e4SLinus Torvalds 
159e99525f9SJeff Dike 	/*
160e99525f9SJeff Dike 	 * We are not using SIG_IGN on purpose, so don't fix it as I thought to
161ed1b58d8SBodo Stroesser 	 * do! If using SIG_IGN, the sigsuspend() call below would not stop on
162e99525f9SJeff Dike 	 * SIGWINCH.
163e99525f9SJeff Dike 	 */
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	signal(SIGWINCH, winch_handler);
1661da177e4SLinus Torvalds 	sigfillset(&sigs);
167ed1b58d8SBodo Stroesser 	/* Block all signals possible. */
1681da177e4SLinus Torvalds 	if (sigprocmask(SIG_SETMASK, &sigs, NULL) < 0) {
169e99525f9SJeff Dike 		printk(UM_KERN_ERR "winch_thread : sigprocmask failed, "
170e99525f9SJeff Dike 		       "errno = %d\n", errno);
1711da177e4SLinus Torvalds 		exit(1);
1721da177e4SLinus Torvalds 	}
173ed1b58d8SBodo Stroesser 	/* In sigsuspend(), block anything else than SIGWINCH. */
174ed1b58d8SBodo Stroesser 	sigdelset(&sigs, SIGWINCH);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	if (setsid() < 0) {
177e99525f9SJeff Dike 		printk(UM_KERN_ERR "winch_thread : setsid failed, errno = %d\n",
178e99525f9SJeff Dike 		       errno);
1791da177e4SLinus Torvalds 		exit(1);
1801da177e4SLinus Torvalds 	}
1811da177e4SLinus Torvalds 
1828ca842c4SJeff Dike 	if (ioctl(pty_fd, TIOCSCTTY, 0) < 0) {
1838ca842c4SJeff Dike 		printk(UM_KERN_ERR "winch_thread : TIOCSCTTY failed on "
1848ca842c4SJeff Dike 		       "fd %d err = %d\n", pty_fd, errno);
1858ca842c4SJeff Dike 		exit(1);
1868ca842c4SJeff Dike 	}
1878ca842c4SJeff Dike 
1888ca842c4SJeff Dike 	if (tcsetpgrp(pty_fd, os_getpid()) < 0) {
1898ca842c4SJeff Dike 		printk(UM_KERN_ERR "winch_thread : tcsetpgrp failed on "
1908ca842c4SJeff Dike 		       "fd %d err = %d\n", pty_fd, errno);
1911da177e4SLinus Torvalds 		exit(1);
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 
194e99525f9SJeff Dike 	/*
195e99525f9SJeff Dike 	 * These are synchronization calls between various UML threads on the
1961da177e4SLinus Torvalds 	 * host - since they are not different kernel threads, we cannot use
1971da177e4SLinus Torvalds 	 * kernel semaphores. We don't use SysV semaphores because they are
198e99525f9SJeff Dike 	 * persistent.
199e99525f9SJeff Dike 	 */
2008ca842c4SJeff Dike 	count = read(pipe_fd, &c, sizeof(c));
2011da177e4SLinus Torvalds 	if (count != sizeof(c))
202e99525f9SJeff Dike 		printk(UM_KERN_ERR "winch_thread : failed to read "
2038ca842c4SJeff Dike 		       "synchronization byte, err = %d\n", errno);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	while(1) {
206e99525f9SJeff Dike 		/*
207e99525f9SJeff Dike 		 * This will be interrupted by SIGWINCH only, since
20842a359e3SJeff Dike 		 * other signals are blocked.
20942a359e3SJeff Dike 		 */
210ed1b58d8SBodo Stroesser 		sigsuspend(&sigs);
2111da177e4SLinus Torvalds 
2128ca842c4SJeff Dike 		count = write(pipe_fd, &c, sizeof(c));
2131da177e4SLinus Torvalds 		if (count != sizeof(c))
214e99525f9SJeff Dike 			printk(UM_KERN_ERR "winch_thread : write failed, "
2158ca842c4SJeff Dike 			       "err = %d\n", errno);
2161da177e4SLinus Torvalds 	}
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
winch_tramp(int fd,struct tty_port * port,int * fd_out,unsigned long * stack_out)2192116bda6SRichard Weinberger static int winch_tramp(int fd, struct tty_port *port, int *fd_out,
22042a359e3SJeff Dike 		       unsigned long *stack_out)
2211da177e4SLinus Torvalds {
2221da177e4SLinus Torvalds 	struct winch_data data;
223*57ae0b67SJohannes Berg 	int fds[2], n, err, pid;
2241da177e4SLinus Torvalds 	char c;
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	err = os_pipe(fds, 1, 1);
2271da177e4SLinus Torvalds 	if (err < 0) {
228e99525f9SJeff Dike 		printk(UM_KERN_ERR "winch_tramp : os_pipe failed, err = %d\n",
229e99525f9SJeff Dike 		       -err);
2301f96ddb4SJeff Dike 		goto out;
2311da177e4SLinus Torvalds 	}
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	data = ((struct winch_data) { .pty_fd 		= fd,
2341d2ddcfbSJeff Dike 				      .pipe_fd 		= fds[1] } );
235e99525f9SJeff Dike 	/*
236e99525f9SJeff Dike 	 * CLONE_FILES so this thread doesn't hold open files which are open
23742a359e3SJeff Dike 	 * now, but later closed in a different thread.  This is a
23842a359e3SJeff Dike 	 * problem with /dev/net/tun, which if held open by this
23942a359e3SJeff Dike 	 * thread, prevents the TUN/TAP device from being reused.
2401d2ddcfbSJeff Dike 	 */
241*57ae0b67SJohannes Berg 	pid = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
242*57ae0b67SJohannes Berg 	if (pid < 0) {
243*57ae0b67SJohannes Berg 		err = pid;
244e99525f9SJeff Dike 		printk(UM_KERN_ERR "fork of winch_thread failed - errno = %d\n",
245e99525f9SJeff Dike 		       -err);
2461f96ddb4SJeff Dike 		goto out_close;
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	*fd_out = fds[0];
2508ca842c4SJeff Dike 	n = read(fds[0], &c, sizeof(c));
2511da177e4SLinus Torvalds 	if (n != sizeof(c)) {
252e99525f9SJeff Dike 		printk(UM_KERN_ERR "winch_tramp : failed to read "
253e99525f9SJeff Dike 		       "synchronization byte\n");
2548ca842c4SJeff Dike 		printk(UM_KERN_ERR "read failed, err = %d\n", errno);
255e99525f9SJeff Dike 		printk(UM_KERN_ERR "fd %d will not support SIGWINCH\n", fd);
2561f96ddb4SJeff Dike 		err = -EINVAL;
2571d2ddcfbSJeff Dike 		goto out_close;
2581da177e4SLinus Torvalds 	}
25989df6bfcSEduard-Gabriel Munteanu 
260ccf1236eSZhen Lei 	err = os_set_fd_block(*fd_out, 0);
261ccf1236eSZhen Lei 	if (err) {
262e99525f9SJeff Dike 		printk(UM_KERN_ERR "winch_tramp: failed to set thread_fd "
263e99525f9SJeff Dike 		       "non-blocking.\n");
26489df6bfcSEduard-Gabriel Munteanu 		goto out_close;
26589df6bfcSEduard-Gabriel Munteanu 	}
26689df6bfcSEduard-Gabriel Munteanu 
267*57ae0b67SJohannes Berg 	return pid;
2681f96ddb4SJeff Dike 
2691f96ddb4SJeff Dike  out_close:
2708ca842c4SJeff Dike 	close(fds[1]);
2718ca842c4SJeff Dike 	close(fds[0]);
2721f96ddb4SJeff Dike  out:
2731f96ddb4SJeff Dike 	return err;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
register_winch(int fd,struct tty_port * port)2762116bda6SRichard Weinberger void register_winch(int fd, struct tty_port *port)
2771da177e4SLinus Torvalds {
27842a359e3SJeff Dike 	unsigned long stack;
27942a359e3SJeff Dike 	int pid, thread, count, thread_fd = -1;
2801da177e4SLinus Torvalds 	char c = 1;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	if (!isatty(fd))
2831da177e4SLinus Torvalds 		return;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	pid = tcgetpgrp(fd);
2862116bda6SRichard Weinberger 	if (is_skas_winch(pid, fd, port)) {
2872116bda6SRichard Weinberger 		register_winch_irq(-1, fd, -1, port, 0);
28817e05209SAl Viro 		return;
28917e05209SAl Viro 	}
29017e05209SAl Viro 
29117e05209SAl Viro 	if (pid == -1) {
2922116bda6SRichard Weinberger 		thread = winch_tramp(fd, port, &thread_fd, &stack);
29342a359e3SJeff Dike 		if (thread < 0)
29442a359e3SJeff Dike 			return;
29542a359e3SJeff Dike 
2962116bda6SRichard Weinberger 		register_winch_irq(thread_fd, fd, thread, port, stack);
2971da177e4SLinus Torvalds 
2988ca842c4SJeff Dike 		count = write(thread_fd, &c, sizeof(c));
2991da177e4SLinus Torvalds 		if (count != sizeof(c))
300e99525f9SJeff Dike 			printk(UM_KERN_ERR "register_winch : failed to write "
3018ca842c4SJeff Dike 			       "synchronization byte, err = %d\n", errno);
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds }
304