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
25 #ifdef HAVE_SYS_POLL_H
35 #if defined(linux) && defined(__SIGRTMIN)
36 /* the signal used by linuxthreads as exit signal for clone() threads */
37 # define SIG_PTHREAD_CANCEL (__SIGRTMIN+1)
40 typedef void (*signal_callback
)(void);
44 struct object obj
; /* object header */
45 struct fd
*fd
; /* file descriptor for the pipe side */
46 int pipe_write
; /* unix fd for the pipe write side */
47 volatile int pending
; /* is signal pending? */
48 signal_callback callback
; /* callback function */
51 static void handler_dump( struct object
*obj
, int verbose
);
52 static void handler_destroy( struct object
*obj
);
54 static const struct object_ops handler_ops
=
56 sizeof(struct handler
), /* size */
57 handler_dump
, /* dump */
58 no_add_queue
, /* add_queue */
59 NULL
, /* remove_queue */
62 no_get_fd
, /* get_fd */
63 handler_destroy
/* destroy */
66 static void handler_poll_event( struct fd
*fd
, int event
);
68 static const struct fd_ops handler_fd_ops
=
70 NULL
, /* get_poll_events */
71 handler_poll_event
, /* poll_event */
73 no_get_file_info
, /* get_file_info */
74 no_queue_async
/* queue_async */
77 static struct handler
*handler_sighup
;
78 static struct handler
*handler_sigterm
;
79 static struct handler
*handler_sigint
;
80 static struct handler
*handler_sigchld
;
81 static struct handler
*handler_sigio
;
83 static sigset_t blocked_sigset
;
85 /* create a signal handler */
86 static struct handler
*create_handler( signal_callback callback
)
88 struct handler
*handler
;
91 if (pipe( fd
) == -1) return NULL
;
92 if (!(handler
= alloc_object( &handler_ops
)))
98 handler
->pipe_write
= fd
[1];
100 handler
->callback
= callback
;
102 if (!(handler
->fd
= create_anonymous_fd( &handler_fd_ops
, fd
[0], &handler
->obj
)))
104 release_object( handler
);
107 set_fd_events( handler
->fd
, POLLIN
);
111 /* handle a signal received for a given handler */
112 static void do_signal( struct handler
*handler
)
114 if (!handler
->pending
)
117 handler
->pending
= 1;
118 write( handler
->pipe_write
, &dummy
, 1 );
122 static void handler_dump( struct object
*obj
, int verbose
)
124 struct handler
*handler
= (struct handler
*)obj
;
125 fprintf( stderr
, "Signal handler fd=%p\n", handler
->fd
);
128 static void handler_destroy( struct object
*obj
)
130 struct handler
*handler
= (struct handler
*)obj
;
131 if (handler
->fd
) release_object( handler
->fd
);
132 close( handler
->pipe_write
);
135 static void handler_poll_event( struct fd
*fd
, int event
)
137 struct handler
*handler
= get_fd_user( fd
);
139 if (event
& (POLLERR
| POLLHUP
))
141 /* this is not supposed to happen */
142 fprintf( stderr
, "wineserver: Error on signal handler pipe\n" );
143 release_object( handler
);
145 else if (event
& POLLIN
)
149 handler
->pending
= 0;
150 read( get_unix_fd( handler
->fd
), &dummy
, 1 );
155 /* SIGHUP callback */
156 static void sighup_callback(void)
163 /* SIGTERM callback */
164 static void sigterm_callback(void)
170 /* SIGINT callback */
171 static void sigint_callback(void)
173 kill_all_processes( NULL
, 1 );
179 static void do_sighup()
181 do_signal( handler_sighup
);
184 /* SIGTERM handler */
185 static void do_sigterm()
187 do_signal( handler_sigterm
);
191 static void do_sigint()
193 do_signal( handler_sigint
);
196 /* SIGCHLD handler */
197 static void do_sigchld()
199 do_signal( handler_sigchld
);
203 #ifdef HAVE_SIGINFO_T_SI_FD
204 static void do_sigio( int signum
, siginfo_t
*si
, void *x
)
206 do_signal( handler_sigio
);
207 do_change_notify( si
->si_fd
);
211 void init_signals(void)
213 struct sigaction action
;
215 if (!(handler_sighup
= create_handler( sighup_callback
))) goto error
;
216 if (!(handler_sigterm
= create_handler( sigterm_callback
))) goto error
;
217 if (!(handler_sigint
= create_handler( sigint_callback
))) goto error
;
218 if (!(handler_sigchld
= create_handler( sigchld_callback
))) goto error
;
219 if (!(handler_sigio
= create_handler( sigio_callback
))) goto error
;
221 sigemptyset( &blocked_sigset
);
222 sigaddset( &blocked_sigset
, SIGCHLD
);
223 sigaddset( &blocked_sigset
, SIGHUP
);
224 sigaddset( &blocked_sigset
, SIGINT
);
225 sigaddset( &blocked_sigset
, SIGIO
);
226 sigaddset( &blocked_sigset
, SIGQUIT
);
227 sigaddset( &blocked_sigset
, SIGTERM
);
228 #ifdef SIG_PTHREAD_CANCEL
229 sigaddset( &blocked_sigset
, SIG_PTHREAD_CANCEL
);
232 action
.sa_mask
= blocked_sigset
;
234 action
.sa_handler
= do_sigchld
;
235 sigaction( SIGCHLD
, &action
, NULL
);
236 #ifdef SIG_PTHREAD_CANCEL
237 sigaction( SIG_PTHREAD_CANCEL
, &action
, NULL
);
239 action
.sa_handler
= do_sighup
;
240 sigaction( SIGHUP
, &action
, NULL
);
241 action
.sa_handler
= do_sigint
;
242 sigaction( SIGINT
, &action
, NULL
);
243 action
.sa_handler
= do_sigterm
;
244 sigaction( SIGQUIT
, &action
, NULL
);
245 sigaction( SIGTERM
, &action
, NULL
);
246 #ifdef HAVE_SIGINFO_T_SI_FD
247 action
.sa_sigaction
= do_sigio
;
248 action
.sa_flags
= SA_SIGINFO
;
249 sigaction( SIGIO
, &action
, NULL
);
254 fprintf( stderr
, "failed to initialize signal handlers\n" );
258 void close_signals(void)
260 sigprocmask( SIG_BLOCK
, &blocked_sigset
, NULL
);
261 release_object( handler_sighup
);
262 release_object( handler_sigterm
);
263 release_object( handler_sigint
);
264 release_object( handler_sigchld
);
265 release_object( handler_sigio
);