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