sigio_user.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. return 0;
  187. }
  188. static int need_poll(int n)
  189. {
  190. if(n <= next_poll.size){
  191. next_poll.used = n;
  192. return(0);
  193. }
  194. kfree(next_poll.poll);
  195. next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
  196. if(next_poll.poll == NULL){
  197. printk("need_poll : failed to allocate new pollfds\n");
  198. next_poll.size = 0;
  199. next_poll.used = 0;
  200. return(-1);
  201. }
  202. next_poll.size = n;
  203. next_poll.used = n;
  204. return(0);
  205. }
  206. /* Must be called with sigio_lock held, because it's needed by the marked
  207. * critical section. */
  208. static void update_thread(void)
  209. {
  210. unsigned long flags;
  211. int n;
  212. char c;
  213. flags = set_signals(0);
  214. n = os_write_file(sigio_private[0], &c, sizeof(c));
  215. if(n != sizeof(c)){
  216. printk("update_thread : write failed, err = %d\n", -n);
  217. goto fail;
  218. }
  219. n = os_read_file(sigio_private[0], &c, sizeof(c));
  220. if(n != sizeof(c)){
  221. printk("update_thread : read failed, err = %d\n", -n);
  222. goto fail;
  223. }
  224. set_signals(flags);
  225. return;
  226. fail:
  227. /* Critical section start */
  228. if(write_sigio_pid != -1)
  229. os_kill_process(write_sigio_pid, 1);
  230. write_sigio_pid = -1;
  231. os_close_file(sigio_private[0]);
  232. os_close_file(sigio_private[1]);
  233. os_close_file(write_sigio_fds[0]);
  234. os_close_file(write_sigio_fds[1]);
  235. /* Critical section end */
  236. set_signals(flags);
  237. }
  238. int add_sigio_fd(int fd, int read)
  239. {
  240. int err = 0, i, n, events;
  241. sigio_lock();
  242. for(i = 0; i < current_poll.used; i++){
  243. if(current_poll.poll[i].fd == fd)
  244. goto out;
  245. }
  246. n = current_poll.used + 1;
  247. err = need_poll(n);
  248. if(err)
  249. goto out;
  250. for(i = 0; i < current_poll.used; i++)
  251. next_poll.poll[i] = current_poll.poll[i];
  252. if(read) events = POLLIN;
  253. else events = POLLOUT;
  254. next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
  255. .events = events,
  256. .revents = 0 });
  257. update_thread();
  258. out:
  259. sigio_unlock();
  260. return(err);
  261. }
  262. int ignore_sigio_fd(int fd)
  263. {
  264. struct pollfd *p;
  265. int err = 0, i, n = 0;
  266. sigio_lock();
  267. for(i = 0; i < current_poll.used; i++){
  268. if(current_poll.poll[i].fd == fd) break;
  269. }
  270. if(i == current_poll.used)
  271. goto out;
  272. err = need_poll(current_poll.used - 1);
  273. if(err)
  274. goto out;
  275. for(i = 0; i < current_poll.used; i++){
  276. p = &current_poll.poll[i];
  277. if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
  278. }
  279. if(n == i){
  280. printk("ignore_sigio_fd : fd %d not found\n", fd);
  281. err = -1;
  282. goto out;
  283. }
  284. update_thread();
  285. out:
  286. sigio_unlock();
  287. return(err);
  288. }
  289. static struct pollfd* setup_initial_poll(int fd)
  290. {
  291. struct pollfd *p;
  292. p = um_kmalloc(sizeof(struct pollfd));
  293. if (p == NULL) {
  294. printk("setup_initial_poll : failed to allocate poll\n");
  295. return NULL;
  296. }
  297. *p = ((struct pollfd) { .fd = fd,
  298. .events = POLLIN,
  299. .revents = 0 });
  300. return p;
  301. }
  302. void write_sigio_workaround(void)
  303. {
  304. unsigned long stack;
  305. struct pollfd *p;
  306. int err;
  307. int l_write_sigio_fds[2];
  308. int l_sigio_private[2];
  309. int l_write_sigio_pid;
  310. /* We call this *tons* of times - and most ones we must just fail. */
  311. sigio_lock();
  312. l_write_sigio_pid = write_sigio_pid;
  313. sigio_unlock();
  314. if (l_write_sigio_pid != -1)
  315. return;
  316. err = os_pipe(l_write_sigio_fds, 1, 1);
  317. if(err < 0){
  318. printk("write_sigio_workaround - os_pipe 1 failed, "
  319. "err = %d\n", -err);
  320. return;
  321. }
  322. err = os_pipe(l_sigio_private, 1, 1);
  323. if(err < 0){
  324. printk("write_sigio_workaround - os_pipe 1 failed, "
  325. "err = %d\n", -err);
  326. goto out_close1;
  327. }
  328. p = setup_initial_poll(l_sigio_private[1]);
  329. if(!p)
  330. goto out_close2;
  331. sigio_lock();
  332. /* Did we race? Don't try to optimize this, please, it's not so likely
  333. * to happen, and no more than once at the boot. */
  334. if(write_sigio_pid != -1)
  335. goto out_unlock;
  336. write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
  337. CLONE_FILES | CLONE_VM, &stack, 0);
  338. if (write_sigio_pid < 0)
  339. goto out_clear;
  340. if (write_sigio_irq(l_write_sigio_fds[0]))
  341. goto out_kill;
  342. /* Success, finally. */
  343. memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
  344. memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
  345. current_poll = ((struct pollfds) { .poll = p,
  346. .used = 1,
  347. .size = 1 });
  348. sigio_unlock();
  349. return;
  350. out_kill:
  351. l_write_sigio_pid = write_sigio_pid;
  352. write_sigio_pid = -1;
  353. sigio_unlock();
  354. /* Going to call waitpid, avoid holding the lock. */
  355. os_kill_process(l_write_sigio_pid, 1);
  356. goto out_free;
  357. out_clear:
  358. write_sigio_pid = -1;
  359. out_unlock:
  360. sigio_unlock();
  361. out_free:
  362. kfree(p);
  363. out_close2:
  364. os_close_file(l_sigio_private[0]);
  365. os_close_file(l_sigio_private[1]);
  366. out_close1:
  367. os_close_file(l_write_sigio_fds[0]);
  368. os_close_file(l_write_sigio_fds[1]);
  369. return;
  370. }
  371. int read_sigio_fd(int fd)
  372. {
  373. int n;
  374. char c;
  375. n = os_read_file(fd, &c, sizeof(c));
  376. if(n != sizeof(c)){
  377. if(n < 0) {
  378. printk("read_sigio_fd - read failed, err = %d\n", -n);
  379. return(n);
  380. }
  381. else {
  382. printk("read_sigio_fd - short read, bytes = %d\n", n);
  383. return(-EIO);
  384. }
  385. }
  386. return(n);
  387. }
  388. static void sigio_cleanup(void)
  389. {
  390. if (write_sigio_pid != -1) {
  391. os_kill_process(write_sigio_pid, 1);
  392. write_sigio_pid = -1;
  393. }
  394. }
  395. __uml_exitcall(sigio_cleanup);