sigio_user.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. /* Changed during early boot */
  22. int pty_output_sigio = 0;
  23. int pty_close_sigio = 0;
  24. /* Used as a flag during SIGIO testing early in boot */
  25. static volatile int got_sigio = 0;
  26. void __init handler(int sig)
  27. {
  28. got_sigio = 1;
  29. }
  30. struct openpty_arg {
  31. int master;
  32. int slave;
  33. int err;
  34. };
  35. static void openpty_cb(void *arg)
  36. {
  37. struct openpty_arg *info = arg;
  38. info->err = 0;
  39. if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
  40. info->err = -errno;
  41. }
  42. void __init check_one_sigio(void (*proc)(int, int))
  43. {
  44. struct sigaction old, new;
  45. struct openpty_arg pty = { .master = -1, .slave = -1 };
  46. int master, slave, err;
  47. initial_thread_cb(openpty_cb, &pty);
  48. if(pty.err){
  49. printk("openpty failed, errno = %d\n", -pty.err);
  50. return;
  51. }
  52. master = pty.master;
  53. slave = pty.slave;
  54. if((master == -1) || (slave == -1)){
  55. printk("openpty failed to allocate a pty\n");
  56. return;
  57. }
  58. /* Not now, but complain so we now where we failed. */
  59. err = raw(master);
  60. if (err < 0)
  61. panic("check_sigio : __raw failed, errno = %d\n", -err);
  62. err = os_sigio_async(master, slave);
  63. if(err < 0)
  64. panic("tty_fds : sigio_async failed, err = %d\n", -err);
  65. if(sigaction(SIGIO, NULL, &old) < 0)
  66. panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
  67. new = old;
  68. new.sa_handler = handler;
  69. if(sigaction(SIGIO, &new, NULL) < 0)
  70. panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
  71. got_sigio = 0;
  72. (*proc)(master, slave);
  73. os_close_file(master);
  74. os_close_file(slave);
  75. if(sigaction(SIGIO, &old, NULL) < 0)
  76. panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
  77. }
  78. static void tty_output(int master, int slave)
  79. {
  80. int n;
  81. char buf[512];
  82. printk("Checking that host ptys support output SIGIO...");
  83. memset(buf, 0, sizeof(buf));
  84. while(os_write_file(master, buf, sizeof(buf)) > 0) ;
  85. if(errno != EAGAIN)
  86. panic("check_sigio : write failed, errno = %d\n", errno);
  87. while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
  88. if (got_sigio) {
  89. printk("Yes\n");
  90. pty_output_sigio = 1;
  91. } else if (n == -EAGAIN) {
  92. printk("No, enabling workaround\n");
  93. } else {
  94. panic("check_sigio : read failed, err = %d\n", n);
  95. }
  96. }
  97. static void tty_close(int master, int slave)
  98. {
  99. printk("Checking that host ptys support SIGIO on close...");
  100. os_close_file(slave);
  101. if(got_sigio){
  102. printk("Yes\n");
  103. pty_close_sigio = 1;
  104. }
  105. else printk("No, enabling workaround\n");
  106. }
  107. void __init check_sigio(void)
  108. {
  109. if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
  110. (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
  111. printk("No pseudo-terminals available - skipping pty SIGIO "
  112. "check\n");
  113. return;
  114. }
  115. check_one_sigio(tty_output);
  116. check_one_sigio(tty_close);
  117. }
  118. /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
  119. * exitcall.
  120. */
  121. static int write_sigio_pid = -1;
  122. /* These arrays are initialized before the sigio thread is started, and
  123. * the descriptors closed after it is killed. So, it can't see them change.
  124. * On the UML side, they are changed under the sigio_lock.
  125. */
  126. static int write_sigio_fds[2] = { -1, -1 };
  127. static int sigio_private[2] = { -1, -1 };
  128. struct pollfds {
  129. struct pollfd *poll;
  130. int size;
  131. int used;
  132. };
  133. /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
  134. * synchronizes with it.
  135. */
  136. struct pollfds current_poll = {
  137. .poll = NULL,
  138. .size = 0,
  139. .used = 0
  140. };
  141. struct pollfds next_poll = {
  142. .poll = NULL,
  143. .size = 0,
  144. .used = 0
  145. };
  146. static int write_sigio_thread(void *unused)
  147. {
  148. struct pollfds *fds, tmp;
  149. struct pollfd *p;
  150. int i, n, respond_fd;
  151. char c;
  152. signal(SIGWINCH, SIG_IGN);
  153. fds = &current_poll;
  154. while(1){
  155. n = poll(fds->poll, fds->used, -1);
  156. if(n < 0){
  157. if(errno == EINTR) continue;
  158. printk("write_sigio_thread : poll returned %d, "
  159. "errno = %d\n", n, errno);
  160. }
  161. for(i = 0; i < fds->used; i++){
  162. p = &fds->poll[i];
  163. if(p->revents == 0) continue;
  164. if(p->fd == sigio_private[1]){
  165. n = os_read_file(sigio_private[1], &c, sizeof(c));
  166. if(n != sizeof(c))
  167. printk("write_sigio_thread : "
  168. "read failed, err = %d\n", -n);
  169. tmp = current_poll;
  170. current_poll = next_poll;
  171. next_poll = tmp;
  172. respond_fd = sigio_private[1];
  173. }
  174. else {
  175. respond_fd = write_sigio_fds[1];
  176. fds->used--;
  177. memmove(&fds->poll[i], &fds->poll[i + 1],
  178. (fds->used - i) * sizeof(*fds->poll));
  179. }
  180. n = os_write_file(respond_fd, &c, sizeof(c));
  181. if(n != sizeof(c))
  182. printk("write_sigio_thread : write failed, "
  183. "err = %d\n", -n);
  184. }
  185. }
  186. }
  187. static int need_poll(int n)
  188. {
  189. if(n <= next_poll.size){
  190. next_poll.used = n;
  191. return(0);
  192. }
  193. kfree(next_poll.poll);
  194. next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
  195. if(next_poll.poll == NULL){
  196. printk("need_poll : failed to allocate new pollfds\n");
  197. next_poll.size = 0;
  198. next_poll.used = 0;
  199. return(-1);
  200. }
  201. next_poll.size = n;
  202. next_poll.used = n;
  203. return(0);
  204. }
  205. /* Must be called with sigio_lock held, because it's needed by the marked
  206. * critical section. */
  207. static void update_thread(void)
  208. {
  209. unsigned long flags;
  210. int n;
  211. char c;
  212. flags = set_signals(0);
  213. n = os_write_file(sigio_private[0], &c, sizeof(c));
  214. if(n != sizeof(c)){
  215. printk("update_thread : write failed, err = %d\n", -n);
  216. goto fail;
  217. }
  218. n = os_read_file(sigio_private[0], &c, sizeof(c));
  219. if(n != sizeof(c)){
  220. printk("update_thread : read failed, err = %d\n", -n);
  221. goto fail;
  222. }
  223. set_signals(flags);
  224. return;
  225. fail:
  226. /* Critical section start */
  227. if(write_sigio_pid != -1)
  228. os_kill_process(write_sigio_pid, 1);
  229. write_sigio_pid = -1;
  230. os_close_file(sigio_private[0]);
  231. os_close_file(sigio_private[1]);
  232. os_close_file(write_sigio_fds[0]);
  233. os_close_file(write_sigio_fds[1]);
  234. /* Critical section end */
  235. set_signals(flags);
  236. }
  237. int add_sigio_fd(int fd, int read)
  238. {
  239. int err = 0, i, n, events;
  240. sigio_lock();
  241. for(i = 0; i < current_poll.used; i++){
  242. if(current_poll.poll[i].fd == fd)
  243. goto out;
  244. }
  245. n = current_poll.used + 1;
  246. err = need_poll(n);
  247. if(err)
  248. goto out;
  249. for(i = 0; i < current_poll.used; i++)
  250. next_poll.poll[i] = current_poll.poll[i];
  251. if(read) events = POLLIN;
  252. else events = POLLOUT;
  253. next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
  254. .events = events,
  255. .revents = 0 });
  256. update_thread();
  257. out:
  258. sigio_unlock();
  259. return(err);
  260. }
  261. int ignore_sigio_fd(int fd)
  262. {
  263. struct pollfd *p;
  264. int err = 0, i, n = 0;
  265. sigio_lock();
  266. for(i = 0; i < current_poll.used; i++){
  267. if(current_poll.poll[i].fd == fd) break;
  268. }
  269. if(i == current_poll.used)
  270. goto out;
  271. err = need_poll(current_poll.used - 1);
  272. if(err)
  273. goto out;
  274. for(i = 0; i < current_poll.used; i++){
  275. p = &current_poll.poll[i];
  276. if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
  277. }
  278. if(n == i){
  279. printk("ignore_sigio_fd : fd %d not found\n", fd);
  280. err = -1;
  281. goto out;
  282. }
  283. update_thread();
  284. out:
  285. sigio_unlock();
  286. return(err);
  287. }
  288. static int setup_initial_poll(int fd)
  289. {
  290. struct pollfd *p;
  291. p = um_kmalloc_atomic(sizeof(struct pollfd));
  292. if(p == NULL){
  293. printk("setup_initial_poll : failed to allocate poll\n");
  294. return(-1);
  295. }
  296. *p = ((struct pollfd) { .fd = fd,
  297. .events = POLLIN,
  298. .revents = 0 });
  299. current_poll = ((struct pollfds) { .poll = p,
  300. .used = 1,
  301. .size = 1 });
  302. return(0);
  303. }
  304. void write_sigio_workaround(void)
  305. {
  306. unsigned long stack;
  307. int err;
  308. sigio_lock();
  309. if(write_sigio_pid != -1)
  310. goto out;
  311. err = os_pipe(write_sigio_fds, 1, 1);
  312. if(err < 0){
  313. printk("write_sigio_workaround - os_pipe 1 failed, "
  314. "err = %d\n", -err);
  315. goto out;
  316. }
  317. err = os_pipe(sigio_private, 1, 1);
  318. if(err < 0){
  319. printk("write_sigio_workaround - os_pipe 2 failed, "
  320. "err = %d\n", -err);
  321. goto out_close1;
  322. }
  323. if(setup_initial_poll(sigio_private[1]))
  324. goto out_close2;
  325. write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
  326. CLONE_FILES | CLONE_VM, &stack, 0);
  327. if(write_sigio_pid < 0) goto out_close2;
  328. if(write_sigio_irq(write_sigio_fds[0]))
  329. goto out_kill;
  330. out:
  331. sigio_unlock();
  332. return;
  333. out_kill:
  334. os_kill_process(write_sigio_pid, 1);
  335. write_sigio_pid = -1;
  336. out_close2:
  337. os_close_file(sigio_private[0]);
  338. os_close_file(sigio_private[1]);
  339. out_close1:
  340. os_close_file(write_sigio_fds[0]);
  341. os_close_file(write_sigio_fds[1]);
  342. sigio_unlock();
  343. }
  344. int read_sigio_fd(int fd)
  345. {
  346. int n;
  347. char c;
  348. n = os_read_file(fd, &c, sizeof(c));
  349. if(n != sizeof(c)){
  350. if(n < 0) {
  351. printk("read_sigio_fd - read failed, err = %d\n", -n);
  352. return(n);
  353. }
  354. else {
  355. printk("read_sigio_fd - short read, bytes = %d\n", n);
  356. return(-EIO);
  357. }
  358. }
  359. return(n);
  360. }
  361. static void sigio_cleanup(void)
  362. {
  363. if (write_sigio_pid != -1) {
  364. os_kill_process(write_sigio_pid, 1);
  365. write_sigio_pid = -1;
  366. }
  367. }
  368. __uml_exitcall(sigio_cleanup);