Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2002 - 2008 Jeff Dike (jdike@{addtoit,linux.intel}.com)
0004  */
0005 
0006 #include <linux/minmax.h>
0007 #include <unistd.h>
0008 #include <errno.h>
0009 #include <fcntl.h>
0010 #include <poll.h>
0011 #include <pty.h>
0012 #include <sched.h>
0013 #include <signal.h>
0014 #include <string.h>
0015 #include <kern_util.h>
0016 #include <init.h>
0017 #include <os.h>
0018 #include <sigio.h>
0019 #include <um_malloc.h>
0020 
0021 /*
0022  * Protected by sigio_lock(), also used by sigio_cleanup, which is an
0023  * exitcall.
0024  */
0025 static int write_sigio_pid = -1;
0026 static unsigned long write_sigio_stack;
0027 
0028 /*
0029  * These arrays are initialized before the sigio thread is started, and
0030  * the descriptors closed after it is killed.  So, it can't see them change.
0031  * On the UML side, they are changed under the sigio_lock.
0032  */
0033 #define SIGIO_FDS_INIT {-1, -1}
0034 
0035 static int write_sigio_fds[2] = SIGIO_FDS_INIT;
0036 static int sigio_private[2] = SIGIO_FDS_INIT;
0037 
0038 struct pollfds {
0039     struct pollfd *poll;
0040     int size;
0041     int used;
0042 };
0043 
0044 /*
0045  * Protected by sigio_lock().  Used by the sigio thread, but the UML thread
0046  * synchronizes with it.
0047  */
0048 static struct pollfds current_poll;
0049 static struct pollfds next_poll;
0050 static struct pollfds all_sigio_fds;
0051 
0052 static int write_sigio_thread(void *unused)
0053 {
0054     struct pollfds *fds;
0055     struct pollfd *p;
0056     int i, n, respond_fd;
0057     char c;
0058 
0059     os_fix_helper_signals();
0060     fds = &current_poll;
0061     while (1) {
0062         n = poll(fds->poll, fds->used, -1);
0063         if (n < 0) {
0064             if (errno == EINTR)
0065                 continue;
0066             printk(UM_KERN_ERR "write_sigio_thread : poll returned "
0067                    "%d, errno = %d\n", n, errno);
0068         }
0069         for (i = 0; i < fds->used; i++) {
0070             p = &fds->poll[i];
0071             if (p->revents == 0)
0072                 continue;
0073             if (p->fd == sigio_private[1]) {
0074                 CATCH_EINTR(n = read(sigio_private[1], &c,
0075                              sizeof(c)));
0076                 if (n != sizeof(c))
0077                     printk(UM_KERN_ERR
0078                            "write_sigio_thread : "
0079                            "read on socket failed, "
0080                            "err = %d\n", errno);
0081                 swap(current_poll, next_poll);
0082                 respond_fd = sigio_private[1];
0083             }
0084             else {
0085                 respond_fd = write_sigio_fds[1];
0086                 fds->used--;
0087                 memmove(&fds->poll[i], &fds->poll[i + 1],
0088                     (fds->used - i) * sizeof(*fds->poll));
0089             }
0090 
0091             CATCH_EINTR(n = write(respond_fd, &c, sizeof(c)));
0092             if (n != sizeof(c))
0093                 printk(UM_KERN_ERR "write_sigio_thread : "
0094                        "write on socket failed, err = %d\n",
0095                        errno);
0096         }
0097     }
0098 
0099     return 0;
0100 }
0101 
0102 static int need_poll(struct pollfds *polls, int n)
0103 {
0104     struct pollfd *new;
0105 
0106     if (n <= polls->size)
0107         return 0;
0108 
0109     new = uml_kmalloc(n * sizeof(struct pollfd), UM_GFP_ATOMIC);
0110     if (new == NULL) {
0111         printk(UM_KERN_ERR "need_poll : failed to allocate new "
0112                "pollfds\n");
0113         return -ENOMEM;
0114     }
0115 
0116     memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
0117     kfree(polls->poll);
0118 
0119     polls->poll = new;
0120     polls->size = n;
0121     return 0;
0122 }
0123 
0124 /*
0125  * Must be called with sigio_lock held, because it's needed by the marked
0126  * critical section.
0127  */
0128 static void update_thread(void)
0129 {
0130     unsigned long flags;
0131     int n;
0132     char c;
0133 
0134     flags = um_set_signals_trace(0);
0135     CATCH_EINTR(n = write(sigio_private[0], &c, sizeof(c)));
0136     if (n != sizeof(c)) {
0137         printk(UM_KERN_ERR "update_thread : write failed, err = %d\n",
0138                errno);
0139         goto fail;
0140     }
0141 
0142     CATCH_EINTR(n = read(sigio_private[0], &c, sizeof(c)));
0143     if (n != sizeof(c)) {
0144         printk(UM_KERN_ERR "update_thread : read failed, err = %d\n",
0145                errno);
0146         goto fail;
0147     }
0148 
0149     um_set_signals_trace(flags);
0150     return;
0151  fail:
0152     /* Critical section start */
0153     if (write_sigio_pid != -1) {
0154         os_kill_process(write_sigio_pid, 1);
0155         free_stack(write_sigio_stack, 0);
0156     }
0157     write_sigio_pid = -1;
0158     close(sigio_private[0]);
0159     close(sigio_private[1]);
0160     close(write_sigio_fds[0]);
0161     close(write_sigio_fds[1]);
0162     /* Critical section end */
0163     um_set_signals_trace(flags);
0164 }
0165 
0166 int __add_sigio_fd(int fd)
0167 {
0168     struct pollfd *p;
0169     int err, i, n;
0170 
0171     for (i = 0; i < all_sigio_fds.used; i++) {
0172         if (all_sigio_fds.poll[i].fd == fd)
0173             break;
0174     }
0175     if (i == all_sigio_fds.used)
0176         return -ENOSPC;
0177 
0178     p = &all_sigio_fds.poll[i];
0179 
0180     for (i = 0; i < current_poll.used; i++) {
0181         if (current_poll.poll[i].fd == fd)
0182             return 0;
0183     }
0184 
0185     n = current_poll.used;
0186     err = need_poll(&next_poll, n + 1);
0187     if (err)
0188         return err;
0189 
0190     memcpy(next_poll.poll, current_poll.poll,
0191            current_poll.used * sizeof(struct pollfd));
0192     next_poll.poll[n] = *p;
0193     next_poll.used = n + 1;
0194     update_thread();
0195 
0196     return 0;
0197 }
0198 
0199 
0200 int add_sigio_fd(int fd)
0201 {
0202     int err;
0203 
0204     sigio_lock();
0205     err = __add_sigio_fd(fd);
0206     sigio_unlock();
0207 
0208     return err;
0209 }
0210 
0211 int __ignore_sigio_fd(int fd)
0212 {
0213     struct pollfd *p;
0214     int err, i, n = 0;
0215 
0216     /*
0217      * This is called from exitcalls elsewhere in UML - if
0218      * sigio_cleanup has already run, then update_thread will hang
0219      * or fail because the thread is no longer running.
0220      */
0221     if (write_sigio_pid == -1)
0222         return -EIO;
0223 
0224     for (i = 0; i < current_poll.used; i++) {
0225         if (current_poll.poll[i].fd == fd)
0226             break;
0227     }
0228     if (i == current_poll.used)
0229         return -ENOENT;
0230 
0231     err = need_poll(&next_poll, current_poll.used - 1);
0232     if (err)
0233         return err;
0234 
0235     for (i = 0; i < current_poll.used; i++) {
0236         p = &current_poll.poll[i];
0237         if (p->fd != fd)
0238             next_poll.poll[n++] = *p;
0239     }
0240     next_poll.used = current_poll.used - 1;
0241 
0242     update_thread();
0243 
0244     return 0;
0245 }
0246 
0247 int ignore_sigio_fd(int fd)
0248 {
0249     int err;
0250 
0251     sigio_lock();
0252     err = __ignore_sigio_fd(fd);
0253     sigio_unlock();
0254 
0255     return err;
0256 }
0257 
0258 static struct pollfd *setup_initial_poll(int fd)
0259 {
0260     struct pollfd *p;
0261 
0262     p = uml_kmalloc(sizeof(struct pollfd), UM_GFP_KERNEL);
0263     if (p == NULL) {
0264         printk(UM_KERN_ERR "setup_initial_poll : failed to allocate "
0265                "poll\n");
0266         return NULL;
0267     }
0268     *p = ((struct pollfd) { .fd     = fd,
0269                 .events     = POLLIN,
0270                 .revents    = 0 });
0271     return p;
0272 }
0273 
0274 static void write_sigio_workaround(void)
0275 {
0276     struct pollfd *p;
0277     int err;
0278     int l_write_sigio_fds[2];
0279     int l_sigio_private[2];
0280     int l_write_sigio_pid;
0281 
0282     /* We call this *tons* of times - and most ones we must just fail. */
0283     sigio_lock();
0284     l_write_sigio_pid = write_sigio_pid;
0285     sigio_unlock();
0286 
0287     if (l_write_sigio_pid != -1)
0288         return;
0289 
0290     err = os_pipe(l_write_sigio_fds, 1, 1);
0291     if (err < 0) {
0292         printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 1 failed, "
0293                "err = %d\n", -err);
0294         return;
0295     }
0296     err = os_pipe(l_sigio_private, 1, 1);
0297     if (err < 0) {
0298         printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 2 failed, "
0299                "err = %d\n", -err);
0300         goto out_close1;
0301     }
0302 
0303     p = setup_initial_poll(l_sigio_private[1]);
0304     if (!p)
0305         goto out_close2;
0306 
0307     sigio_lock();
0308 
0309     /*
0310      * Did we race? Don't try to optimize this, please, it's not so likely
0311      * to happen, and no more than once at the boot.
0312      */
0313     if (write_sigio_pid != -1)
0314         goto out_free;
0315 
0316     current_poll = ((struct pollfds) { .poll    = p,
0317                        .used    = 1,
0318                        .size    = 1 });
0319 
0320     if (write_sigio_irq(l_write_sigio_fds[0]))
0321         goto out_clear_poll;
0322 
0323     memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
0324     memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
0325 
0326     write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
0327                         CLONE_FILES | CLONE_VM,
0328                         &write_sigio_stack);
0329 
0330     if (write_sigio_pid < 0)
0331         goto out_clear;
0332 
0333     sigio_unlock();
0334     return;
0335 
0336 out_clear:
0337     write_sigio_pid = -1;
0338     write_sigio_fds[0] = -1;
0339     write_sigio_fds[1] = -1;
0340     sigio_private[0] = -1;
0341     sigio_private[1] = -1;
0342 out_clear_poll:
0343     current_poll = ((struct pollfds) { .poll    = NULL,
0344                        .size    = 0,
0345                        .used    = 0 });
0346 out_free:
0347     sigio_unlock();
0348     kfree(p);
0349 out_close2:
0350     close(l_sigio_private[0]);
0351     close(l_sigio_private[1]);
0352 out_close1:
0353     close(l_write_sigio_fds[0]);
0354     close(l_write_sigio_fds[1]);
0355 }
0356 
0357 void sigio_broken(int fd)
0358 {
0359     int err;
0360 
0361     write_sigio_workaround();
0362 
0363     sigio_lock();
0364     err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
0365     if (err) {
0366         printk(UM_KERN_ERR "maybe_sigio_broken - failed to add pollfd "
0367                "for descriptor %d\n", fd);
0368         goto out;
0369     }
0370 
0371     all_sigio_fds.poll[all_sigio_fds.used++] =
0372         ((struct pollfd) { .fd      = fd,
0373                    .events  = POLLIN,
0374                    .revents     = 0 });
0375 out:
0376     sigio_unlock();
0377 }
0378 
0379 /* Changed during early boot */
0380 static int pty_output_sigio;
0381 
0382 void maybe_sigio_broken(int fd)
0383 {
0384     if (!isatty(fd))
0385         return;
0386 
0387     if (pty_output_sigio)
0388         return;
0389 
0390     sigio_broken(fd);
0391 }
0392 
0393 static void sigio_cleanup(void)
0394 {
0395     if (write_sigio_pid == -1)
0396         return;
0397 
0398     os_kill_process(write_sigio_pid, 1);
0399     free_stack(write_sigio_stack, 0);
0400     write_sigio_pid = -1;
0401 }
0402 
0403 __uml_exitcall(sigio_cleanup);
0404 
0405 /* Used as a flag during SIGIO testing early in boot */
0406 static int got_sigio;
0407 
0408 static void __init handler(int sig)
0409 {
0410     got_sigio = 1;
0411 }
0412 
0413 struct openpty_arg {
0414     int master;
0415     int slave;
0416     int err;
0417 };
0418 
0419 static void openpty_cb(void *arg)
0420 {
0421     struct openpty_arg *info = arg;
0422 
0423     info->err = 0;
0424     if (openpty(&info->master, &info->slave, NULL, NULL, NULL))
0425         info->err = -errno;
0426 }
0427 
0428 static int async_pty(int master, int slave)
0429 {
0430     int flags;
0431 
0432     flags = fcntl(master, F_GETFL);
0433     if (flags < 0)
0434         return -errno;
0435 
0436     if ((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
0437         (fcntl(master, F_SETOWN, os_getpid()) < 0))
0438         return -errno;
0439 
0440     if ((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
0441         return -errno;
0442 
0443     return 0;
0444 }
0445 
0446 static void __init check_one_sigio(void (*proc)(int, int))
0447 {
0448     struct sigaction old, new;
0449     struct openpty_arg pty = { .master = -1, .slave = -1 };
0450     int master, slave, err;
0451 
0452     initial_thread_cb(openpty_cb, &pty);
0453     if (pty.err) {
0454         printk(UM_KERN_ERR "check_one_sigio failed, errno = %d\n",
0455                -pty.err);
0456         return;
0457     }
0458 
0459     master = pty.master;
0460     slave = pty.slave;
0461 
0462     if ((master == -1) || (slave == -1)) {
0463         printk(UM_KERN_ERR "check_one_sigio failed to allocate a "
0464                "pty\n");
0465         return;
0466     }
0467 
0468     /* Not now, but complain so we now where we failed. */
0469     err = raw(master);
0470     if (err < 0) {
0471         printk(UM_KERN_ERR "check_one_sigio : raw failed, errno = %d\n",
0472               -err);
0473         return;
0474     }
0475 
0476     err = async_pty(master, slave);
0477     if (err < 0) {
0478         printk(UM_KERN_ERR "check_one_sigio : sigio_async failed, "
0479                "err = %d\n", -err);
0480         return;
0481     }
0482 
0483     if (sigaction(SIGIO, NULL, &old) < 0) {
0484         printk(UM_KERN_ERR "check_one_sigio : sigaction 1 failed, "
0485                "errno = %d\n", errno);
0486         return;
0487     }
0488 
0489     new = old;
0490     new.sa_handler = handler;
0491     if (sigaction(SIGIO, &new, NULL) < 0) {
0492         printk(UM_KERN_ERR "check_one_sigio : sigaction 2 failed, "
0493                "errno = %d\n", errno);
0494         return;
0495     }
0496 
0497     got_sigio = 0;
0498     (*proc)(master, slave);
0499 
0500     close(master);
0501     close(slave);
0502 
0503     if (sigaction(SIGIO, &old, NULL) < 0)
0504         printk(UM_KERN_ERR "check_one_sigio : sigaction 3 failed, "
0505                "errno = %d\n", errno);
0506 }
0507 
0508 static void tty_output(int master, int slave)
0509 {
0510     int n;
0511     char buf[512];
0512 
0513     printk(UM_KERN_INFO "Checking that host ptys support output SIGIO...");
0514 
0515     memset(buf, 0, sizeof(buf));
0516 
0517     while (write(master, buf, sizeof(buf)) > 0) ;
0518     if (errno != EAGAIN)
0519         printk(UM_KERN_ERR "tty_output : write failed, errno = %d\n",
0520                errno);
0521     while (((n = read(slave, buf, sizeof(buf))) > 0) &&
0522            !({ barrier(); got_sigio; }))
0523         ;
0524 
0525     if (got_sigio) {
0526         printk(UM_KERN_CONT "Yes\n");
0527         pty_output_sigio = 1;
0528     } else if (n == -EAGAIN)
0529         printk(UM_KERN_CONT "No, enabling workaround\n");
0530     else
0531         printk(UM_KERN_CONT "tty_output : read failed, err = %d\n", n);
0532 }
0533 
0534 static void __init check_sigio(void)
0535 {
0536     if ((access("/dev/ptmx", R_OK) < 0) &&
0537         (access("/dev/ptyp0", R_OK) < 0)) {
0538         printk(UM_KERN_WARNING "No pseudo-terminals available - "
0539                "skipping pty SIGIO check\n");
0540         return;
0541     }
0542     check_one_sigio(tty_output);
0543 }
0544 
0545 /* Here because it only does the SIGIO testing for now */
0546 void __init os_check_bugs(void)
0547 {
0548     check_sigio();
0549 }