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
28 #ifdef HAVE_SYS_POLL_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)
43 typedef void (*signal_callback
)(void);
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 */
65 no_signal
, /* signal */
66 no_get_fd
, /* get_fd */
67 no_map_access
, /* map_access */
68 no_lookup_name
, /* lookup_name */
69 no_close_handle
, /* close_handle */
70 handler_destroy
/* destroy */
73 static void handler_poll_event( struct fd
*fd
, int event
);
75 static const struct fd_ops handler_fd_ops
=
77 NULL
, /* get_poll_events */
78 handler_poll_event
, /* poll_event */
80 no_get_file_info
, /* get_file_info */
81 no_queue_async
, /* queue_async */
82 no_cancel_async
/* cancel_async */
85 static struct handler
*handler_sighup
;
86 static struct handler
*handler_sigterm
;
87 static struct handler
*handler_sigint
;
88 static struct handler
*handler_sigchld
;
89 static struct handler
*handler_sigio
;
91 static sigset_t blocked_sigset
;
95 /* create a signal handler */
96 static struct handler
*create_handler( signal_callback callback
)
98 struct handler
*handler
;
101 if (pipe( fd
) == -1) return NULL
;
102 if (!(handler
= alloc_object( &handler_ops
)))
108 handler
->pipe_write
= fd
[1];
109 handler
->pending
= 0;
110 handler
->callback
= callback
;
112 if (!(handler
->fd
= create_anonymous_fd( &handler_fd_ops
, fd
[0], &handler
->obj
)))
114 release_object( handler
);
117 set_fd_events( handler
->fd
, POLLIN
);
121 /* handle a signal received for a given handler */
122 static void do_signal( struct handler
*handler
)
124 if (!handler
->pending
)
127 handler
->pending
= 1;
128 write( handler
->pipe_write
, &dummy
, 1 );
132 static void handler_dump( struct object
*obj
, int verbose
)
134 struct handler
*handler
= (struct handler
*)obj
;
135 fprintf( stderr
, "Signal handler fd=%p\n", handler
->fd
);
138 static void handler_destroy( struct object
*obj
)
140 struct handler
*handler
= (struct handler
*)obj
;
141 if (handler
->fd
) release_object( handler
->fd
);
142 close( handler
->pipe_write
);
145 static void handler_poll_event( struct fd
*fd
, int event
)
147 struct handler
*handler
= get_fd_user( fd
);
149 if (event
& (POLLERR
| POLLHUP
))
151 /* this is not supposed to happen */
152 fprintf( stderr
, "wineserver: Error on signal handler pipe\n" );
153 release_object( handler
);
155 else if (event
& POLLIN
)
159 handler
->pending
= 0;
160 read( get_unix_fd( handler
->fd
), &dummy
, 1 );
165 /* SIGHUP callback */
166 static void sighup_callback(void)
173 /* SIGTERM callback */
174 static void sigterm_callback(void)
180 /* SIGINT callback */
181 static void sigint_callback(void)
183 kill_all_processes( NULL
, 1 );
189 static void do_sighup( int signum
)
191 do_signal( handler_sighup
);
194 /* SIGTERM handler */
195 static void do_sigterm( int signum
)
197 do_signal( handler_sigterm
);
201 static void do_sigint( int signum
)
203 do_signal( handler_sigint
);
206 /* SIGALRM handler */
207 static void do_sigalrm( int signum
)
212 /* SIGCHLD handler */
213 static void do_sigchld( int signum
)
215 do_signal( handler_sigchld
);
219 #ifdef HAVE_SIGINFO_T_SI_FD
220 static void do_sigio( int signum
, siginfo_t
*si
, void *x
)
222 do_signal( handler_sigio
);
223 do_change_notify( si
->si_fd
);
227 void start_watchdog(void)
233 void stop_watchdog(void)
239 int watchdog_triggered(void)
241 return watchdog
!= 0;
244 void init_signals(void)
246 struct sigaction action
;
248 if (!(handler_sighup
= create_handler( sighup_callback
))) goto error
;
249 if (!(handler_sigterm
= create_handler( sigterm_callback
))) goto error
;
250 if (!(handler_sigint
= create_handler( sigint_callback
))) goto error
;
251 if (!(handler_sigchld
= create_handler( sigchld_callback
))) goto error
;
252 if (!(handler_sigio
= create_handler( sigio_callback
))) goto error
;
254 sigemptyset( &blocked_sigset
);
255 sigaddset( &blocked_sigset
, SIGCHLD
);
256 sigaddset( &blocked_sigset
, SIGHUP
);
257 sigaddset( &blocked_sigset
, SIGINT
);
258 sigaddset( &blocked_sigset
, SIGALRM
);
259 sigaddset( &blocked_sigset
, SIGIO
);
260 sigaddset( &blocked_sigset
, SIGQUIT
);
261 sigaddset( &blocked_sigset
, SIGTERM
);
262 #ifdef SIG_PTHREAD_CANCEL
263 sigaddset( &blocked_sigset
, SIG_PTHREAD_CANCEL
);
266 action
.sa_mask
= blocked_sigset
;
268 action
.sa_handler
= do_sigchld
;
269 sigaction( SIGCHLD
, &action
, NULL
);
270 #ifdef SIG_PTHREAD_CANCEL
271 sigaction( SIG_PTHREAD_CANCEL
, &action
, NULL
);
273 action
.sa_handler
= do_sighup
;
274 sigaction( SIGHUP
, &action
, NULL
);
275 action
.sa_handler
= do_sigint
;
276 sigaction( SIGINT
, &action
, NULL
);
277 action
.sa_handler
= do_sigalrm
;
278 sigaction( SIGALRM
, &action
, NULL
);
279 action
.sa_handler
= do_sigterm
;
280 sigaction( SIGQUIT
, &action
, NULL
);
281 sigaction( SIGTERM
, &action
, NULL
);
282 action
.sa_handler
= SIG_IGN
;
283 sigaction( SIGXFSZ
, &action
, NULL
);
284 #ifdef HAVE_SIGINFO_T_SI_FD
285 action
.sa_sigaction
= do_sigio
;
286 action
.sa_flags
= SA_SIGINFO
;
287 sigaction( SIGIO
, &action
, NULL
);
292 fprintf( stderr
, "failed to initialize signal handlers\n" );
296 void close_signals(void)
298 sigprocmask( SIG_BLOCK
, &blocked_sigset
, NULL
);
299 release_object( handler_sighup
);
300 release_object( handler_sigterm
);
301 release_object( handler_sigint
);
302 release_object( handler_sigchld
);
303 release_object( handler_sigio
);