sigio.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. struct pollfds current_poll = {
  41. .poll = NULL,
  42. .size = 0,
  43. .used = 0
  44. };
  45. 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. 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. sigio_lock();
  171. for(i = 0; i < current_poll.used; i++){
  172. if(current_poll.poll[i].fd == fd) break;
  173. }
  174. if(i == current_poll.used)
  175. goto out;
  176. err = need_poll(current_poll.used - 1);
  177. if(err)
  178. goto out;
  179. for(i = 0; i < current_poll.used; i++){
  180. p = &current_poll.poll[i];
  181. if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
  182. }
  183. if(n == i){
  184. printk("ignore_sigio_fd : fd %d not found\n", fd);
  185. err = -1;
  186. goto out;
  187. }
  188. update_thread();
  189. out:
  190. sigio_unlock();
  191. return(err);
  192. }
  193. static struct pollfd *setup_initial_poll(int fd)
  194. {
  195. struct pollfd *p;
  196. p = um_kmalloc(sizeof(struct pollfd));
  197. if (p == NULL) {
  198. printk("setup_initial_poll : failed to allocate poll\n");
  199. return NULL;
  200. }
  201. *p = ((struct pollfd) { .fd = fd,
  202. .events = POLLIN,
  203. .revents = 0 });
  204. return p;
  205. }
  206. void write_sigio_workaround(void)
  207. {
  208. unsigned long stack;
  209. struct pollfd *p;
  210. int err;
  211. int l_write_sigio_fds[2];
  212. int l_sigio_private[2];
  213. int l_write_sigio_pid;
  214. /* We call this *tons* of times - and most ones we must just fail. */
  215. sigio_lock();
  216. l_write_sigio_pid = write_sigio_pid;
  217. sigio_unlock();
  218. if (l_write_sigio_pid != -1)
  219. return;
  220. err = os_pipe(l_write_sigio_fds, 1, 1);
  221. if(err < 0){
  222. printk("write_sigio_workaround - os_pipe 1 failed, "
  223. "err = %d\n", -err);
  224. return;
  225. }
  226. err = os_pipe(l_sigio_private, 1, 1);
  227. if(err < 0){
  228. printk("write_sigio_workaround - os_pipe 2 failed, "
  229. "err = %d\n", -err);
  230. goto out_close1;
  231. }
  232. p = setup_initial_poll(l_sigio_private[1]);
  233. if(!p)
  234. goto out_close2;
  235. sigio_lock();
  236. /* Did we race? Don't try to optimize this, please, it's not so likely
  237. * to happen, and no more than once at the boot. */
  238. if(write_sigio_pid != -1)
  239. goto out_free;
  240. current_poll = ((struct pollfds) { .poll = p,
  241. .used = 1,
  242. .size = 1 });
  243. if (write_sigio_irq(l_write_sigio_fds[0]))
  244. goto out_clear_poll;
  245. memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
  246. memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
  247. write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
  248. CLONE_FILES | CLONE_VM, &stack, 0);
  249. if (write_sigio_pid < 0)
  250. goto out_clear;
  251. sigio_unlock();
  252. return;
  253. out_clear:
  254. write_sigio_pid = -1;
  255. write_sigio_fds[0] = -1;
  256. write_sigio_fds[1] = -1;
  257. sigio_private[0] = -1;
  258. sigio_private[1] = -1;
  259. out_clear_poll:
  260. current_poll = ((struct pollfds) { .poll = NULL,
  261. .size = 0,
  262. .used = 0 });
  263. out_free:
  264. sigio_unlock();
  265. kfree(p);
  266. out_close2:
  267. close(l_sigio_private[0]);
  268. close(l_sigio_private[1]);
  269. out_close1:
  270. close(l_write_sigio_fds[0]);
  271. close(l_write_sigio_fds[1]);
  272. }
  273. void sigio_cleanup(void)
  274. {
  275. if(write_sigio_pid != -1){
  276. os_kill_process(write_sigio_pid, 1);
  277. write_sigio_pid = -1;
  278. }
  279. }