sigio.c 11 KB

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