/arch/um/kernel/sigio_user.c

https://bitbucket.org/evzijst/gittest · C · 431 lines · 347 code · 63 blank · 21 comment · 82 complexity · f5c7caed4fbe3dad93d491731b41c7e3 MD5 · raw file

  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. 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. if(next_poll.poll != NULL) 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(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);