Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #include <signal.h> |
| 24 | #include <stdio.h> |
Phil Krylov | 8e772c8 | 2006-03-24 03:49:50 +0300 | [diff] [blame] | 25 | #include <sys/time.h> |
Steven Edwards | 5727918 | 2005-03-04 12:38:36 +0000 | [diff] [blame] | 26 | #ifdef HAVE_POLL_H |
| 27 | #include <poll.h> |
| 28 | #endif |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 29 | #ifdef HAVE_SYS_POLL_H |
| 30 | #include <sys/poll.h> |
| 31 | #endif |
Mike McCormack | 0cd0626 | 2006-03-01 01:07:04 +0900 | [diff] [blame] | 32 | #ifdef HAVE_SYS_RESOURCE_H |
| 33 | #include <sys/resource.h> |
| 34 | #endif |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 35 | #include <unistd.h> |
| 36 | |
| 37 | #include "file.h" |
| 38 | #include "object.h" |
| 39 | #include "process.h" |
| 40 | #include "thread.h" |
Alexandre Julliard | 3a4c04d | 2006-08-14 20:40:31 +0200 | [diff] [blame] | 41 | #include "request.h" |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 42 | |
Alexandre Julliard | f9f37be | 2003-10-24 04:29:01 +0000 | [diff] [blame] | 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 | |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 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 */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 70 | no_signal, /* signal */ |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 71 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame] | 72 | no_map_access, /* map_access */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 73 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 74 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 75 | handler_destroy /* destroy */ |
| 76 | }; |
| 77 | |
| 78 | static void handler_poll_event( struct fd *fd, int event ); |
| 79 | |
| 80 | static const struct fd_ops handler_fd_ops = |
| 81 | { |
| 82 | NULL, /* get_poll_events */ |
| 83 | handler_poll_event, /* poll_event */ |
| 84 | no_flush, /* flush */ |
| 85 | no_get_file_info, /* get_file_info */ |
Eric Pouech | 4634447 | 2005-01-14 19:54:38 +0000 | [diff] [blame] | 86 | no_queue_async, /* queue_async */ |
| 87 | no_cancel_async /* cancel_async */ |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | static struct handler *handler_sighup; |
| 91 | static struct handler *handler_sigterm; |
| 92 | static struct handler *handler_sigint; |
| 93 | static struct handler *handler_sigchld; |
| 94 | static struct handler *handler_sigio; |
| 95 | |
Alexandre Julliard | 9d99a04 | 2005-08-19 14:01:43 +0000 | [diff] [blame] | 96 | static int watchdog; |
| 97 | |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 98 | /* create a signal handler */ |
| 99 | static struct handler *create_handler( signal_callback callback ) |
| 100 | { |
| 101 | struct handler *handler; |
| 102 | int fd[2]; |
| 103 | |
| 104 | if (pipe( fd ) == -1) return NULL; |
| 105 | if (!(handler = alloc_object( &handler_ops ))) |
| 106 | { |
| 107 | close( fd[0] ); |
| 108 | close( fd[1] ); |
| 109 | return NULL; |
| 110 | } |
| 111 | handler->pipe_write = fd[1]; |
| 112 | handler->pending = 0; |
| 113 | handler->callback = callback; |
| 114 | |
| 115 | if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj ))) |
| 116 | { |
| 117 | release_object( handler ); |
| 118 | return NULL; |
| 119 | } |
| 120 | set_fd_events( handler->fd, POLLIN ); |
Alexandre Julliard | b00fb17 | 2006-03-22 20:32:04 +0100 | [diff] [blame] | 121 | make_object_static( &handler->obj ); |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 122 | return handler; |
| 123 | } |
| 124 | |
| 125 | /* handle a signal received for a given handler */ |
| 126 | static void do_signal( struct handler *handler ) |
| 127 | { |
| 128 | if (!handler->pending) |
| 129 | { |
Alexandre Julliard | 17480ac | 2003-04-02 01:44:01 +0000 | [diff] [blame] | 130 | char dummy = 0; |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 131 | handler->pending = 1; |
| 132 | write( handler->pipe_write, &dummy, 1 ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | static void handler_dump( struct object *obj, int verbose ) |
| 137 | { |
| 138 | struct handler *handler = (struct handler *)obj; |
| 139 | fprintf( stderr, "Signal handler fd=%p\n", handler->fd ); |
| 140 | } |
| 141 | |
| 142 | static void handler_destroy( struct object *obj ) |
| 143 | { |
| 144 | struct handler *handler = (struct handler *)obj; |
| 145 | if (handler->fd) release_object( handler->fd ); |
| 146 | close( handler->pipe_write ); |
| 147 | } |
| 148 | |
| 149 | static void handler_poll_event( struct fd *fd, int event ) |
| 150 | { |
| 151 | struct handler *handler = get_fd_user( fd ); |
| 152 | |
| 153 | if (event & (POLLERR | POLLHUP)) |
| 154 | { |
| 155 | /* this is not supposed to happen */ |
| 156 | fprintf( stderr, "wineserver: Error on signal handler pipe\n" ); |
| 157 | release_object( handler ); |
| 158 | } |
| 159 | else if (event & POLLIN) |
| 160 | { |
| 161 | char dummy; |
| 162 | |
| 163 | handler->pending = 0; |
| 164 | read( get_unix_fd( handler->fd ), &dummy, 1 ); |
| 165 | handler->callback(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /* SIGHUP callback */ |
| 170 | static void sighup_callback(void) |
| 171 | { |
| 172 | #ifdef DEBUG_OBJECTS |
| 173 | dump_objects(); |
| 174 | #endif |
| 175 | } |
| 176 | |
| 177 | /* SIGTERM callback */ |
| 178 | static void sigterm_callback(void) |
| 179 | { |
| 180 | flush_registry(); |
| 181 | exit(1); |
| 182 | } |
| 183 | |
| 184 | /* SIGINT callback */ |
| 185 | static void sigint_callback(void) |
| 186 | { |
| 187 | kill_all_processes( NULL, 1 ); |
| 188 | flush_registry(); |
Alexandre Julliard | 3a4c04d | 2006-08-14 20:40:31 +0200 | [diff] [blame] | 189 | shutdown_master_socket(); |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 192 | /* SIGHUP handler */ |
Robert Shearman | c516571 | 2005-05-25 18:41:09 +0000 | [diff] [blame] | 193 | static void do_sighup( int signum ) |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 194 | { |
| 195 | do_signal( handler_sighup ); |
| 196 | } |
| 197 | |
| 198 | /* SIGTERM handler */ |
Robert Shearman | c516571 | 2005-05-25 18:41:09 +0000 | [diff] [blame] | 199 | static void do_sigterm( int signum ) |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 200 | { |
| 201 | do_signal( handler_sigterm ); |
| 202 | } |
| 203 | |
| 204 | /* SIGINT handler */ |
Robert Shearman | c516571 | 2005-05-25 18:41:09 +0000 | [diff] [blame] | 205 | static void do_sigint( int signum ) |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 206 | { |
| 207 | do_signal( handler_sigint ); |
| 208 | } |
| 209 | |
Alexandre Julliard | 9d99a04 | 2005-08-19 14:01:43 +0000 | [diff] [blame] | 210 | /* SIGALRM handler */ |
| 211 | static void do_sigalrm( int signum ) |
| 212 | { |
| 213 | watchdog = 1; |
| 214 | } |
| 215 | |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 216 | /* SIGCHLD handler */ |
Robert Shearman | c516571 | 2005-05-25 18:41:09 +0000 | [diff] [blame] | 217 | static void do_sigchld( int signum ) |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 218 | { |
| 219 | do_signal( handler_sigchld ); |
| 220 | } |
| 221 | |
Mike McCormack | 0cd0626 | 2006-03-01 01:07:04 +0900 | [diff] [blame] | 222 | /* SIGSEGV handler */ |
| 223 | static void do_sigsegv( int signum ) |
| 224 | { |
| 225 | fprintf( stderr, "wineserver crashed, please report this.\n"); |
| 226 | abort(); |
| 227 | } |
| 228 | |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 229 | /* SIGIO handler */ |
Yorick Hardy | 3f11d48 | 2003-04-13 01:06:14 +0000 | [diff] [blame] | 230 | #ifdef HAVE_SIGINFO_T_SI_FD |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 231 | static void do_sigio( int signum, siginfo_t *si, void *x ) |
| 232 | { |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 233 | do_signal( handler_sigio ); |
Alexandre Julliard | 3e588e3 | 2003-03-26 23:41:43 +0000 | [diff] [blame] | 234 | do_change_notify( si->si_fd ); |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 235 | } |
Yorick Hardy | 3f11d48 | 2003-04-13 01:06:14 +0000 | [diff] [blame] | 236 | #endif |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 237 | |
Alexandre Julliard | 9d99a04 | 2005-08-19 14:01:43 +0000 | [diff] [blame] | 238 | void start_watchdog(void) |
| 239 | { |
| 240 | alarm( 3 ); |
| 241 | watchdog = 0; |
| 242 | } |
| 243 | |
| 244 | void stop_watchdog(void) |
| 245 | { |
| 246 | alarm( 0 ); |
| 247 | watchdog = 0; |
| 248 | } |
| 249 | |
| 250 | int watchdog_triggered(void) |
| 251 | { |
| 252 | return watchdog != 0; |
| 253 | } |
| 254 | |
Mike McCormack | 0cd0626 | 2006-03-01 01:07:04 +0900 | [diff] [blame] | 255 | static int core_dump_disabled( void ) |
| 256 | { |
| 257 | int r = 0; |
| 258 | #ifdef RLIMIT_CORE |
| 259 | struct rlimit lim; |
| 260 | |
| 261 | r = !getrlimit(RLIMIT_CORE, &lim) && (lim.rlim_cur == 0); |
| 262 | #endif |
| 263 | return r; |
| 264 | } |
| 265 | |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 266 | void init_signals(void) |
| 267 | { |
| 268 | struct sigaction action; |
Alexandre Julliard | b00fb17 | 2006-03-22 20:32:04 +0100 | [diff] [blame] | 269 | sigset_t blocked_sigset; |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 270 | |
| 271 | if (!(handler_sighup = create_handler( sighup_callback ))) goto error; |
| 272 | if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error; |
| 273 | if (!(handler_sigint = create_handler( sigint_callback ))) goto error; |
| 274 | if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error; |
| 275 | if (!(handler_sigio = create_handler( sigio_callback ))) goto error; |
| 276 | |
| 277 | sigemptyset( &blocked_sigset ); |
| 278 | sigaddset( &blocked_sigset, SIGCHLD ); |
| 279 | sigaddset( &blocked_sigset, SIGHUP ); |
| 280 | sigaddset( &blocked_sigset, SIGINT ); |
Alexandre Julliard | 9d99a04 | 2005-08-19 14:01:43 +0000 | [diff] [blame] | 281 | sigaddset( &blocked_sigset, SIGALRM ); |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 282 | sigaddset( &blocked_sigset, SIGIO ); |
| 283 | sigaddset( &blocked_sigset, SIGQUIT ); |
| 284 | sigaddset( &blocked_sigset, SIGTERM ); |
Alexandre Julliard | f9f37be | 2003-10-24 04:29:01 +0000 | [diff] [blame] | 285 | #ifdef SIG_PTHREAD_CANCEL |
| 286 | sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL ); |
| 287 | #endif |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 288 | |
| 289 | action.sa_mask = blocked_sigset; |
| 290 | action.sa_flags = 0; |
| 291 | action.sa_handler = do_sigchld; |
| 292 | sigaction( SIGCHLD, &action, NULL ); |
Alexandre Julliard | f9f37be | 2003-10-24 04:29:01 +0000 | [diff] [blame] | 293 | #ifdef SIG_PTHREAD_CANCEL |
| 294 | sigaction( SIG_PTHREAD_CANCEL, &action, NULL ); |
| 295 | #endif |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 296 | action.sa_handler = do_sighup; |
| 297 | sigaction( SIGHUP, &action, NULL ); |
| 298 | action.sa_handler = do_sigint; |
| 299 | sigaction( SIGINT, &action, NULL ); |
Alexandre Julliard | 9d99a04 | 2005-08-19 14:01:43 +0000 | [diff] [blame] | 300 | action.sa_handler = do_sigalrm; |
| 301 | sigaction( SIGALRM, &action, NULL ); |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 302 | action.sa_handler = do_sigterm; |
| 303 | sigaction( SIGQUIT, &action, NULL ); |
| 304 | sigaction( SIGTERM, &action, NULL ); |
Mike McCormack | 0cd0626 | 2006-03-01 01:07:04 +0900 | [diff] [blame] | 305 | if (core_dump_disabled()) |
| 306 | { |
| 307 | action.sa_handler = do_sigsegv; |
| 308 | sigaction( SIGSEGV, &action, NULL ); |
| 309 | } |
Alexandre Julliard | 72f87b8 | 2004-05-01 02:50:06 +0000 | [diff] [blame] | 310 | action.sa_handler = SIG_IGN; |
| 311 | sigaction( SIGXFSZ, &action, NULL ); |
Yorick Hardy | 3f11d48 | 2003-04-13 01:06:14 +0000 | [diff] [blame] | 312 | #ifdef HAVE_SIGINFO_T_SI_FD |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 313 | action.sa_sigaction = do_sigio; |
| 314 | action.sa_flags = SA_SIGINFO; |
| 315 | sigaction( SIGIO, &action, NULL ); |
Yorick Hardy | 3f11d48 | 2003-04-13 01:06:14 +0000 | [diff] [blame] | 316 | #endif |
Alexandre Julliard | 9037f4b | 2003-03-26 01:32:18 +0000 | [diff] [blame] | 317 | return; |
| 318 | |
| 319 | error: |
| 320 | fprintf( stderr, "failed to initialize signal handlers\n" ); |
| 321 | exit(1); |
| 322 | } |