- got rid of include/async.h
[wine/multimedia.git] / server / signal.c
blob0fe7df3f654c5a8620c66ffd84cc923f68c92b0a
1 /*
2 * Server signal handling
4 * Copyright (C) 2003 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <signal.h>
24 #include <stdio.h>
25 #ifdef HAVE_SYS_POLL_H
26 #include <sys/poll.h>
27 #endif
28 #include <unistd.h>
30 #include "file.h"
31 #include "object.h"
32 #include "process.h"
33 #include "thread.h"
35 #if defined(linux) && defined(__SIGRTMIN)
36 /* the signal used by linuxthreads as exit signal for clone() threads */
37 # define SIG_PTHREAD_CANCEL (__SIGRTMIN+1)
38 #endif
40 typedef void (*signal_callback)(void);
42 struct handler
44 struct object obj; /* object header */
45 struct fd *fd; /* file descriptor for the pipe side */
46 int pipe_write; /* unix fd for the pipe write side */
47 volatile int pending; /* is signal pending? */
48 signal_callback callback; /* callback function */
51 static void handler_dump( struct object *obj, int verbose );
52 static void handler_destroy( struct object *obj );
54 static const struct object_ops handler_ops =
56 sizeof(struct handler), /* size */
57 handler_dump, /* dump */
58 no_add_queue, /* add_queue */
59 NULL, /* remove_queue */
60 NULL, /* signaled */
61 NULL, /* satisfied */
62 no_get_fd, /* get_fd */
63 handler_destroy /* destroy */
66 static void handler_poll_event( struct fd *fd, int event );
68 static const struct fd_ops handler_fd_ops =
70 NULL, /* get_poll_events */
71 handler_poll_event, /* poll_event */
72 no_flush, /* flush */
73 no_get_file_info, /* get_file_info */
74 no_queue_async, /* queue_async */
75 no_cancel_async /* cancel_async */
78 static struct handler *handler_sighup;
79 static struct handler *handler_sigterm;
80 static struct handler *handler_sigint;
81 static struct handler *handler_sigchld;
82 static struct handler *handler_sigio;
84 static sigset_t blocked_sigset;
86 /* create a signal handler */
87 static struct handler *create_handler( signal_callback callback )
89 struct handler *handler;
90 int fd[2];
92 if (pipe( fd ) == -1) return NULL;
93 if (!(handler = alloc_object( &handler_ops )))
95 close( fd[0] );
96 close( fd[1] );
97 return NULL;
99 handler->pipe_write = fd[1];
100 handler->pending = 0;
101 handler->callback = callback;
103 if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj )))
105 release_object( handler );
106 return NULL;
108 set_fd_events( handler->fd, POLLIN );
109 return handler;
112 /* handle a signal received for a given handler */
113 static void do_signal( struct handler *handler )
115 if (!handler->pending)
117 char dummy = 0;
118 handler->pending = 1;
119 write( handler->pipe_write, &dummy, 1 );
123 static void handler_dump( struct object *obj, int verbose )
125 struct handler *handler = (struct handler *)obj;
126 fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
129 static void handler_destroy( struct object *obj )
131 struct handler *handler = (struct handler *)obj;
132 if (handler->fd) release_object( handler->fd );
133 close( handler->pipe_write );
136 static void handler_poll_event( struct fd *fd, int event )
138 struct handler *handler = get_fd_user( fd );
140 if (event & (POLLERR | POLLHUP))
142 /* this is not supposed to happen */
143 fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
144 release_object( handler );
146 else if (event & POLLIN)
148 char dummy;
150 handler->pending = 0;
151 read( get_unix_fd( handler->fd ), &dummy, 1 );
152 handler->callback();
156 /* SIGHUP callback */
157 static void sighup_callback(void)
159 #ifdef DEBUG_OBJECTS
160 dump_objects();
161 #endif
164 /* SIGTERM callback */
165 static void sigterm_callback(void)
167 flush_registry();
168 exit(1);
171 /* SIGINT callback */
172 static void sigint_callback(void)
174 kill_all_processes( NULL, 1 );
175 flush_registry();
176 exit(1);
179 /* SIGHUP handler */
180 static void do_sighup()
182 do_signal( handler_sighup );
185 /* SIGTERM handler */
186 static void do_sigterm()
188 do_signal( handler_sigterm );
191 /* SIGINT handler */
192 static void do_sigint()
194 do_signal( handler_sigint );
197 /* SIGCHLD handler */
198 static void do_sigchld()
200 do_signal( handler_sigchld );
203 /* SIGIO handler */
204 #ifdef HAVE_SIGINFO_T_SI_FD
205 static void do_sigio( int signum, siginfo_t *si, void *x )
207 do_signal( handler_sigio );
208 do_change_notify( si->si_fd );
210 #endif
212 void init_signals(void)
214 struct sigaction action;
216 if (!(handler_sighup = create_handler( sighup_callback ))) goto error;
217 if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
218 if (!(handler_sigint = create_handler( sigint_callback ))) goto error;
219 if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
220 if (!(handler_sigio = create_handler( sigio_callback ))) goto error;
222 sigemptyset( &blocked_sigset );
223 sigaddset( &blocked_sigset, SIGCHLD );
224 sigaddset( &blocked_sigset, SIGHUP );
225 sigaddset( &blocked_sigset, SIGINT );
226 sigaddset( &blocked_sigset, SIGIO );
227 sigaddset( &blocked_sigset, SIGQUIT );
228 sigaddset( &blocked_sigset, SIGTERM );
229 #ifdef SIG_PTHREAD_CANCEL
230 sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL );
231 #endif
233 action.sa_mask = blocked_sigset;
234 action.sa_flags = 0;
235 action.sa_handler = do_sigchld;
236 sigaction( SIGCHLD, &action, NULL );
237 #ifdef SIG_PTHREAD_CANCEL
238 sigaction( SIG_PTHREAD_CANCEL, &action, NULL );
239 #endif
240 action.sa_handler = do_sighup;
241 sigaction( SIGHUP, &action, NULL );
242 action.sa_handler = do_sigint;
243 sigaction( SIGINT, &action, NULL );
244 action.sa_handler = do_sigterm;
245 sigaction( SIGQUIT, &action, NULL );
246 sigaction( SIGTERM, &action, NULL );
247 action.sa_handler = SIG_IGN;
248 sigaction( SIGXFSZ, &action, NULL );
249 #ifdef HAVE_SIGINFO_T_SI_FD
250 action.sa_sigaction = do_sigio;
251 action.sa_flags = SA_SIGINFO;
252 sigaction( SIGIO, &action, NULL );
253 #endif
254 return;
256 error:
257 fprintf( stderr, "failed to initialize signal handlers\n" );
258 exit(1);
261 void close_signals(void)
263 sigprocmask( SIG_BLOCK, &blocked_sigset, NULL );
264 release_object( handler_sighup );
265 release_object( handler_sigterm );
266 release_object( handler_sigint );
267 release_object( handler_sigchld );
268 release_object( handler_sigio );