ntdll: Simplify the platform-specific dispatcher interface.
[wine.git] / server / signal.c
blob0c22c157f2b9e63fc65a1bf28c446d9308b3f292
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <signal.h>
24 #include <stdio.h>
25 #include <sys/time.h>
26 #ifdef HAVE_POLL_H
27 #include <poll.h>
28 #endif
29 #ifdef HAVE_SYS_POLL_H
30 #include <sys/poll.h>
31 #endif
32 #ifdef HAVE_SYS_RESOURCE_H
33 #include <sys/resource.h>
34 #endif
35 #include <unistd.h>
37 #include "file.h"
38 #include "object.h"
39 #include "process.h"
40 #include "thread.h"
41 #include "request.h"
43 #if defined(linux) && defined(__SIGRTMIN)
44 /* the signal used by linuxthreads as exit signal for clone() threads */
45 # define SIG_PTHREAD_CANCEL (__SIGRTMIN+1)
46 #endif
48 typedef void (*signal_callback)(void);
50 struct handler
52 struct object obj; /* object header */
53 struct fd *fd; /* file descriptor for the pipe side */
54 int pipe_write; /* unix fd for the pipe write side */
55 volatile int pending; /* is signal pending? */
56 signal_callback callback; /* callback function */
59 static void handler_dump( struct object *obj, int verbose );
60 static void handler_destroy( struct object *obj );
62 static const struct object_ops handler_ops =
64 sizeof(struct handler), /* size */
65 &no_type, /* type */
66 handler_dump, /* dump */
67 no_add_queue, /* add_queue */
68 NULL, /* remove_queue */
69 NULL, /* signaled */
70 NULL, /* satisfied */
71 no_signal, /* signal */
72 no_get_fd, /* get_fd */
73 default_map_access, /* map_access */
74 default_get_sd, /* get_sd */
75 default_set_sd, /* set_sd */
76 no_get_full_name, /* get_full_name */
77 no_lookup_name, /* lookup_name */
78 no_link_name, /* link_name */
79 NULL, /* unlink_name */
80 no_open_file, /* open_file */
81 no_kernel_obj_list, /* get_kernel_obj_list */
82 no_close_handle, /* close_handle */
83 handler_destroy /* destroy */
86 static void handler_poll_event( struct fd *fd, int event );
88 static const struct fd_ops handler_fd_ops =
90 NULL, /* get_poll_events */
91 handler_poll_event, /* poll_event */
92 NULL, /* flush */
93 NULL, /* get_fd_type */
94 NULL, /* ioctl */
95 NULL, /* queue_async */
96 NULL /* reselect_async */
99 static struct handler *handler_sighup;
100 static struct handler *handler_sigterm;
101 static struct handler *handler_sigint;
102 static struct handler *handler_sigchld;
103 static struct handler *handler_sigio;
105 static int watchdog;
107 /* create a signal handler */
108 static struct handler *create_handler( signal_callback callback )
110 struct handler *handler;
111 int fd[2];
113 if (pipe( fd ) == -1) return NULL;
114 if (!(handler = alloc_object( &handler_ops )))
116 close( fd[0] );
117 close( fd[1] );
118 return NULL;
120 handler->pipe_write = fd[1];
121 handler->pending = 0;
122 handler->callback = callback;
124 if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj, 0 )))
126 release_object( handler );
127 return NULL;
129 set_fd_events( handler->fd, POLLIN );
130 make_object_permanent( &handler->obj );
131 return handler;
134 /* handle a signal received for a given handler */
135 static void do_signal( struct handler *handler )
137 if (!handler->pending)
139 char dummy = 0;
140 handler->pending = 1;
141 write( handler->pipe_write, &dummy, 1 );
145 static void handler_dump( struct object *obj, int verbose )
147 struct handler *handler = (struct handler *)obj;
148 fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
151 static void handler_destroy( struct object *obj )
153 struct handler *handler = (struct handler *)obj;
154 if (handler->fd) release_object( handler->fd );
155 close( handler->pipe_write );
158 static void handler_poll_event( struct fd *fd, int event )
160 struct handler *handler = get_fd_user( fd );
162 if (event & (POLLERR | POLLHUP))
164 /* this is not supposed to happen */
165 fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
166 release_object( handler );
168 else if (event & POLLIN)
170 char dummy;
172 handler->pending = 0;
173 read( get_unix_fd( handler->fd ), &dummy, 1 );
174 handler->callback();
178 /* SIGHUP callback */
179 static void sighup_callback(void)
181 #ifdef DEBUG_OBJECTS
182 dump_objects();
183 #endif
186 /* SIGTERM callback */
187 static void sigterm_callback(void)
189 flush_registry();
190 exit(1);
193 /* SIGINT callback */
194 static void sigint_callback(void)
196 shutdown_master_socket();
199 /* SIGHUP handler */
200 static void do_sighup( int signum )
202 do_signal( handler_sighup );
205 /* SIGTERM handler */
206 static void do_sigterm( int signum )
208 do_signal( handler_sigterm );
211 /* SIGINT handler */
212 static void do_sigint( int signum )
214 do_signal( handler_sigint );
217 /* SIGALRM handler */
218 static void do_sigalrm( int signum )
220 watchdog = 1;
223 /* SIGCHLD handler */
224 static void do_sigchld( int signum )
226 do_signal( handler_sigchld );
229 /* SIGSEGV handler */
230 static void do_sigsegv( int signum )
232 fprintf( stderr, "wineserver crashed, please enable coredumps (ulimit -c unlimited) and restart.\n");
233 abort();
236 /* SIGIO handler */
237 #ifdef HAVE_SIGINFO_T_SI_FD
238 static void do_sigio( int signum, siginfo_t *si, void *x )
240 do_signal( handler_sigio );
241 do_change_notify( si->si_fd );
243 #endif
245 void start_watchdog(void)
247 alarm( 3 );
248 watchdog = 0;
251 void stop_watchdog(void)
253 alarm( 0 );
254 watchdog = 0;
257 int watchdog_triggered(void)
259 return watchdog != 0;
262 static int core_dump_disabled( void )
264 int r = 0;
265 #ifdef RLIMIT_CORE
266 struct rlimit lim;
268 r = !getrlimit(RLIMIT_CORE, &lim) && (lim.rlim_cur == 0);
269 #endif
270 return r;
273 void init_signals(void)
275 struct sigaction action;
276 sigset_t blocked_sigset;
278 if (!(handler_sighup = create_handler( sighup_callback ))) goto error;
279 if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
280 if (!(handler_sigint = create_handler( sigint_callback ))) goto error;
281 if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
282 if (!(handler_sigio = create_handler( sigio_callback ))) goto error;
284 sigemptyset( &blocked_sigset );
285 sigaddset( &blocked_sigset, SIGCHLD );
286 sigaddset( &blocked_sigset, SIGHUP );
287 sigaddset( &blocked_sigset, SIGINT );
288 sigaddset( &blocked_sigset, SIGALRM );
289 sigaddset( &blocked_sigset, SIGIO );
290 sigaddset( &blocked_sigset, SIGQUIT );
291 sigaddset( &blocked_sigset, SIGTERM );
292 #ifdef SIG_PTHREAD_CANCEL
293 sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL );
294 #endif
296 action.sa_mask = blocked_sigset;
297 action.sa_flags = 0;
298 action.sa_handler = do_sigchld;
299 sigaction( SIGCHLD, &action, NULL );
300 #ifdef SIG_PTHREAD_CANCEL
301 sigaction( SIG_PTHREAD_CANCEL, &action, NULL );
302 #endif
303 action.sa_handler = do_sighup;
304 sigaction( SIGHUP, &action, NULL );
305 action.sa_handler = do_sigint;
306 sigaction( SIGINT, &action, NULL );
307 action.sa_handler = do_sigalrm;
308 sigaction( SIGALRM, &action, NULL );
309 action.sa_handler = do_sigterm;
310 sigaction( SIGQUIT, &action, NULL );
311 sigaction( SIGTERM, &action, NULL );
312 if (core_dump_disabled())
314 action.sa_handler = do_sigsegv;
315 sigaction( SIGSEGV, &action, NULL );
317 action.sa_handler = SIG_IGN;
318 sigaction( SIGXFSZ, &action, NULL );
319 #ifdef HAVE_SIGINFO_T_SI_FD
320 action.sa_sigaction = do_sigio;
321 action.sa_flags = SA_SIGINFO;
322 sigaction( SIGIO, &action, NULL );
323 #endif
324 return;
326 error:
327 fprintf( stderr, "failed to initialize signal handlers\n" );
328 exit(1);