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