sigio.c 11 KB

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