2004-07-16 19:56:36 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
Copyright 2004-2008 Lennart Poettering
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2009-03-03 20:23:02 +00:00
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
2004-07-16 19:56:36 +00:00
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2014-11-26 14:14:51 +01:00
|
|
|
along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
|
2004-07-16 19:56:36 +00:00
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
2006-01-10 17:51:06 +00:00
|
|
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#endif
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/core-error.h>
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/core-util.h>
|
2011-08-10 10:30:15 +02:00
|
|
|
#include <pulsecore/i18n.h>
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/log.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-02-17 12:10:58 +00:00
|
|
|
#include "mainloop-signal.h"
|
|
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
struct pa_signal_event {
|
2004-06-23 23:17:30 +00:00
|
|
|
int sig;
|
2006-01-10 17:51:06 +00:00
|
|
|
#ifdef HAVE_SIGACTION
|
2004-06-23 23:17:30 +00:00
|
|
|
struct sigaction saved_sigaction;
|
2006-01-10 17:51:06 +00:00
|
|
|
#else
|
|
|
|
|
void (*saved_handler)(int sig);
|
|
|
|
|
#endif
|
2004-06-23 23:17:30 +00:00
|
|
|
void *userdata;
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_signal_cb_t callback;
|
|
|
|
|
pa_signal_destroy_cb_t destroy_callback;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_signal_event *previous, *next;
|
2004-06-23 23:17:30 +00:00
|
|
|
};
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static pa_mainloop_api *api = NULL;
|
2004-06-23 23:17:30 +00:00
|
|
|
static int signal_pipe[2] = { -1, -1 };
|
2006-01-11 01:17:39 +00:00
|
|
|
static pa_io_event* io_event = NULL;
|
|
|
|
|
static pa_signal_event *signals = NULL;
|
2004-06-23 23:17:30 +00:00
|
|
|
|
|
|
|
|
static void signal_handler(int sig) {
|
2007-10-30 18:35:08 +00:00
|
|
|
int saved_errno;
|
|
|
|
|
|
|
|
|
|
saved_errno = errno;
|
|
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
#ifndef HAVE_SIGACTION
|
|
|
|
|
signal(sig, signal_handler);
|
|
|
|
|
#endif
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2012-03-30 11:04:33 +03:00
|
|
|
/* XXX: If writing fails, there's nothing we can do? */
|
|
|
|
|
(void) pa_write(signal_pipe[1], &sig, sizeof(sig), NULL);
|
2007-10-30 18:35:08 +00:00
|
|
|
|
|
|
|
|
errno = saved_errno;
|
2006-01-10 17:51:06 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void dispatch(pa_mainloop_api*a, int sig) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_signal_event *s;
|
2006-01-10 17:51:06 +00:00
|
|
|
|
2007-01-04 13:43:45 +00:00
|
|
|
for (s = signals; s; s = s->next)
|
2006-01-10 17:51:06 +00:00
|
|
|
if (s->sig == sig) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(s->callback);
|
2006-01-10 17:51:06 +00:00
|
|
|
s->callback(a, s, sig, s->userdata);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-09 16:20:29 +02:00
|
|
|
static void callback(pa_mainloop_api*a, pa_io_event*e, int fd, pa_io_event_flags_t f, void *userdata) {
|
2004-09-03 20:14:23 +00:00
|
|
|
ssize_t r;
|
|
|
|
|
int sig;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(e);
|
|
|
|
|
pa_assert(f == PA_IO_EVENT_INPUT);
|
|
|
|
|
pa_assert(e == io_event);
|
|
|
|
|
pa_assert(fd == signal_pipe[0]);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-07-14 22:42:01 +00:00
|
|
|
if ((r = pa_read(signal_pipe[0], &sig, sizeof(sig), NULL)) < 0) {
|
2004-09-03 20:14:23 +00:00
|
|
|
if (errno == EAGAIN)
|
2004-06-23 23:17:30 +00:00
|
|
|
return;
|
|
|
|
|
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("read(): %s", pa_cstrerror(errno));
|
2004-09-03 20:14:23 +00:00
|
|
|
return;
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-09-03 20:14:23 +00:00
|
|
|
if (r != sizeof(sig)) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("short read()");
|
2004-09-03 20:14:23 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2006-01-10 17:51:06 +00:00
|
|
|
|
|
|
|
|
dispatch(a, sig);
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa_signal_init(pa_mainloop_api *a) {
|
2006-04-19 11:53:24 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(!api);
|
|
|
|
|
pa_assert(signal_pipe[0] == -1);
|
|
|
|
|
pa_assert(signal_pipe[1] == -1);
|
|
|
|
|
pa_assert(!io_event);
|
2006-01-10 17:51:06 +00:00
|
|
|
|
2009-10-30 03:32:38 +01:00
|
|
|
if (pa_pipe_cloexec(signal_pipe) < 0) {
|
2006-08-18 21:38:40 +00:00
|
|
|
pa_log("pipe(): %s", pa_cstrerror(errno));
|
2004-06-23 23:17:30 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_make_fd_nonblock(signal_pipe[0]);
|
|
|
|
|
pa_make_fd_nonblock(signal_pipe[1]);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
|
|
|
|
api = a;
|
2006-01-10 17:51:06 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert_se(io_event = api->io_new(api, signal_pipe[0], PA_IO_EVENT_INPUT, callback, NULL));
|
2006-01-10 17:51:06 +00:00
|
|
|
|
2004-06-23 23:17:30 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pa_signal_done(void) {
|
2004-08-05 19:53:57 +00:00
|
|
|
while (signals)
|
|
|
|
|
pa_signal_free(signals);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
if (io_event) {
|
|
|
|
|
pa_assert(api);
|
|
|
|
|
api->io_free(io_event);
|
|
|
|
|
io_event = NULL;
|
|
|
|
|
}
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_close_pipe(signal_pipe);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
|
|
|
|
api = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_signal_event* pa_signal_new(int sig, pa_signal_cb_t _callback, void *userdata) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_signal_event *e = NULL;
|
2006-01-10 17:51:06 +00:00
|
|
|
|
|
|
|
|
#ifdef HAVE_SIGACTION
|
2004-06-23 23:17:30 +00:00
|
|
|
struct sigaction sa;
|
2006-01-10 17:51:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(sig > 0);
|
|
|
|
|
pa_assert(_callback);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-06 18:54:13 +02:00
|
|
|
pa_init_i18n();
|
|
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
for (e = signals; e; e = e->next)
|
|
|
|
|
if (e->sig == sig)
|
2009-03-31 20:31:15 +02:00
|
|
|
return NULL;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
e = pa_xnew(pa_signal_event, 1);
|
2004-08-05 19:53:57 +00:00
|
|
|
e->sig = sig;
|
2006-01-11 01:17:39 +00:00
|
|
|
e->callback = _callback;
|
2004-08-05 19:53:57 +00:00
|
|
|
e->userdata = userdata;
|
|
|
|
|
e->destroy_callback = NULL;
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
#ifdef HAVE_SIGACTION
|
2004-06-23 23:17:30 +00:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
|
sa.sa_handler = signal_handler;
|
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
|
sa.sa_flags = SA_RESTART;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
if (sigaction(sig, &sa, &e->saved_sigaction) < 0)
|
2006-01-10 17:51:06 +00:00
|
|
|
#else
|
|
|
|
|
if ((e->saved_handler = signal(sig, signal_handler)) == SIG_ERR)
|
|
|
|
|
#endif
|
2004-06-23 23:17:30 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
e->previous = NULL;
|
|
|
|
|
e->next = signals;
|
|
|
|
|
signals = e;
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
return e;
|
2004-06-23 23:17:30 +00:00
|
|
|
fail:
|
2009-03-31 20:31:15 +02:00
|
|
|
pa_xfree(e);
|
2004-06-23 23:17:30 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_signal_free(pa_signal_event *e) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(e);
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
if (e->next)
|
|
|
|
|
e->next->previous = e->previous;
|
|
|
|
|
if (e->previous)
|
|
|
|
|
e->previous->next = e->next;
|
2004-06-23 23:17:30 +00:00
|
|
|
else
|
2004-08-05 19:53:57 +00:00
|
|
|
signals = e->next;
|
|
|
|
|
|
2006-01-10 17:51:06 +00:00
|
|
|
#ifdef HAVE_SIGACTION
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert_se(sigaction(e->sig, &e->saved_sigaction, NULL) == 0);
|
2006-01-10 17:51:06 +00:00
|
|
|
#else
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert_se(signal(e->sig, e->saved_handler) == signal_handler);
|
2006-01-10 17:51:06 +00:00
|
|
|
#endif
|
2004-08-05 19:53:57 +00:00
|
|
|
|
|
|
|
|
if (e->destroy_callback)
|
|
|
|
|
e->destroy_callback(api, e, e->userdata);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-08-05 19:53:57 +00:00
|
|
|
pa_xfree(e);
|
|
|
|
|
}
|
2004-06-23 23:17:30 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
void pa_signal_set_destroy(pa_signal_event *e, pa_signal_destroy_cb_t _callback) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(e);
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
e->destroy_callback = _callback;
|
2004-06-23 23:17:30 +00:00
|
|
|
}
|