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 <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 = set_signals(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. set_signals(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. set_signals(flags);
  146. }
  147. int add_sigio_fd(int fd)
  148. {
  149. struct pollfd *p;
  150. int err = 0, i, n;
  151. sigio_lock();
  152. for (i = 0; i < all_sigio_fds.used; i++) {
  153. if (all_sigio_fds.poll[i].fd == fd)
  154. break;
  155. }
  156. if (i == all_sigio_fds.used)
  157. goto out;
  158. p = &all_sigio_fds.poll[i];
  159. for (i = 0; i < current_poll.used; i++) {
  160. if (current_poll.poll[i].fd == fd)
  161. goto out;
  162. }
  163. n = current_poll.used;
  164. err = need_poll(&next_poll, n + 1);
  165. if (err)
  166. goto out;
  167. memcpy(next_poll.poll, current_poll.poll,
  168. current_poll.used * sizeof(struct pollfd));
  169. next_poll.poll[n] = *p;
  170. next_poll.used = n + 1;
  171. update_thread();
  172. out:
  173. sigio_unlock();
  174. return err;
  175. }
  176. int ignore_sigio_fd(int fd)
  177. {
  178. struct pollfd *p;
  179. int err = 0, i, n = 0;
  180. /*
  181. * This is called from exitcalls elsewhere in UML - if
  182. * sigio_cleanup has already run, then update_thread will hang
  183. * or fail because the thread is no longer running.
  184. */
  185. if (write_sigio_pid == -1)
  186. return -EIO;
  187. sigio_lock();
  188. for (i = 0; i < current_poll.used; i++) {
  189. if (current_poll.poll[i].fd == fd)
  190. break;
  191. }
  192. if (i == current_poll.used)
  193. goto out;
  194. err = need_poll(&next_poll, current_poll.used - 1);
  195. if (err)
  196. goto out;
  197. for (i = 0; i < current_poll.used; i++) {
  198. p = &current_poll.poll[i];
  199. if (p->fd != fd)
  200. next_poll.poll[n++] = *p;
  201. }
  202. next_poll.used = current_poll.used - 1;
  203. update_thread();
  204. out:
  205. sigio_unlock();
  206. return err;
  207. }
  208. static struct pollfd *setup_initial_poll(int fd)
  209. {
  210. struct pollfd *p;
  211. p = uml_kmalloc(sizeof(struct pollfd), UM_GFP_KERNEL);
  212. if (p == NULL) {
  213. printk(UM_KERN_ERR "setup_initial_poll : failed to allocate "
  214. "poll\n");
  215. return NULL;
  216. }
  217. *p = ((struct pollfd) { .fd = fd,
  218. .events = POLLIN,
  219. .revents = 0 });
  220. return p;
  221. }
  222. static void write_sigio_workaround(void)
  223. {
  224. struct pollfd *p;
  225. int err;
  226. int l_write_sigio_fds[2];
  227. int l_sigio_private[2];
  228. int l_write_sigio_pid;
  229. /* We call this *tons* of times - and most ones we must just fail. */
  230. sigio_lock();
  231. l_write_sigio_pid = write_sigio_pid;
  232. sigio_unlock();
  233. if (l_write_sigio_pid != -1)
  234. return;
  235. err = os_pipe(l_write_sigio_fds, 1, 1);
  236. if (err < 0) {
  237. printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 1 failed, "
  238. "err = %d\n", -err);
  239. return;
  240. }
  241. err = os_pipe(l_sigio_private, 1, 1);
  242. if (err < 0) {
  243. printk(UM_KERN_ERR "write_sigio_workaround - os_pipe 2 failed, "
  244. "err = %d\n", -err);
  245. goto out_close1;
  246. }
  247. p = setup_initial_poll(l_sigio_private[1]);
  248. if (!p)
  249. goto out_close2;
  250. sigio_lock();
  251. /*
  252. * Did we race? Don't try to optimize this, please, it's not so likely
  253. * to happen, and no more than once at the boot.
  254. */
  255. if (write_sigio_pid != -1)
  256. goto out_free;
  257. current_poll = ((struct pollfds) { .poll = p,
  258. .used = 1,
  259. .size = 1 });
  260. if (write_sigio_irq(l_write_sigio_fds[0]))
  261. goto out_clear_poll;
  262. memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
  263. memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
  264. write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
  265. CLONE_FILES | CLONE_VM,
  266. &write_sigio_stack);
  267. if (write_sigio_pid < 0)
  268. goto out_clear;
  269. sigio_unlock();
  270. return;
  271. out_clear:
  272. write_sigio_pid = -1;
  273. write_sigio_fds[0] = -1;
  274. write_sigio_fds[1] = -1;
  275. sigio_private[0] = -1;
  276. sigio_private[1] = -1;
  277. out_clear_poll:
  278. current_poll = ((struct pollfds) { .poll = NULL,
  279. .size = 0,
  280. .used = 0 });
  281. out_free:
  282. sigio_unlock();
  283. kfree(p);
  284. out_close2:
  285. close(l_sigio_private[0]);
  286. close(l_sigio_private[1]);
  287. out_close1:
  288. close(l_write_sigio_fds[0]);
  289. close(l_write_sigio_fds[1]);
  290. }
  291. void sigio_broken(int fd, int read)
  292. {
  293. int err;
  294. write_sigio_workaround();
  295. sigio_lock();
  296. err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
  297. if (err) {
  298. printk(UM_KERN_ERR "maybe_sigio_broken - failed to add pollfd "
  299. "for descriptor %d\n", fd);
  300. goto out;
  301. }
  302. all_sigio_fds.poll[all_sigio_fds.used++] =
  303. ((struct pollfd) { .fd = fd,
  304. .events = read ? POLLIN : POLLOUT,
  305. .revents = 0 });
  306. out:
  307. sigio_unlock();
  308. }
  309. /* Changed during early boot */
  310. static int pty_output_sigio;
  311. static int pty_close_sigio;
  312. void maybe_sigio_broken(int fd, int read)
  313. {
  314. if (!isatty(fd))
  315. return;
  316. if ((read || pty_output_sigio) && (!read || pty_close_sigio))
  317. return;
  318. sigio_broken(fd, read);
  319. }
  320. static void sigio_cleanup(void)
  321. {
  322. if (write_sigio_pid == -1)
  323. return;
  324. os_kill_process(write_sigio_pid, 1);
  325. free_stack(write_sigio_stack, 0);
  326. write_sigio_pid = -1;
  327. }
  328. __uml_exitcall(sigio_cleanup);
  329. /* Used as a flag during SIGIO testing early in boot */
  330. static int got_sigio;
  331. static void __init handler(int sig)
  332. {
  333. got_sigio = 1;
  334. }
  335. struct openpty_arg {
  336. int master;
  337. int slave;
  338. int err;
  339. };
  340. static void openpty_cb(void *arg)
  341. {
  342. struct openpty_arg *info = arg;
  343. info->err = 0;
  344. if (openpty(&info->master, &info->slave, NULL, NULL, NULL))
  345. info->err = -errno;
  346. }
  347. static int async_pty(int master, int slave)
  348. {
  349. int flags;
  350. flags = fcntl(master, F_GETFL);
  351. if (flags < 0)
  352. return -errno;
  353. if ((fcntl(master, F_SETFL, flags | O_NONBLOCK | O_ASYNC) < 0) ||
  354. (fcntl(master, F_SETOWN, os_getpid()) < 0))
  355. return -errno;
  356. if ((fcntl(slave, F_SETFL, flags | O_NONBLOCK) < 0))
  357. return -errno;
  358. return 0;
  359. }
  360. static void __init check_one_sigio(void (*proc)(int, int))
  361. {
  362. struct sigaction old, new;
  363. struct openpty_arg pty = { .master = -1, .slave = -1 };
  364. int master, slave, err;
  365. initial_thread_cb(openpty_cb, &pty);
  366. if (pty.err) {
  367. printk(UM_KERN_ERR "check_one_sigio failed, errno = %d\n",
  368. -pty.err);
  369. return;
  370. }
  371. master = pty.master;
  372. slave = pty.slave;
  373. if ((master == -1) || (slave == -1)) {
  374. printk(UM_KERN_ERR "check_one_sigio failed to allocate a "
  375. "pty\n");
  376. return;
  377. }
  378. /* Not now, but complain so we now where we failed. */
  379. err = raw(master);
  380. if (err < 0) {
  381. printk(UM_KERN_ERR "check_one_sigio : raw failed, errno = %d\n",
  382. -err);
  383. return;
  384. }
  385. err = async_pty(master, slave);
  386. if (err < 0) {
  387. printk(UM_KERN_ERR "check_one_sigio : sigio_async failed, "
  388. "err = %d\n", -err);
  389. return;
  390. }
  391. if (sigaction(SIGIO, NULL, &old) < 0) {
  392. printk(UM_KERN_ERR "check_one_sigio : sigaction 1 failed, "
  393. "errno = %d\n", errno);
  394. return;
  395. }
  396. new = old;
  397. new.sa_handler = handler;
  398. if (sigaction(SIGIO, &new, NULL) < 0) {
  399. printk(UM_KERN_ERR "check_one_sigio : sigaction 2 failed, "
  400. "errno = %d\n", errno);
  401. return;
  402. }
  403. got_sigio = 0;
  404. (*proc)(master, slave);
  405. close(master);
  406. close(slave);
  407. if (sigaction(SIGIO, &old, NULL) < 0)
  408. printk(UM_KERN_ERR "check_one_sigio : sigaction 3 failed, "
  409. "errno = %d\n", errno);
  410. }
  411. static void tty_output(int master, int slave)
  412. {
  413. int n;
  414. char buf[512];
  415. printk(UM_KERN_INFO "Checking that host ptys support output SIGIO...");
  416. memset(buf, 0, sizeof(buf));
  417. while (write(master, buf, sizeof(buf)) > 0) ;
  418. if (errno != EAGAIN)
  419. printk(UM_KERN_ERR "tty_output : write failed, errno = %d\n",
  420. errno);
  421. while (((n = read(slave, buf, sizeof(buf))) > 0) &&
  422. !({ barrier(); got_sigio; }))
  423. ;
  424. if (got_sigio) {
  425. printk(UM_KERN_CONT "Yes\n");
  426. pty_output_sigio = 1;
  427. } else if (n == -EAGAIN)
  428. printk(UM_KERN_CONT "No, enabling workaround\n");
  429. else
  430. printk(UM_KERN_CONT "tty_output : read failed, err = %d\n", n);
  431. }
  432. static void tty_close(int master, int slave)
  433. {
  434. printk(UM_KERN_INFO "Checking that host ptys support SIGIO on "
  435. "close...");
  436. close(slave);
  437. if (got_sigio) {
  438. printk(UM_KERN_CONT "Yes\n");
  439. pty_close_sigio = 1;
  440. } else
  441. printk(UM_KERN_CONT "No, enabling workaround\n");
  442. }
  443. static void __init check_sigio(void)
  444. {
  445. if ((access("/dev/ptmx", R_OK) < 0) &&
  446. (access("/dev/ptyp0", R_OK) < 0)) {
  447. printk(UM_KERN_WARNING "No pseudo-terminals available - "
  448. "skipping pty SIGIO check\n");
  449. return;
  450. }
  451. check_one_sigio(tty_output);
  452. check_one_sigio(tty_close);
  453. }
  454. /* Here because it only does the SIGIO testing for now */
  455. void __init os_check_bugs(void)
  456. {
  457. check_sigio();
  458. }