sigio.c 11 KB

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