mshtml: Added config key to specify Gecko path.
[wine] / server / signal.c
1 /*
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_POLL_H
26 #include <poll.h>
27 #endif
28 #ifdef HAVE_SYS_POLL_H
29 #include <sys/poll.h>
30 #endif
31 #include <unistd.h>
32
33 #include "file.h"
34 #include "object.h"
35 #include "process.h"
36 #include "thread.h"
37
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
42
43 typedef void (*signal_callback)(void);
44
45 struct handler
46 {
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 */
52 };
53
54 static void handler_dump( struct object *obj, int verbose );
55 static void handler_destroy( struct object *obj );
56
57 static const struct object_ops handler_ops =
58 {
59     sizeof(struct handler),   /* size */
60     handler_dump,             /* dump */
61     no_add_queue,             /* add_queue */
62     NULL,                     /* remove_queue */
63     NULL,                     /* signaled */
64     NULL,                     /* satisfied */
65     no_signal,                /* signal */
66     no_get_fd,                /* get_fd */
67     no_lookup_name,           /* lookup_name */
68     no_close_handle,          /* close_handle */
69     handler_destroy           /* destroy */
70 };
71
72 static void handler_poll_event( struct fd *fd, int event );
73
74 static const struct fd_ops handler_fd_ops =
75 {
76     NULL,                     /* get_poll_events */
77     handler_poll_event,       /* poll_event */
78     no_flush,                 /* flush */
79     no_get_file_info,         /* get_file_info */
80     no_queue_async,           /* queue_async */
81     no_cancel_async           /* cancel_async */
82 };
83
84 static struct handler *handler_sighup;
85 static struct handler *handler_sigterm;
86 static struct handler *handler_sigint;
87 static struct handler *handler_sigchld;
88 static struct handler *handler_sigio;
89
90 static sigset_t blocked_sigset;
91
92 static int watchdog;
93
94 /* create a signal handler */
95 static struct handler *create_handler( signal_callback callback )
96 {
97     struct handler *handler;
98     int fd[2];
99
100     if (pipe( fd ) == -1) return NULL;
101     if (!(handler = alloc_object( &handler_ops )))
102     {
103         close( fd[0] );
104         close( fd[1] );
105         return NULL;
106     }
107     handler->pipe_write = fd[1];
108     handler->pending    = 0;
109     handler->callback   = callback;
110
111     if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj )))
112     {
113         release_object( handler );
114         return NULL;
115     }
116     set_fd_events( handler->fd, POLLIN );
117     return handler;
118 }
119
120 /* handle a signal received for a given handler */
121 static void do_signal( struct handler *handler )
122 {
123     if (!handler->pending)
124     {
125         char dummy = 0;
126         handler->pending = 1;
127         write( handler->pipe_write, &dummy, 1 );
128     }
129 }
130
131 static void handler_dump( struct object *obj, int verbose )
132 {
133     struct handler *handler = (struct handler *)obj;
134     fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
135 }
136
137 static void handler_destroy( struct object *obj )
138 {
139     struct handler *handler = (struct handler *)obj;
140     if (handler->fd) release_object( handler->fd );
141     close( handler->pipe_write );
142 }
143
144 static void handler_poll_event( struct fd *fd, int event )
145 {
146     struct handler *handler = get_fd_user( fd );
147
148     if (event & (POLLERR | POLLHUP))
149     {
150         /* this is not supposed to happen */
151         fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
152         release_object( handler );
153     }
154     else if (event & POLLIN)
155     {
156         char dummy;
157
158         handler->pending = 0;
159         read( get_unix_fd( handler->fd ), &dummy, 1 );
160         handler->callback();
161     }
162 }
163
164 /* SIGHUP callback */
165 static void sighup_callback(void)
166 {
167 #ifdef DEBUG_OBJECTS
168     dump_objects();
169 #endif
170 }
171
172 /* SIGTERM callback */
173 static void sigterm_callback(void)
174 {
175     flush_registry();
176     exit(1);
177 }
178
179 /* SIGINT callback */
180 static void sigint_callback(void)
181 {
182     kill_all_processes( NULL, 1 );
183     flush_registry();
184     exit(1);
185 }
186
187 /* SIGHUP handler */
188 static void do_sighup( int signum )
189 {
190     do_signal( handler_sighup );
191 }
192
193 /* SIGTERM handler */
194 static void do_sigterm( int signum )
195 {
196     do_signal( handler_sigterm );
197 }
198
199 /* SIGINT handler */
200 static void do_sigint( int signum )
201 {
202     do_signal( handler_sigint );
203 }
204
205 /* SIGALRM handler */
206 static void do_sigalrm( int signum )
207 {
208     watchdog = 1;
209 }
210
211 /* SIGCHLD handler */
212 static void do_sigchld( int signum )
213 {
214     do_signal( handler_sigchld );
215 }
216
217 /* SIGIO handler */
218 #ifdef HAVE_SIGINFO_T_SI_FD
219 static void do_sigio( int signum, siginfo_t *si, void *x )
220 {
221     do_signal( handler_sigio );
222     do_change_notify( si->si_fd );
223 }
224 #endif
225
226 void start_watchdog(void)
227 {
228     alarm( 3 );
229     watchdog = 0;
230 }
231
232 void stop_watchdog(void)
233 {
234     alarm( 0 );
235     watchdog = 0;
236 }
237
238 int watchdog_triggered(void)
239 {
240     return watchdog != 0;
241 }
242
243 void init_signals(void)
244 {
245     struct sigaction action;
246
247     if (!(handler_sighup  = create_handler( sighup_callback ))) goto error;
248     if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
249     if (!(handler_sigint  = create_handler( sigint_callback ))) goto error;
250     if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
251     if (!(handler_sigio   = create_handler( sigio_callback ))) goto error;
252
253     sigemptyset( &blocked_sigset );
254     sigaddset( &blocked_sigset, SIGCHLD );
255     sigaddset( &blocked_sigset, SIGHUP );
256     sigaddset( &blocked_sigset, SIGINT );
257     sigaddset( &blocked_sigset, SIGALRM );
258     sigaddset( &blocked_sigset, SIGIO );
259     sigaddset( &blocked_sigset, SIGQUIT );
260     sigaddset( &blocked_sigset, SIGTERM );
261 #ifdef SIG_PTHREAD_CANCEL
262     sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL );
263 #endif
264
265     action.sa_mask = blocked_sigset;
266     action.sa_flags = 0;
267     action.sa_handler = do_sigchld;
268     sigaction( SIGCHLD, &action, NULL );
269 #ifdef SIG_PTHREAD_CANCEL
270     sigaction( SIG_PTHREAD_CANCEL, &action, NULL );
271 #endif
272     action.sa_handler = do_sighup;
273     sigaction( SIGHUP, &action, NULL );
274     action.sa_handler = do_sigint;
275     sigaction( SIGINT, &action, NULL );
276     action.sa_handler = do_sigalrm;
277     sigaction( SIGALRM, &action, NULL );
278     action.sa_handler = do_sigterm;
279     sigaction( SIGQUIT, &action, NULL );
280     sigaction( SIGTERM, &action, NULL );
281     action.sa_handler = SIG_IGN;
282     sigaction( SIGXFSZ, &action, NULL );
283 #ifdef HAVE_SIGINFO_T_SI_FD
284     action.sa_sigaction = do_sigio;
285     action.sa_flags = SA_SIGINFO;
286     sigaction( SIGIO, &action, NULL );
287 #endif
288     return;
289
290 error:
291     fprintf( stderr, "failed to initialize signal handlers\n" );
292     exit(1);
293 }
294
295 void close_signals(void)
296 {
297     sigprocmask( SIG_BLOCK, &blocked_sigset, NULL );
298     release_object( handler_sighup );
299     release_object( handler_sigterm );
300     release_object( handler_sigint );
301     release_object( handler_sigchld );
302     release_object( handler_sigio );
303 }