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