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
29 #ifdef HAVE_SYS_POLL_H
32 #ifdef HAVE_SYS_RESOURCE_H
33 #include <sys/resource.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)
48 typedef void (*signal_callback
)(void);
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_add_queue
, /* add_queue */
67 NULL
, /* remove_queue */
70 no_signal
, /* signal */
71 no_get_fd
, /* get_fd */
72 no_map_access
, /* map_access */
73 no_lookup_name
, /* lookup_name */
74 no_open_file
, /* open_file */
75 no_close_handle
, /* close_handle */
76 handler_destroy
/* destroy */
79 static void handler_poll_event( struct fd
*fd
, int event
);
81 static const struct fd_ops handler_fd_ops
=
83 NULL
, /* get_poll_events */
84 handler_poll_event
, /* poll_event */
86 NULL
, /* get_fd_type */
88 NULL
, /* queue_async */
89 NULL
, /* reselect_async */
90 NULL
/* cancel_async */
93 static struct handler
*handler_sighup
;
94 static struct handler
*handler_sigterm
;
95 static struct handler
*handler_sigint
;
96 static struct handler
*handler_sigchld
;
97 static struct handler
*handler_sigio
;
101 /* create a signal handler */
102 static struct handler
*create_handler( signal_callback callback
)
104 struct handler
*handler
;
107 if (pipe( fd
) == -1) return NULL
;
108 if (!(handler
= alloc_object( &handler_ops
)))
114 handler
->pipe_write
= fd
[1];
115 handler
->pending
= 0;
116 handler
->callback
= callback
;
118 if (!(handler
->fd
= create_anonymous_fd( &handler_fd_ops
, fd
[0], &handler
->obj
, 0 )))
120 release_object( handler
);
123 set_fd_events( handler
->fd
, POLLIN
);
124 make_object_static( &handler
->obj
);
128 /* handle a signal received for a given handler */
129 static void do_signal( struct handler
*handler
)
131 if (!handler
->pending
)
134 handler
->pending
= 1;
135 write( handler
->pipe_write
, &dummy
, 1 );
139 static void handler_dump( struct object
*obj
, int verbose
)
141 struct handler
*handler
= (struct handler
*)obj
;
142 fprintf( stderr
, "Signal handler fd=%p\n", handler
->fd
);
145 static void handler_destroy( struct object
*obj
)
147 struct handler
*handler
= (struct handler
*)obj
;
148 if (handler
->fd
) release_object( handler
->fd
);
149 close( handler
->pipe_write
);
152 static void handler_poll_event( struct fd
*fd
, int event
)
154 struct handler
*handler
= get_fd_user( fd
);
156 if (event
& (POLLERR
| POLLHUP
))
158 /* this is not supposed to happen */
159 fprintf( stderr
, "wineserver: Error on signal handler pipe\n" );
160 release_object( handler
);
162 else if (event
& POLLIN
)
166 handler
->pending
= 0;
167 read( get_unix_fd( handler
->fd
), &dummy
, 1 );
172 /* SIGHUP callback */
173 static void sighup_callback(void)
180 /* SIGTERM callback */
181 static void sigterm_callback(void)
187 /* SIGINT callback */
188 static void sigint_callback(void)
190 kill_all_processes( NULL
, 1 );
192 shutdown_master_socket();
196 static void do_sighup( int signum
)
198 do_signal( handler_sighup
);
201 /* SIGTERM handler */
202 static void do_sigterm( int signum
)
204 do_signal( handler_sigterm
);
208 static void do_sigint( int signum
)
210 do_signal( handler_sigint
);
213 /* SIGALRM handler */
214 static void do_sigalrm( int signum
)
219 /* SIGCHLD handler */
220 static void do_sigchld( int signum
)
222 do_signal( handler_sigchld
);
225 /* SIGSEGV handler */
226 static void do_sigsegv( int signum
)
228 fprintf( stderr
, "wineserver crashed, please report this.\n");
233 #ifdef HAVE_SIGINFO_T_SI_FD
234 static void do_sigio( int signum
, siginfo_t
*si
, void *x
)
236 do_signal( handler_sigio
);
237 do_change_notify( si
->si_fd
);
241 void start_watchdog(void)
247 void stop_watchdog(void)
253 int watchdog_triggered(void)
255 return watchdog
!= 0;
258 static int core_dump_disabled( void )
264 r
= !getrlimit(RLIMIT_CORE
, &lim
) && (lim
.rlim_cur
== 0);
269 void init_signals(void)
271 struct sigaction action
;
272 sigset_t blocked_sigset
;
274 if (!(handler_sighup
= create_handler( sighup_callback
))) goto error
;
275 if (!(handler_sigterm
= create_handler( sigterm_callback
))) goto error
;
276 if (!(handler_sigint
= create_handler( sigint_callback
))) goto error
;
277 if (!(handler_sigchld
= create_handler( sigchld_callback
))) goto error
;
278 if (!(handler_sigio
= create_handler( sigio_callback
))) goto error
;
280 sigemptyset( &blocked_sigset
);
281 sigaddset( &blocked_sigset
, SIGCHLD
);
282 sigaddset( &blocked_sigset
, SIGHUP
);
283 sigaddset( &blocked_sigset
, SIGINT
);
284 sigaddset( &blocked_sigset
, SIGALRM
);
285 sigaddset( &blocked_sigset
, SIGIO
);
286 sigaddset( &blocked_sigset
, SIGQUIT
);
287 sigaddset( &blocked_sigset
, SIGTERM
);
288 #ifdef SIG_PTHREAD_CANCEL
289 sigaddset( &blocked_sigset
, SIG_PTHREAD_CANCEL
);
292 action
.sa_mask
= blocked_sigset
;
294 action
.sa_handler
= do_sigchld
;
295 sigaction( SIGCHLD
, &action
, NULL
);
296 #ifdef SIG_PTHREAD_CANCEL
297 sigaction( SIG_PTHREAD_CANCEL
, &action
, NULL
);
299 action
.sa_handler
= do_sighup
;
300 sigaction( SIGHUP
, &action
, NULL
);
301 action
.sa_handler
= do_sigint
;
302 sigaction( SIGINT
, &action
, NULL
);
303 action
.sa_handler
= do_sigalrm
;
304 sigaction( SIGALRM
, &action
, NULL
);
305 action
.sa_handler
= do_sigterm
;
306 sigaction( SIGQUIT
, &action
, NULL
);
307 sigaction( SIGTERM
, &action
, NULL
);
308 if (core_dump_disabled())
310 action
.sa_handler
= do_sigsegv
;
311 sigaction( SIGSEGV
, &action
, NULL
);
313 action
.sa_handler
= SIG_IGN
;
314 sigaction( SIGXFSZ
, &action
, NULL
);
315 #ifdef HAVE_SIGINFO_T_SI_FD
316 action
.sa_sigaction
= do_sigio
;
317 action
.sa_flags
= SA_SIGINFO
;
318 sigaction( SIGIO
, &action
, NULL
);
323 fprintf( stderr
, "failed to initialize signal handlers\n" );