sigio.c 11 KB

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