sigio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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 <kern_util.h>
  14. #include <init.h>
  15. #include <os.h>
  16. #include <sigio.h>
  17. #include <um_malloc.h>
  18. /*
  19. * Protected by sigio_lock(), also used by sigio_cleanup, which is an
  20. * exitcall.
  21. */
  22. static int write_sigio_pid = -1;
  23. static unsigned long write_sigio_stack;
  24. /*
  25. * These arrays are initialized before the sigio thread is started, and
  26. * the descriptors closed after it is killed. So, it can't see them change.
  27. * On the UML side, they are changed under the sigio_lock.
  28. */
  29. #define SIGIO_FDS_INIT {-1, -1}
  30. static int write_sigio_fds[2] = SIGIO_FDS_INIT;
  31. static int sigio_private[2] = SIGIO_FDS_INIT;
  32. struct pollfds {
  33. struct pollfd *poll;
  34. int size;
  35. int used;
  36. };
  37. /*
  38. * Protected by sigio_lock(). Used by the sigio thread, but the UML thread
  39. * synchronizes with it.
  40. */
  41. static struct pollfds current_poll;
  42. static struct pollfds next_poll;
  43. static struct pollfds all_sigio_fds;
  44. static int write_sigio_thread(void *unused)
  45. {
  46. struct pollfds *fds, tmp;
  47. struct pollfd *p;
  48. int i, n, respond_fd;
  49. char c;
  50. os_fix_helper_signals();
  51. fds = &current_poll;
  52. while (1) {
  53. n = poll(fds->poll, fds->used, -1);
  54. if (n < 0) {
  55. if (errno == EINTR)
  56. continue;
  57. printk(UM_KERN_ERR "write_sigio_thread : poll returned "
  58. "%d, errno = %d\n", n, errno);
  59. }
  60. for (i = 0; i < fds->used; i++) {
  61. p = &fds->poll[i];
  62. if (p->revents == 0)
  63. continue;
  64. if (p->fd == sigio_private[1]) {
  65. CATCH_EINTR(n = read(sigio_private[1], &c,
  66. sizeof(c)));
  67. if (n != sizeof(c))
  68. printk(UM_KERN_ERR
  69. "write_sigio_thread : "
  70. "read on socket failed, "
  71. "err = %d\n", errno);
  72. tmp = current_poll;
  73. current_poll = next_poll;
  74. next_poll = tmp;
  75. respond_fd = sigio_private[1];
  76. }
  77. else {
  78. respond_fd = write_sigio_fds[1];
  79. fds->used--;
  80. memmove(&fds->poll[i], &fds->poll[i + 1],
  81. (fds->used - i) * sizeof(*fds->poll));
  82. }
  83. CATCH_EINTR(n = write(respond_fd, &c, sizeof(c)));
  84. if (n != sizeof(c))
  85. printk(UM_KERN_ERR "write_sigio_thread : "
  86. "write on socket failed, err = %d\n",
  87. errno);
  88. }
  89. }
  90. return 0;
  91. }
  92. static int need_poll(struct pollfds *polls, int n)
  93. {
  94. struct pollfd *new;
  95. if (n <= polls->size)
  96. return 0;
  97. new = uml_kmalloc(n * sizeof(struct pollfd), UM_GFP_ATOMIC);
  98. if (new == NULL) {
  99. printk(UM_KERN_ERR "need_poll : failed to allocate new "
  100. "pollfds\n");
  101. return -ENOMEM;
  102. }
  103. memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
  104. kfree(polls->poll);
  105. polls->poll = new;
  106. polls->size = n;
  107. return 0;
  108. }
  109. /*
  110. * Must be called with sigio_lock held, because it's needed by the marked
  111. * critical section.
  112. */
  113. static void update_thread(void)
  114. {
  115. unsigned long flags;
  116. int n;
  117. char c;
  118. flags = um_set_signals_trace(0);
  119. CATCH_EINTR(n = write(sigio_private[0], &c, sizeof(c)));
  120. if (n != sizeof(c)) {
  121. printk(UM_KERN_ERR "update_thread : write failed, err = %d\n",
  122. errno);
  123. goto fail;
  124. }
  125. CATCH_EINTR(n = read(sigio_private[0], &c, sizeof(c)));
  126. if (n != sizeof(c)) {
  127. printk(UM_KERN_ERR "update_thread : read failed, err = %d\n",
  128. errno);
  129. goto fail;
  130. }
  131. um_set_signals_trace(flags);
  132. return;
  133. fail:
  134. /* Critical section start */
  135. if (write_sigio_pid != -1) {
  136. os_kill_process(write_sigio_pid, 1);
  137. free_stack(write_sigio_stack, 0);
  138. }
  139. write_sigio_pid = -1;
  140. close(sigio_private[0]);
  141. close(sigio_private[1]);
  142. close(write_sigio_fds[0]);
  143. close(write_sigio_fds[1]);
  144. /* Critical section end */
  145. um_set_signals_trace(flags);
  146. }
  147. int __add_sigio_fd(int fd)
  148. {
  149. struct pollfd *p;
  150. int err, i, n;
  151. for (i = 0; i < all_sigio_fds.used; i++) {
  152. if (all_sigio_fds.poll[i].fd == fd)
  153. break;
  154. }
  155. if (i == all_sigio_fds.used)
  156. return -ENOSPC;
  157. p = &all_sigio_fds.poll[i];
  158. for (i = 0; i < current_poll.used; i++) {
  159. if (current_poll.poll[i].fd == fd)
  160. return 0;
  161. }
  162. n = current_poll.used;
  163. err = need_poll(&next_poll, n + 1);
  164. if (err)
  165. return err;
  166. memcpy(next_poll.poll, current_poll.poll,
  167. current_poll.used * sizeof(struct pollfd));
  168. next_poll.poll[n] = *p;
  169. next_poll.used = n + 1;
  170. update_thread();
  171. return 0;
  172. }
  173. int add_sigio_fd(int fd)
  174. {
  175. int err;
  176. sigio_lock();
  177. err = __add_sigio_fd(fd);
  178. sigio_unlock();
  179. return err;
  180. }
  181. int __ignore_sigio_fd(int fd)
  182. {
  183. struct pollfd *p;
  184. int err, i, n = 0;
  185. /*
  186. * This is called from exitcalls elsewhere in UML - if
  187. * sigio_cleanup has already run, then update_thread will hang
  188. * or fail because the thread is no longer running.
  189. */
  190. if (write_sigio_pid == -1)
  191. return -EIO;
  192. for (i = 0; i < current_poll.used; i++) {
  193. if (current_poll.poll[i].fd == fd)
  194. break;
  195. }
  196. if (i == current_poll.used)
  197. return -ENOENT;
  198. err = need_poll(&next_poll, current_poll.used - 1);
  199. if (err)
  200. return err;
  201. for (i = 0; i < current_poll.used; i++) {
  202. p = &current_poll.poll[i];
  203. if (p->fd != fd)
  204. next_poll.poll[n++] = *p;
  205. }
  206. next_poll.used = current_poll.used - 1;
  207. update_thread();
  208. return 0;
  209. }
  210. int ignore_sigio_fd(int fd)
  211. {
  212. int err;
  213. sigio_lock();
  214. err = __ignore_sigio_fd(fd);
  215. sigio_unlock();
  216. return err;
  217. }
  218. static struct pollfd *setup_initial_poll(int fd)
  219. {
  220. struct pollfd *p;
  221. p = uml_kmalloc(sizeof(struct pollfd), UM_GFP_KERNEL);
  222. if (p == NULL) {
  223. printk(UM_KERN_ERR "setup_initial_poll : failed to allocate "
  224. "poll\n");
  225. return NULL;
  226. }
  227. *p = ((struct pollfd) { .fd = fd,
  228. .events = POLLIN,
  229. .revents = 0 });
  230. return p;
  231. }
  232. static void write_sigio_workaround(void)
  233. {
  234. struct pollfd *p;
  235. int err;
  236. int l_write_sigio_fds[2];
  237. int l_sigio_private[2];
  238. int l_write_sigio_pid;
  239. /* We call this *tons* of times - and most ones we must just fail. */
  240. sigio_lock();
  241. l_write_sigio_pid = write_sigio_pid;
  242. sigio_unlock();
  243. if (l_write_sigio_pid != -1)
  244. return;
  245. err = os_pipe(l_write_sigio_fds, 1, 1);
  246. if (err < 0) {
  247. printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 1 failed, "
  248. "err = %d\n", -err);
  249. return;
  250. }
  251. err = os_pipe(l_sigio_private, 1, 1);
  252. if (err < 0) {
  253. printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 2 failed, "
  254. "err = %d\n", -err);
  255. goto out_close1;
  256. }
  257. p = setup_initial_poll(l_sigio_private[1]);
  258. if (!p)
  259. goto out_close2;
  260. sigio_lock();
  261. /*
  262. * Did we race? Don't try to optimize this, please, it's not so likely
  263. * to happen, and no more than once at the boot.
  264. */
  265. if (write_sigio_pid != -1)
  266. goto out_free;
  267. current_poll = ((struct pollfds) { .poll = p,
  268. .used = 1,
  269. .size = 1 });
  270. if (write_sigio_irq(l_write_sigio_fds[0]))
  271. goto out_clear_poll;
  272. memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
  273. memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
  274. write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
  275. CLONE_FILES | CLONE_VM,
  276. &write_sigio_stack);
  277. if (write_sigio_pid < 0)
  278. goto out_clear;
  279. sigio_unlock();
  280. return;
  281. out_clear:
  282. write_sigio_pid = -1;
  283. write_sigio_fds[0] = -1;
  284. write_sigio_fds[1] = -1;
  285. sigio_private[0] = -1;
  286. sigio_private[1] = -1;
  287. out_clear_poll:
  288. current_poll = ((struct pollfds) { .poll = NULL,
  289. .size = 0,
  290. .used = 0 });
  291. out_free:
  292. sigio_unlock();
  293. kfree(p);
  294. out_close2:
  295. close(l_sigio_private[0]);
  296. close(l_sigio_private[1]);
  297. out_close1:
  298. close(l_write_sigio_fds[0]);
  299. close(l_write_sigio_fds[1]);
  300. }
  301. void sigio_broken(int fd)
  302. {
  303. int err;
  304. write_sigio_workaround();
  305. sigio_lock();
  306. err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
  307. if (err) {
  308. printk(UM_KERN_ERR "maybe_sigio_broken - failed to add pollfd "
  309. "for descriptor %d\n", fd);
  310. goto out;
  311. }
  312. all_sigio_fds.poll[all_sigio_fds.used++] =
  313. ((struct pollfd) { .fd = fd,
  314. .events = POLLIN,
  315. .revents = 0 });
  316. out:
  317. sigio_unlock();
  318. }
  319. /* Changed during early boot */
  320. static int pty_output_sigio;
  321. void maybe_sigio_broken(int fd)
  322. {
  323. if (!isatty(fd))
  324. return;
  325. if (pty_output_sigio)
  326. return;
  327. sigio_broken(fd);
  328. }
  329. static void sigio_cleanup(void)
  330. {
  331. if (write_sigio_pid == -1)
  332. return;
  333. os_kill_process(write_sigio_pid, 1);
  334. free_stack(write_sigio_stack, 0);
  335. write_sigio_pid = -1;
  336. }
  337. __uml_exitcall(sigio_cleanup);
  338. /* Used as a flag during SIGIO testing early in boot */
  339. static int got_sigio;
  340. static void __init handler(int sig)
  341. {
  342. got_sigio = 1;
  343. }
  344. struct openpty_arg {
  345. int master;
  346. int slave;
  347. int err;
  348. };
  349. static void openpty_cb(void *arg)
  350. {
  351. struct openpty_arg *info = arg;
  352. info->err = 0;
  353. if (openpty(&info->master, &info->slave, NULL, NULL, NULL))
  354. info->err = -errno;
  355. }
  356. static int async_pty(int master, int slave)
  357. {
  358. int flags;
  359. flags = fcntl(master, F_GETFL);
  360. if (flags < 0)
  361. return -errno;
  362. if ((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
  363. (fcntl(master, F_SETOWN, os_getpid()) < 0))
  364. return -errno;
  365. if ((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
  366. return -errno;
  367. return 0;
  368. }
  369. static void __init check_one_sigio(void (*proc)(int, int))
  370. {
  371. struct sigaction old, new;
  372. struct openpty_arg pty = { .master = -1, .slave = -1 };
  373. int master, slave, err;
  374. initial_thread_cb(openpty_cb, &pty);
  375. if (pty.err) {
  376. printk(UM_KERN_ERR "check_one_sigio failed, errno = %d\n",
  377. -pty.err);
  378. return;
  379. }
  380. master = pty.master;
  381. slave = pty.slave;
  382. if ((master == -1) || (slave == -1)) {
  383. printk(UM_KERN_ERR "check_one_sigio failed to allocate a "
  384. "pty\n");
  385. return;
  386. }
  387. /* Not now, but complain so we now where we failed. */
  388. err = raw(master);
  389. if (err < 0) {
  390. printk(UM_KERN_ERR "check_one_sigio : raw failed, errno = %d\n",
  391. -err);
  392. return;
  393. }
  394. err = async_pty(master, slave);
  395. if (err < 0) {
  396. printk(UM_KERN_ERR "check_one_sigio : sigio_async failed, "
  397. "err = %d\n", -err);
  398. return;
  399. }
  400. if (sigaction(SIGIO, NULL, &old) < 0) {
  401. printk(UM_KERN_ERR "check_one_sigio : sigaction 1 failed, "
  402. "errno = %d\n", errno);
  403. return;
  404. }
  405. new = old;
  406. new.sa_handler = handler;
  407. if (sigaction(SIGIO, &new, NULL) < 0) {
  408. printk(UM_KERN_ERR "check_one_sigio : sigaction 2 failed, "
  409. "errno = %d\n", errno);
  410. return;
  411. }
  412. got_sigio = 0;
  413. (*proc)(master, slave);
  414. close(master);
  415. close(slave);
  416. if (sigaction(SIGIO, &old, NULL) < 0)
  417. printk(UM_KERN_ERR "check_one_sigio : sigaction 3 failed, "
  418. "errno = %d\n", errno);
  419. }
  420. static void tty_output(int master, int slave)
  421. {
  422. int n;
  423. char buf[512];
  424. printk(UM_KERN_INFO "Checking that host ptys support output SIGIO...");
  425. memset(buf, 0, sizeof(buf));
  426. while (write(master, buf, sizeof(buf)) > 0) ;
  427. if (errno != EAGAIN)
  428. printk(UM_KERN_ERR "tty_output : write failed, errno = %d\n",
  429. errno);
  430. while (((n = read(slave, buf, sizeof(buf))) > 0) &&
  431. !({ barrier(); got_sigio; }))
  432. ;
  433. if (got_sigio) {
  434. printk(UM_KERN_CONT "Yes\n");
  435. pty_output_sigio = 1;
  436. } else if (n == -EAGAIN)
  437. printk(UM_KERN_CONT "No, enabling workaround\n");
  438. else
  439. printk(UM_KERN_CONT "tty_output : read failed, err = %d\n", n);
  440. }
  441. static void __init check_sigio(void)
  442. {
  443. if ((access("/dev/ptmx", R_OK) < 0) &&
  444. (access("/dev/ptyp0", R_OK) < 0)) {
  445. printk(UM_KERN_WARNING "No pseudo-terminals available - "
  446. "skipping pty SIGIO check\n");
  447. return;
  448. }
  449. check_one_sigio(tty_output);
  450. }
  451. /* Here because it only does the SIGIO testing for now */
  452. void __init os_check_bugs(void)
  453. {
  454. check_sigio();
  455. }