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