Fix French translation.
[wine/multimedia.git] / server / signal.c
blob29b1abb86da17dea1b27fdb2a66e878bac604c69
1 /*
2 * Server signal handling
4 * Copyright (C) 2003 Alexandre Julliard
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.
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.
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
21 #include "config.h"
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>
30 #include "file.h"
31 #include "object.h"
32 #include "process.h"
33 #include "thread.h"
35 typedef void (*signal_callback)(void);
37 struct handler
39 struct object obj; /* object header */
40 struct fd *fd; /* file descriptor for the pipe side */
41 int pipe_write; /* unix fd for the pipe write side */
42 volatile int pending; /* is signal pending? */
43 signal_callback callback; /* callback function */
46 static void handler_dump( struct object *obj, int verbose );
47 static void handler_destroy( struct object *obj );
49 static const struct object_ops handler_ops =
51 sizeof(struct handler), /* size */
52 handler_dump, /* dump */
53 no_add_queue, /* add_queue */
54 NULL, /* remove_queue */
55 NULL, /* signaled */
56 NULL, /* satisfied */
57 no_get_fd, /* get_fd */
58 handler_destroy /* destroy */
61 static void handler_poll_event( struct fd *fd, int event );
63 static const struct fd_ops handler_fd_ops =
65 NULL, /* get_poll_events */
66 handler_poll_event, /* poll_event */
67 no_flush, /* flush */
68 no_get_file_info, /* get_file_info */
69 no_queue_async /* queue_async */
72 static struct handler *handler_sighup;
73 static struct handler *handler_sigterm;
74 static struct handler *handler_sigint;
75 static struct handler *handler_sigchld;
76 static struct handler *handler_sigio;
78 static sigset_t blocked_sigset;
80 /* create a signal handler */
81 static struct handler *create_handler( signal_callback callback )
83 struct handler *handler;
84 int fd[2];
86 if (pipe( fd ) == -1) return NULL;
87 if (!(handler = alloc_object( &handler_ops )))
89 close( fd[0] );
90 close( fd[1] );
91 return NULL;
93 handler->pipe_write = fd[1];
94 handler->pending = 0;
95 handler->callback = callback;
97 if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj )))
99 release_object( handler );
100 return NULL;
102 set_fd_events( handler->fd, POLLIN );
103 return handler;
106 /* handle a signal received for a given handler */
107 static void do_signal( struct handler *handler )
109 if (!handler->pending)
111 char dummy = 0;
112 handler->pending = 1;
113 write( handler->pipe_write, &dummy, 1 );
117 static void handler_dump( struct object *obj, int verbose )
119 struct handler *handler = (struct handler *)obj;
120 fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
123 static void handler_destroy( struct object *obj )
125 struct handler *handler = (struct handler *)obj;
126 if (handler->fd) release_object( handler->fd );
127 close( handler->pipe_write );
130 static void handler_poll_event( struct fd *fd, int event )
132 struct handler *handler = get_fd_user( fd );
134 if (event & (POLLERR | POLLHUP))
136 /* this is not supposed to happen */
137 fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
138 release_object( handler );
140 else if (event & POLLIN)
142 char dummy;
144 handler->pending = 0;
145 read( get_unix_fd( handler->fd ), &dummy, 1 );
146 handler->callback();
150 /* SIGHUP callback */
151 static void sighup_callback(void)
153 #ifdef DEBUG_OBJECTS
154 dump_objects();
155 #endif
158 /* SIGTERM callback */
159 static void sigterm_callback(void)
161 flush_registry();
162 exit(1);
165 /* SIGINT callback */
166 static void sigint_callback(void)
168 kill_all_processes( NULL, 1 );
169 flush_registry();
170 exit(1);
173 /* SIGHUP handler */
174 static void do_sighup()
176 do_signal( handler_sighup );
179 /* SIGTERM handler */
180 static void do_sigterm()
182 do_signal( handler_sigterm );
185 /* SIGINT handler */
186 static void do_sigint()
188 do_signal( handler_sigint );
191 /* SIGCHLD handler */
192 static void do_sigchld()
194 do_signal( handler_sigchld );
197 /* SIGIO handler */
198 #ifdef HAVE_SIGINFO_T_SI_FD
199 static void do_sigio( int signum, siginfo_t *si, void *x )
201 do_signal( handler_sigio );
202 do_change_notify( si->si_fd );
204 #endif
206 void init_signals(void)
208 struct sigaction action;
210 if (!(handler_sighup = create_handler( sighup_callback ))) goto error;
211 if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
212 if (!(handler_sigint = create_handler( sigint_callback ))) goto error;
213 if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
214 if (!(handler_sigio = create_handler( sigio_callback ))) goto error;
216 sigemptyset( &blocked_sigset );
217 sigaddset( &blocked_sigset, SIGCHLD );
218 sigaddset( &blocked_sigset, SIGHUP );
219 sigaddset( &blocked_sigset, SIGINT );
220 sigaddset( &blocked_sigset, SIGIO );
221 sigaddset( &blocked_sigset, SIGQUIT );
222 sigaddset( &blocked_sigset, SIGTERM );
224 action.sa_mask = blocked_sigset;
225 action.sa_flags = 0;
226 action.sa_handler = do_sigchld;
227 sigaction( SIGCHLD, &action, NULL );
228 action.sa_handler = do_sighup;
229 sigaction( SIGHUP, &action, NULL );
230 action.sa_handler = do_sigint;
231 sigaction( SIGINT, &action, NULL );
232 action.sa_handler = do_sigterm;
233 sigaction( SIGQUIT, &action, NULL );
234 sigaction( SIGTERM, &action, NULL );
235 #ifdef HAVE_SIGINFO_T_SI_FD
236 action.sa_sigaction = do_sigio;
237 action.sa_flags = SA_SIGINFO;
238 sigaction( SIGIO, &action, NULL );
239 #endif
240 return;
242 error:
243 fprintf( stderr, "failed to initialize signal handlers\n" );
244 exit(1);
247 void close_signals(void)
249 sigprocmask( SIG_BLOCK, &blocked_sigset, NULL );
250 release_object( handler_sighup );
251 release_object( handler_sigterm );
252 release_object( handler_sigint );
253 release_object( handler_sigchld );
254 release_object( handler_sigio );