2003-03-26 02:32:18 +01:00
|
|
|
/*
|
|
|
|
* Server signal handling
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
2005-03-04 13:38:36 +01:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
2003-03-26 02:32:18 +01:00
|
|
|
#ifdef HAVE_SYS_POLL_H
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#endif
|
2006-02-28 17:07:04 +01:00
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
2003-03-26 02:32:18 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "file.h"
|
|
|
|
#include "object.h"
|
|
|
|
#include "process.h"
|
|
|
|
#include "thread.h"
|
|
|
|
|
2003-10-24 06:29:01 +02:00
|
|
|
#if defined(linux) && defined(__SIGRTMIN)
|
|
|
|
/* the signal used by linuxthreads as exit signal for clone() threads */
|
|
|
|
# define SIG_PTHREAD_CANCEL (__SIGRTMIN+1)
|
|
|
|
#endif
|
|
|
|
|
2003-03-26 02:32:18 +01:00
|
|
|
typedef void (*signal_callback)(void);
|
|
|
|
|
|
|
|
struct handler
|
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
|
|
|
struct fd *fd; /* file descriptor for the pipe side */
|
|
|
|
int pipe_write; /* unix fd for the pipe write side */
|
|
|
|
volatile int pending; /* is signal pending? */
|
|
|
|
signal_callback callback; /* callback function */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void handler_dump( struct object *obj, int verbose );
|
|
|
|
static void handler_destroy( struct object *obj );
|
|
|
|
|
|
|
|
static const struct object_ops handler_ops =
|
|
|
|
{
|
|
|
|
sizeof(struct handler), /* size */
|
|
|
|
handler_dump, /* dump */
|
|
|
|
no_add_queue, /* add_queue */
|
|
|
|
NULL, /* remove_queue */
|
|
|
|
NULL, /* signaled */
|
|
|
|
NULL, /* satisfied */
|
2005-04-24 19:35:52 +02:00
|
|
|
no_signal, /* signal */
|
2003-03-26 02:32:18 +01:00
|
|
|
no_get_fd, /* get_fd */
|
2005-12-12 14:57:40 +01:00
|
|
|
no_map_access, /* map_access */
|
2005-11-22 15:55:42 +01:00
|
|
|
no_lookup_name, /* lookup_name */
|
2005-06-09 17:39:52 +02:00
|
|
|
no_close_handle, /* close_handle */
|
2003-03-26 02:32:18 +01:00
|
|
|
handler_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void handler_poll_event( struct fd *fd, int event );
|
|
|
|
|
|
|
|
static const struct fd_ops handler_fd_ops =
|
|
|
|
{
|
|
|
|
NULL, /* get_poll_events */
|
|
|
|
handler_poll_event, /* poll_event */
|
|
|
|
no_flush, /* flush */
|
|
|
|
no_get_file_info, /* get_file_info */
|
2005-01-14 20:54:38 +01:00
|
|
|
no_queue_async, /* queue_async */
|
|
|
|
no_cancel_async /* cancel_async */
|
2003-03-26 02:32:18 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct handler *handler_sighup;
|
|
|
|
static struct handler *handler_sigterm;
|
|
|
|
static struct handler *handler_sigint;
|
|
|
|
static struct handler *handler_sigchld;
|
|
|
|
static struct handler *handler_sigio;
|
|
|
|
|
|
|
|
static sigset_t blocked_sigset;
|
|
|
|
|
2005-08-19 16:01:43 +02:00
|
|
|
static int watchdog;
|
|
|
|
|
2003-03-26 02:32:18 +01:00
|
|
|
/* create a signal handler */
|
|
|
|
static struct handler *create_handler( signal_callback callback )
|
|
|
|
{
|
|
|
|
struct handler *handler;
|
|
|
|
int fd[2];
|
|
|
|
|
|
|
|
if (pipe( fd ) == -1) return NULL;
|
|
|
|
if (!(handler = alloc_object( &handler_ops )))
|
|
|
|
{
|
|
|
|
close( fd[0] );
|
|
|
|
close( fd[1] );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
handler->pipe_write = fd[1];
|
|
|
|
handler->pending = 0;
|
|
|
|
handler->callback = callback;
|
|
|
|
|
|
|
|
if (!(handler->fd = create_anonymous_fd( &handler_fd_ops, fd[0], &handler->obj )))
|
|
|
|
{
|
|
|
|
release_object( handler );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
set_fd_events( handler->fd, POLLIN );
|
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle a signal received for a given handler */
|
|
|
|
static void do_signal( struct handler *handler )
|
|
|
|
{
|
|
|
|
if (!handler->pending)
|
|
|
|
{
|
2003-04-02 03:44:01 +02:00
|
|
|
char dummy = 0;
|
2003-03-26 02:32:18 +01:00
|
|
|
handler->pending = 1;
|
|
|
|
write( handler->pipe_write, &dummy, 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handler_dump( struct object *obj, int verbose )
|
|
|
|
{
|
|
|
|
struct handler *handler = (struct handler *)obj;
|
|
|
|
fprintf( stderr, "Signal handler fd=%p\n", handler->fd );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handler_destroy( struct object *obj )
|
|
|
|
{
|
|
|
|
struct handler *handler = (struct handler *)obj;
|
|
|
|
if (handler->fd) release_object( handler->fd );
|
|
|
|
close( handler->pipe_write );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handler_poll_event( struct fd *fd, int event )
|
|
|
|
{
|
|
|
|
struct handler *handler = get_fd_user( fd );
|
|
|
|
|
|
|
|
if (event & (POLLERR | POLLHUP))
|
|
|
|
{
|
|
|
|
/* this is not supposed to happen */
|
|
|
|
fprintf( stderr, "wineserver: Error on signal handler pipe\n" );
|
|
|
|
release_object( handler );
|
|
|
|
}
|
|
|
|
else if (event & POLLIN)
|
|
|
|
{
|
|
|
|
char dummy;
|
|
|
|
|
|
|
|
handler->pending = 0;
|
|
|
|
read( get_unix_fd( handler->fd ), &dummy, 1 );
|
|
|
|
handler->callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGHUP callback */
|
|
|
|
static void sighup_callback(void)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_OBJECTS
|
|
|
|
dump_objects();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGTERM callback */
|
|
|
|
static void sigterm_callback(void)
|
|
|
|
{
|
|
|
|
flush_registry();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGINT callback */
|
|
|
|
static void sigint_callback(void)
|
|
|
|
{
|
|
|
|
kill_all_processes( NULL, 1 );
|
|
|
|
flush_registry();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGHUP handler */
|
2005-05-25 20:41:09 +02:00
|
|
|
static void do_sighup( int signum )
|
2003-03-26 02:32:18 +01:00
|
|
|
{
|
|
|
|
do_signal( handler_sighup );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGTERM handler */
|
2005-05-25 20:41:09 +02:00
|
|
|
static void do_sigterm( int signum )
|
2003-03-26 02:32:18 +01:00
|
|
|
{
|
|
|
|
do_signal( handler_sigterm );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGINT handler */
|
2005-05-25 20:41:09 +02:00
|
|
|
static void do_sigint( int signum )
|
2003-03-26 02:32:18 +01:00
|
|
|
{
|
|
|
|
do_signal( handler_sigint );
|
|
|
|
}
|
|
|
|
|
2005-08-19 16:01:43 +02:00
|
|
|
/* SIGALRM handler */
|
|
|
|
static void do_sigalrm( int signum )
|
|
|
|
{
|
|
|
|
watchdog = 1;
|
|
|
|
}
|
|
|
|
|
2003-03-26 02:32:18 +01:00
|
|
|
/* SIGCHLD handler */
|
2005-05-25 20:41:09 +02:00
|
|
|
static void do_sigchld( int signum )
|
2003-03-26 02:32:18 +01:00
|
|
|
{
|
|
|
|
do_signal( handler_sigchld );
|
|
|
|
}
|
|
|
|
|
2006-02-28 17:07:04 +01:00
|
|
|
/* SIGSEGV handler */
|
|
|
|
static void do_sigsegv( int signum )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "wineserver crashed, please report this.\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2003-03-26 02:32:18 +01:00
|
|
|
/* SIGIO handler */
|
2003-04-13 03:06:14 +02:00
|
|
|
#ifdef HAVE_SIGINFO_T_SI_FD
|
2003-03-26 02:32:18 +01:00
|
|
|
static void do_sigio( int signum, siginfo_t *si, void *x )
|
|
|
|
{
|
|
|
|
do_signal( handler_sigio );
|
2003-03-27 00:41:43 +01:00
|
|
|
do_change_notify( si->si_fd );
|
2003-03-26 02:32:18 +01:00
|
|
|
}
|
2003-04-13 03:06:14 +02:00
|
|
|
#endif
|
2003-03-26 02:32:18 +01:00
|
|
|
|
2005-08-19 16:01:43 +02:00
|
|
|
void start_watchdog(void)
|
|
|
|
{
|
|
|
|
alarm( 3 );
|
|
|
|
watchdog = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop_watchdog(void)
|
|
|
|
{
|
|
|
|
alarm( 0 );
|
|
|
|
watchdog = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int watchdog_triggered(void)
|
|
|
|
{
|
|
|
|
return watchdog != 0;
|
|
|
|
}
|
|
|
|
|
2006-02-28 17:07:04 +01:00
|
|
|
static int core_dump_disabled( void )
|
|
|
|
{
|
|
|
|
int r = 0;
|
|
|
|
#ifdef RLIMIT_CORE
|
|
|
|
struct rlimit lim;
|
|
|
|
|
|
|
|
r = !getrlimit(RLIMIT_CORE, &lim) && (lim.rlim_cur == 0);
|
|
|
|
#endif
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2003-03-26 02:32:18 +01:00
|
|
|
void init_signals(void)
|
|
|
|
{
|
|
|
|
struct sigaction action;
|
|
|
|
|
|
|
|
if (!(handler_sighup = create_handler( sighup_callback ))) goto error;
|
|
|
|
if (!(handler_sigterm = create_handler( sigterm_callback ))) goto error;
|
|
|
|
if (!(handler_sigint = create_handler( sigint_callback ))) goto error;
|
|
|
|
if (!(handler_sigchld = create_handler( sigchld_callback ))) goto error;
|
|
|
|
if (!(handler_sigio = create_handler( sigio_callback ))) goto error;
|
|
|
|
|
|
|
|
sigemptyset( &blocked_sigset );
|
|
|
|
sigaddset( &blocked_sigset, SIGCHLD );
|
|
|
|
sigaddset( &blocked_sigset, SIGHUP );
|
|
|
|
sigaddset( &blocked_sigset, SIGINT );
|
2005-08-19 16:01:43 +02:00
|
|
|
sigaddset( &blocked_sigset, SIGALRM );
|
2003-03-26 02:32:18 +01:00
|
|
|
sigaddset( &blocked_sigset, SIGIO );
|
|
|
|
sigaddset( &blocked_sigset, SIGQUIT );
|
|
|
|
sigaddset( &blocked_sigset, SIGTERM );
|
2003-10-24 06:29:01 +02:00
|
|
|
#ifdef SIG_PTHREAD_CANCEL
|
|
|
|
sigaddset( &blocked_sigset, SIG_PTHREAD_CANCEL );
|
|
|
|
#endif
|
2003-03-26 02:32:18 +01:00
|
|
|
|
|
|
|
action.sa_mask = blocked_sigset;
|
|
|
|
action.sa_flags = 0;
|
|
|
|
action.sa_handler = do_sigchld;
|
|
|
|
sigaction( SIGCHLD, &action, NULL );
|
2003-10-24 06:29:01 +02:00
|
|
|
#ifdef SIG_PTHREAD_CANCEL
|
|
|
|
sigaction( SIG_PTHREAD_CANCEL, &action, NULL );
|
|
|
|
#endif
|
2003-03-26 02:32:18 +01:00
|
|
|
action.sa_handler = do_sighup;
|
|
|
|
sigaction( SIGHUP, &action, NULL );
|
|
|
|
action.sa_handler = do_sigint;
|
|
|
|
sigaction( SIGINT, &action, NULL );
|
2005-08-19 16:01:43 +02:00
|
|
|
action.sa_handler = do_sigalrm;
|
|
|
|
sigaction( SIGALRM, &action, NULL );
|
2003-03-26 02:32:18 +01:00
|
|
|
action.sa_handler = do_sigterm;
|
|
|
|
sigaction( SIGQUIT, &action, NULL );
|
|
|
|
sigaction( SIGTERM, &action, NULL );
|
2006-02-28 17:07:04 +01:00
|
|
|
if (core_dump_disabled())
|
|
|
|
{
|
|
|
|
action.sa_handler = do_sigsegv;
|
|
|
|
sigaction( SIGSEGV, &action, NULL );
|
|
|
|
}
|
2004-05-01 04:50:06 +02:00
|
|
|
action.sa_handler = SIG_IGN;
|
|
|
|
sigaction( SIGXFSZ, &action, NULL );
|
2003-04-13 03:06:14 +02:00
|
|
|
#ifdef HAVE_SIGINFO_T_SI_FD
|
2003-03-26 02:32:18 +01:00
|
|
|
action.sa_sigaction = do_sigio;
|
|
|
|
action.sa_flags = SA_SIGINFO;
|
|
|
|
sigaction( SIGIO, &action, NULL );
|
2003-04-13 03:06:14 +02:00
|
|
|
#endif
|
2003-03-26 02:32:18 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
fprintf( stderr, "failed to initialize signal handlers\n" );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_signals(void)
|
|
|
|
{
|
|
|
|
sigprocmask( SIG_BLOCK, &blocked_sigset, NULL );
|
|
|
|
release_object( handler_sighup );
|
|
|
|
release_object( handler_sigterm );
|
|
|
|
release_object( handler_sigint );
|
|
|
|
release_object( handler_sigchld );
|
|
|
|
release_object( handler_sigio );
|
|
|
|
}
|