sigio.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
  22. * exitcall.
  23. */
  24. static int write_sigio_pid = -1;
  25. /* These arrays are initialized before the sigio thread is started, and
  26. * the descriptors closed after it is killed. So, it can't see them change.
  27. * On the UML side, they are changed under the sigio_lock.
  28. */
  29. #define SIGIO_FDS_INIT {-1, -1}
  30. static int write_sigio_fds[2] = SIGIO_FDS_INIT;
  31. static int sigio_private[2] = SIGIO_FDS_INIT;
  32. struct pollfds {
  33. struct pollfd *poll;
  34. int size;
  35. int used;
  36. };
  37. /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
  38. * synchronizes with it.
  39. */
  40. static struct pollfds current_poll = {
  41. .poll = NULL,
  42. .size = 0,
  43. .used = 0
  44. };
  45. static struct pollfds next_poll = {
  46. .poll = NULL,
  47. .size = 0,
  48. .used = 0
  49. };
  50. static int write_sigio_thread(void *unused)
  51. {
  52. struct pollfds *fds, tmp;
  53. struct pollfd *p;
  54. int i, n, respond_fd;
  55. char c;
  56. signal(SIGWINCH, SIG_IGN);
  57. fds = &current_poll;
  58. while(1){
  59. n = poll(fds->poll, fds->used, -1);
  60. if(n < 0){
  61. if(errno == EINTR) continue;
  62. printk("write_sigio_thread : poll returned %d, "
  63. "errno = %d\n", n, errno);
  64. }
  65. for(i = 0; i < fds->used; i++){
  66. p = &fds->poll[i];
  67. if(p->revents == 0) continue;
  68. if(p->fd == sigio_private[1]){
  69. n = os_read_file(sigio_private[1], &c, sizeof(c));
  70. if(n != sizeof(c))
  71. printk("write_sigio_thread : "
  72. "read failed, err = %d\n", -n);
  73. tmp = current_poll;
  74. current_poll = next_poll;
  75. next_poll = tmp;
  76. respond_fd = sigio_private[1];
  77. }
  78. else {
  79. respond_fd = write_sigio_fds[1];
  80. fds->used--;
  81. memmove(&fds->poll[i], &fds->poll[i + 1],
  82. (fds->used - i) * sizeof(*fds->poll));
  83. }
  84. n = os_write_file(respond_fd, &c, sizeof(c));
  85. if(n != sizeof(c))
  86. printk("write_sigio_thread : write failed, "
  87. "err = %d\n", -n);
  88. }
  89. }
  90. return 0;
  91. }
  92. static int need_poll(int n)
  93. {
  94. if(n <= next_poll.size){
  95. next_poll.used = n;
  96. return(0);
  97. }
  98. kfree(next_poll.poll);
  99. next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
  100. if(next_poll.poll == NULL){
  101. printk("need_poll : failed to allocate new pollfds\n");
  102. next_poll.size = 0;
  103. next_poll.used = 0;
  104. return(-1);
  105. }
  106. next_poll.size = n;
  107. next_poll.used = n;
  108. return(0);
  109. }
  110. /* Must be called with sigio_lock held, because it's needed by the marked
  111. * critical section. */
  112. static void update_thread(void)
  113. {
  114. unsigned long flags;
  115. int n;
  116. char c;
  117. flags = set_signals(0);
  118. n = os_write_file(sigio_private[0], &c, sizeof(c));
  119. if(n != sizeof(c)){
  120. printk("update_thread : write failed, err = %d\n", -n);
  121. goto fail;
  122. }
  123. n = os_read_file(sigio_private[0], &c, sizeof(c));
  124. if(n != sizeof(c)){
  125. printk("update_thread : read failed, err = %d\n", -n);
  126. goto fail;
  127. }
  128. set_signals(flags);
  129. return;
  130. fail:
  131. /* Critical section start */
  132. if(write_sigio_pid != -1)
  133. os_kill_process(write_sigio_pid, 1);
  134. write_sigio_pid = -1;
  135. close(sigio_private[0]);
  136. close(sigio_private[1]);
  137. close(write_sigio_fds[0]);
  138. close(write_sigio_fds[1]);
  139. /* Critical section end */
  140. set_signals(flags);
  141. }
  142. static int add_sigio_fd(int fd, int read)
  143. {
  144. int err = 0, i, n, events;
  145. sigio_lock();
  146. for(i = 0; i < current_poll.used; i++){
  147. if(current_poll.poll[i].fd == fd)
  148. goto out;
  149. }
  150. n = current_poll.used + 1;
  151. err = need_poll(n);
  152. if(err)
  153. goto out;
  154. for(i = 0; i < current_poll.used; i++)
  155. next_poll.poll[i] = current_poll.poll[i];
  156. if(read) events = POLLIN;
  157. else events = POLLOUT;
  158. next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
  159. .events = events,
  160. .revents = 0 });
  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(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) next_poll.poll[n++] = current_poll.poll[i];
  188. }
  189. if(n == i){
  190. printk("ignore_sigio_fd : fd %d not found\n", fd);
  191. err = -1;
  192. goto out;
  193. }
  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 = um_kmalloc(sizeof(struct pollfd));
  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. unsigned long stack;
  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, &stack, 0);
  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. void maybe_sigio_broken(int fd, int read)
  280. {
  281. if(!isatty(fd))
  282. return;
  283. if((read || pty_output_sigio) && (!read || pty_close_sigio))
  284. return;
  285. write_sigio_workaround();
  286. add_sigio_fd(fd, read);
  287. }
  288. static void sigio_cleanup(void)
  289. {
  290. if(write_sigio_pid != -1){
  291. os_kill_process(write_sigio_pid, 1);
  292. write_sigio_pid = -1;
  293. }
  294. }
  295. __uml_exitcall(sigio_cleanup);