mirror of
https://github.com/samba-team/samba.git
synced 2025-12-16 00:23:52 +03:00
r20930: use sigaction() instead of signal()
add support for sa_flags argument to event_add_signal(). These are
passed to sigaction(). Special handling is provided for SA_RESETHAND
(which tells the event system to remove the handler after the signal)
and SA_SIGINFO which allows the siginfo structure to be received per
signal
(This used to be commit 1bb10b6cf7)
This commit is contained in:
committed by
Gerald (Jerry) Carter
parent
c6174d5d6f
commit
cf8eef4ad8
@@ -241,14 +241,18 @@ struct timed_event *event_add_timed(struct event_context *ev, TALLOC_CTX *mem_ct
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
add a signal event
|
add a signal event
|
||||||
|
|
||||||
|
sa_flags are flags to sigaction(2)
|
||||||
|
|
||||||
return NULL on failure
|
return NULL on failure
|
||||||
*/
|
*/
|
||||||
struct signal_event *event_add_signal(struct event_context *ev, TALLOC_CTX *mem_ctx,
|
struct signal_event *event_add_signal(struct event_context *ev, TALLOC_CTX *mem_ctx,
|
||||||
int signum,
|
int signum,
|
||||||
|
int sa_flags,
|
||||||
event_signal_handler_t handler,
|
event_signal_handler_t handler,
|
||||||
void *private_data)
|
void *private_data)
|
||||||
{
|
{
|
||||||
return ev->ops->add_signal(ev, mem_ctx, signum, handler, private_data);
|
return ev->ops->add_signal(ev, mem_ctx, signum, sa_flags, handler, private_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ typedef void (*event_fd_handler_t)(struct event_context *, struct fd_event *,
|
|||||||
typedef void (*event_timed_handler_t)(struct event_context *, struct timed_event *,
|
typedef void (*event_timed_handler_t)(struct event_context *, struct timed_event *,
|
||||||
struct timeval , void *);
|
struct timeval , void *);
|
||||||
typedef void (*event_signal_handler_t)(struct event_context *, struct signal_event *,
|
typedef void (*event_signal_handler_t)(struct event_context *, struct signal_event *,
|
||||||
int , int, void *);
|
int , int, void *, void *);
|
||||||
typedef void (*event_aio_handler_t)(struct event_context *, struct aio_event *,
|
typedef void (*event_aio_handler_t)(struct event_context *, struct aio_event *,
|
||||||
int, void *);
|
int, void *);
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ struct timed_event *event_add_timed(struct event_context *ev, TALLOC_CTX *mem_ct
|
|||||||
void *private);
|
void *private);
|
||||||
|
|
||||||
struct signal_event *event_add_signal(struct event_context *ev, TALLOC_CTX *mem_ctx,
|
struct signal_event *event_add_signal(struct event_context *ev, TALLOC_CTX *mem_ctx,
|
||||||
int signum,
|
int signum, int sa_flags,
|
||||||
event_signal_handler_t handler,
|
event_signal_handler_t handler,
|
||||||
void *private);
|
void *private);
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ struct event_ops {
|
|||||||
/* signal functions */
|
/* signal functions */
|
||||||
struct signal_event *(*add_signal)(struct event_context *ev,
|
struct signal_event *(*add_signal)(struct event_context *ev,
|
||||||
TALLOC_CTX *mem_ctx,
|
TALLOC_CTX *mem_ctx,
|
||||||
int signum,
|
int signum, int sa_flags,
|
||||||
event_signal_handler_t handler,
|
event_signal_handler_t handler,
|
||||||
void *private_data);
|
void *private_data);
|
||||||
|
|
||||||
@@ -89,6 +89,7 @@ struct signal_event {
|
|||||||
event_signal_handler_t handler;
|
event_signal_handler_t handler;
|
||||||
void *private_data;
|
void *private_data;
|
||||||
int signum;
|
int signum;
|
||||||
|
int sa_flags;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* aio event is private to the aio backend */
|
/* aio event is private to the aio backend */
|
||||||
@@ -122,6 +123,7 @@ struct timeval common_event_loop_delay(struct event_context *);
|
|||||||
struct signal_event *common_event_add_signal(struct event_context *ev,
|
struct signal_event *common_event_add_signal(struct event_context *ev,
|
||||||
TALLOC_CTX *mem_ctx,
|
TALLOC_CTX *mem_ctx,
|
||||||
int signum,
|
int signum,
|
||||||
|
int sa_flags,
|
||||||
event_signal_handler_t handler,
|
event_signal_handler_t handler,
|
||||||
void *private_data);
|
void *private_data);
|
||||||
int common_event_check_signal(struct event_context *ev);
|
int common_event_check_signal(struct event_context *ev);
|
||||||
|
|||||||
@@ -29,14 +29,22 @@
|
|||||||
|
|
||||||
#define NUM_SIGNALS 64
|
#define NUM_SIGNALS 64
|
||||||
|
|
||||||
|
/* maximum number of SA_SIGINFO signals to hold in the queue */
|
||||||
|
#define SA_INFO_QUEUE_COUNT 10
|
||||||
|
|
||||||
/*
|
/*
|
||||||
the poor design of signals means that this table must be static global
|
the poor design of signals means that this table must be static global
|
||||||
*/
|
*/
|
||||||
static struct {
|
static struct {
|
||||||
struct signal_event *sig_handlers[NUM_SIGNALS];
|
struct signal_event *sig_handlers[NUM_SIGNALS];
|
||||||
|
struct sigaction oldact[NUM_SIGNALS];
|
||||||
uint32_t signal_count[NUM_SIGNALS];
|
uint32_t signal_count[NUM_SIGNALS];
|
||||||
uint32_t got_signal;
|
uint32_t got_signal;
|
||||||
int pipe_hack[2];
|
int pipe_hack[2];
|
||||||
|
#ifdef SA_SIGINFO
|
||||||
|
/* with SA_SIGINFO we get quite a lot of info per signal */
|
||||||
|
siginfo_t sig_info[NUM_SIGNALS][SA_INFO_QUEUE_COUNT];
|
||||||
|
#endif
|
||||||
} sig_state;
|
} sig_state;
|
||||||
|
|
||||||
|
|
||||||
@@ -52,6 +60,27 @@ static void signal_handler(int signum)
|
|||||||
write(sig_state.pipe_hack[1], &c, 1);
|
write(sig_state.pipe_hack[1], &c, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef SA_SIGINFO
|
||||||
|
/*
|
||||||
|
signal handler with SA_SIGINFO - redirects to registered signals
|
||||||
|
*/
|
||||||
|
static void signal_handler_info(int signum, siginfo_t *info, void *uctx)
|
||||||
|
{
|
||||||
|
sig_state.sig_info[signum][sig_state.signal_count[signum]] = *info;
|
||||||
|
|
||||||
|
signal_handler(signum);
|
||||||
|
|
||||||
|
/* handle SA_SIGINFO */
|
||||||
|
if (sig_state.signal_count[signum] == SA_INFO_QUEUE_COUNT) {
|
||||||
|
/* we've filled the info array - block this signal until
|
||||||
|
these ones are delivered */
|
||||||
|
sigset_t set;
|
||||||
|
sigemptyset(&set);
|
||||||
|
sigaddset(&set, signum);
|
||||||
|
sigprocmask(SIG_BLOCK, &set, NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
destroy a signal event
|
destroy a signal event
|
||||||
@@ -61,7 +90,8 @@ static int signal_event_destructor(struct signal_event *se)
|
|||||||
se->event_ctx->num_signal_handlers--;
|
se->event_ctx->num_signal_handlers--;
|
||||||
DLIST_REMOVE(sig_state.sig_handlers[se->signum], se);
|
DLIST_REMOVE(sig_state.sig_handlers[se->signum], se);
|
||||||
if (sig_state.sig_handlers[se->signum] == NULL) {
|
if (sig_state.sig_handlers[se->signum] == NULL) {
|
||||||
signal(se->signum, SIG_DFL);
|
/* restore old handler, if any */
|
||||||
|
sigaction(se->signum, &sig_state.oldact[se->signum], NULL);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -82,10 +112,11 @@ static void signal_pipe_handler(struct event_context *ev, struct fd_event *fde,
|
|||||||
return NULL on failure (memory allocation error)
|
return NULL on failure (memory allocation error)
|
||||||
*/
|
*/
|
||||||
struct signal_event *common_event_add_signal(struct event_context *ev,
|
struct signal_event *common_event_add_signal(struct event_context *ev,
|
||||||
TALLOC_CTX *mem_ctx,
|
TALLOC_CTX *mem_ctx,
|
||||||
int signum,
|
int signum,
|
||||||
event_signal_handler_t handler,
|
int sa_flags,
|
||||||
void *private_data)
|
event_signal_handler_t handler,
|
||||||
|
void *private_data)
|
||||||
{
|
{
|
||||||
struct signal_event *se;
|
struct signal_event *se;
|
||||||
|
|
||||||
@@ -100,15 +131,32 @@ struct signal_event *common_event_add_signal(struct event_context *ev,
|
|||||||
se->handler = handler;
|
se->handler = handler;
|
||||||
se->private_data = private_data;
|
se->private_data = private_data;
|
||||||
se->signum = signum;
|
se->signum = signum;
|
||||||
|
se->sa_flags = sa_flags;
|
||||||
|
|
||||||
|
/* only install a signal handler if not already installed */
|
||||||
if (sig_state.sig_handlers[signum] == NULL) {
|
if (sig_state.sig_handlers[signum] == NULL) {
|
||||||
signal(signum, signal_handler);
|
struct sigaction act;
|
||||||
|
ZERO_STRUCT(act);
|
||||||
|
act.sa_handler = signal_handler;
|
||||||
|
act.sa_flags = sa_flags;
|
||||||
|
#ifdef SA_SIGINFO
|
||||||
|
if (sa_flags & SA_SIGINFO) {
|
||||||
|
act.sa_handler = NULL;
|
||||||
|
act.sa_sigaction = signal_handler_info;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (sigaction(signum, &act, &sig_state.oldact[signum]) == -1) {
|
||||||
|
talloc_free(se);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DLIST_ADD(sig_state.sig_handlers[signum], se);
|
DLIST_ADD(sig_state.sig_handlers[signum], se);
|
||||||
|
|
||||||
talloc_set_destructor(se, signal_event_destructor);
|
talloc_set_destructor(se, signal_event_destructor);
|
||||||
|
|
||||||
|
/* we need to setup the pipe hack handler if not already
|
||||||
|
setup */
|
||||||
if (ev->pipe_fde == NULL) {
|
if (ev->pipe_fde == NULL) {
|
||||||
if (sig_state.pipe_hack[0] == 0 &&
|
if (sig_state.pipe_hack[0] == 0 &&
|
||||||
sig_state.pipe_hack[1] == 0) {
|
sig_state.pipe_hack[1] == 0) {
|
||||||
@@ -142,7 +190,29 @@ int common_event_check_signal(struct event_context *ev)
|
|||||||
struct signal_event *se, *next;
|
struct signal_event *se, *next;
|
||||||
for (se=sig_state.sig_handlers[i];se;se=next) {
|
for (se=sig_state.sig_handlers[i];se;se=next) {
|
||||||
next = se->next;
|
next = se->next;
|
||||||
se->handler(ev, se, i, count, se->private_data);
|
#ifdef SA_SIGINFO
|
||||||
|
if (se->sa_flags & SA_SIGINFO) {
|
||||||
|
int j;
|
||||||
|
for (j=0;j<count;j++) {
|
||||||
|
se->handler(ev, se, i, 1,
|
||||||
|
(void*)&sig_state.sig_info[i][j],
|
||||||
|
se->private_data);
|
||||||
|
}
|
||||||
|
if (count == SA_INFO_QUEUE_COUNT) {
|
||||||
|
/* we'd filled the queue, unblock the
|
||||||
|
signal now */
|
||||||
|
sigset_t set;
|
||||||
|
sigemptyset(&set);
|
||||||
|
sigaddset(&set, i);
|
||||||
|
sigprocmask(SIG_UNBLOCK, &set, NULL);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
se->handler(ev, se, i, count, NULL, se->private_data);
|
||||||
|
if (se->sa_flags & SA_RESETHAND) {
|
||||||
|
talloc_free(se);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
sig_state.signal_count[i] -= count;
|
sig_state.signal_count[i] -= count;
|
||||||
sig_state.got_signal -= count;
|
sig_state.got_signal -= count;
|
||||||
|
|||||||
@@ -40,4 +40,8 @@
|
|||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef SA_RESETHAND
|
||||||
|
#define SA_RESETHAND SA_ONESHOT
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -25,63 +25,50 @@
|
|||||||
#include "system/filesys.h"
|
#include "system/filesys.h"
|
||||||
#include "torture/torture.h"
|
#include "torture/torture.h"
|
||||||
|
|
||||||
static int write_fd, read_fd;
|
|
||||||
static struct fd_event *fde;
|
|
||||||
static int te_count;
|
|
||||||
static int fde_count;
|
static int fde_count;
|
||||||
static struct torture_context *test;
|
|
||||||
|
|
||||||
static void fde_handler(struct event_context *ev_ctx, struct fd_event *f,
|
static void fde_handler(struct event_context *ev_ctx, struct fd_event *f,
|
||||||
uint16_t flags, void *private)
|
uint16_t flags, void *private)
|
||||||
{
|
{
|
||||||
int *fd = private;
|
int *fd = private;
|
||||||
|
char c;
|
||||||
torture_comment(test, "event[%d] fd[%d] events[0x%08X]%s%s\n",
|
#ifdef SA_SIGINFO
|
||||||
fde_count, *fd, flags,
|
kill(getpid(), SIGUSR1);
|
||||||
(flags & EVENT_FD_READ)?" EVENT_FD_READ":"",
|
#endif
|
||||||
(flags & EVENT_FD_WRITE)?" EVENT_FD_WRITE":"");
|
kill(getpid(), SIGALRM);
|
||||||
|
read(fd[0], &c, 1);
|
||||||
if (fde_count > 5) {
|
write(fd[1], &c, 1);
|
||||||
torture_result(test, TORTURE_FAIL,
|
|
||||||
__location__": got more than fde 5 events - bug!");
|
|
||||||
talloc_free(fde);
|
|
||||||
fde = NULL;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
event_set_fd_flags(fde, 0);
|
|
||||||
fde_count++;
|
fde_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void timed_handler(struct event_context *ev_ctx, struct timed_event *te,
|
static void finished_handler(struct event_context *ev_ctx, struct timed_event *te,
|
||||||
struct timeval tval, void *private)
|
struct timeval tval, void *private)
|
||||||
{
|
{
|
||||||
torture_comment(test, "timed_handler called[%d]\n", te_count);
|
int *finished = private;
|
||||||
if (te_count > 2) {
|
(*finished) = 1;
|
||||||
close(write_fd);
|
|
||||||
write_fd = -1;
|
|
||||||
}
|
|
||||||
if (te_count > 5) {
|
|
||||||
torture_comment(test, "remove fd event!\n");
|
|
||||||
talloc_free(fde);
|
|
||||||
fde = NULL;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
te_count++;
|
|
||||||
event_add_timed(ev_ctx, ev_ctx, timeval_current_ofs(0,500), timed_handler, private);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool test_event_context(struct torture_context *torture_ctx,
|
static void count_handler(struct event_context *ev_ctx, struct signal_event *te,
|
||||||
const void *test_data)
|
int signum, int count, void *info, void *private)
|
||||||
|
{
|
||||||
|
int *countp = private;
|
||||||
|
(*countp) += count;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool test_event_context(struct torture_context *test,
|
||||||
|
const void *test_data)
|
||||||
{
|
{
|
||||||
struct event_context *ev_ctx;
|
struct event_context *ev_ctx;
|
||||||
int fd[2] = { -1, -1 };
|
int fd[2] = { -1, -1 };
|
||||||
const char *backend = (const char *)test_data;
|
const char *backend = (const char *)test_data;
|
||||||
TALLOC_CTX *mem_ctx = torture_ctx;
|
int alarm_count=0, info_count=0;
|
||||||
|
struct fd_event *fde;
|
||||||
|
struct signal_event *se1, *se2, *se3;
|
||||||
|
int finished=0;
|
||||||
|
struct timeval t;
|
||||||
|
char c = 0;
|
||||||
|
|
||||||
test = torture_ctx;
|
ev_ctx = event_context_init_byname(test, backend);
|
||||||
|
|
||||||
ev_ctx = event_context_init_byname(mem_ctx, backend);
|
|
||||||
if (ev_ctx == NULL) {
|
if (ev_ctx == NULL) {
|
||||||
torture_comment(test, "event backend '%s' not supported\n", backend);
|
torture_comment(test, "event backend '%s' not supported\n", backend);
|
||||||
return true;
|
return true;
|
||||||
@@ -90,29 +77,51 @@ static bool test_event_context(struct torture_context *torture_ctx,
|
|||||||
torture_comment(test, "Testing event backend '%s'\n", backend);
|
torture_comment(test, "Testing event backend '%s'\n", backend);
|
||||||
|
|
||||||
/* reset globals */
|
/* reset globals */
|
||||||
write_fd = -1;
|
|
||||||
read_fd = -1;
|
|
||||||
fde = NULL;
|
|
||||||
te_count = 0;
|
|
||||||
fde_count = 0;
|
fde_count = 0;
|
||||||
|
|
||||||
/* create a pipe */
|
/* create a pipe */
|
||||||
pipe(fd);
|
pipe(fd);
|
||||||
read_fd = fd[0];
|
|
||||||
write_fd = fd[1];
|
|
||||||
|
|
||||||
fde = event_add_fd(ev_ctx, ev_ctx, read_fd, EVENT_FD_READ,
|
fde = event_add_fd(ev_ctx, ev_ctx, fd[0], EVENT_FD_READ,
|
||||||
fde_handler, &read_fd);
|
fde_handler, fd);
|
||||||
|
|
||||||
event_add_timed(ev_ctx, ev_ctx, timeval_current_ofs(0,500),
|
event_add_timed(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
|
||||||
timed_handler, fde);
|
finished_handler, &finished);
|
||||||
|
|
||||||
event_loop_wait(ev_ctx);
|
se1 = event_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESTART, count_handler, &alarm_count);
|
||||||
|
se2 = event_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESETHAND, count_handler, &alarm_count);
|
||||||
|
#ifdef SA_SIGINFO
|
||||||
|
se3 = event_add_signal(ev_ctx, ev_ctx, SIGUSR1, SA_SIGINFO, count_handler, &info_count);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
write(fd[1], &c, 1);
|
||||||
|
|
||||||
|
t = timeval_current();
|
||||||
|
while (!finished) {
|
||||||
|
event_loop_once(ev_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
talloc_free(fde);
|
||||||
|
close(fd[0]);
|
||||||
|
close(fd[1]);
|
||||||
|
|
||||||
|
while (alarm_count < fde_count+1) {
|
||||||
|
event_loop_once(ev_ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
torture_comment(test, "Got %.2f pipe events/sec\n", fde_count/timeval_elapsed(&t));
|
||||||
|
|
||||||
|
talloc_free(se1);
|
||||||
|
|
||||||
|
torture_assert_int_equal(test, alarm_count, 1+fde_count, "alarm count mismatch");
|
||||||
|
|
||||||
|
#ifdef SA_SIGINFO
|
||||||
|
talloc_free(se3);
|
||||||
|
torture_assert_int_equal(test, info_count, fde_count, "info count mismatch");
|
||||||
|
#endif
|
||||||
|
|
||||||
close(read_fd);
|
|
||||||
close(write_fd);
|
|
||||||
|
|
||||||
talloc_free(ev_ctx);
|
talloc_free(ev_ctx);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user