sigio.c 11 KB

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