odbc32: Correct SQLSetConnectOptionW length parameter type.
[wine.git] / server / signal.c
blob19b76d44c16947036fe53b535bb5eb6a793d6a4f
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 #include <poll.h>
27 #ifdef HAVE_SYS_RESOURCE_H
28 #include <sys/resource.h>
29 #endif
30 #include <unistd.h>
32 #include "file.h"
33 #include "object.h"
34 #include "process.h"
35 #include "thread.h"
36 #include "request.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 &no_type, /* type */
61 handler_dump, /* dump */
62 no_add_queue, /* add_queue */
63 NULL, /* remove_queue */
64 NULL, /* signaled */
65 NULL, /* satisfied */
66 no_signal, /* signal */
67 no_get_fd, /* get_fd */
68 default_map_access, /* map_access */
69 default_get_sd, /* get_sd */
70 default_set_sd, /* set_sd */
71 no_get_full_name, /* get_full_name */
72 no_lookup_name, /* lookup_name */
73 no_link_name, /* link_name */
74 NULL, /* unlink_name */
75 no_open_file, /* open_file */
76 no_kernel_obj_list, /* get_kernel_obj_list */
77 no_close_handle, /* close_handle */
78 handler_destroy /* destroy */
81 static void handler_poll_event( struct fd *fd, int event );
83 static const struct fd_ops handler_fd_ops =
85 NULL, /* get_poll_events */
86 handler_poll_event, /* poll_event */
87 NULL, /* flush */
88 NULL, /* get_fd_type */
89 NULL, /* ioctl */
90 NULL, /* queue_async */
91 NULL /* reselect_async */
94 static struct handler *handler_sighup;
95 static struct handler *handler_sigterm;
96 static struct handler *handler_sigint;
97 static struct handler *handler_sigchld;
98 static struct handler *handler_sigio;
100 static int watchdog;
102 /* create a signal handler */
103 static struct handler *create_handler( signal_callback callback )
105 struct handler *handler;
106 int fd[2];
108 if (pipe( fd ) == -1) return NULL;
109 if (!(handler = alloc_object( &handler_ops )))
111 close( fd[0] );
112 close( fd[1] );
113 return NULL;
115 handler->pipe_write = fd[1];
116 handler->pending = 0;
117 handler->callback = callback;
119 if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj, 0 )))
121 release_object( handler );
122 return NULL;
124 set_fd_events( handler->fd, POLLIN );
125 make_object_permanent( &handler->obj );
126 return handler;
129 /* handle a signal received for a given handler */
130 static void do_signal( struct handler *handler )
132 if (!handler->pending)
134 char dummy = 0;
135 handler->pending = 1;
136 write( handler->pipe_write, &dummy, 1 );
140 static void handler_dump( struct object *obj, int verbose )
142 struct handler *handler = (struct handler *)obj;
143 fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
146 static void handler_destroy( struct object *obj )
148 struct handler *handler = (struct handler *)obj;
149 if (handler->fd) release_object( handler->fd );
150 close( handler->pipe_write );
153 static void handler_poll_event( struct fd *fd, int event )
155 struct handler *handler = get_fd_user( fd );
157 if (event & (POLLERR | POLLHUP))
159 /* this is not supposed to happen */
160 fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
161 release_object( handler );
163 else if (event & POLLIN)
165 char dummy;
167 handler->pending = 0;
168 read( get_unix_fd( handler->fd ), &dummy, 1 );
169 handler->callback();
173 /* SIGHUP callback */
174 static void sighup_callback(void)
176 #ifdef DEBUG_OBJECTS
177 dump_objects();
178 #endif
181 /* SIGTERM callback */
182 static void sigterm_callback(void)
184 flush_registry();
185 exit(1);
188 /* SIGINT callback */
189 static void sigint_callback(void)
191 shutdown_master_socket();
194 /* SIGHUP handler */
195 static void do_sighup( int signum )
197 do_signal( handler_sighup );
200 /* SIGTERM handler */
201 static void do_sigterm( int signum )
203 do_signal( handler_sigterm );
206 /* SIGINT handler */
207 static void do_sigint( int signum )
209 do_signal( handler_sigint );
212 /* SIGALRM handler */
213 static void do_sigalrm( int signum )
215 watchdog = 1;
218 /* SIGCHLD handler */
219 static void do_sigchld( int signum )
221 do_signal( handler_sigchld );
224 /* SIGSEGV handler */
225 static void do_sigsegv( int signum )
227 fprintf( stderr, "wineserver crashed, please enable coredumps (ulimit -c unlimited) and restart.\n");
228 abort();
231 /* SIGIO handler */
232 #ifdef HAVE_SIGINFO_T_SI_FD
233 static void do_sigio( int signum, siginfo_t *si, void *x )
235 do_signal( handler_sigio );
236 do_change_notify( si->si_fd );
238 #endif
240 void start_watchdog(void)
242 alarm( 3 );
243 watchdog = 0;
246 void stop_watchdog(void)
248 alarm( 0 );
249 watchdog = 0;
252 int watchdog_triggered(void)
254 return watchdog != 0;
257 static int core_dump_disabled( void )
259 int r = 0;
260 #ifdef RLIMIT_CORE
261 struct rlimit lim;
263 r = !getrlimit(RLIMIT_CORE, &lim) && (lim.rlim_cur == 0);
264 #endif
265 return r;
268 void init_signals(void)
270 struct sigaction action;
271 sigset_t blocked_sigset;
273 if (!(handler_sighup = create_handler( sighup_callback ))) goto error;
274 if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
275 if (!(handler_sigint = create_handler( sigint_callback ))) goto error;
276 if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
277 if (!(handler_sigio = create_handler( sigio_callback ))) goto error;
279 sigemptyset( &blocked_sigset );
280 sigaddset( &blocked_sigset, SIGCHLD );
281 sigaddset( &blocked_sigset, SIGHUP );
282 sigaddset( &blocked_sigset, SIGINT );
283 sigaddset( &blocked_sigset, SIGALRM );
284 sigaddset( &blocked_sigset, SIGIO );
285 sigaddset( &blocked_sigset, SIGQUIT );
286 sigaddset( &blocked_sigset, SIGTERM );
287 #ifdef SIG_PTHREAD_CANCEL
288 sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL );
289 #endif
291 action.sa_mask = blocked_sigset;
292 action.sa_flags = 0;
293 action.sa_handler = do_sigchld;
294 sigaction( SIGCHLD, &action, NULL );
295 #ifdef SIG_PTHREAD_CANCEL
296 sigaction( SIG_PTHREAD_CANCEL, &action, NULL );
297 #endif
298 action.sa_handler = do_sighup;
299 sigaction( SIGHUP, &action, NULL );
300 action.sa_handler = do_sigint;
301 sigaction( SIGINT, &action, NULL );
302 action.sa_handler = do_sigalrm;
303 sigaction( SIGALRM, &action, NULL );
304 action.sa_handler = do_sigterm;
305 sigaction( SIGQUIT, &action, NULL );
306 sigaction( SIGTERM, &action, NULL );
307 if (core_dump_disabled())
309 action.sa_handler = do_sigsegv;
310 sigaction( SIGSEGV, &action, NULL );
312 action.sa_handler = SIG_IGN;
313 sigaction( SIGXFSZ, &action, NULL );
314 #ifdef HAVE_SIGINFO_T_SI_FD
315 action.sa_sigaction = do_sigio;
316 action.sa_flags = SA_SIGINFO;
317 sigaction( SIGIO, &action, NULL );
318 #endif
319 return;
321 error:
322 fprintf( stderr, "failed to initialize signal handlers\n" );
323 exit(1);