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