[PATCH] uml: make some symbols static
[linux-2.6.git] / arch / um / os-Linux / sigio.c
blob0ecac563c7b32fccc6cb58ba734b3d794f18b1ab
1 /*
2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
4 */
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"
23 /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
24 * exitcall.
26 static int write_sigio_pid = -1;
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.
32 #define SIGIO_FDS_INIT {-1, -1}
34 static int write_sigio_fds[2] = SIGIO_FDS_INIT;
35 static int sigio_private[2] = SIGIO_FDS_INIT;
37 struct pollfds {
38 struct pollfd *poll;
39 int size;
40 int used;
43 /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
44 * synchronizes with it.
46 static struct pollfds current_poll = {
47 .poll = NULL,
48 .size = 0,
49 .used = 0
52 static struct pollfds next_poll = {
53 .poll = NULL,
54 .size = 0,
55 .used = 0
58 static int write_sigio_thread(void *unused)
60 struct pollfds *fds, tmp;
61 struct pollfd *p;
62 int i, n, respond_fd;
63 char c;
65 signal(SIGWINCH, SIG_IGN);
66 fds = &current_poll;
67 while(1){
68 n = poll(fds->poll, fds->used, -1);
69 if(n < 0){
70 if(errno == EINTR) continue;
71 printk("write_sigio_thread : poll returned %d, "
72 "errno = %d\n", n, errno);
74 for(i = 0; i < fds->used; i++){
75 p = &fds->poll[i];
76 if(p->revents == 0) continue;
77 if(p->fd == sigio_private[1]){
78 n = os_read_file(sigio_private[1], &c, sizeof(c));
79 if(n != sizeof(c))
80 printk("write_sigio_thread : "
81 "read failed, err = %d\n", -n);
82 tmp = current_poll;
83 current_poll = next_poll;
84 next_poll = tmp;
85 respond_fd = sigio_private[1];
87 else {
88 respond_fd = write_sigio_fds[1];
89 fds->used--;
90 memmove(&fds->poll[i], &fds->poll[i + 1],
91 (fds->used - i) * sizeof(*fds->poll));
94 n = os_write_file(respond_fd, &c, sizeof(c));
95 if(n != sizeof(c))
96 printk("write_sigio_thread : write failed, "
97 "err = %d\n", -n);
101 return 0;
104 static int need_poll(int n)
106 if(n <= next_poll.size){
107 next_poll.used = n;
108 return(0);
110 kfree(next_poll.poll);
111 next_poll.poll = um_kmalloc_atomic(n * sizeof(struct pollfd));
112 if(next_poll.poll == NULL){
113 printk("need_poll : failed to allocate new pollfds\n");
114 next_poll.size = 0;
115 next_poll.used = 0;
116 return(-1);
118 next_poll.size = n;
119 next_poll.used = n;
120 return(0);
123 /* Must be called with sigio_lock held, because it's needed by the marked
124 * critical section. */
125 static void update_thread(void)
127 unsigned long flags;
128 int n;
129 char c;
131 flags = set_signals(0);
132 n = os_write_file(sigio_private[0], &c, sizeof(c));
133 if(n != sizeof(c)){
134 printk("update_thread : write failed, err = %d\n", -n);
135 goto fail;
138 n = os_read_file(sigio_private[0], &c, sizeof(c));
139 if(n != sizeof(c)){
140 printk("update_thread : read failed, err = %d\n", -n);
141 goto fail;
144 set_signals(flags);
145 return;
146 fail:
147 /* Critical section start */
148 if(write_sigio_pid != -1)
149 os_kill_process(write_sigio_pid, 1);
150 write_sigio_pid = -1;
151 close(sigio_private[0]);
152 close(sigio_private[1]);
153 close(write_sigio_fds[0]);
154 close(write_sigio_fds[1]);
155 /* Critical section end */
156 set_signals(flags);
159 static int add_sigio_fd(int fd, int read)
161 int err = 0, i, n, events;
163 sigio_lock();
164 for(i = 0; i < current_poll.used; i++){
165 if(current_poll.poll[i].fd == fd)
166 goto out;
169 n = current_poll.used + 1;
170 err = need_poll(n);
171 if(err)
172 goto out;
174 for(i = 0; i < current_poll.used; i++)
175 next_poll.poll[i] = current_poll.poll[i];
177 if(read) events = POLLIN;
178 else events = POLLOUT;
180 next_poll.poll[n - 1] = ((struct pollfd) { .fd = fd,
181 .events = events,
182 .revents = 0 });
183 update_thread();
184 out:
185 sigio_unlock();
186 return(err);
189 int ignore_sigio_fd(int fd)
191 struct pollfd *p;
192 int err = 0, i, n = 0;
194 /* This is called from exitcalls elsewhere in UML - if
195 * sigio_cleanup has already run, then update_thread will hang
196 * or fail because the thread is no longer running.
198 if(write_sigio_pid == -1)
199 return -EIO;
201 sigio_lock();
202 for(i = 0; i < current_poll.used; i++){
203 if(current_poll.poll[i].fd == fd) break;
205 if(i == current_poll.used)
206 goto out;
208 err = need_poll(current_poll.used - 1);
209 if(err)
210 goto out;
212 for(i = 0; i < current_poll.used; i++){
213 p = &current_poll.poll[i];
214 if(p->fd != fd) next_poll.poll[n++] = current_poll.poll[i];
216 if(n == i){
217 printk("ignore_sigio_fd : fd %d not found\n", fd);
218 err = -1;
219 goto out;
222 update_thread();
223 out:
224 sigio_unlock();
225 return err;
228 static struct pollfd *setup_initial_poll(int fd)
230 struct pollfd *p;
232 p = um_kmalloc(sizeof(struct pollfd));
233 if (p == NULL) {
234 printk("setup_initial_poll : failed to allocate poll\n");
235 return NULL;
237 *p = ((struct pollfd) { .fd = fd,
238 .events = POLLIN,
239 .revents = 0 });
240 return p;
243 static void write_sigio_workaround(void)
245 unsigned long stack;
246 struct pollfd *p;
247 int err;
248 int l_write_sigio_fds[2];
249 int l_sigio_private[2];
250 int l_write_sigio_pid;
252 /* We call this *tons* of times - and most ones we must just fail. */
253 sigio_lock();
254 l_write_sigio_pid = write_sigio_pid;
255 sigio_unlock();
257 if (l_write_sigio_pid != -1)
258 return;
260 err = os_pipe(l_write_sigio_fds, 1, 1);
261 if(err < 0){
262 printk("write_sigio_workaround - os_pipe 1 failed, "
263 "err = %d\n", -err);
264 return;
266 err = os_pipe(l_sigio_private, 1, 1);
267 if(err < 0){
268 printk("write_sigio_workaround - os_pipe 2 failed, "
269 "err = %d\n", -err);
270 goto out_close1;
273 p = setup_initial_poll(l_sigio_private[1]);
274 if(!p)
275 goto out_close2;
277 sigio_lock();
279 /* Did we race? Don't try to optimize this, please, it's not so likely
280 * to happen, and no more than once at the boot. */
281 if(write_sigio_pid != -1)
282 goto out_free;
284 current_poll = ((struct pollfds) { .poll = p,
285 .used = 1,
286 .size = 1 });
288 if (write_sigio_irq(l_write_sigio_fds[0]))
289 goto out_clear_poll;
291 memcpy(write_sigio_fds, l_write_sigio_fds, sizeof(l_write_sigio_fds));
292 memcpy(sigio_private, l_sigio_private, sizeof(l_sigio_private));
294 write_sigio_pid = run_helper_thread(write_sigio_thread, NULL,
295 CLONE_FILES | CLONE_VM, &stack, 0);
297 if (write_sigio_pid < 0)
298 goto out_clear;
300 sigio_unlock();
301 return;
303 out_clear:
304 write_sigio_pid = -1;
305 write_sigio_fds[0] = -1;
306 write_sigio_fds[1] = -1;
307 sigio_private[0] = -1;
308 sigio_private[1] = -1;
309 out_clear_poll:
310 current_poll = ((struct pollfds) { .poll = NULL,
311 .size = 0,
312 .used = 0 });
313 out_free:
314 sigio_unlock();
315 kfree(p);
316 out_close2:
317 close(l_sigio_private[0]);
318 close(l_sigio_private[1]);
319 out_close1:
320 close(l_write_sigio_fds[0]);
321 close(l_write_sigio_fds[1]);
324 void maybe_sigio_broken(int fd, int read)
326 if(!isatty(fd))
327 return;
329 if((read || pty_output_sigio) && (!read || pty_close_sigio))
330 return;
332 write_sigio_workaround();
333 add_sigio_fd(fd, read);
336 static void sigio_cleanup(void)
338 if(write_sigio_pid != -1){
339 os_kill_process(write_sigio_pid, 1);
340 write_sigio_pid = -1;
344 __uml_exitcall(sigio_cleanup);