Define RPC_FC_RANGE.
[wine.git] / server / signal.c
blob28880155349e390531bd2e95e36aaef74053eaa2
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_POLL_H
26 #include <poll.h>
27 #endif
28 #ifdef HAVE_SYS_POLL_H
29 #include <sys/poll.h>
30 #endif
31 #include <unistd.h>
33 #include "file.h"
34 #include "object.h"
35 #include "process.h"
36 #include "thread.h"
38 #if defined(linux) && defined(__SIGRTMIN)
39 /* the signal used by linuxthreads as exit signal for clone() threads */
40 # define SIG_PTHREAD_CANCEL (__SIGRTMIN+1)
41 #endif
43 typedef void (*signal_callback)(void);
45 struct handler
47 struct object obj; /* object header */
48 struct fd *fd; /* file descriptor for the pipe side */
49 int pipe_write; /* unix fd for the pipe write side */
50 volatile int pending; /* is signal pending? */
51 signal_callback callback; /* callback function */
54 static void handler_dump( struct object *obj, int verbose );
55 static void handler_destroy( struct object *obj );
57 static const struct object_ops handler_ops =
59 sizeof(struct handler), /* size */
60 handler_dump, /* dump */
61 no_add_queue, /* add_queue */
62 NULL, /* remove_queue */
63 NULL, /* signaled */
64 NULL, /* satisfied */
65 no_signal, /* signal */
66 no_get_fd, /* get_fd */
67 no_close_handle, /* close_handle */
68 handler_destroy /* destroy */
71 static void handler_poll_event( struct fd *fd, int event );
73 static const struct fd_ops handler_fd_ops =
75 NULL, /* get_poll_events */
76 handler_poll_event, /* poll_event */
77 no_flush, /* flush */
78 no_get_file_info, /* get_file_info */
79 no_queue_async, /* queue_async */
80 no_cancel_async /* cancel_async */
83 static struct handler *handler_sighup;
84 static struct handler *handler_sigterm;
85 static struct handler *handler_sigint;
86 static struct handler *handler_sigchld;
87 static struct handler *handler_sigio;
89 static sigset_t blocked_sigset;
91 static int watchdog;
93 /* create a signal handler */
94 static struct handler *create_handler( signal_callback callback )
96 struct handler *handler;
97 int fd[2];
99 if (pipe( fd ) == -1) return NULL;
100 if (!(handler = alloc_object( &handler_ops )))
102 close( fd[0] );
103 close( fd[1] );
104 return NULL;
106 handler->pipe_write = fd[1];
107 handler->pending = 0;
108 handler->callback = callback;
110 if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj )))
112 release_object( handler );
113 return NULL;
115 set_fd_events( handler->fd, POLLIN );
116 return handler;
119 /* handle a signal received for a given handler */
120 static void do_signal( struct handler *handler )
122 if (!handler->pending)
124 char dummy = 0;
125 handler->pending = 1;
126 write( handler->pipe_write, &dummy, 1 );
130 static void handler_dump( struct object *obj, int verbose )
132 struct handler *handler = (struct handler *)obj;
133 fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
136 static void handler_destroy( struct object *obj )
138 struct handler *handler = (struct handler *)obj;
139 if (handler->fd) release_object( handler->fd );
140 close( handler->pipe_write );
143 static void handler_poll_event( struct fd *fd, int event )
145 struct handler *handler = get_fd_user( fd );
147 if (event & (POLLERR | POLLHUP))
149 /* this is not supposed to happen */
150 fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
151 release_object( handler );
153 else if (event & POLLIN)
155 char dummy;
157 handler->pending = 0;
158 read( get_unix_fd( handler->fd ), &dummy, 1 );
159 handler->callback();
163 /* SIGHUP callback */
164 static void sighup_callback(void)
166 #ifdef DEBUG_OBJECTS
167 dump_objects();
168 #endif
171 /* SIGTERM callback */
172 static void sigterm_callback(void)
174 flush_registry();
175 exit(1);
178 /* SIGINT callback */
179 static void sigint_callback(void)
181 kill_all_processes( NULL, 1 );
182 flush_registry();
183 exit(1);
186 /* SIGHUP handler */
187 static void do_sighup( int signum )
189 do_signal( handler_sighup );
192 /* SIGTERM handler */
193 static void do_sigterm( int signum )
195 do_signal( handler_sigterm );
198 /* SIGINT handler */
199 static void do_sigint( int signum )
201 do_signal( handler_sigint );
204 /* SIGALRM handler */
205 static void do_sigalrm( int signum )
207 watchdog = 1;
210 /* SIGCHLD handler */
211 static void do_sigchld( int signum )
213 do_signal( handler_sigchld );
216 /* SIGIO handler */
217 #ifdef HAVE_SIGINFO_T_SI_FD
218 static void do_sigio( int signum, siginfo_t *si, void *x )
220 do_signal( handler_sigio );
221 do_change_notify( si->si_fd );
223 #endif
225 void start_watchdog(void)
227 alarm( 3 );
228 watchdog = 0;
231 void stop_watchdog(void)
233 alarm( 0 );
234 watchdog = 0;
237 int watchdog_triggered(void)
239 return watchdog != 0;
242 void init_signals(void)
244 struct sigaction action;
246 if (!(handler_sighup = create_handler( sighup_callback ))) goto error;
247 if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
248 if (!(handler_sigint = create_handler( sigint_callback ))) goto error;
249 if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
250 if (!(handler_sigio = create_handler( sigio_callback ))) goto error;
252 sigemptyset( &blocked_sigset );
253 sigaddset( &blocked_sigset, SIGCHLD );
254 sigaddset( &blocked_sigset, SIGHUP );
255 sigaddset( &blocked_sigset, SIGINT );
256 sigaddset( &blocked_sigset, SIGALRM );
257 sigaddset( &blocked_sigset, SIGIO );
258 sigaddset( &blocked_sigset, SIGQUIT );
259 sigaddset( &blocked_sigset, SIGTERM );
260 #ifdef SIG_PTHREAD_CANCEL
261 sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL );
262 #endif
264 action.sa_mask = blocked_sigset;
265 action.sa_flags = 0;
266 action.sa_handler = do_sigchld;
267 sigaction( SIGCHLD, &action, NULL );
268 #ifdef SIG_PTHREAD_CANCEL
269 sigaction( SIG_PTHREAD_CANCEL, &action, NULL );
270 #endif
271 action.sa_handler = do_sighup;
272 sigaction( SIGHUP, &action, NULL );
273 action.sa_handler = do_sigint;
274 sigaction( SIGINT, &action, NULL );
275 action.sa_handler = do_sigalrm;
276 sigaction( SIGALRM, &action, NULL );
277 action.sa_handler = do_sigterm;
278 sigaction( SIGQUIT, &action, NULL );
279 sigaction( SIGTERM, &action, NULL );
280 action.sa_handler = SIG_IGN;
281 sigaction( SIGXFSZ, &action, NULL );
282 #ifdef HAVE_SIGINFO_T_SI_FD
283 action.sa_sigaction = do_sigio;
284 action.sa_flags = SA_SIGINFO;
285 sigaction( SIGIO, &action, NULL );
286 #endif
287 return;
289 error:
290 fprintf( stderr, "failed to initialize signal handlers\n" );
291 exit(1);
294 void close_signals(void)
296 sigprocmask( SIG_BLOCK, &blocked_sigset, NULL );
297 release_object( handler_sighup );
298 release_object( handler_sigterm );
299 release_object( handler_sigint );
300 release_object( handler_sigchld );
301 release_object( handler_sigio );