{
struct signal_task *st = &signal_task_struct;
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
- PARA_EMERG_LOG("failed to ignore SIGPIPE\n");
- exit(EXIT_FAILURE);
- }
+ para_sigaction(SIGPIPE, SIG_IGN);
st->fd = para_signal_init();
PARA_INFO_LOG("signal pipe: fd %d\n", st->fd);
para_install_sighandler(SIGINT);
struct private_alsa_write_data *pad = wn->private_data;
struct writer_node_group *wng = wn->wng;
size_t frames, bytes = *wng->loaded - wn->written;
- unsigned char *data = (unsigned char*)wng->buf + wn->written;
+ unsigned char *data = (unsigned char*)*wng->bufp + wn->written;
struct timeval tv;
snd_pcm_sframes_t ret;
para_install_sighandler(SIGINT);
para_install_sighandler(SIGTERM);
para_install_sighandler(SIGHUP);
- signal(SIGPIPE, SIG_IGN);
+ para_sigaction(SIGPIPE, SIG_IGN);
}
static void clear_slot(int slot_num)
PARA_INFO_LOG("opening %s filters\n", audio_formats[s->format]);
s->fc = para_calloc(sizeof(struct filter_chain));
s->fc->filter_nodes = para_malloc(nf * sizeof(struct filter_node));
- s->fc->inbuf = s->receiver_node->buf;
+ s->fc->inbufp = &s->receiver_node->buf;
s->fc->in_loaded = &s->receiver_node->loaded;
s->fc->input_error = &s->receiver_node->task.error;
s->fc->task.pre_select = filter_pre_select;
f->open(fn);
PARA_NOTICE_LOG("%s filter %d/%d (%s) started in slot %d\n",
audio_formats[s->format], i, nf, f->name, slot_num);
- s->fc->outbuf = fn->buf;
+ s->fc->outbufp = &fn->buf;
s->fc->out_loaded = &fn->loaded;
}
register_task(&s->fc->task);
else
s->wng = wng_new(a->num_writers);
if (s->fc) {
- s->wng->buf = s->fc->outbuf;
+ s->wng->bufp = s->fc->outbufp;
s->wng->loaded = s->fc->out_loaded;
s->wng->input_error = &s->fc->task.error;
s->wng->channels = &s->fc->channels;
s->fc->output_error = &s->wng->task.error;
PARA_INFO_LOG("samplerate: %d\n", *s->wng->samplerate);
} else {
- s->wng->buf = s->receiver_node->buf;
+ s->wng->bufp = &s->receiver_node->buf;
s->wng->loaded = &s->receiver_node->loaded;
s->wng->input_error = &s->receiver_node->task.error;
}
}
if (ct->status == CL_RECEIVING) {
stdout_set_defaults(&sot);
- sot.buf = ct->buf;
+ sot.bufp = &ct->buf;
sot.loaded = &ct->loaded;
sot.input_error = &ct->task.error;
register_task(&sot.task);
/** the client task structure */
struct task task;
/** the buffer used for handshake and receiving */
- char buf[CLIENT_BUFSIZE];
+ char *buf;
/** number of bytes loaded in \p buf */
size_t loaded;
/** non-zero if the pre_select hook added \p fd to the read fd set */
disable_crypt(ct->fd);
close(ct->fd);
}
+ free(ct->buf);
free(ct->user);
free(ct->config_file);
free(ct->key_file);
int ret;
struct client_task *ct = para_calloc(sizeof(struct client_task));
+ ct->buf = para_malloc(CLIENT_BUFSIZE);
*ct_ptr = ct;
ct->fd = -1;
ret = -E_CLIENT_SYNTAX;
#include "user_list.h"
#include "server_command_list.h"
#include "afs_command_list.h"
+#include "sched.h"
+#include "signal.h"
/** Commands including options must be shorter than this. */
#define MAX_COMMAND_LEN 32768
extern struct misc_meta_data *mmd;
extern struct sender senders[];
-static void dummy(int s)
+static void dummy(__a_unused int s)
{
- /*
- * At least on Solaris, SIGUSR1 is one-shot, i.e. the signal action is
- * restored to the default state once the signal handler has been
- * called.
- */
- if (s == SIGUSR1)
- signal(SIGUSR1, dummy);
}
static void mmd_dup(struct misc_meta_data *new_mmd)
char *status, *flags; /* vss status info */
char *ut = uptime_str();
long offset = (nmmd->offset + 500) / 1000;
- struct timeval now;
+ struct timeval current_time;
struct tm mtime_tm;
/* report real status */
localtime_r(&nmmd->mtime, &mtime_tm);
strftime(mtime, 29, "%b %d %Y", &mtime_tm);
}
- gettimeofday(&now, NULL);
+ gettimeofday(¤t_time, NULL);
ret = make_message(
"%s: %zu\n" /* file size */
"%s: %s\n" /* mtime */
(long unsigned)nmmd->stream_start.tv_sec,
(long unsigned)nmmd->stream_start.tv_usec,
status_item_list[SI_CURRENT_TIME],
- (long unsigned)now.tv_sec,
- (long unsigned)now.tv_usec,
+ (long unsigned)current_time.tv_sec,
+ (long unsigned)current_time.tv_usec,
nmmd->afd.verbose_ls_output
struct misc_meta_data tmp, *nmmd = &tmp;
char *s;
- signal(SIGUSR1, dummy);
+ para_sigaction(SIGUSR1, dummy);
if (argc > 1)
num = atoi(argv[1]);
}
+static void reset_signals(void)
+{
+ para_sigaction(SIGCHLD, SIG_IGN);
+ para_sigaction(SIGINT, SIG_DFL);
+ para_sigaction(SIGTERM, SIG_DFL);
+ para_sigaction(SIGHUP, SIG_DFL);
+}
+
/**
* Perform user authentication and execute a command.
*
char *p, *command = NULL;
size_t numbytes;
- signal(SIGCHLD, SIG_IGN);
- signal(SIGINT, SIG_DFL);
- signal(SIGTERM, SIG_DFL);
- signal(SIGHUP, SIG_DFL);
-
+ reset_signals();
/* we need a blocking fd here as recv() might return EAGAIN otherwise. */
ret = mark_fd_blocking(fd);
if (ret < 0)
#define HTTP_SEND_ERRORS
#define GGO_ERRORS
#define COLOR_ERRORS
+#define SIGNAL_ERRORS
+
extern const char **para_errlist[];
PARA_ERROR(FILTER_SYNTAX, "syntax error"), \
-#define SIGNAL_ERRORS \
- PARA_ERROR(SIGNAL_SIG_ERR, "signal() returned SIG_ERR"), \
-
-
#define STRING_ERRORS \
PARA_ERROR(ATOI_OVERFLOW, "value too large"), \
PARA_ERROR(STRTOLL, "unknown strtoll error"), \
*/
#define NUM_FEC_GROUPS 3
-/** Size of the output buffer of the fecdec filter. */
-#define FECDEC_OUTBUF_SIZE (1024 * 1024) /* FIXME: This has to depend on the fec params */
+/** Default size of the output buffer of the fecdec filter. */
+#define FECDEC_DEFAULT_OUTBUF_SIZE (3 * 1024)
+/** Maximal size of the output buffer of the fecdec filter. */
+#define FECDEC_MAX_OUTBUF_SIZE (1024 * 1024)
/** Data read from the header of a slice. */
struct fec_header {
static int decode_group(struct fecdec_group *fg, struct filter_node *fn)
{
int i, ret, sb = fg->h.slice_bytes;
- size_t written = 0;
+ size_t written = 0, need;
struct private_fecdec_data *pfd = fn->private_data;
enum fec_group_usability u = group_is_usable(fg, pfd);
PARA_DEBUG_LOG("writing group %d (%d/%d decoded data bytes)\n",
fg->h.group_num, fg->h.group_bytes,
fg->h.data_slices_per_group * sb);
+ need = fn->loaded + (fg->h.data_slices_per_group - i)* sb;
+ if (need > fn->bufsize) {
+ fn->bufsize = PARA_MAX(fn->bufsize * 2, need);
+ if (fn->bufsize > FECDEC_MAX_OUTBUF_SIZE)
+ return -E_FECDEC_OVERRUN;
+ PARA_INFO_LOG("increasing fec buf to %zu\n", fn->bufsize);
+ fn->buf = para_realloc(fn->buf, fn->bufsize);
+ }
for (; i < fg->h.data_slices_per_group; i++) {
size_t n = sb;
if (n + written > fg->h.group_bytes)
n = fg->h.group_bytes - written;
- if (fn->loaded + n > fn->bufsize)
- return -E_FECDEC_OVERRUN;
memcpy(fn->buf + fn->loaded, fg->data[i], n);
fn->loaded += n;
written += n;
static void fecdec_open(struct filter_node *fn)
{
- fn->bufsize = FECDEC_OUTBUF_SIZE;
+ fn->bufsize = FECDEC_DEFAULT_OUTBUF_SIZE;
fn->buf = para_malloc(fn->bufsize);
fn->private_data = para_calloc(sizeof(struct private_fecdec_data));
fn->loaded = 0;
if (!FD_ISSET(pfwd->fd, &s->wfds))
return 1;
// PARA_INFO_LOG("writing %zd\n", *wng->loaded);
- ret = write(pfwd->fd, wng->buf + wn->written,
+ ret = write(pfwd->fd, *wng->bufp + wn->written,
*wng->loaded - wn->written);
if (ret < 0)
return -E_FW_WRITE;
struct filter *f = filters + fn->filter_num;
f->open(fn);
PARA_INFO_LOG("opened %s filter\n", f->name);
- fc->outbuf = fn->buf;
+ fc->outbufp = &fn->buf;
fc->out_loaded = &fn->loaded;
}
}
return -E_NO_FILTERS;
fc->num_filters = conf.filter_given;
fc->filter_nodes = para_malloc(fc->num_filters * sizeof(struct filter_node));
- fc->inbuf = sit->buf;
+ fc->inbufp = &sit->buf;
fc->in_loaded = &sit->loaded;
fc->input_error = &sit->task.error;
fc->task.error = 0;
sit->output_error = &fc->task.error;
stdout_set_defaults(sot);
- sot->buf = fc->outbuf;
+ sot->bufp = fc->outbufp;
sot->loaded = fc->out_loaded;
sot->input_error = &fc->task.error;
* buffer used to read from stdin for para_filter; the output buffer of the
* current receiver for para_audiod).
*/
- char *inbuf;
+ char **inbufp;
/**
* The output buffer of the filter chain.
*
* Points to the output buffer of the last filter in the filter chain.
*/
- char *outbuf;
+ char **outbufp;
/** Contains the number of bytes loaded in the input buffer. */
size_t *in_loaded;
/** Contains the number of bytes loaded in the output buffer. */
return;
}
again:
- ib = fc->inbuf;
+ ib = *fc->inbufp;
loaded = fc->in_loaded;
conv = 0;
FOR_EACH_FILTER_NODE(fn, fc, i) {
para_install_sighandler(SIGCHLD);
para_install_sighandler(SIGWINCH);
para_install_sighandler(SIGUSR1);
-// signal(SIGPIPE, SIG_IGN);
- signal(SIGHUP, SIG_IGN);
+ para_sigaction(SIGHUP, SIG_IGN);
}
__noreturn static void do_exit(int ret)
{
- signal(SIGTERM, SIG_IGN);
+ para_sigaction(SIGTERM, SIG_IGN);
kill(0, SIGTERM);
exit(ret);
}
r_opened = 1;
stdout_set_defaults(&sot);
- sot.buf = rn.buf;
+ sot.bufp = &rn.buf;
sot.loaded = &rn.loaded;
sot.input_error = &rn.task.error;
register_task(&sot.task);
st->task.post_select = signal_post_select;
sprintf(st->task.status, "signal task");
+ PARA_NOTICE_LOG("setting up signal handling\n");
st->fd = para_signal_init(); /* always successful */
-
- PARA_NOTICE_LOG("setting up signal handlers\n");
- if (para_install_sighandler(SIGINT) < 0)
- goto err;
- if (para_install_sighandler(SIGTERM) < 0)
- goto err;
- if (para_install_sighandler(SIGHUP) < 0)
- goto err;
- if (para_install_sighandler(SIGCHLD) < 0)
- goto err;
- if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- goto err;
+ para_install_sighandler(SIGINT);
+ para_install_sighandler(SIGTERM);
+ para_install_sighandler(SIGHUP);
+ para_install_sighandler(SIGCHLD);
+ para_sigaction(SIGPIPE, SIG_IGN);
add_close_on_fork_list(st->fd);
register_task(&st->task);
- return;
-err:
- PARA_EMERG_LOG("could not install signal handlers\n");
- exit(EXIT_FAILURE);
}
static void command_pre_select(struct sched *s, struct task *t)
* before the child gets a chance to ignore this signal -- only the
* good die young.
*/
- if (signal(SIGUSR1, SIG_IGN) == SIG_ERR) {
- PARA_EMERG_LOG("failed to ignore SIGUSR1\n");
- exit(EXIT_FAILURE);
- }
+ para_sigaction(SIGUSR1, SIG_IGN);
/*
* We have to install a SIGCHLD handler before the afs process is being
* forked off. Otherwise, para_server does not notice if afs dies before
* the SIGCHLD handler has been installed by init_signal_task() below.
*/
- if (signal(SIGCHLD, tmp_sigchld_handler) == SIG_ERR) {
- PARA_EMERG_LOG("failed to install temporary SIGCHLD handler\n");
- exit(EXIT_FAILURE);
- }
+ para_sigaction(SIGCHLD, tmp_sigchld_handler);
PARA_NOTICE_LOG("initializing the audio file selector\n");
afs_socket = init_afs();
init_signal_task();
}
/**
- * Wrapper around signal(2).
+ * Install the given handler for the given signal.
*
* \param sig The number of the signal to catch.
+ * \param handler to be installed, \p SIG_IGN, or \p SIG_DFL.
*
- * This installs the generic signal handler for the given signal.
+ * This either succeeds or calls exit(EXIT_FAILURE).
*
- * \return This function returns 1 on success and \p -E_SIGNAL_SIG_ERR on errors.
- *
- * \sa signal(2).
+ * \sa sigaction(2).
*/
-int para_install_sighandler(int sig)
+void para_sigaction(int sig, void (*handler)(int))
{
+ struct sigaction act;
+
PARA_DEBUG_LOG("catching signal %d\n", sig);
- return signal(sig, &generic_signal_handler) == SIG_ERR? -E_SIGNAL_SIG_ERR : 1;
+ act.sa_handler = handler;
+ sigemptyset(&act.sa_mask);
+ act.sa_flags = 0;
+ if (sig == SIGALRM) {
+ #ifdef SA_INTERRUPT /* SunOS */
+ act.sa_flags |= SA_INTERRUPT;
+ #endif
+ } else {
+ #ifdef SA_RESTART /* BSD */
+ act.sa_flags |= SA_RESTART;
+ #endif
+ }
+ if (sigaction(sig, &act, NULL) >= 0)
+ return;
+ PARA_EMERG_LOG("failed to install signal handler for signal %d\n",
+ sig);
+ exit(EXIT_FAILURE);
+}
+
+/**
+ * Install the generic signal handler for the given signal number.
+ *
+ * \param sig The number of the signal to catch.
+ *
+ * \sa signal(2), sigaction(2).
+ */
+void para_install_sighandler(int sig)
+{
+ para_sigaction(sig, &generic_signal_handler);
}
/**
};
int para_signal_init(void);
-int para_install_sighandler(int);
+void para_sigaction(int sig, void (*handler)(int));
+void para_install_sighandler(int);
void para_reap_children(void);
int para_reap_child(pid_t *pid);
int para_next_signal(void);
}
if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
return;
- ret = write(STDOUT_FILENO, sot->buf, *sot->loaded);
+ ret = write(STDOUT_FILENO, *sot->bufp, *sot->loaded);
if (ret < 0) {
t->error = -ERRNO_TO_PARA_ERROR(errno);
return;
}
*sot->loaded -= ret;
if (*sot->loaded)
- memmove(sot->buf, sot->buf + ret, *sot->loaded);
+ memmove(*sot->bufp, *sot->bufp + ret, *sot->loaded);
}
/**
* The task structure used for writing to stdout.
*/
struct stdout_task {
- /** Pointer to the data buffer. */
- char *buf;
+ /** Pointer to the data buffer pointer. */
+ char **bufp;
/** Number of bytes loaded in \a buf. */
size_t *loaded;
/** Pointer to the error variable of the feeding task. */
sit.bufsize = conf.bufsize_arg * 1024;
sit.buf = para_malloc(sit.bufsize);
- wng->buf = sit.buf;
+ wng->bufp = &sit.buf;
wng->loaded = &sit.loaded;
wng->input_error = &sit.task.error;
/** Non-zero if an error or end of file was encountered by the feeding task. */
int *input_error;
/** Current output buffer. */
- char *buf;
+ char **bufp;
/** Number of bytes loaded in the output buffer. */
size_t *loaded;
/** Number of audio channels of the current stream. */
}
if (*g->loaded && min_written) {
// PARA_INFO_LOG("moving %zd bytes\n", *g->loaded);
- memmove(g->buf, g->buf + min_written, *g->loaded);
+ memmove(*g->bufp, *g->bufp + min_written, *g->loaded);
}
}