eloop: remove old signal sources
Remove the old non-shared signal sources in favor of shared-signals. Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
This commit is contained in:
parent
524cfb0093
commit
21f6bbb15d
142
src/eloop.c
142
src/eloop.c
@ -91,14 +91,6 @@ struct ev_fd {
|
||||
int fd;
|
||||
};
|
||||
|
||||
struct ev_signal {
|
||||
unsigned long ref;
|
||||
|
||||
struct ev_fd *fd;
|
||||
ev_signal_cb cb;
|
||||
void *data;
|
||||
};
|
||||
|
||||
struct ev_timer {
|
||||
unsigned long ref;
|
||||
|
||||
@ -433,140 +425,6 @@ int ev_eloop_update_fd(struct ev_fd *fd, int mask)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ev_signal_new(struct ev_signal **out)
|
||||
{
|
||||
struct ev_signal *sig;
|
||||
int ret;
|
||||
|
||||
if (!out)
|
||||
return -EINVAL;
|
||||
|
||||
sig = malloc(sizeof(*sig));
|
||||
if (!sig)
|
||||
return -ENOMEM;
|
||||
|
||||
memset(sig, 0, sizeof(*sig));
|
||||
sig->ref = 1;
|
||||
|
||||
ret = ev_fd_new(&sig->fd);
|
||||
if (ret) {
|
||||
free(sig);
|
||||
return ret;
|
||||
}
|
||||
|
||||
*out = sig;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ev_signal_ref(struct ev_signal *sig)
|
||||
{
|
||||
if (!sig)
|
||||
return;
|
||||
|
||||
++sig->ref;
|
||||
}
|
||||
|
||||
void ev_signal_unref(struct ev_signal *sig)
|
||||
{
|
||||
if (!sig || !sig->ref || --sig->ref)
|
||||
return;
|
||||
|
||||
ev_fd_unref(sig->fd);
|
||||
free(sig);
|
||||
}
|
||||
|
||||
int ev_eloop_new_signal(struct ev_eloop *loop, struct ev_signal **out,
|
||||
int signum, ev_signal_cb cb, void *data)
|
||||
{
|
||||
struct ev_signal *sig;
|
||||
int ret;
|
||||
|
||||
if (!out || !loop || !cb || signum < 0)
|
||||
return -EINVAL;
|
||||
|
||||
ret = ev_signal_new(&sig);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
ret = ev_eloop_add_signal(loop, sig, signum, cb, data);
|
||||
if (ret) {
|
||||
ev_signal_unref(sig);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ev_signal_unref(sig);
|
||||
*out = sig;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void signal_cb(struct ev_fd *fd, int mask, void *data)
|
||||
{
|
||||
struct ev_signal *sig = data;
|
||||
struct signalfd_siginfo signal_info;
|
||||
int len;
|
||||
|
||||
if (mask & EV_READABLE) {
|
||||
len = read(fd->fd, &signal_info, sizeof(signal_info));
|
||||
if (len != sizeof(signal_info))
|
||||
log_warn("cannot read signalfd");
|
||||
else
|
||||
sig->cb(sig, signal_info.ssi_signo, sig->data);
|
||||
} else if (mask & (EV_HUP | EV_ERR)) {
|
||||
log_warn("HUP/ERR on signal source");
|
||||
}
|
||||
}
|
||||
|
||||
int ev_eloop_add_signal(struct ev_eloop *loop, struct ev_signal *sig,
|
||||
int signum, ev_signal_cb cb, void *data)
|
||||
{
|
||||
sigset_t mask;
|
||||
int ret, fd;
|
||||
|
||||
if (!loop || !sig || signum < 0 || !cb)
|
||||
return -EINVAL;
|
||||
|
||||
if (sig->fd->loop)
|
||||
return -EALREADY;
|
||||
|
||||
sigemptyset(&mask);
|
||||
sigaddset(&mask, signum);
|
||||
|
||||
fd = signalfd(-1, &mask, SFD_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return -errno;
|
||||
|
||||
ret = ev_eloop_add_fd(loop, sig->fd, fd, EV_READABLE, signal_cb, sig);
|
||||
if (ret) {
|
||||
close(fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
pthread_sigmask(SIG_BLOCK, &mask, NULL);
|
||||
sig->cb = cb;
|
||||
sig->data = data;
|
||||
ev_signal_ref(sig);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ev_eloop_rm_signal(struct ev_signal *sig)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (!sig || !sig->fd->loop)
|
||||
return;
|
||||
|
||||
fd = sig->fd->fd;
|
||||
ev_eloop_rm_fd(sig->fd);
|
||||
close(fd);
|
||||
ev_signal_unref(sig);
|
||||
|
||||
/*
|
||||
* We cannot unblock the signal here because we do not know whether some
|
||||
* other subsystem also added the signal to the sigprocmask.
|
||||
*/
|
||||
}
|
||||
|
||||
static void shared_signal_cb(struct ev_fd *fd, int mask, void *data)
|
||||
{
|
||||
struct ev_signal_shared *sig = data;
|
||||
|
13
src/eloop.h
13
src/eloop.h
@ -42,13 +42,10 @@
|
||||
struct ev_eloop;
|
||||
struct ev_idle;
|
||||
struct ev_fd;
|
||||
struct ev_signal;
|
||||
struct ev_timer;
|
||||
|
||||
typedef void (*ev_idle_cb) (struct ev_idle *idle, void *data);
|
||||
typedef void (*ev_fd_cb) (struct ev_fd *fd, int mask, void *data);
|
||||
typedef void (*ev_signal_cb)
|
||||
(struct ev_signal *sig, int signum, void *data);
|
||||
typedef void (*ev_signal_shared_cb)
|
||||
(struct ev_eloop *eloop, struct signalfd_siginfo *info, void *data);
|
||||
typedef void (*ev_timer_cb)
|
||||
@ -102,16 +99,6 @@ int ev_eloop_update_fd(struct ev_fd *fd, int mask);
|
||||
|
||||
/* signal sources */
|
||||
|
||||
int ev_signal_new(struct ev_signal **out);
|
||||
void ev_signal_ref(struct ev_signal *sig);
|
||||
void ev_signal_unref(struct ev_signal *sig);
|
||||
|
||||
int ev_eloop_new_signal(struct ev_eloop *loop, struct ev_signal **out,
|
||||
int signum, ev_signal_cb cb, void *data);
|
||||
int ev_eloop_add_signal(struct ev_eloop *loop, struct ev_signal *sig,
|
||||
int signum, ev_signal_cb cb, void *data);
|
||||
void ev_eloop_rm_signal(struct ev_signal *sig);
|
||||
|
||||
int ev_eloop_register_signal_cb(struct ev_eloop *loop, int signum,
|
||||
ev_signal_shared_cb cb, void *data);
|
||||
void ev_eloop_unregister_signal_cb(struct ev_eloop *loop, int signum,
|
||||
|
Loading…
x
Reference in New Issue
Block a user