sigio_user.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 "helper.h"
  21. #include "os.h"
  22. /* Changed during early boot */
  23. int pty_output_sigio = 0;
  24. int pty_close_sigio = 0;
  25. /* Used as a flag during SIGIO testing early in boot */
  26. static volatile int got_sigio = 0;
  27. void __init handler(int sig)
  28. {
  29. got_sigio = 1;
  30. }
  31. struct openpty_arg {
  32. int master;
  33. int slave;
  34. int err;
  35. };
  36. static void openpty_cb(void *arg)
  37. {
  38. struct openpty_arg *info = arg;
  39. info->err = 0;
  40. if(openpty(&info->master, &info->slave, NULL, NULL, NULL))
  41. info->err = -errno;
  42. }
  43. void __init check_one_sigio(void (*proc)(int, int))
  44. {
  45. struct sigaction old, new;
  46. struct openpty_arg pty = { .master = -1, .slave = -1 };
  47. int master, slave, err;
  48. initial_thread_cb(openpty_cb, &pty);
  49. if(pty.err){
  50. printk("openpty failed, errno = %d\n", -pty.err);
  51. return;
  52. }
  53. master = pty.master;
  54. slave = pty.slave;
  55. if((master == -1) || (slave == -1)){
  56. printk("openpty failed to allocate a pty\n");
  57. return;
  58. }
  59. /* Not now, but complain so we now where we failed. */
  60. err = raw(master);
  61. if (err < 0)
  62. panic("check_sigio : __raw failed, errno = %d\n", -err);
  63. err = os_sigio_async(master, slave);
  64. if(err < 0)
  65. panic("tty_fds : sigio_async failed, err = %d\n", -err);
  66. if(sigaction(SIGIO, NULL, &old) < 0)
  67. panic("check_sigio : sigaction 1 failed, errno = %d\n", errno);
  68. new = old;
  69. new.sa_handler = handler;
  70. if(sigaction(SIGIO, &new, NULL) < 0)
  71. panic("check_sigio : sigaction 2 failed, errno = %d\n", errno);
  72. got_sigio = 0;
  73. (*proc)(master, slave);
  74. os_close_file(master);
  75. os_close_file(slave);
  76. if(sigaction(SIGIO, &old, NULL) < 0)
  77. panic("check_sigio : sigaction 3 failed, errno = %d\n", errno);
  78. }
  79. static void tty_output(int master, int slave)
  80. {
  81. int n;
  82. char buf[512];
  83. printk("Checking that host ptys support output SIGIO...");
  84. memset(buf, 0, sizeof(buf));
  85. while(os_write_file(master, buf, sizeof(buf)) > 0) ;
  86. if(errno != EAGAIN)
  87. panic("check_sigio : write failed, errno = %d\n", errno);
  88. while(((n = os_read_file(slave, buf, sizeof(buf))) > 0) && !got_sigio) ;
  89. if (got_sigio) {
  90. printk("Yes\n");
  91. pty_output_sigio = 1;
  92. } else if (n == -EAGAIN) {
  93. printk("No, enabling workaround\n");
  94. } else {
  95. panic("check_sigio : read failed, err = %d\n", n);
  96. }
  97. }
  98. static void tty_close(int master, int slave)
  99. {
  100. printk("Checking that host ptys support SIGIO on close...");
  101. os_close_file(slave);
  102. if(got_sigio){
  103. printk("Yes\n");
  104. pty_close_sigio = 1;
  105. }
  106. else printk("No, enabling workaround\n");
  107. }
  108. void __init check_sigio(void)
  109. {
  110. if((os_access("/dev/ptmx", OS_ACC_R_OK) < 0) &&
  111. (os_access("/dev/ptyp0", OS_ACC_R_OK) < 0)){
  112. printk("No pseudo-terminals available - skipping pty SIGIO "
  113. "check\n");
  114. return;
  115. }
  116. check_one_sigio(tty_output);
  117. check_one_sigio(tty_close);
  118. }
  119. /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
  120. * exitcall.
  121. */
  122. static int write_sigio_pid = -1;
  123. /* These arrays are initialized before the sigio thread is started, and
  124. * the descriptors closed after it is killed. So, it can't see them change.
  125. * On the UML side, they are changed under the sigio_lock.
  126. */
  127. static int write_sigio_fds[2] = { -1, -1 };
  128. static int sigio_private[2] = { -1, -1 };
  129. struct pollfds {
  130. struct pollfd *poll;
  131. int size;
  132. int used;
  133. };
  134. /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
  135. * synchronizes with it.
  136. */
  137. struct pollfds current_poll = {
  138. .poll = NULL,
  139. .size = 0,
  140. .used = 0
  141. };
  142. struct pollfds next_poll = {
  143. .poll = NULL,
  144. .size = 0,
  145. .used = 0
  146. };
  147. static int write_sigio_thread(void *unused)
  148. {
  149. struct pollfds *fds, tmp;
  150. struct pollfd *p;
  151. int i, n, respond_fd;
  152. char c;
  153. signal(SIGWINCH, SIG_IGN);
  154. fds = &current_poll;
  155. while(1){
  156. n = poll(fds->poll, fds->used, -1);
  157. if(n < 0){
  158. if(errno == EINTR) continue;
  159. printk("write_sigio_thread : poll returned %d, "
  160. "errno = %d\n", n, errno);
  161. }
  162. for(i = 0; i < fds->used; i++){
  163. p = &fds->poll[i];
  164. if(p->revents == 0) continue;
  165. if(p->fd == sigio_private[1]){
  166. n = os_read_file(sigio_private[1], &c, sizeof(c));
  167. if(n != sizeof(c))
  168. printk("write_sigio_thread : "
  169. "read failed, err = %d\n", -n);
  170. tmp = current_poll;
  171. current_poll = next_poll;
  172. next_poll = tmp;
  173. respond_fd = sigio_private[1];
  174. }
  175. else {
  176. respond_fd = write_sigio_fds[1];
  177. fds->used--;
  178. memmove(&fds->poll[i], &fds->poll[i + 1],
  179. (fds->used - i) * sizeof(*fds->poll));
  180. }
  181. n = os_write_file(respond_fd, &c, sizeof(c));
  182. if(n != sizeof(c))
  183. printk("write_sigio_thread : write failed, "
  184. "err = %d\n", -n);
  185. }
  186. }
  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. if(next_poll.poll != NULL) 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 int 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(-1);
  296. }
  297. *p = ((struct pollfd) { .fd = fd,
  298. .events = POLLIN,
  299. .revents = 0 });
  300. current_poll = ((struct pollfds) { .poll = p,
  301. .used = 1,
  302. .size = 1 });
  303. return(0);
  304. }
  305. void write_sigio_workaround(void)
  306. {
  307. unsigned long stack;
  308. int err;
  309. sigio_lock();
  310. if(write_sigio_pid != -1)
  311. goto out;
  312. err = os_pipe(write_sigio_fds, 1, 1);
  313. if(err < 0){
  314. printk("write_sigio_workaround - os_pipe 1 failed, "
  315. "err = %d\n", -err);
  316. goto out;
  317. }
  318. err = os_pipe(sigio_private, 1, 1);
  319. if(err < 0){
  320. printk("write_sigio_workaround - os_pipe 2 failed, "
  321. "err = %d\n", -err);
  322. goto out_close1;
  323. }
  324. if(setup_initial_poll(sigio_private[1]))
  325. goto out_close2;
  326. write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
  327. CLONE_FILES | CLONE_VM, &stack, 0);
  328. if(write_sigio_pid < 0) goto out_close2;
  329. if(write_sigio_irq(write_sigio_fds[0]))
  330. goto out_kill;
  331. out:
  332. sigio_unlock();
  333. return;
  334. out_kill:
  335. os_kill_process(write_sigio_pid, 1);
  336. write_sigio_pid = -1;
  337. out_close2:
  338. os_close_file(sigio_private[0]);
  339. os_close_file(sigio_private[1]);
  340. out_close1:
  341. os_close_file(write_sigio_fds[0]);
  342. os_close_file(write_sigio_fds[1]);
  343. sigio_unlock();
  344. }
  345. int read_sigio_fd(int fd)
  346. {
  347. int n;
  348. char c;
  349. n = os_read_file(fd, &c, sizeof(c));
  350. if(n != sizeof(c)){
  351. if(n < 0) {
  352. printk("read_sigio_fd - read failed, err = %d\n", -n);
  353. return(n);
  354. }
  355. else {
  356. printk("read_sigio_fd - short read, bytes = %d\n", n);
  357. return(-EIO);
  358. }
  359. }
  360. return(n);
  361. }
  362. static void sigio_cleanup(void)
  363. {
  364. if (write_sigio_pid != -1) {
  365. os_kill_process(write_sigio_pid, 1);
  366. write_sigio_pid = -1;
  367. }
  368. }
  369. __uml_exitcall(sigio_cleanup);