wined3d: Fix / update reported driver model version.
[wine/zf.git] / server / signal.c
blobc81f6b8e9892c4a80e6e07b02904b3d00eb68817
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 handler_dump, /* dump */
66 no_get_type, /* get_type */
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 no_map_access, /* map_access */
74 default_get_sd, /* get_sd */
75 default_set_sd, /* set_sd */
76 no_lookup_name, /* lookup_name */
77 no_link_name, /* link_name */
78 NULL, /* unlink_name */
79 no_open_file, /* open_file */
80 no_kernel_obj_list, /* get_kernel_obj_list */
81 no_close_handle, /* close_handle */
82 handler_destroy /* destroy */
85 static void handler_poll_event( struct fd *fd, int event );
87 static const struct fd_ops handler_fd_ops =
89 NULL, /* get_poll_events */
90 handler_poll_event, /* poll_event */
91 NULL, /* flush */
92 NULL, /* get_fd_type */
93 NULL, /* ioctl */
94 NULL, /* queue_async */
95 NULL /* reselect_async */
98 static struct handler *handler_sighup;
99 static struct handler *handler_sigterm;
100 static struct handler *handler_sigint;
101 static struct handler *handler_sigchld;
102 static struct handler *handler_sigio;
104 static int watchdog;
106 /* create a signal handler */
107 static struct handler *create_handler( signal_callback callback )
109 struct handler *handler;
110 int fd[2];
112 if (pipe( fd ) == -1) return NULL;
113 if (!(handler = alloc_object( &handler_ops )))
115 close( fd[0] );
116 close( fd[1] );
117 return NULL;
119 handler->pipe_write = fd[1];
120 handler->pending = 0;
121 handler->callback = callback;
123 if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj, 0 )))
125 release_object( handler );
126 return NULL;
128 set_fd_events( handler->fd, POLLIN );
129 make_object_static( &handler->obj );
130 return handler;
133 /* handle a signal received for a given handler */
134 static void do_signal( struct handler *handler )
136 if (!handler->pending)
138 char dummy = 0;
139 handler->pending = 1;
140 write( handler->pipe_write, &dummy, 1 );
144 static void handler_dump( struct object *obj, int verbose )
146 struct handler *handler = (struct handler *)obj;
147 fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
150 static void handler_destroy( struct object *obj )
152 struct handler *handler = (struct handler *)obj;
153 if (handler->fd) release_object( handler->fd );
154 close( handler->pipe_write );
157 static void handler_poll_event( struct fd *fd, int event )
159 struct handler *handler = get_fd_user( fd );
161 if (event & (POLLERR | POLLHUP))
163 /* this is not supposed to happen */
164 fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
165 release_object( handler );
167 else if (event & POLLIN)
169 char dummy;
171 handler->pending = 0;
172 read( get_unix_fd( handler->fd ), &dummy, 1 );
173 handler->callback();
177 /* SIGHUP callback */
178 static void sighup_callback(void)
180 #ifdef DEBUG_OBJECTS
181 dump_objects();
182 #endif
185 /* SIGTERM callback */
186 static void sigterm_callback(void)
188 flush_registry();
189 exit(1);
192 /* SIGINT callback */
193 static void sigint_callback(void)
195 shutdown_master_socket();
198 /* SIGHUP handler */
199 static void do_sighup( int signum )
201 do_signal( handler_sighup );
204 /* SIGTERM handler */
205 static void do_sigterm( int signum )
207 do_signal( handler_sigterm );
210 /* SIGINT handler */
211 static void do_sigint( int signum )
213 do_signal( handler_sigint );
216 /* SIGALRM handler */
217 static void do_sigalrm( int signum )
219 watchdog = 1;
222 /* SIGCHLD handler */
223 static void do_sigchld( int signum )
225 do_signal( handler_sigchld );
228 /* SIGSEGV handler */
229 static void do_sigsegv( int signum )
231 fprintf( stderr, "wineserver crashed, please enable coredumps (ulimit -c unlimited) and restart.\n");
232 abort();
235 /* SIGIO handler */
236 #ifdef HAVE_SIGINFO_T_SI_FD
237 static void do_sigio( int signum, siginfo_t *si, void *x )
239 do_signal( handler_sigio );
240 do_change_notify( si->si_fd );
242 #endif
244 void start_watchdog(void)
246 alarm( 3 );
247 watchdog = 0;
250 void stop_watchdog(void)
252 alarm( 0 );
253 watchdog = 0;
256 int watchdog_triggered(void)
258 return watchdog != 0;
261 static int core_dump_disabled( void )
263 int r = 0;
264 #ifdef RLIMIT_CORE
265 struct rlimit lim;
267 r = !getrlimit(RLIMIT_CORE, &lim) && (lim.rlim_cur == 0);
268 #endif
269 return r;
272 void init_signals(void)
274 struct sigaction action;
275 sigset_t blocked_sigset;
277 if (!(handler_sighup = create_handler( sighup_callback ))) goto error;
278 if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
279 if (!(handler_sigint = create_handler( sigint_callback ))) goto error;
280 if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
281 if (!(handler_sigio = create_handler( sigio_callback ))) goto error;
283 sigemptyset( &blocked_sigset );
284 sigaddset( &blocked_sigset, SIGCHLD );
285 sigaddset( &blocked_sigset, SIGHUP );
286 sigaddset( &blocked_sigset, SIGINT );
287 sigaddset( &blocked_sigset, SIGALRM );
288 sigaddset( &blocked_sigset, SIGIO );
289 sigaddset( &blocked_sigset, SIGQUIT );
290 sigaddset( &blocked_sigset, SIGTERM );
291 #ifdef SIG_PTHREAD_CANCEL
292 sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL );
293 #endif
295 action.sa_mask = blocked_sigset;
296 action.sa_flags = 0;
297 action.sa_handler = do_sigchld;
298 sigaction( SIGCHLD, &action, NULL );
299 #ifdef SIG_PTHREAD_CANCEL
300 sigaction( SIG_PTHREAD_CANCEL, &action, NULL );
301 #endif
302 action.sa_handler = do_sighup;
303 sigaction( SIGHUP, &action, NULL );
304 action.sa_handler = do_sigint;
305 sigaction( SIGINT, &action, NULL );
306 action.sa_handler = do_sigalrm;
307 sigaction( SIGALRM, &action, NULL );
308 action.sa_handler = do_sigterm;
309 sigaction( SIGQUIT, &action, NULL );
310 sigaction( SIGTERM, &action, NULL );
311 if (core_dump_disabled())
313 action.sa_handler = do_sigsegv;
314 sigaction( SIGSEGV, &action, NULL );
316 action.sa_handler = SIG_IGN;
317 sigaction( SIGXFSZ, &action, NULL );
318 #ifdef HAVE_SIGINFO_T_SI_FD
319 action.sa_sigaction = do_sigio;
320 action.sa_flags = SA_SIGINFO;
321 sigaction( SIGIO, &action, NULL );
322 #endif
323 return;
325 error:
326 fprintf( stderr, "failed to initialize signal handlers\n" );
327 exit(1);