riched32: Skeleton of richedit 1.0 test plus WM_SETTEXT test.
[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     default_get_sd,           /* get_sd */
74     default_set_sd,           /* set_sd */
75     no_lookup_name,           /* lookup_name */
76     no_open_file,             /* open_file */
77     no_close_handle,          /* close_handle */
78     handler_destroy           /* destroy */
79 };
80
81 static void handler_poll_event( struct fd *fd, int event );
82
83 static const struct fd_ops handler_fd_ops =
84 {
85     NULL,                     /* get_poll_events */
86     handler_poll_event,       /* poll_event */
87     NULL,                     /* flush */
88     NULL,                     /* get_fd_type */
89     NULL,                     /* ioctl */
90     NULL,                     /* queue_async */
91     NULL,                     /* reselect_async */
92     NULL                      /* cancel_async */
93 };
94
95 static struct handler *handler_sighup;
96 static struct handler *handler_sigterm;
97 static struct handler *handler_sigint;
98 static struct handler *handler_sigchld;
99 static struct handler *handler_sigio;
100
101 static int watchdog;
102
103 /* create a signal handler */
104 static struct handler *create_handler( signal_callback callback )
105 {
106     struct handler *handler;
107     int fd[2];
108
109     if (pipe( fd ) == -1) return NULL;
110     if (!(handler = alloc_object( &handler_ops )))
111     {
112         close( fd[0] );
113         close( fd[1] );
114         return NULL;
115     }
116     handler->pipe_write = fd[1];
117     handler->pending    = 0;
118     handler->callback   = callback;
119
120     if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj, 0 )))
121     {
122         release_object( handler );
123         return NULL;
124     }
125     set_fd_events( handler->fd, POLLIN );
126     make_object_static( &handler->obj );
127     return handler;
128 }
129
130 /* handle a signal received for a given handler */
131 static void do_signal( struct handler *handler )
132 {
133     if (!handler->pending)
134     {
135         char dummy = 0;
136         handler->pending = 1;
137         write( handler->pipe_write, &dummy, 1 );
138     }
139 }
140
141 static void handler_dump( struct object *obj, int verbose )
142 {
143     struct handler *handler = (struct handler *)obj;
144     fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
145 }
146
147 static void handler_destroy( struct object *obj )
148 {
149     struct handler *handler = (struct handler *)obj;
150     if (handler->fd) release_object( handler->fd );
151     close( handler->pipe_write );
152 }
153
154 static void handler_poll_event( struct fd *fd, int event )
155 {
156     struct handler *handler = get_fd_user( fd );
157
158     if (event & (POLLERR | POLLHUP))
159     {
160         /* this is not supposed to happen */
161         fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
162         release_object( handler );
163     }
164     else if (event & POLLIN)
165     {
166         char dummy;
167
168         handler->pending = 0;
169         read( get_unix_fd( handler->fd ), &dummy, 1 );
170         handler->callback();
171     }
172 }
173
174 /* SIGHUP callback */
175 static void sighup_callback(void)
176 {
177 #ifdef DEBUG_OBJECTS
178     dump_objects();
179 #endif
180 }
181
182 /* SIGTERM callback */
183 static void sigterm_callback(void)
184 {
185     flush_registry();
186     exit(1);
187 }
188
189 /* SIGINT callback */
190 static void sigint_callback(void)
191 {
192     kill_all_processes( NULL, 1 );
193     flush_registry();
194     shutdown_master_socket();
195 }
196
197 /* SIGHUP handler */
198 static void do_sighup( int signum )
199 {
200     do_signal( handler_sighup );
201 }
202
203 /* SIGTERM handler */
204 static void do_sigterm( int signum )
205 {
206     do_signal( handler_sigterm );
207 }
208
209 /* SIGINT handler */
210 static void do_sigint( int signum )
211 {
212     do_signal( handler_sigint );
213 }
214
215 /* SIGALRM handler */
216 static void do_sigalrm( int signum )
217 {
218     watchdog = 1;
219 }
220
221 /* SIGCHLD handler */
222 static void do_sigchld( int signum )
223 {
224     do_signal( handler_sigchld );
225 }
226
227 /* SIGSEGV handler */
228 static void do_sigsegv( int signum )
229 {
230     fprintf( stderr, "wineserver crashed, please enable coredumps (ulimit -c unlimited) and restart.\n");
231     abort();
232 }
233
234 /* SIGIO handler */
235 #ifdef HAVE_SIGINFO_T_SI_FD
236 static void do_sigio( int signum, siginfo_t *si, void *x )
237 {
238     do_signal( handler_sigio );
239     do_change_notify( si->si_fd );
240 }
241 #endif
242
243 void start_watchdog(void)
244 {
245     alarm( 3 );
246     watchdog = 0;
247 }
248
249 void stop_watchdog(void)
250 {
251     alarm( 0 );
252     watchdog = 0;
253 }
254
255 int watchdog_triggered(void)
256 {
257     return watchdog != 0;
258 }
259
260 static int core_dump_disabled( void )
261 {
262     int r = 0;
263 #ifdef RLIMIT_CORE
264     struct rlimit lim;
265
266     r = !getrlimit(RLIMIT_CORE, &lim) && (lim.rlim_cur == 0);
267 #endif
268     return r;
269 }
270
271 void init_signals(void)
272 {
273     struct sigaction action;
274     sigset_t blocked_sigset;
275
276     if (!(handler_sighup  = create_handler( sighup_callback ))) goto error;
277     if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
278     if (!(handler_sigint  = create_handler( sigint_callback ))) goto error;
279     if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
280     if (!(handler_sigio   = create_handler( sigio_callback ))) goto error;
281
282     sigemptyset( &blocked_sigset );
283     sigaddset( &blocked_sigset, SIGCHLD );
284     sigaddset( &blocked_sigset, SIGHUP );
285     sigaddset( &blocked_sigset, SIGINT );
286     sigaddset( &blocked_sigset, SIGALRM );
287     sigaddset( &blocked_sigset, SIGIO );
288     sigaddset( &blocked_sigset, SIGQUIT );
289     sigaddset( &blocked_sigset, SIGTERM );
290 #ifdef SIG_PTHREAD_CANCEL
291     sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL );
292 #endif
293
294     action.sa_mask = blocked_sigset;
295     action.sa_flags = 0;
296     action.sa_handler = do_sigchld;
297     sigaction( SIGCHLD, &action, NULL );
298 #ifdef SIG_PTHREAD_CANCEL
299     sigaction( SIG_PTHREAD_CANCEL, &action, NULL );
300 #endif
301     action.sa_handler = do_sighup;
302     sigaction( SIGHUP, &action, NULL );
303     action.sa_handler = do_sigint;
304     sigaction( SIGINT, &action, NULL );
305     action.sa_handler = do_sigalrm;
306     sigaction( SIGALRM, &action, NULL );
307     action.sa_handler = do_sigterm;
308     sigaction( SIGQUIT, &action, NULL );
309     sigaction( SIGTERM, &action, NULL );
310     if (core_dump_disabled())
311     {
312         action.sa_handler = do_sigsegv;
313         sigaction( SIGSEGV, &action, NULL );
314     }
315     action.sa_handler = SIG_IGN;
316     sigaction( SIGXFSZ, &action, NULL );
317 #ifdef HAVE_SIGINFO_T_SI_FD
318     action.sa_sigaction = do_sigio;
319     action.sa_flags = SA_SIGINFO;
320     sigaction( SIGIO, &action, NULL );
321 #endif
322     return;
323
324 error:
325     fprintf( stderr, "failed to initialize signal handlers\n" );
326     exit(1);
327 }