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