Commit | Line | Data |
---|---|---|
63ae2a94 JD |
1 | /* |
2 | * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com) | |
3 | * Licensed under the GPL | |
4 | */ | |
5 | ||
6 | #include <stdlib.h> | |
7 | #include <unistd.h> | |
8 | #include <errno.h> | |
9 | #include <signal.h> | |
10 | #include <string.h> | |
11 | #include <sys/poll.h> | |
12 | #include <sys/types.h> | |
13 | #include <sys/time.h> | |
63ae2a94 JD |
14 | #include "user.h" |
15 | #include "process.h" | |
16 | #include "sigio.h" | |
17 | #include "irq_user.h" | |
18 | #include "os.h" | |
c13e5690 | 19 | #include "um_malloc.h" |
63ae2a94 | 20 | |
f2e62992 JD |
21 | /* |
22 | * Locked by irq_lock in arch/um/kernel/irq.c. Changed by os_create_pollfd | |
23 | * and os_free_irq_by_cb, which are called under irq_lock. | |
24 | */ | |
63ae2a94 JD |
25 | static struct pollfd *pollfds = NULL; |
26 | static int pollfds_num = 0; | |
27 | static int pollfds_size = 0; | |
28 | ||
29 | int os_waiting_for_events(struct irq_fd *active_fds) | |
30 | { | |
31 | struct irq_fd *irq_fd; | |
32 | int i, n, err; | |
33 | ||
34 | n = poll(pollfds, pollfds_num, 0); | |
191ef966 | 35 | if (n < 0) { |
63ae2a94 | 36 | err = -errno; |
191ef966 | 37 | if (errno != EINTR) |
63ae2a94 JD |
38 | printk("sigio_handler: os_waiting_for_events:" |
39 | " poll returned %d, errno = %d\n", n, errno); | |
40 | return err; | |
41 | } | |
42 | ||
191ef966 | 43 | if (n == 0) |
63ae2a94 JD |
44 | return 0; |
45 | ||
46 | irq_fd = active_fds; | |
47 | ||
191ef966 JJ |
48 | for (i = 0; i < pollfds_num; i++) { |
49 | if (pollfds[i].revents != 0) { | |
63ae2a94 JD |
50 | irq_fd->current_events = pollfds[i].revents; |
51 | pollfds[i].fd = -1; | |
52 | } | |
53 | irq_fd = irq_fd->next; | |
54 | } | |
55 | return n; | |
56 | } | |
57 | ||
63ae2a94 JD |
58 | int os_create_pollfd(int fd, int events, void *tmp_pfd, int size_tmpfds) |
59 | { | |
60 | if (pollfds_num == pollfds_size) { | |
61 | if (size_tmpfds <= pollfds_size * sizeof(pollfds[0])) { | |
62 | /* return min size needed for new pollfds area */ | |
f2e62992 | 63 | return (pollfds_size + 1) * sizeof(pollfds[0]); |
63ae2a94 JD |
64 | } |
65 | ||
191ef966 | 66 | if (pollfds != NULL) { |
63ae2a94 JD |
67 | memcpy(tmp_pfd, pollfds, |
68 | sizeof(pollfds[0]) * pollfds_size); | |
69 | /* remove old pollfds */ | |
70 | kfree(pollfds); | |
71 | } | |
72 | pollfds = tmp_pfd; | |
73 | pollfds_size++; | |
191ef966 JJ |
74 | } else |
75 | kfree(tmp_pfd); /* remove not used tmp_pfd */ | |
63ae2a94 | 76 | |
191ef966 JJ |
77 | pollfds[pollfds_num] = ((struct pollfd) { .fd = fd, |
78 | .events = events, | |
79 | .revents = 0 }); | |
63ae2a94 JD |
80 | pollfds_num++; |
81 | ||
191ef966 | 82 | return 0; |
63ae2a94 JD |
83 | } |
84 | ||
85 | void os_free_irq_by_cb(int (*test)(struct irq_fd *, void *), void *arg, | |
86 | struct irq_fd *active_fds, struct irq_fd ***last_irq_ptr2) | |
87 | { | |
88 | struct irq_fd **prev; | |
89 | int i = 0; | |
90 | ||
91 | prev = &active_fds; | |
191ef966 JJ |
92 | while (*prev != NULL) { |
93 | if ((*test)(*prev, arg)) { | |
63ae2a94 | 94 | struct irq_fd *old_fd = *prev; |
191ef966 JJ |
95 | if ((pollfds[i].fd != -1) && |
96 | (pollfds[i].fd != (*prev)->fd)) { | |
63ae2a94 JD |
97 | printk("os_free_irq_by_cb - mismatch between " |
98 | "active_fds and pollfds, fd %d vs %d\n", | |
99 | (*prev)->fd, pollfds[i].fd); | |
100 | goto out; | |
101 | } | |
102 | ||
103 | pollfds_num--; | |
104 | ||
105 | /* This moves the *whole* array after pollfds[i] | |
106 | * (though it doesn't spot as such)! | |
107 | */ | |
63ae2a94 JD |
108 | memmove(&pollfds[i], &pollfds[i + 1], |
109 | (pollfds_num - i) * sizeof(pollfds[0])); | |
110 | if(*last_irq_ptr2 == &old_fd->next) | |
111 | *last_irq_ptr2 = prev; | |
112 | ||
113 | *prev = (*prev)->next; | |
114 | if(old_fd->type == IRQ_WRITE) | |
115 | ignore_sigio_fd(old_fd->fd); | |
116 | kfree(old_fd); | |
117 | continue; | |
118 | } | |
119 | prev = &(*prev)->next; | |
120 | i++; | |
121 | } | |
122 | out: | |
123 | return; | |
124 | } | |
125 | ||
63ae2a94 JD |
126 | int os_get_pollfd(int i) |
127 | { | |
191ef966 | 128 | return pollfds[i].fd; |
63ae2a94 JD |
129 | } |
130 | ||
131 | void os_set_pollfd(int i, int fd) | |
132 | { | |
133 | pollfds[i].fd = fd; | |
134 | } | |
135 | ||
136 | void os_set_ioignore(void) | |
137 | { | |
4b84c69b | 138 | signal(SIGIO, SIG_IGN); |
63ae2a94 JD |
139 | } |
140 | ||
141 | void init_irq_signals(int on_sigstack) | |
142 | { | |
63ae2a94 JD |
143 | int flags; |
144 | ||
145 | flags = on_sigstack ? SA_ONSTACK : 0; | |
63ae2a94 | 146 | |
63ae2a94 | 147 | set_handler(SIGIO, (__sighandler_t) sig_handler, flags | SA_RESTART, |
61b63c55 | 148 | SIGUSR1, SIGIO, SIGWINCH, SIGVTALRM, -1); |
63ae2a94 JD |
149 | signal(SIGWINCH, SIG_IGN); |
150 | } |