Prevent crash when no URL is specified.
[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_get_fd,                /* get_fd */
66     handler_destroy           /* destroy */
67 };
68
69 static void handler_poll_event( struct fd *fd, int event );
70
71 static const struct fd_ops handler_fd_ops =
72 {
73     NULL,                     /* get_poll_events */
74     handler_poll_event,       /* poll_event */
75     no_flush,                 /* flush */
76     no_get_file_info,         /* get_file_info */
77     no_queue_async,           /* queue_async */
78     no_cancel_async           /* cancel_async */
79 };
80
81 static struct handler *handler_sighup;
82 static struct handler *handler_sigterm;
83 static struct handler *handler_sigint;
84 static struct handler *handler_sigchld;
85 static struct handler *handler_sigio;
86
87 static sigset_t blocked_sigset;
88
89 /* create a signal handler */
90 static struct handler *create_handler( signal_callback callback )
91 {
92     struct handler *handler;
93     int fd[2];
94
95     if (pipe( fd ) == -1) return NULL;
96     if (!(handler = alloc_object( &handler_ops )))
97     {
98         close( fd[0] );
99         close( fd[1] );
100         return NULL;
101     }
102     handler->pipe_write = fd[1];
103     handler->pending    = 0;
104     handler->callback   = callback;
105
106     if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj )))
107     {
108         release_object( handler );
109         return NULL;
110     }
111     set_fd_events( handler->fd, POLLIN );
112     return handler;
113 }
114
115 /* handle a signal received for a given handler */
116 static void do_signal( struct handler *handler )
117 {
118     if (!handler->pending)
119     {
120         char dummy = 0;
121         handler->pending = 1;
122         write( handler->pipe_write, &dummy, 1 );
123     }
124 }
125
126 static void handler_dump( struct object *obj, int verbose )
127 {
128     struct handler *handler = (struct handler *)obj;
129     fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
130 }
131
132 static void handler_destroy( struct object *obj )
133 {
134     struct handler *handler = (struct handler *)obj;
135     if (handler->fd) release_object( handler->fd );
136     close( handler->pipe_write );
137 }
138
139 static void handler_poll_event( struct fd *fd, int event )
140 {
141     struct handler *handler = get_fd_user( fd );
142
143     if (event & (POLLERR | POLLHUP))
144     {
145         /* this is not supposed to happen */
146         fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
147         release_object( handler );
148     }
149     else if (event & POLLIN)
150     {
151         char dummy;
152
153         handler->pending = 0;
154         read( get_unix_fd( handler->fd ), &dummy, 1 );
155         handler->callback();
156     }
157 }
158
159 /* SIGHUP callback */
160 static void sighup_callback(void)
161 {
162 #ifdef DEBUG_OBJECTS
163     dump_objects();
164 #endif
165 }
166
167 /* SIGTERM callback */
168 static void sigterm_callback(void)
169 {
170     flush_registry();
171     exit(1);
172 }
173
174 /* SIGINT callback */
175 static void sigint_callback(void)
176 {
177     kill_all_processes( NULL, 1 );
178     flush_registry();
179     exit(1);
180 }
181
182 /* SIGHUP handler */
183 static void do_sighup()
184 {
185     do_signal( handler_sighup );
186 }
187
188 /* SIGTERM handler */
189 static void do_sigterm()
190 {
191     do_signal( handler_sigterm );
192 }
193
194 /* SIGINT handler */
195 static void do_sigint()
196 {
197     do_signal( handler_sigint );
198 }
199
200 /* SIGCHLD handler */
201 static void do_sigchld()
202 {
203     do_signal( handler_sigchld );
204 }
205
206 /* SIGIO handler */
207 #ifdef HAVE_SIGINFO_T_SI_FD
208 static void do_sigio( int signum, siginfo_t *si, void *x )
209 {
210     do_signal( handler_sigio );
211     do_change_notify( si->si_fd );
212 }
213 #endif
214
215 void init_signals(void)
216 {
217     struct sigaction action;
218
219     if (!(handler_sighup  = create_handler( sighup_callback ))) goto error;
220     if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
221     if (!(handler_sigint  = create_handler( sigint_callback ))) goto error;
222     if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
223     if (!(handler_sigio   = create_handler( sigio_callback ))) goto error;
224
225     sigemptyset( &blocked_sigset );
226     sigaddset( &blocked_sigset, SIGCHLD );
227     sigaddset( &blocked_sigset, SIGHUP );
228     sigaddset( &blocked_sigset, SIGINT );
229     sigaddset( &blocked_sigset, SIGIO );
230     sigaddset( &blocked_sigset, SIGQUIT );
231     sigaddset( &blocked_sigset, SIGTERM );
232 #ifdef SIG_PTHREAD_CANCEL
233     sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL );
234 #endif
235
236     action.sa_mask = blocked_sigset;
237     action.sa_flags = 0;
238     action.sa_handler = do_sigchld;
239     sigaction( SIGCHLD, &action, NULL );
240 #ifdef SIG_PTHREAD_CANCEL
241     sigaction( SIG_PTHREAD_CANCEL, &action, NULL );
242 #endif
243     action.sa_handler = do_sighup;
244     sigaction( SIGHUP, &action, NULL );
245     action.sa_handler = do_sigint;
246     sigaction( SIGINT, &action, NULL );
247     action.sa_handler = do_sigterm;
248     sigaction( SIGQUIT, &action, NULL );
249     sigaction( SIGTERM, &action, NULL );
250     action.sa_handler = SIG_IGN;
251     sigaction( SIGXFSZ, &action, NULL );
252 #ifdef HAVE_SIGINFO_T_SI_FD
253     action.sa_sigaction = do_sigio;
254     action.sa_flags = SA_SIGINFO;
255     sigaction( SIGIO, &action, NULL );
256 #endif
257     return;
258
259 error:
260     fprintf( stderr, "failed to initialize signal handlers\n" );
261     exit(1);
262 }
263
264 void close_signals(void)
265 {
266     sigprocmask( SIG_BLOCK, &blocked_sigset, NULL );
267     release_object( handler_sighup );
268     release_object( handler_sigterm );
269     release_object( handler_sigint );
270     release_object( handler_sigchld );
271     release_object( handler_sigio );
272 }