2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
14 #include <sys/socket.h>
18 #include "kern_util.h"
19 #include "user_util.h"
22 #include "um_malloc.h"
24 /* Protected by sigio_lock(), also used by sigio_cleanup, which is an
27 static int write_sigio_pid
= -1;
29 /* These arrays are initialized before the sigio thread is started, and
30 * the descriptors closed after it is killed. So, it can't see them change.
31 * On the UML side, they are changed under the sigio_lock.
33 #define SIGIO_FDS_INIT {-1, -1}
35 static int write_sigio_fds
[2] = SIGIO_FDS_INIT
;
36 static int sigio_private
[2] = SIGIO_FDS_INIT
;
44 /* Protected by sigio_lock(). Used by the sigio thread, but the UML thread
45 * synchronizes with it.
47 static struct pollfds current_poll
;
48 static struct pollfds next_poll
;
49 static struct pollfds all_sigio_fds
;
51 static int write_sigio_thread(void *unused
)
53 struct pollfds
*fds
, tmp
;
58 signal(SIGWINCH
, SIG_IGN
);
61 n
= poll(fds
->poll
, fds
->used
, -1);
63 if(errno
== EINTR
) continue;
64 printk("write_sigio_thread : poll returned %d, "
65 "errno = %d\n", n
, errno
);
67 for(i
= 0; i
< fds
->used
; i
++){
69 if(p
->revents
== 0) continue;
70 if(p
->fd
== sigio_private
[1]){
71 n
= os_read_file(sigio_private
[1], &c
, sizeof(c
));
73 printk("write_sigio_thread : "
74 "read on socket failed, "
77 current_poll
= next_poll
;
79 respond_fd
= sigio_private
[1];
82 respond_fd
= write_sigio_fds
[1];
84 memmove(&fds
->poll
[i
], &fds
->poll
[i
+ 1],
85 (fds
->used
- i
) * sizeof(*fds
->poll
));
88 n
= os_write_file(respond_fd
, &c
, sizeof(c
));
90 printk("write_sigio_thread : write on socket "
91 "failed, err = %d\n", -n
);
98 static int need_poll(struct pollfds
*polls
, int n
)
105 new = um_kmalloc_atomic(n
* sizeof(struct pollfd
));
107 printk("need_poll : failed to allocate new pollfds\n");
111 memcpy(new, polls
->poll
, polls
->used
* sizeof(struct pollfd
));
119 /* Must be called with sigio_lock held, because it's needed by the marked
122 static void update_thread(void)
128 flags
= set_signals(0);
129 n
= os_write_file(sigio_private
[0], &c
, sizeof(c
));
131 printk("update_thread : write failed, err = %d\n", -n
);
135 n
= os_read_file(sigio_private
[0], &c
, sizeof(c
));
137 printk("update_thread : read failed, err = %d\n", -n
);
144 /* Critical section start */
145 if(write_sigio_pid
!= -1)
146 os_kill_process(write_sigio_pid
, 1);
147 write_sigio_pid
= -1;
148 close(sigio_private
[0]);
149 close(sigio_private
[1]);
150 close(write_sigio_fds
[0]);
151 close(write_sigio_fds
[1]);
152 /* Critical section end */
156 int add_sigio_fd(int fd
)
162 for(i
= 0; i
< all_sigio_fds
.used
; i
++){
163 if(all_sigio_fds
.poll
[i
].fd
== fd
)
166 if(i
== all_sigio_fds
.used
)
169 p
= &all_sigio_fds
.poll
[i
];
171 for(i
= 0; i
< current_poll
.used
; i
++){
172 if(current_poll
.poll
[i
].fd
== fd
)
176 n
= current_poll
.used
;
177 err
= need_poll(&next_poll
, n
+ 1);
181 memcpy(next_poll
.poll
, current_poll
.poll
,
182 current_poll
.used
* sizeof(struct pollfd
));
183 next_poll
.poll
[n
] = *p
;
184 next_poll
.used
= n
+ 1;
191 int ignore_sigio_fd(int fd
)
194 int err
= 0, i
, n
= 0;
196 /* This is called from exitcalls elsewhere in UML - if
197 * sigio_cleanup has already run, then update_thread will hang
198 * or fail because the thread is no longer running.
200 if(write_sigio_pid
== -1)
204 for(i
= 0; i
< current_poll
.used
; i
++){
205 if(current_poll
.poll
[i
].fd
== fd
) break;
207 if(i
== current_poll
.used
)
210 err
= need_poll(&next_poll
, current_poll
.used
- 1);
214 for(i
= 0; i
< current_poll
.used
; i
++){
215 p
= ¤t_poll
.poll
[i
];
217 next_poll
.poll
[n
++] = *p
;
219 next_poll
.used
= current_poll
.used
- 1;
227 static struct pollfd
*setup_initial_poll(int fd
)
231 p
= um_kmalloc(sizeof(struct pollfd
));
233 printk("setup_initial_poll : failed to allocate poll\n");
236 *p
= ((struct pollfd
) { .fd
= fd
,
242 static void write_sigio_workaround(void)
247 int l_write_sigio_fds
[2];
248 int l_sigio_private
[2];
249 int l_write_sigio_pid
;
251 /* We call this *tons* of times - and most ones we must just fail. */
253 l_write_sigio_pid
= write_sigio_pid
;
256 if (l_write_sigio_pid
!= -1)
259 err
= os_pipe(l_write_sigio_fds
, 1, 1);
261 printk("write_sigio_workaround - os_pipe 1 failed, "
265 err
= os_pipe(l_sigio_private
, 1, 1);
267 printk("write_sigio_workaround - os_pipe 2 failed, "
272 p
= setup_initial_poll(l_sigio_private
[1]);
278 /* Did we race? Don't try to optimize this, please, it's not so likely
279 * to happen, and no more than once at the boot. */
280 if(write_sigio_pid
!= -1)
283 current_poll
= ((struct pollfds
) { .poll
= p
,
287 if (write_sigio_irq(l_write_sigio_fds
[0]))
290 memcpy(write_sigio_fds
, l_write_sigio_fds
, sizeof(l_write_sigio_fds
));
291 memcpy(sigio_private
, l_sigio_private
, sizeof(l_sigio_private
));
293 write_sigio_pid
= run_helper_thread(write_sigio_thread
, NULL
,
294 CLONE_FILES
| CLONE_VM
, &stack
, 0);
296 if (write_sigio_pid
< 0)
303 write_sigio_pid
= -1;
304 write_sigio_fds
[0] = -1;
305 write_sigio_fds
[1] = -1;
306 sigio_private
[0] = -1;
307 sigio_private
[1] = -1;
309 current_poll
= ((struct pollfds
) { .poll
= NULL
,
316 close(l_sigio_private
[0]);
317 close(l_sigio_private
[1]);
319 close(l_write_sigio_fds
[0]);
320 close(l_write_sigio_fds
[1]);
323 void maybe_sigio_broken(int fd
, int read
)
330 if((read
|| pty_output_sigio
) && (!read
|| pty_close_sigio
))
333 write_sigio_workaround();
336 err
= need_poll(&all_sigio_fds
, all_sigio_fds
.used
+ 1);
340 all_sigio_fds
.poll
[all_sigio_fds
.used
++] =
341 ((struct pollfd
) { .fd
= fd
,
342 .events
= read
? POLLIN
: POLLOUT
,
348 static void sigio_cleanup(void)
350 if(write_sigio_pid
!= -1){
351 os_kill_process(write_sigio_pid
, 1);
352 write_sigio_pid
= -1;
356 __uml_exitcall(sigio_cleanup
);
This page took 0.11827 seconds and 6 git commands to generate.