sigio.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. if(n <= polls->size){
  90. polls->used = n;
  91. return 0;
  92. }
  93. kfree(polls->poll);
  94. polls->poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
  95. if(polls->poll == NULL){
  96. printk("need_poll : failed to allocate new pollfds\n");
  97. polls->size = 0;
  98. polls->used = 0;
  99. return -ENOMEM;
  100. }
  101. polls->size = n;
  102. polls->used = 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 = os_write_file(sigio_private[0], &c, sizeof(c));
  115. if(n != sizeof(c)){
  116. printk("update_thread : write failed, err = %d\n", -n);
  117. goto fail;
  118. }
  119. n = os_read_file(sigio_private[0], &c, sizeof(c));
  120. if(n != sizeof(c)){
  121. printk("update_thread : read failed, err = %d\n", -n);
  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 + 1;
  155. err = need_poll(&next_poll, n);
  156. if(err)
  157. goto out;
  158. for(i = 0; i < current_poll.used; i++)
  159. next_poll.poll[i] = current_poll.poll[i];
  160. next_poll.poll[n - 1] = *p;
  161. update_thread();
  162. out:
  163. sigio_unlock();
  164. return err;
  165. }
  166. int ignore_sigio_fd(int fd)
  167. {
  168. struct pollfd *p;
  169. int err = 0, i, n = 0;
  170. /* This is called from exitcalls elsewhere in UML - if
  171. * sigio_cleanup has already run, then update_thread will hang
  172. * or fail because the thread is no longer running.
  173. */
  174. if(write_sigio_pid == -1)
  175. return -EIO;
  176. sigio_lock();
  177. for(i = 0; i < current_poll.used; i++){
  178. if(current_poll.poll[i].fd == fd) break;
  179. }
  180. if(i == current_poll.used)
  181. goto out;
  182. err = need_poll(&next_poll, current_poll.used - 1);
  183. if(err)
  184. goto out;
  185. for(i = 0; i < current_poll.used; i++){
  186. p = &current_poll.poll[i];
  187. if(p->fd != fd)
  188. next_poll.poll[n++] = *p;
  189. }
  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. printk("maybe_sigio_broken - failed to add pollfd\n");
  287. goto out;
  288. }
  289. all_sigio_fds.poll[all_sigio_fds.used++] =
  290. ((struct pollfd) { .fd = fd,
  291. .events = read ? POLLIN : POLLOUT,
  292. .revents = 0 });
  293. out:
  294. sigio_unlock();
  295. }
  296. static void sigio_cleanup(void)
  297. {
  298. if(write_sigio_pid != -1){
  299. os_kill_process(write_sigio_pid, 1);
  300. write_sigio_pid = -1;
  301. }
  302. }
  303. __uml_exitcall(sigio_cleanup);