Accept headers without \r\n ending in HttpAddRequestHeaders.
[wine.git] / server / signal.c
blob99d9b82dc08a70e9f17c8c1315c4f871e772b100
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 #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
40 typedef void (*signal_callback)(void);
42 struct handler
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 */
51 static void handler_dump( struct object *obj, int verbose );
52 static void handler_destroy( struct object *obj );
54 static const struct object_ops handler_ops =
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 */
66 static void handler_poll_event( struct fd *fd, int event );
68 static const struct fd_ops handler_fd_ops =
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 */
77 static struct handler *handler_sighup;
78 static struct handler *handler_sigterm;
79 static struct handler *handler_sigint;
80 static struct handler *handler_sigchld;
81 static struct handler *handler_sigio;
83 static sigset_t blocked_sigset;
85 /* create a signal handler */
86 static struct handler *create_handler( signal_callback callback )
88 struct handler *handler;
89 int fd[2];
91 if (pipe( fd ) == -1) return NULL;
92 if (!(handler = alloc_object( &handler_ops )))
94 close( fd[0] );
95 close( fd[1] );
96 return NULL;
98 handler->pipe_write = fd[1];
99 handler->pending = 0;
100 handler->callback = callback;
102 if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj )))
104 release_object( handler );
105 return NULL;
107 set_fd_events( handler->fd, POLLIN );
108 return handler;
111 /* handle a signal received for a given handler */
112 static void do_signal( struct handler *handler )
114 if (!handler->pending)
116 char dummy = 0;
117 handler->pending = 1;
118 write( handler->pipe_write, &dummy, 1 );
122 static void handler_dump( struct object *obj, int verbose )
124 struct handler *handler = (struct handler *)obj;
125 fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
128 static void handler_destroy( struct object *obj )
130 struct handler *handler = (struct handler *)obj;
131 if (handler->fd) release_object( handler->fd );
132 close( handler->pipe_write );
135 static void handler_poll_event( struct fd *fd, int event )
137 struct handler *handler = get_fd_user( fd );
139 if (event & (POLLERR | POLLHUP))
141 /* this is not supposed to happen */
142 fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
143 release_object( handler );
145 else if (event & POLLIN)
147 char dummy;
149 handler->pending = 0;
150 read( get_unix_fd( handler->fd ), &dummy, 1 );
151 handler->callback();
155 /* SIGHUP callback */
156 static void sighup_callback(void)
158 #ifdef DEBUG_OBJECTS
159 dump_objects();
160 #endif
163 /* SIGTERM callback */
164 static void sigterm_callback(void)
166 flush_registry();
167 exit(1);
170 /* SIGINT callback */
171 static void sigint_callback(void)
173 kill_all_processes( NULL, 1 );
174 flush_registry();
175 exit(1);
178 /* SIGHUP handler */
179 static void do_sighup()
181 do_signal( handler_sighup );
184 /* SIGTERM handler */
185 static void do_sigterm()
187 do_signal( handler_sigterm );
190 /* SIGINT handler */
191 static void do_sigint()
193 do_signal( handler_sigint );
196 /* SIGCHLD handler */
197 static void do_sigchld()
199 do_signal( handler_sigchld );
202 /* SIGIO handler */
203 #ifdef HAVE_SIGINFO_T_SI_FD
204 static void do_sigio( int signum, siginfo_t *si, void *x )
206 do_signal( handler_sigio );
207 do_change_notify( si->si_fd );
209 #endif
211 void init_signals(void)
213 struct sigaction action;
215 if (!(handler_sighup = create_handler( sighup_callback ))) goto error;
216 if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
217 if (!(handler_sigint = create_handler( sigint_callback ))) goto error;
218 if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
219 if (!(handler_sigio = create_handler( sigio_callback ))) goto error;
221 sigemptyset( &blocked_sigset );
222 sigaddset( &blocked_sigset, SIGCHLD );
223 sigaddset( &blocked_sigset, SIGHUP );
224 sigaddset( &blocked_sigset, SIGINT );
225 sigaddset( &blocked_sigset, SIGIO );
226 sigaddset( &blocked_sigset, SIGQUIT );
227 sigaddset( &blocked_sigset, SIGTERM );
228 #ifdef SIG_PTHREAD_CANCEL
229 sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL );
230 #endif
232 action.sa_mask = blocked_sigset;
233 action.sa_flags = 0;
234 action.sa_handler = do_sigchld;
235 sigaction( SIGCHLD, &action, NULL );
236 #ifdef SIG_PTHREAD_CANCEL
237 sigaction( SIG_PTHREAD_CANCEL, &action, NULL );
238 #endif
239 action.sa_handler = do_sighup;
240 sigaction( SIGHUP, &action, NULL );
241 action.sa_handler = do_sigint;
242 sigaction( SIGINT, &action, NULL );
243 action.sa_handler = do_sigterm;
244 sigaction( SIGQUIT, &action, NULL );
245 sigaction( SIGTERM, &action, NULL );
246 action.sa_handler = SIG_IGN;
247 sigaction( SIGXFSZ, &action, NULL );
248 #ifdef HAVE_SIGINFO_T_SI_FD
249 action.sa_sigaction = do_sigio;
250 action.sa_flags = SA_SIGINFO;
251 sigaction( SIGIO, &action, NULL );
252 #endif
253 return;
255 error:
256 fprintf( stderr, "failed to initialize signal handlers\n" );
257 exit(1);
260 void close_signals(void)
262 sigprocmask( SIG_BLOCK, &blocked_sigset, NULL );
263 release_object( handler_sighup );
264 release_object( handler_sigterm );
265 release_object( handler_sigint );
266 release_object( handler_sigchld );
267 release_object( handler_sigio );