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