diff options
author | Jeff Dike <jdike@addtoit.com> | 2006-03-27 04:14:33 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-03-27 11:44:38 -0500 |
commit | f206aabb035318ac4bafbf0b87798335de3634df (patch) | |
tree | 0fac4b6f593af478f0792e6c94f0881fd81645cd /arch/um/os-Linux/sigio.c | |
parent | 8e367065eea04a6fde5cb8f3854164af99c45693 (diff) |
[PATCH] uml: move sigio_user.c to os-Linux/sigio.c
The serial UML OS-abstraction layer patch (um/kernel dir).
This moves sigio_user.c to os-Linux dir
Signed-off-by: Gennady Sharapov <Gennady.V.Sharapov@intel.com>
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'arch/um/os-Linux/sigio.c')
-rw-r--r-- | arch/um/os-Linux/sigio.c | 324 |
1 files changed, 324 insertions, 0 deletions
diff --git a/arch/um/os-Linux/sigio.c b/arch/um/os-Linux/sigio.c new file mode 100644 index 000000000000..7604c404c4c2 --- /dev/null +++ b/arch/um/os-Linux/sigio.c | |||
@@ -0,0 +1,324 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2002 Jeff Dike (jdike@karaya.com) | ||
3 | * Licensed under the GPL | ||
4 | */ | ||
5 | |||
6 | #include <unistd.h> | ||
7 | #include <stdlib.h> | ||
8 | #include <termios.h> | ||
9 | #include <pty.h> | ||
10 | #include <signal.h> | ||
11 | #include <errno.h> | ||
12 | #include <string.h> | ||
13 | #include <sched.h> | ||
14 | #include <sys/socket.h> | ||
15 | #include <sys/poll.h> | ||
16 | #include "init.h" | ||
17 | #include "user.h" | ||
18 | #include "kern_util.h" | ||
19 | #include "user_util.h" | ||
20 | #include "sigio.h" | ||
21 | #include "os.h" | ||
22 | |||
23 | /* Protected by sigio_lock(), also used by sigio_cleanup, which is an | ||
24 | * exitcall. | ||
25 | */ | ||
26 | static int write_sigio_pid = -1; | ||
27 | |||
28 | /* These arrays are initialized before the sigio thread is started, and | ||
29 | * the descriptors closed after it is killed. So, it can't see them change. | ||
30 | * On the UML side, they are changed under the sigio_lock. | ||
31 | */ | ||
32 | static int write_sigio_fds[2] = { -1, -1 }; | ||
33 | static int sigio_private[2] = { -1, -1 }; | ||
34 | |||
35 | struct pollfds { | ||
36 | struct pollfd *poll; | ||
37 | int size; | ||
38 | int used; | ||
39 | }; | ||
40 | |||
41 | /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread | ||
42 | * synchronizes with it. | ||
43 | */ | ||
44 | struct pollfds current_poll = { | ||
45 | .poll = NULL, | ||
46 | .size = 0, | ||
47 | .used = 0 | ||
48 | }; | ||
49 | |||
50 | struct pollfds next_poll = { | ||
51 | .poll = NULL, | ||
52 | .size = 0, | ||
53 | .used = 0 | ||
54 | }; | ||
55 | |||
56 | static int write_sigio_thread(void *unused) | ||
57 | { | ||
58 | struct pollfds *fds, tmp; | ||
59 | struct pollfd *p; | ||
60 | int i, n, respond_fd; | ||
61 | char c; | ||
62 | |||
63 | signal(SIGWINCH, SIG_IGN); | ||
64 | fds = ¤t_poll; | ||
65 | while(1){ | ||
66 | n = poll(fds->poll, fds->used, -1); | ||
67 | if(n < 0){ | ||
68 | if(errno == EINTR) continue; | ||
69 | printk("write_sigio_thread : poll returned %d, " | ||
70 | "errno = %d\n", n, errno); | ||
71 | } | ||
72 | for(i = 0; i < fds->used; i++){ | ||
73 | p = &fds->poll[i]; | ||
74 | if(p->revents == 0) continue; | ||
75 | if(p->fd == sigio_private[1]){ | ||
76 | n = os_read_file(sigio_private[1], &c, sizeof(c)); | ||
77 | if(n != sizeof(c)) | ||
78 | printk("write_sigio_thread : " | ||
79 | "read failed, err = %d\n", -n); | ||
80 | tmp = current_poll; | ||
81 | current_poll = next_poll; | ||
82 | next_poll = tmp; | ||
83 | respond_fd = sigio_private[1]; | ||
84 | } | ||
85 | else { | ||
86 | respond_fd = write_sigio_fds[1]; | ||
87 | fds->used--; | ||
88 | memmove(&fds->poll[i], &fds->poll[i + 1], | ||
89 | (fds->used - i) * sizeof(*fds->poll)); | ||
90 | } | ||
91 | |||
92 | n = os_write_file(respond_fd, &c, sizeof(c)); | ||
93 | if(n != sizeof(c)) | ||
94 | printk("write_sigio_thread : write failed, " | ||
95 | "err = %d\n", -n); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | return 0; | ||
100 | } | ||
101 | |||
102 | static int need_poll(int n) | ||
103 | { | ||
104 | if(n <= next_poll.size){ | ||
105 | next_poll.used = n; | ||
106 | return(0); | ||
107 | } | ||
108 | kfree(next_poll.poll); | ||
109 | next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd)); | ||
110 | if(next_poll.poll == NULL){ | ||
111 | printk("need_poll : failed to allocate new pollfds\n"); | ||
112 | next_poll.size = 0; | ||
113 | next_poll.used = 0; | ||
114 | return(-1); | ||
115 | } | ||
116 | next_poll.size = n; | ||
117 | next_poll.used = n; | ||
118 | return(0); | ||
119 | } | ||
120 | |||
121 | /* Must be called with sigio_lock held, because it's needed by the marked | ||
122 | * critical section. */ | ||
123 | static void update_thread(void) | ||
124 | { | ||
125 | unsigned long flags; | ||
126 | int n; | ||
127 | char c; | ||
128 | |||
129 | flags = set_signals(0); | ||
130 | n = os_write_file(sigio_private[0], &c, sizeof(c)); | ||
131 | if(n != sizeof(c)){ | ||
132 | printk("update_thread : write failed, err = %d\n", -n); | ||
133 | goto fail; | ||
134 | } | ||
135 | |||
136 | n = os_read_file(sigio_private[0], &c, sizeof(c)); | ||
137 | if(n != sizeof(c)){ | ||
138 | printk("update_thread : read failed, err = %d\n", -n); | ||
139 | goto fail; | ||
140 | } | ||
141 | |||
142 | set_signals(flags); | ||
143 | return; | ||
144 | fail: | ||
145 | /* Critical section start */ | ||
146 | if(write_sigio_pid != -1) | ||
147 | os_kill_process(write_sigio_pid, 1); | ||
148 | write_sigio_pid = -1; | ||
149 | close(sigio_private[0]); | ||
150 | close(sigio_private[1]); | ||
151 | close(write_sigio_fds[0]); | ||
152 | close(write_sigio_fds[1]); | ||
153 | /* Critical section end */ | ||
154 | set_signals(flags); | ||
155 | } | ||
156 | |||
157 | int add_sigio_fd(int fd, int read) | ||
158 | { | ||
159 | int err = 0, i, n, events; | ||
160 | |||
161 | sigio_lock(); | ||
162 | for(i = 0; i < current_poll.used; i++){ | ||
163 | if(current_poll.poll[i].fd == fd) | ||
164 | goto out; | ||
165 | } | ||
166 | |||
167 | n = current_poll.used + 1; | ||
168 | err = need_poll(n); | ||
169 | if(err) | ||
170 | goto out; | ||
171 | |||
172 | for(i = 0; i < current_poll.used; i++) | ||
173 | next_poll.poll[i] = current_poll.poll[i]; | ||
174 | |||
175 | if(read) events = POLLIN; | ||
176 | else events = POLLOUT; | ||
177 | |||
178 | next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd, | ||
179 | .events = events, | ||
180 | .revents = 0 }); | ||
181 | update_thread(); | ||
182 | out: | ||
183 | sigio_unlock(); | ||
184 | return(err); | ||
185 | } | ||
186 | |||
187 | int ignore_sigio_fd(int fd) | ||
188 | { | ||
189 | struct pollfd *p; | ||
190 | int err = 0, i, n = 0; | ||
191 | |||
192 | sigio_lock(); | ||
193 | for(i = 0; i < current_poll.used; i++){ | ||
194 | if(current_poll.poll[i].fd == fd) break; | ||
195 | } | ||
196 | if(i == current_poll.used) | ||
197 | goto out; | ||
198 | |||
199 | err = need_poll(current_poll.used - 1); | ||
200 | if(err) | ||
201 | goto out; | ||
202 | |||
203 | for(i = 0; i < current_poll.used; i++){ | ||
204 | p = ¤t_poll.poll[i]; | ||
205 | if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i]; | ||
206 | } | ||
207 | if(n == i){ | ||
208 | printk("ignore_sigio_fd : fd %d not found\n", fd); | ||
209 | err = -1; | ||
210 | goto out; | ||
211 | } | ||
212 | |||
213 | update_thread(); | ||
214 | out: | ||
215 | sigio_unlock(); | ||
216 | return(err); | ||
217 | } | ||
218 | |||
219 | static struct pollfd *setup_initial_poll(int fd) | ||
220 | { | ||
221 | struct pollfd *p; | ||
222 | |||
223 | p = um_kmalloc(sizeof(struct pollfd)); | ||
224 | if (p == NULL) { | ||
225 | printk("setup_initial_poll : failed to allocate poll\n"); | ||
226 | return NULL; | ||
227 | } | ||
228 | *p = ((struct pollfd) { .fd = fd, | ||
229 | .events = POLLIN, | ||
230 | .revents = 0 }); | ||
231 | return p; | ||
232 | } | ||
233 | |||
234 | void write_sigio_workaround(void) | ||
235 | { | ||
236 | unsigned long stack; | ||
237 | struct pollfd *p; | ||
238 | int err; | ||
239 | int l_write_sigio_fds[2]; | ||
240 | int l_sigio_private[2]; | ||
241 | int l_write_sigio_pid; | ||
242 | |||
243 | /* We call this *tons* of times - and most ones we must just fail. */ | ||
244 | sigio_lock(); | ||
245 | l_write_sigio_pid = write_sigio_pid; | ||
246 | sigio_unlock(); | ||
247 | |||
248 | if (l_write_sigio_pid != -1) | ||
249 | return; | ||
250 | |||
251 | err = os_pipe(l_write_sigio_fds, 1, 1); | ||
252 | if(err < 0){ | ||
253 | printk("write_sigio_workaround - os_pipe 1 failed, " | ||
254 | "err = %d\n", -err); | ||
255 | return; | ||
256 | } | ||
257 | err = os_pipe(l_sigio_private, 1, 1); | ||
258 | if(err < 0){ | ||
259 | printk("write_sigio_workaround - os_pipe 2 failed, " | ||
260 | "err = %d\n", -err); | ||
261 | goto out_close1; | ||
262 | } | ||
263 | |||
264 | p = setup_initial_poll(l_sigio_private[1]); | ||
265 | if(!p) | ||
266 | goto out_close2; | ||
267 | |||
268 | sigio_lock(); | ||
269 | |||
270 | /* Did we race? Don't try to optimize this, please, it's not so likely | ||
271 | * to happen, and no more than once at the boot. */ | ||
272 | if(write_sigio_pid != -1) | ||
273 | goto out_unlock; | ||
274 | |||
275 | write_sigio_pid = run_helper_thread(write_sigio_thread, NULL, | ||
276 | CLONE_FILES | CLONE_VM, &stack, 0); | ||
277 | |||
278 | if (write_sigio_pid < 0) | ||
279 | goto out_clear; | ||
280 | |||
281 | if (write_sigio_irq(l_write_sigio_fds[0])) | ||
282 | goto out_kill; | ||
283 | |||
284 | /* Success, finally. */ | ||
285 | memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds)); | ||
286 | memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private)); | ||
287 | |||
288 | current_poll = ((struct pollfds) { .poll = p, | ||
289 | .used = 1, | ||
290 | .size = 1 }); | ||
291 | |||
292 | sigio_unlock(); | ||
293 | return; | ||
294 | |||
295 | out_kill: | ||
296 | l_write_sigio_pid = write_sigio_pid; | ||
297 | write_sigio_pid = -1; | ||
298 | sigio_unlock(); | ||
299 | /* Going to call waitpid, avoid holding the lock. */ | ||
300 | os_kill_process(l_write_sigio_pid, 1); | ||
301 | goto out_free; | ||
302 | |||
303 | out_clear: | ||
304 | write_sigio_pid = -1; | ||
305 | out_unlock: | ||
306 | sigio_unlock(); | ||
307 | out_free: | ||
308 | kfree(p); | ||
309 | out_close2: | ||
310 | close(l_sigio_private[0]); | ||
311 | close(l_sigio_private[1]); | ||
312 | out_close1: | ||
313 | close(l_write_sigio_fds[0]); | ||
314 | close(l_write_sigio_fds[1]); | ||
315 | return; | ||
316 | } | ||
317 | |||
318 | void sigio_cleanup(void) | ||
319 | { | ||
320 | if(write_sigio_pid != -1){ | ||
321 | os_kill_process(write_sigio_pid, 1); | ||
322 | write_sigio_pid = -1; | ||
323 | } | ||
324 | } | ||