sigio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2002 - 2008 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  4. */
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <poll.h>
  9. #include <pty.h>
  10. #include <sched.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <sys/epoll.h>
  14. #include <asm/unistd.h>
  15. #include <kern_util.h>
  16. #include <init.h>
  17. #include <os.h>
  18. #include <sigio.h>
  19. #include <um_malloc.h>
  20. /*
  21. * Protected by sigio_lock(), also used by sigio_cleanup, which is an
  22. * exitcall.
  23. */
  24. static struct os_helper_thread *write_sigio_td;
  25. static int epollfd = -1;
  26. #define MAX_EPOLL_EVENTS 64
  27. static struct epoll_event epoll_events[MAX_EPOLL_EVENTS];
  28. static void *write_sigio_thread(void *unused)
  29. {
  30. int pid = getpid();
  31. int r;
  32. os_fix_helper_thread_signals();
  33. while (1) {
  34. r = epoll_wait(epollfd, epoll_events, MAX_EPOLL_EVENTS, -1);
  35. if (r < 0) {
  36. if (errno == EINTR)
  37. continue;
  38. printk(UM_KERN_ERR "%s: epoll_wait failed, errno = %d\n",
  39. __func__, errno);
  40. }
  41. CATCH_EINTR(r = syscall(__NR_tgkill, pid, pid, SIGIO));
  42. if (r < 0)
  43. printk(UM_KERN_ERR "%s: tgkill failed, errno = %d\n",
  44. __func__, errno);
  45. }
  46. return NULL;
  47. }
  48. int __add_sigio_fd(int fd)
  49. {
  50. struct epoll_event event = {
  51. .data.fd = fd,
  52. .events = EPOLLIN | EPOLLET,
  53. };
  54. int r;
  55. CATCH_EINTR(r = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event));
  56. return r < 0 ? -errno : 0;
  57. }
  58. int add_sigio_fd(int fd)
  59. {
  60. int err;
  61. sigio_lock();
  62. err = __add_sigio_fd(fd);
  63. sigio_unlock();
  64. return err;
  65. }
  66. int __ignore_sigio_fd(int fd)
  67. {
  68. struct epoll_event event;
  69. int r;
  70. CATCH_EINTR(r = epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, &event));
  71. return r < 0 ? -errno : 0;
  72. }
  73. int ignore_sigio_fd(int fd)
  74. {
  75. int err;
  76. sigio_lock();
  77. err = __ignore_sigio_fd(fd);
  78. sigio_unlock();
  79. return err;
  80. }
  81. static void write_sigio_workaround(void)
  82. {
  83. int err;
  84. sigio_lock();
  85. if (write_sigio_td)
  86. goto out;
  87. epollfd = epoll_create(MAX_EPOLL_EVENTS);
  88. if (epollfd < 0) {
  89. printk(UM_KERN_ERR "%s: epoll_create failed, errno = %d\n",
  90. __func__, errno);
  91. goto out;
  92. }
  93. err = os_run_helper_thread(&write_sigio_td, write_sigio_thread, NULL);
  94. if (err < 0) {
  95. printk(UM_KERN_ERR "%s: os_run_helper_thread failed, errno = %d\n",
  96. __func__, -err);
  97. close(epollfd);
  98. epollfd = -1;
  99. goto out;
  100. }
  101. out:
  102. sigio_unlock();
  103. }
  104. void sigio_broken(void)
  105. {
  106. write_sigio_workaround();
  107. }
  108. /* Changed during early boot */
  109. static int pty_output_sigio;
  110. void maybe_sigio_broken(int fd)
  111. {
  112. if (!isatty(fd))
  113. return;
  114. if (pty_output_sigio)
  115. return;
  116. sigio_broken();
  117. }
  118. static void sigio_cleanup(void)
  119. {
  120. if (!write_sigio_td)
  121. return;
  122. os_kill_helper_thread(write_sigio_td);
  123. write_sigio_td = NULL;
  124. }
  125. __uml_exitcall(sigio_cleanup);
  126. /* Used as a flag during SIGIO testing early in boot */
  127. static int got_sigio;
  128. static void __init handler(int sig)
  129. {
  130. got_sigio = 1;
  131. }
  132. struct openpty_arg {
  133. int master;
  134. int slave;
  135. int err;
  136. };
  137. static void openpty_cb(void *arg)
  138. {
  139. struct openpty_arg *info = arg;
  140. info->err = 0;
  141. if (openpty(&info->master, &info->slave, NULL, NULL, NULL))
  142. info->err = -errno;
  143. }
  144. static int async_pty(int master, int slave)
  145. {
  146. int flags;
  147. flags = fcntl(master, F_GETFL);
  148. if (flags < 0)
  149. return -errno;
  150. if ((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
  151. (fcntl(master, F_SETOWN, os_getpid()) < 0))
  152. return -errno;
  153. if ((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
  154. return -errno;
  155. return 0;
  156. }
  157. static void __init check_one_sigio(void (*proc)(int, int))
  158. {
  159. struct sigaction old, new;
  160. struct openpty_arg pty = { .master = -1, .slave = -1 };
  161. int master, slave, err;
  162. initial_thread_cb(openpty_cb, &pty);
  163. if (pty.err) {
  164. printk(UM_KERN_ERR "check_one_sigio failed, errno = %d\n",
  165. -pty.err);
  166. return;
  167. }
  168. master = pty.master;
  169. slave = pty.slave;
  170. if ((master == -1) || (slave == -1)) {
  171. printk(UM_KERN_ERR "check_one_sigio failed to allocate a "
  172. "pty\n");
  173. return;
  174. }
  175. /* Not now, but complain so we now where we failed. */
  176. err = raw(master);
  177. if (err < 0) {
  178. printk(UM_KERN_ERR "check_one_sigio : raw failed, errno = %d\n",
  179. -err);
  180. return;
  181. }
  182. err = async_pty(master, slave);
  183. if (err < 0) {
  184. printk(UM_KERN_ERR "check_one_sigio : sigio_async failed, "
  185. "err = %d\n", -err);
  186. return;
  187. }
  188. if (sigaction(SIGIO, NULL, &old) < 0) {
  189. printk(UM_KERN_ERR "check_one_sigio : sigaction 1 failed, "
  190. "errno = %d\n", errno);
  191. return;
  192. }
  193. new = old;
  194. new.sa_handler = handler;
  195. if (sigaction(SIGIO, &new, NULL) < 0) {
  196. printk(UM_KERN_ERR "check_one_sigio : sigaction 2 failed, "
  197. "errno = %d\n", errno);
  198. return;
  199. }
  200. got_sigio = 0;
  201. (*proc)(master, slave);
  202. close(master);
  203. close(slave);
  204. if (sigaction(SIGIO, &old, NULL) < 0)
  205. printk(UM_KERN_ERR "check_one_sigio : sigaction 3 failed, "
  206. "errno = %d\n", errno);
  207. }
  208. static void tty_output(int master, int slave)
  209. {
  210. int n;
  211. char buf[512];
  212. printk(UM_KERN_INFO "Checking that host ptys support output SIGIO...");
  213. memset(buf, 0, sizeof(buf));
  214. while (write(master, buf, sizeof(buf)) > 0) ;
  215. if (errno != EAGAIN)
  216. printk(UM_KERN_ERR "tty_output : write failed, errno = %d\n",
  217. errno);
  218. while (((n = read(slave, buf, sizeof(buf))) > 0) &&
  219. !({ barrier(); got_sigio; }))
  220. ;
  221. if (got_sigio) {
  222. printk(UM_KERN_CONT "Yes\n");
  223. pty_output_sigio = 1;
  224. } else if (n == -EAGAIN)
  225. printk(UM_KERN_CONT "No, enabling workaround\n");
  226. else
  227. printk(UM_KERN_CONT "tty_output : read failed, err = %d\n", n);
  228. }
  229. static void __init check_sigio(void)
  230. {
  231. if ((access("/dev/ptmx", R_OK) < 0) &&
  232. (access("/dev/ptyp0", R_OK) < 0)) {
  233. printk(UM_KERN_WARNING "No pseudo-terminals available - "
  234. "skipping pty SIGIO check\n");
  235. return;
  236. }
  237. check_one_sigio(tty_output);
  238. }
  239. /* Here because it only does the SIGIO testing for now */
  240. void __init os_check_bugs(void)
  241. {
  242. check_sigio();
  243. }