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
25 #ifdef HAVE_SYS_POLL_H
35 typedef void (*signal_callback)(void);
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 */
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 */
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;
86 if (pipe( fd ) == -1) return NULL;
87 if (!(handler = alloc_object( &handler_ops )))
93 handler->pipe_write = fd[1];
95 handler->callback = callback;
97 if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj )))
99 release_object( handler );
102 set_fd_events( handler->fd, POLLIN );
106 /* handle a signal received for a given handler */
107 static void do_signal( struct handler *handler )
109 if (!handler->pending)
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)
144 handler->pending = 0;
145 read( get_unix_fd( handler->fd ), &dummy, 1 );
150 /* SIGHUP callback */
151 static void sighup_callback(void)
158 /* SIGTERM callback */
159 static void sigterm_callback(void)
165 /* SIGINT callback */
166 static void sigint_callback(void)
168 kill_all_processes( NULL, 1 );
174 static void do_sighup()
176 do_signal( handler_sighup );
179 /* SIGTERM handler */
180 static void do_sigterm()
182 do_signal( handler_sigterm );
186 static void do_sigint()
188 do_signal( handler_sigint );
191 /* SIGCHLD handler */
192 static void do_sigchld()
194 do_signal( handler_sigchld );
198 static void do_sigio( int signum, siginfo_t *si, void *x )
200 do_signal( handler_sigio );
201 do_change_notify( si->si_fd );
204 void init_signals(void)
206 struct sigaction action;
208 if (!(handler_sighup = create_handler( sighup_callback ))) goto error;
209 if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
210 if (!(handler_sigint = create_handler( sigint_callback ))) goto error;
211 if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
212 if (!(handler_sigio = create_handler( sigio_callback ))) goto error;
214 sigemptyset( &blocked_sigset );
215 sigaddset( &blocked_sigset, SIGCHLD );
216 sigaddset( &blocked_sigset, SIGHUP );
217 sigaddset( &blocked_sigset, SIGINT );
218 sigaddset( &blocked_sigset, SIGIO );
219 sigaddset( &blocked_sigset, SIGQUIT );
220 sigaddset( &blocked_sigset, SIGTERM );
222 action.sa_mask = blocked_sigset;
224 action.sa_handler = do_sigchld;
225 sigaction( SIGCHLD, &action, NULL );
226 action.sa_handler = do_sighup;
227 sigaction( SIGHUP, &action, NULL );
228 action.sa_handler = do_sigint;
229 sigaction( SIGINT, &action, NULL );
230 action.sa_handler = do_sigterm;
231 sigaction( SIGQUIT, &action, NULL );
232 sigaction( SIGTERM, &action, NULL );
233 action.sa_sigaction = do_sigio;
234 action.sa_flags = SA_SIGINFO;
235 sigaction( SIGIO, &action, NULL );
239 fprintf( stderr, "failed to initialize signal handlers\n" );
243 void close_signals(void)
245 sigprocmask( SIG_BLOCK, &blocked_sigset, NULL );
246 release_object( handler_sighup );
247 release_object( handler_sigterm );
248 release_object( handler_sigint );
249 release_object( handler_sigchld );
250 release_object( handler_sigio );