blob: 99d9b82dc08a70e9f17c8c1315c4f871e772b100 [file] [log] [blame]
Alexandre Julliard9037f4b2003-03-26 01:32:18 +00001/*
2 * Server signal handling
3 *
4 * Copyright (C) 2003 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include "config.h"
22
23#include <signal.h>
24#include <stdio.h>
25#ifdef HAVE_SYS_POLL_H
26#include <sys/poll.h>
27#endif
28#include <unistd.h>
29
30#include "file.h"
31#include "object.h"
32#include "process.h"
33#include "thread.h"
34
Alexandre Julliardf9f37be2003-10-24 04:29:01 +000035#if defined(linux) && defined(__SIGRTMIN)
36/* the signal used by linuxthreads as exit signal for clone() threads */
37# define SIG_PTHREAD_CANCEL (__SIGRTMIN+1)
38#endif
39
Alexandre Julliard9037f4b2003-03-26 01:32:18 +000040typedef void (*signal_callback)(void);
41
42struct handler
43{
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 */
49};
50
51static void handler_dump( struct object *obj, int verbose );
52static void handler_destroy( struct object *obj );
53
54static const struct object_ops handler_ops =
55{
56 sizeof(struct handler), /* size */
57 handler_dump, /* dump */
58 no_add_queue, /* add_queue */
59 NULL, /* remove_queue */
60 NULL, /* signaled */
61 NULL, /* satisfied */
62 no_get_fd, /* get_fd */
63 handler_destroy /* destroy */
64};
65
66static void handler_poll_event( struct fd *fd, int event );
67
68static const struct fd_ops handler_fd_ops =
69{
70 NULL, /* get_poll_events */
71 handler_poll_event, /* poll_event */
72 no_flush, /* flush */
73 no_get_file_info, /* get_file_info */
74 no_queue_async /* queue_async */
75};
76
77static struct handler *handler_sighup;
78static struct handler *handler_sigterm;
79static struct handler *handler_sigint;
80static struct handler *handler_sigchld;
81static struct handler *handler_sigio;
82
83static sigset_t blocked_sigset;
84
85/* create a signal handler */
86static struct handler *create_handler( signal_callback callback )
87{
88 struct handler *handler;
89 int fd[2];
90
91 if (pipe( fd ) == -1) return NULL;
92 if (!(handler = alloc_object( &handler_ops )))
93 {
94 close( fd[0] );
95 close( fd[1] );
96 return NULL;
97 }
98 handler->pipe_write = fd[1];
99 handler->pending = 0;
100 handler->callback = callback;
101
102 if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj )))
103 {
104 release_object( handler );
105 return NULL;
106 }
107 set_fd_events( handler->fd, POLLIN );
108 return handler;
109}
110
111/* handle a signal received for a given handler */
112static void do_signal( struct handler *handler )
113{
114 if (!handler->pending)
115 {
Alexandre Julliard17480ac2003-04-02 01:44:01 +0000116 char dummy = 0;
Alexandre Julliard9037f4b2003-03-26 01:32:18 +0000117 handler->pending = 1;
118 write( handler->pipe_write, &dummy, 1 );
119 }
120}
121
122static void handler_dump( struct object *obj, int verbose )
123{
124 struct handler *handler = (struct handler *)obj;
125 fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
126}
127
128static void handler_destroy( struct object *obj )
129{
130 struct handler *handler = (struct handler *)obj;
131 if (handler->fd) release_object( handler->fd );
132 close( handler->pipe_write );
133}
134
135static void handler_poll_event( struct fd *fd, int event )
136{
137 struct handler *handler = get_fd_user( fd );
138
139 if (event & (POLLERR | POLLHUP))
140 {
141 /* this is not supposed to happen */
142 fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
143 release_object( handler );
144 }
145 else if (event & POLLIN)
146 {
147 char dummy;
148
149 handler->pending = 0;
150 read( get_unix_fd( handler->fd ), &dummy, 1 );
151 handler->callback();
152 }
153}
154
155/* SIGHUP callback */
156static void sighup_callback(void)
157{
158#ifdef DEBUG_OBJECTS
159 dump_objects();
160#endif
161}
162
163/* SIGTERM callback */
164static void sigterm_callback(void)
165{
166 flush_registry();
167 exit(1);
168}
169
170/* SIGINT callback */
171static void sigint_callback(void)
172{
173 kill_all_processes( NULL, 1 );
174 flush_registry();
175 exit(1);
176}
177
Alexandre Julliard9037f4b2003-03-26 01:32:18 +0000178/* SIGHUP handler */
179static void do_sighup()
180{
181 do_signal( handler_sighup );
182}
183
184/* SIGTERM handler */
185static void do_sigterm()
186{
187 do_signal( handler_sigterm );
188}
189
190/* SIGINT handler */
191static void do_sigint()
192{
193 do_signal( handler_sigint );
194}
195
196/* SIGCHLD handler */
197static void do_sigchld()
198{
199 do_signal( handler_sigchld );
200}
201
202/* SIGIO handler */
Yorick Hardy3f11d482003-04-13 01:06:14 +0000203#ifdef HAVE_SIGINFO_T_SI_FD
Alexandre Julliard9037f4b2003-03-26 01:32:18 +0000204static void do_sigio( int signum, siginfo_t *si, void *x )
205{
Alexandre Julliard9037f4b2003-03-26 01:32:18 +0000206 do_signal( handler_sigio );
Alexandre Julliard3e588e32003-03-26 23:41:43 +0000207 do_change_notify( si->si_fd );
Alexandre Julliard9037f4b2003-03-26 01:32:18 +0000208}
Yorick Hardy3f11d482003-04-13 01:06:14 +0000209#endif
Alexandre Julliard9037f4b2003-03-26 01:32:18 +0000210
211void init_signals(void)
212{
213 struct sigaction action;
214
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;
220
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 );
Alexandre Julliardf9f37be2003-10-24 04:29:01 +0000228#ifdef SIG_PTHREAD_CANCEL
229 sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL );
230#endif
Alexandre Julliard9037f4b2003-03-26 01:32:18 +0000231
232 action.sa_mask = blocked_sigset;
233 action.sa_flags = 0;
234 action.sa_handler = do_sigchld;
235 sigaction( SIGCHLD, &action, NULL );
Alexandre Julliardf9f37be2003-10-24 04:29:01 +0000236#ifdef SIG_PTHREAD_CANCEL
237 sigaction( SIG_PTHREAD_CANCEL, &action, NULL );
238#endif
Alexandre Julliard9037f4b2003-03-26 01:32:18 +0000239 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 );
Alexandre Julliard72f87b82004-05-01 02:50:06 +0000246 action.sa_handler = SIG_IGN;
247 sigaction( SIGXFSZ, &action, NULL );
Yorick Hardy3f11d482003-04-13 01:06:14 +0000248#ifdef HAVE_SIGINFO_T_SI_FD
Alexandre Julliard9037f4b2003-03-26 01:32:18 +0000249 action.sa_sigaction = do_sigio;
250 action.sa_flags = SA_SIGINFO;
251 sigaction( SIGIO, &action, NULL );
Yorick Hardy3f11d482003-04-13 01:06:14 +0000252#endif
Alexandre Julliard9037f4b2003-03-26 01:32:18 +0000253 return;
254
255error:
256 fprintf( stderr, "failed to initialize signal handlers\n" );
257 exit(1);
258}
259
260void close_signals(void)
261{
262 sigprocmask( SIG_BLOCK, &blocked_sigset, NULL );
263 release_object( handler_sighup );
264 release_object( handler_sigterm );
265 release_object( handler_sigint );
266 release_object( handler_sigchld );
267 release_object( handler_sigio );
268}