xref: /OK3568_Linux_fs/kernel/arch/um/drivers/chan_user.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <stdlib.h>
7*4882a593Smuzhiyun #include <unistd.h>
8*4882a593Smuzhiyun #include <errno.h>
9*4882a593Smuzhiyun #include <sched.h>
10*4882a593Smuzhiyun #include <signal.h>
11*4882a593Smuzhiyun #include <termios.h>
12*4882a593Smuzhiyun #include <sys/ioctl.h>
13*4882a593Smuzhiyun #include "chan_user.h"
14*4882a593Smuzhiyun #include <os.h>
15*4882a593Smuzhiyun #include <um_malloc.h>
16*4882a593Smuzhiyun 
generic_close(int fd,void * unused)17*4882a593Smuzhiyun void generic_close(int fd, void *unused)
18*4882a593Smuzhiyun {
19*4882a593Smuzhiyun 	close(fd);
20*4882a593Smuzhiyun }
21*4882a593Smuzhiyun 
generic_read(int fd,char * c_out,void * unused)22*4882a593Smuzhiyun int generic_read(int fd, char *c_out, void *unused)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun 	int n;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun 	n = read(fd, c_out, sizeof(*c_out));
27*4882a593Smuzhiyun 	if (n > 0)
28*4882a593Smuzhiyun 		return n;
29*4882a593Smuzhiyun 	else if (n == 0)
30*4882a593Smuzhiyun 		return -EIO;
31*4882a593Smuzhiyun 	else if (errno == EAGAIN)
32*4882a593Smuzhiyun 		return 0;
33*4882a593Smuzhiyun 	return -errno;
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /* XXX Trivial wrapper around write */
37*4882a593Smuzhiyun 
generic_write(int fd,const char * buf,int n,void * unused)38*4882a593Smuzhiyun int generic_write(int fd, const char *buf, int n, void *unused)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	int err;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	err = write(fd, buf, n);
43*4882a593Smuzhiyun 	if (err > 0)
44*4882a593Smuzhiyun 		return err;
45*4882a593Smuzhiyun 	else if (errno == EAGAIN)
46*4882a593Smuzhiyun 		return 0;
47*4882a593Smuzhiyun 	else if (err == 0)
48*4882a593Smuzhiyun 		return -EIO;
49*4882a593Smuzhiyun 	return -errno;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
generic_window_size(int fd,void * unused,unsigned short * rows_out,unsigned short * cols_out)52*4882a593Smuzhiyun int generic_window_size(int fd, void *unused, unsigned short *rows_out,
53*4882a593Smuzhiyun 			unsigned short *cols_out)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	struct winsize size;
56*4882a593Smuzhiyun 	int ret;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	if (ioctl(fd, TIOCGWINSZ, &size) < 0)
59*4882a593Smuzhiyun 		return -errno;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	ret = ((*rows_out != size.ws_row) || (*cols_out != size.ws_col));
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	*rows_out = size.ws_row;
64*4882a593Smuzhiyun 	*cols_out = size.ws_col;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	return ret;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
generic_free(void * data)69*4882a593Smuzhiyun void generic_free(void *data)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	kfree(data);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
generic_console_write(int fd,const char * buf,int n)74*4882a593Smuzhiyun int generic_console_write(int fd, const char *buf, int n)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	sigset_t old, no_sigio;
77*4882a593Smuzhiyun 	struct termios save, new;
78*4882a593Smuzhiyun 	int err;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	if (isatty(fd)) {
81*4882a593Smuzhiyun 		sigemptyset(&no_sigio);
82*4882a593Smuzhiyun 		sigaddset(&no_sigio, SIGIO);
83*4882a593Smuzhiyun 		if (sigprocmask(SIG_BLOCK, &no_sigio, &old))
84*4882a593Smuzhiyun 			goto error;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 		CATCH_EINTR(err = tcgetattr(fd, &save));
87*4882a593Smuzhiyun 		if (err)
88*4882a593Smuzhiyun 			goto error;
89*4882a593Smuzhiyun 		new = save;
90*4882a593Smuzhiyun 		/*
91*4882a593Smuzhiyun 		 * The terminal becomes a bit less raw, to handle \n also as
92*4882a593Smuzhiyun 		 * "Carriage Return", not only as "New Line". Otherwise, the new
93*4882a593Smuzhiyun 		 * line won't start at the first column.
94*4882a593Smuzhiyun 		 */
95*4882a593Smuzhiyun 		new.c_oflag |= OPOST;
96*4882a593Smuzhiyun 		CATCH_EINTR(err = tcsetattr(fd, TCSAFLUSH, &new));
97*4882a593Smuzhiyun 		if (err)
98*4882a593Smuzhiyun 			goto error;
99*4882a593Smuzhiyun 	}
100*4882a593Smuzhiyun 	err = generic_write(fd, buf, n, NULL);
101*4882a593Smuzhiyun 	/*
102*4882a593Smuzhiyun 	 * Restore raw mode, in any case; we *must* ignore any error apart
103*4882a593Smuzhiyun 	 * EINTR, except for debug.
104*4882a593Smuzhiyun 	 */
105*4882a593Smuzhiyun 	if (isatty(fd)) {
106*4882a593Smuzhiyun 		CATCH_EINTR(tcsetattr(fd, TCSAFLUSH, &save));
107*4882a593Smuzhiyun 		sigprocmask(SIG_SETMASK, &old, NULL);
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	return err;
111*4882a593Smuzhiyun error:
112*4882a593Smuzhiyun 	return -errno;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /*
116*4882a593Smuzhiyun  * UML SIGWINCH handling
117*4882a593Smuzhiyun  *
118*4882a593Smuzhiyun  * The point of this is to handle SIGWINCH on consoles which have host
119*4882a593Smuzhiyun  * ttys and relay them inside UML to whatever might be running on the
120*4882a593Smuzhiyun  * console and cares about the window size (since SIGWINCH notifies
121*4882a593Smuzhiyun  * about terminal size changes).
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  * So, we have a separate thread for each host tty attached to a UML
124*4882a593Smuzhiyun  * device (side-issue - I'm annoyed that one thread can't have
125*4882a593Smuzhiyun  * multiple controlling ttys for the purpose of handling SIGWINCH, but
126*4882a593Smuzhiyun  * I imagine there are other reasons that doesn't make any sense).
127*4882a593Smuzhiyun  *
128*4882a593Smuzhiyun  * SIGWINCH can't be received synchronously, so you have to set up to
129*4882a593Smuzhiyun  * receive it as a signal.  That being the case, if you are going to
130*4882a593Smuzhiyun  * wait for it, it is convenient to sit in sigsuspend() and wait for
131*4882a593Smuzhiyun  * the signal to bounce you out of it (see below for how we make sure
132*4882a593Smuzhiyun  * to exit only on SIGWINCH).
133*4882a593Smuzhiyun  */
134*4882a593Smuzhiyun 
winch_handler(int sig)135*4882a593Smuzhiyun static void winch_handler(int sig)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun struct winch_data {
140*4882a593Smuzhiyun 	int pty_fd;
141*4882a593Smuzhiyun 	int pipe_fd;
142*4882a593Smuzhiyun };
143*4882a593Smuzhiyun 
winch_thread(void * arg)144*4882a593Smuzhiyun static int winch_thread(void *arg)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	struct winch_data *data = arg;
147*4882a593Smuzhiyun 	sigset_t sigs;
148*4882a593Smuzhiyun 	int pty_fd, pipe_fd;
149*4882a593Smuzhiyun 	int count;
150*4882a593Smuzhiyun 	char c = 1;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	pty_fd = data->pty_fd;
153*4882a593Smuzhiyun 	pipe_fd = data->pipe_fd;
154*4882a593Smuzhiyun 	count = write(pipe_fd, &c, sizeof(c));
155*4882a593Smuzhiyun 	if (count != sizeof(c))
156*4882a593Smuzhiyun 		printk(UM_KERN_ERR "winch_thread : failed to write "
157*4882a593Smuzhiyun 		       "synchronization byte, err = %d\n", -count);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/*
160*4882a593Smuzhiyun 	 * We are not using SIG_IGN on purpose, so don't fix it as I thought to
161*4882a593Smuzhiyun 	 * do! If using SIG_IGN, the sigsuspend() call below would not stop on
162*4882a593Smuzhiyun 	 * SIGWINCH.
163*4882a593Smuzhiyun 	 */
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	signal(SIGWINCH, winch_handler);
166*4882a593Smuzhiyun 	sigfillset(&sigs);
167*4882a593Smuzhiyun 	/* Block all signals possible. */
168*4882a593Smuzhiyun 	if (sigprocmask(SIG_SETMASK, &sigs, NULL) < 0) {
169*4882a593Smuzhiyun 		printk(UM_KERN_ERR "winch_thread : sigprocmask failed, "
170*4882a593Smuzhiyun 		       "errno = %d\n", errno);
171*4882a593Smuzhiyun 		exit(1);
172*4882a593Smuzhiyun 	}
173*4882a593Smuzhiyun 	/* In sigsuspend(), block anything else than SIGWINCH. */
174*4882a593Smuzhiyun 	sigdelset(&sigs, SIGWINCH);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	if (setsid() < 0) {
177*4882a593Smuzhiyun 		printk(UM_KERN_ERR "winch_thread : setsid failed, errno = %d\n",
178*4882a593Smuzhiyun 		       errno);
179*4882a593Smuzhiyun 		exit(1);
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	if (ioctl(pty_fd, TIOCSCTTY, 0) < 0) {
183*4882a593Smuzhiyun 		printk(UM_KERN_ERR "winch_thread : TIOCSCTTY failed on "
184*4882a593Smuzhiyun 		       "fd %d err = %d\n", pty_fd, errno);
185*4882a593Smuzhiyun 		exit(1);
186*4882a593Smuzhiyun 	}
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	if (tcsetpgrp(pty_fd, os_getpid()) < 0) {
189*4882a593Smuzhiyun 		printk(UM_KERN_ERR "winch_thread : tcsetpgrp failed on "
190*4882a593Smuzhiyun 		       "fd %d err = %d\n", pty_fd, errno);
191*4882a593Smuzhiyun 		exit(1);
192*4882a593Smuzhiyun 	}
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	/*
195*4882a593Smuzhiyun 	 * These are synchronization calls between various UML threads on the
196*4882a593Smuzhiyun 	 * host - since they are not different kernel threads, we cannot use
197*4882a593Smuzhiyun 	 * kernel semaphores. We don't use SysV semaphores because they are
198*4882a593Smuzhiyun 	 * persistent.
199*4882a593Smuzhiyun 	 */
200*4882a593Smuzhiyun 	count = read(pipe_fd, &c, sizeof(c));
201*4882a593Smuzhiyun 	if (count != sizeof(c))
202*4882a593Smuzhiyun 		printk(UM_KERN_ERR "winch_thread : failed to read "
203*4882a593Smuzhiyun 		       "synchronization byte, err = %d\n", errno);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	while(1) {
206*4882a593Smuzhiyun 		/*
207*4882a593Smuzhiyun 		 * This will be interrupted by SIGWINCH only, since
208*4882a593Smuzhiyun 		 * other signals are blocked.
209*4882a593Smuzhiyun 		 */
210*4882a593Smuzhiyun 		sigsuspend(&sigs);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 		count = write(pipe_fd, &c, sizeof(c));
213*4882a593Smuzhiyun 		if (count != sizeof(c))
214*4882a593Smuzhiyun 			printk(UM_KERN_ERR "winch_thread : write failed, "
215*4882a593Smuzhiyun 			       "err = %d\n", errno);
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun 
winch_tramp(int fd,struct tty_port * port,int * fd_out,unsigned long * stack_out)219*4882a593Smuzhiyun static int winch_tramp(int fd, struct tty_port *port, int *fd_out,
220*4882a593Smuzhiyun 		       unsigned long *stack_out)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	struct winch_data data;
223*4882a593Smuzhiyun 	int fds[2], n, err, pid;
224*4882a593Smuzhiyun 	char c;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	err = os_pipe(fds, 1, 1);
227*4882a593Smuzhiyun 	if (err < 0) {
228*4882a593Smuzhiyun 		printk(UM_KERN_ERR "winch_tramp : os_pipe failed, err = %d\n",
229*4882a593Smuzhiyun 		       -err);
230*4882a593Smuzhiyun 		goto out;
231*4882a593Smuzhiyun 	}
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	data = ((struct winch_data) { .pty_fd 		= fd,
234*4882a593Smuzhiyun 				      .pipe_fd 		= fds[1] } );
235*4882a593Smuzhiyun 	/*
236*4882a593Smuzhiyun 	 * CLONE_FILES so this thread doesn't hold open files which are open
237*4882a593Smuzhiyun 	 * now, but later closed in a different thread.  This is a
238*4882a593Smuzhiyun 	 * problem with /dev/net/tun, which if held open by this
239*4882a593Smuzhiyun 	 * thread, prevents the TUN/TAP device from being reused.
240*4882a593Smuzhiyun 	 */
241*4882a593Smuzhiyun 	pid = run_helper_thread(winch_thread, &data, CLONE_FILES, stack_out);
242*4882a593Smuzhiyun 	if (pid < 0) {
243*4882a593Smuzhiyun 		err = pid;
244*4882a593Smuzhiyun 		printk(UM_KERN_ERR "fork of winch_thread failed - errno = %d\n",
245*4882a593Smuzhiyun 		       -err);
246*4882a593Smuzhiyun 		goto out_close;
247*4882a593Smuzhiyun 	}
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	*fd_out = fds[0];
250*4882a593Smuzhiyun 	n = read(fds[0], &c, sizeof(c));
251*4882a593Smuzhiyun 	if (n != sizeof(c)) {
252*4882a593Smuzhiyun 		printk(UM_KERN_ERR "winch_tramp : failed to read "
253*4882a593Smuzhiyun 		       "synchronization byte\n");
254*4882a593Smuzhiyun 		printk(UM_KERN_ERR "read failed, err = %d\n", errno);
255*4882a593Smuzhiyun 		printk(UM_KERN_ERR "fd %d will not support SIGWINCH\n", fd);
256*4882a593Smuzhiyun 		err = -EINVAL;
257*4882a593Smuzhiyun 		goto out_close;
258*4882a593Smuzhiyun 	}
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	err = os_set_fd_block(*fd_out, 0);
261*4882a593Smuzhiyun 	if (err) {
262*4882a593Smuzhiyun 		printk(UM_KERN_ERR "winch_tramp: failed to set thread_fd "
263*4882a593Smuzhiyun 		       "non-blocking.\n");
264*4882a593Smuzhiyun 		goto out_close;
265*4882a593Smuzhiyun 	}
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	return pid;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun  out_close:
270*4882a593Smuzhiyun 	close(fds[1]);
271*4882a593Smuzhiyun 	close(fds[0]);
272*4882a593Smuzhiyun  out:
273*4882a593Smuzhiyun 	return err;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
register_winch(int fd,struct tty_port * port)276*4882a593Smuzhiyun void register_winch(int fd, struct tty_port *port)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	unsigned long stack;
279*4882a593Smuzhiyun 	int pid, thread, count, thread_fd = -1;
280*4882a593Smuzhiyun 	char c = 1;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	if (!isatty(fd))
283*4882a593Smuzhiyun 		return;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	pid = tcgetpgrp(fd);
286*4882a593Smuzhiyun 	if (is_skas_winch(pid, fd, port)) {
287*4882a593Smuzhiyun 		register_winch_irq(-1, fd, -1, port, 0);
288*4882a593Smuzhiyun 		return;
289*4882a593Smuzhiyun 	}
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	if (pid == -1) {
292*4882a593Smuzhiyun 		thread = winch_tramp(fd, port, &thread_fd, &stack);
293*4882a593Smuzhiyun 		if (thread < 0)
294*4882a593Smuzhiyun 			return;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 		register_winch_irq(thread_fd, fd, thread, port, stack);
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 		count = write(thread_fd, &c, sizeof(c));
299*4882a593Smuzhiyun 		if (count != sizeof(c))
300*4882a593Smuzhiyun 			printk(UM_KERN_ERR "register_winch : failed to write "
301*4882a593Smuzhiyun 			       "synchronization byte, err = %d\n", errno);
302*4882a593Smuzhiyun 	}
303*4882a593Smuzhiyun }
304