sigio.c 11 KB

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