sigio.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 <errno.h>
  11. #include <string.h>
  12. #include <sched.h>
  13. #include <sys/socket.h>
  14. #include <sys/poll.h>
  15. #include "init.h"
  16. #include "user.h"
  17. #include "kern_util.h"
  18. #include "user_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. /* These arrays are initialized before the sigio thread is started, and
  27. * the descriptors closed after it is killed. So, it can't see them change.
  28. * On the UML side, they are changed under the sigio_lock.
  29. */
  30. #define SIGIO_FDS_INIT {-1, -1}
  31. static int write_sigio_fds[2] = SIGIO_FDS_INIT;
  32. static int sigio_private[2] = SIGIO_FDS_INIT;
  33. struct pollfds {
  34. struct pollfd *poll;
  35. int size;
  36. int used;
  37. };
  38. /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
  39. * synchronizes with it.
  40. */
  41. static struct pollfds current_poll;
  42. static struct pollfds next_poll;
  43. static struct pollfds all_sigio_fds;
  44. static int write_sigio_thread(void *unused)
  45. {
  46. struct pollfds *fds, tmp;
  47. struct pollfd *p;
  48. int i, n, respond_fd;
  49. char c;
  50. signal(SIGWINCH, SIG_IGN);
  51. fds = &current_poll;
  52. while(1){
  53. n = poll(fds->poll, fds->used, -1);
  54. if(n < 0){
  55. if(errno == EINTR) continue;
  56. printk("write_sigio_thread : poll returned %d, "
  57. "errno = %d\n", n, errno);
  58. }
  59. for(i = 0; i < fds->used; i++){
  60. p = &fds->poll[i];
  61. if(p->revents == 0) continue;
  62. if(p->fd == sigio_private[1]){
  63. n = os_read_file(sigio_private[1], &c, sizeof(c));
  64. if(n != sizeof(c))
  65. printk("write_sigio_thread : "
  66. "read on socket failed, "
  67. "err = %d\n", -n);
  68. tmp = current_poll;
  69. current_poll = next_poll;
  70. next_poll = tmp;
  71. respond_fd = sigio_private[1];
  72. }
  73. else {
  74. respond_fd = write_sigio_fds[1];
  75. fds->used--;
  76. memmove(&fds->poll[i], &fds->poll[i + 1],
  77. (fds->used - i) * sizeof(*fds->poll));
  78. }
  79. n = os_write_file(respond_fd, &c, sizeof(c));
  80. if(n != sizeof(c))
  81. printk("write_sigio_thread : write on socket "
  82. "failed, err = %d\n", -n);
  83. }
  84. }
  85. return 0;
  86. }
  87. static int need_poll(struct pollfds *polls, int n)
  88. {
  89. struct pollfd *new;
  90. if(n <= polls->size)
  91. return 0;
  92. new = um_kmalloc_atomic(n * sizeof(struct pollfd));
  93. if(new == NULL){
  94. printk("need_poll : failed to allocate new pollfds\n");
  95. return -ENOMEM;
  96. }
  97. memcpy(new, polls->poll, polls->used * sizeof(struct pollfd));
  98. kfree(polls->poll);
  99. polls->poll = new;
  100. polls->size = n;
  101. return 0;
  102. }
  103. /* Must be called with sigio_lock held, because it's needed by the marked
  104. * critical section.
  105. */
  106. static void update_thread(void)
  107. {
  108. unsigned long flags;
  109. int n;
  110. char c;
  111. flags = set_signals(0);
  112. n = os_write_file(sigio_private[0], &c, sizeof(c));
  113. if(n != sizeof(c)){
  114. printk("update_thread : write failed, err = %d\n", -n);
  115. goto fail;
  116. }
  117. n = os_read_file(sigio_private[0], &c, sizeof(c));
  118. if(n != sizeof(c)){
  119. printk("update_thread : read failed, err = %d\n", -n);
  120. goto fail;
  121. }
  122. set_signals(flags);
  123. return;
  124. fail:
  125. /* Critical section start */
  126. if(write_sigio_pid != -1)
  127. os_kill_process(write_sigio_pid, 1);
  128. write_sigio_pid = -1;
  129. close(sigio_private[0]);
  130. close(sigio_private[1]);
  131. close(write_sigio_fds[0]);
  132. close(write_sigio_fds[1]);
  133. /* Critical section end */
  134. set_signals(flags);
  135. }
  136. int add_sigio_fd(int fd)
  137. {
  138. struct pollfd *p;
  139. int err = 0, i, n;
  140. sigio_lock();
  141. for(i = 0; i < all_sigio_fds.used; i++){
  142. if(all_sigio_fds.poll[i].fd == fd)
  143. break;
  144. }
  145. if(i == all_sigio_fds.used)
  146. goto out;
  147. p = &all_sigio_fds.poll[i];
  148. for(i = 0; i < current_poll.used; i++){
  149. if(current_poll.poll[i].fd == fd)
  150. goto out;
  151. }
  152. n = current_poll.used;
  153. err = need_poll(&next_poll, n + 1);
  154. if(err)
  155. goto out;
  156. memcpy(next_poll.poll, current_poll.poll,
  157. current_poll.used * sizeof(struct pollfd));
  158. next_poll.poll[n] = *p;
  159. next_poll.used = n + 1;
  160. update_thread();
  161. out:
  162. sigio_unlock();
  163. return err;
  164. }
  165. int ignore_sigio_fd(int fd)
  166. {
  167. struct pollfd *p;
  168. int err = 0, i, n = 0;
  169. /* This is called from exitcalls elsewhere in UML - if
  170. * sigio_cleanup has already run, then update_thread will hang
  171. * or fail because the thread is no longer running.
  172. */
  173. if(write_sigio_pid == -1)
  174. return -EIO;
  175. sigio_lock();
  176. for(i = 0; i < current_poll.used; i++){
  177. if(current_poll.poll[i].fd == fd) break;
  178. }
  179. if(i == current_poll.used)
  180. goto out;
  181. err = need_poll(&next_poll, current_poll.used - 1);
  182. if(err)
  183. goto out;
  184. for(i = 0; i < current_poll.used; i++){
  185. p = &current_poll.poll[i];
  186. if(p->fd != fd)
  187. next_poll.poll[n++] = *p;
  188. }
  189. next_poll.used = current_poll.used - 1;
  190. update_thread();
  191. out:
  192. sigio_unlock();
  193. return err;
  194. }
  195. static struct pollfd *setup_initial_poll(int fd)
  196. {
  197. struct pollfd *p;
  198. p = um_kmalloc(sizeof(struct pollfd));
  199. if (p == NULL) {
  200. printk("setup_initial_poll : failed to allocate poll\n");
  201. return NULL;
  202. }
  203. *p = ((struct pollfd) { .fd = fd,
  204. .events = POLLIN,
  205. .revents = 0 });
  206. return p;
  207. }
  208. static void write_sigio_workaround(void)
  209. {
  210. unsigned long stack;
  211. struct pollfd *p;
  212. int err;
  213. int l_write_sigio_fds[2];
  214. int l_sigio_private[2];
  215. int l_write_sigio_pid;
  216. /* We call this *tons* of times - and most ones we must just fail. */
  217. sigio_lock();
  218. l_write_sigio_pid = write_sigio_pid;
  219. sigio_unlock();
  220. if (l_write_sigio_pid != -1)
  221. return;
  222. err = os_pipe(l_write_sigio_fds, 1, 1);
  223. if(err < 0){
  224. printk("write_sigio_workaround - os_pipe 1 failed, "
  225. "err = %d\n", -err);
  226. return;
  227. }
  228. err = os_pipe(l_sigio_private, 1, 1);
  229. if(err < 0){
  230. printk("write_sigio_workaround - os_pipe 2 failed, "
  231. "err = %d\n", -err);
  232. goto out_close1;
  233. }
  234. p = setup_initial_poll(l_sigio_private[1]);
  235. if(!p)
  236. goto out_close2;
  237. sigio_lock();
  238. /* Did we race? Don't try to optimize this, please, it's not so likely
  239. * to happen, and no more than once at the boot. */
  240. if(write_sigio_pid != -1)
  241. goto out_free;
  242. current_poll = ((struct pollfds) { .poll = p,
  243. .used = 1,
  244. .size = 1 });
  245. if (write_sigio_irq(l_write_sigio_fds[0]))
  246. goto out_clear_poll;
  247. memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
  248. memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
  249. write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
  250. CLONE_FILES | CLONE_VM, &stack, 0);
  251. if (write_sigio_pid < 0)
  252. goto out_clear;
  253. sigio_unlock();
  254. return;
  255. out_clear:
  256. write_sigio_pid = -1;
  257. write_sigio_fds[0] = -1;
  258. write_sigio_fds[1] = -1;
  259. sigio_private[0] = -1;
  260. sigio_private[1] = -1;
  261. out_clear_poll:
  262. current_poll = ((struct pollfds) { .poll = NULL,
  263. .size = 0,
  264. .used = 0 });
  265. out_free:
  266. sigio_unlock();
  267. kfree(p);
  268. out_close2:
  269. close(l_sigio_private[0]);
  270. close(l_sigio_private[1]);
  271. out_close1:
  272. close(l_write_sigio_fds[0]);
  273. close(l_write_sigio_fds[1]);
  274. }
  275. void maybe_sigio_broken(int fd, int read)
  276. {
  277. int err;
  278. if(!isatty(fd))
  279. return;
  280. if((read || pty_output_sigio) && (!read || pty_close_sigio))
  281. return;
  282. write_sigio_workaround();
  283. sigio_lock();
  284. err = need_poll(&all_sigio_fds, all_sigio_fds.used + 1);
  285. if(err)
  286. goto out;
  287. all_sigio_fds.poll[all_sigio_fds.used++] =
  288. ((struct pollfd) { .fd = fd,
  289. .events = read ? POLLIN : POLLOUT,
  290. .revents = 0 });
  291. out:
  292. sigio_unlock();
  293. }
  294. static void sigio_cleanup(void)
  295. {
  296. if(write_sigio_pid != -1){
  297. os_kill_process(write_sigio_pid, 1);
  298. write_sigio_pid = -1;
  299. }
  300. }
  301. __uml_exitcall(sigio_cleanup);