X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=blobdiff_plain;f=server.c;h=8e8cb5fdcdd31e012137a83a4661e2728ac345e4;hp=71aeaf1ce725209c4a39f0506d1d41169999c0e4;hb=4744d937c4160898d1fe151257606430750e580c;hpb=a5927501e41fa3fca2975452617474e78ffecc48 diff --git a/server.c b/server.c index 71aeaf1c..8e8cb5fd 100644 --- a/server.c +++ b/server.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1997-2009 Andre Noll + * Copyright (C) 1997-2012 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ @@ -17,15 +17,17 @@ * \ref audioc.c, \ref afh.c * - Server: \ref server_command, \ref sender, * - Audio file selector: \ref audio_format_handler, \ref afs_table, - * - Client: \ref receiver, \ref receiver_node, \ref filter, \ref filter_node. + * - Client: \ref receiver, \ref receiver_node, \ref filter, + * \ref filter_node, \ref writer_node. * * * The gory details, listed by topic: * * - Audio format handlers: \ref send_common.c \ref mp3_afh.c, - * \ref ogg_afh.c, \ref aac_afh.c, \ref wma_afh.c, + * \ref ogg_afh.c, \ref aac_afh.c, \ref wma_afh.c, \ref spx_afh.c * - Decoders: \ref mp3dec_filter.c, \ref oggdec_filter.c, - * \ref aacdec_filter.c, \ref wmadec_filter.c, + * \ref aacdec_filter.c, \ref wmadec_filter.c, spxdec_filter.c, + * \ref flacdec_filter.c, * - Volume normalizer: \ref compress_filter.c, * - Output: \ref alsa_write.c, \ref osx_write.c, \ref oss_write.c, * - http: \ref http_recv.c, \ref http_send.c, @@ -50,22 +52,22 @@ * - Spawning processes: \ref exec.c, * - Inter process communication: \ref ipc.c, * - Blob tables: \ref blob.c, - * - The error subssystem: \ref error.h. + * - The error subsystem: \ref error.h. * - Access control for paraslash senders: \ref acl.c, \ref acl.h. + * - Internal crypto API: \ref crypt.h. + * - interactive sessions (libreadline): \ref interactive.c. * * Low-level data structures: * * - Doubly linked lists: \ref list.h, * - Ring buffer: \ref ringbuffer.c, \ref ringbuffer.h, - * - Hashing: \ref hash.h, \ref sha1.h, \ref sha1.c, - * - Crypto: \ref crypt.c. + * - openssl: \ref crypt.c + * - libgcrypt: \ref gcrypt.c * - Forward error correction: \ref fec.c. */ #include -#include #include -#include #include #include @@ -77,19 +79,22 @@ #include "string.h" #include "afs.h" #include "server.h" +#include "list.h" +#include "send.h" +#include "sched.h" #include "vss.h" #include "config.h" #include "close_on_fork.h" -#include "list.h" -#include "send.h" #include "net.h" #include "daemon.h" #include "ipc.h" #include "fd.h" -#include "sched.h" #include "signal.h" #include "user_list.h" #include "color.h" +#include "version.h" + +__printf_2_3 void (*para_log)(int, const char*, ...) = daemon_log; /** Define the array of error lists needed by para_server. */ INIT_SERVER_ERRLISTS; @@ -120,6 +125,7 @@ int mmd_mutex; /** The file containing user information (public key, permissions). */ static char *user_list_file = NULL; +static struct sched sched; /** The task responsible for server command handling. */ struct server_command_task { @@ -146,17 +152,14 @@ static int want_colors(void) static void init_colors_or_die(void) { - int ret, i; + int i; if (!want_colors()) return; daemon_set_flag(DF_COLOR_LOG); daemon_set_default_log_colors(); - for (i = 0; i < conf.log_color_given; i++) { - ret = daemon_set_log_color(conf.log_color_arg[i]); - if (ret < 0) - exit(EXIT_FAILURE); - } + for (i = 0; i < conf.log_color_given; i++) + daemon_set_log_color_or_die(conf.log_color_arg[i]); } /* @@ -276,15 +279,13 @@ static void handle_sighup(void) kill(mmd->afs_pid, SIGHUP); } -static void signal_post_select(struct sched *s, struct task *t) +static void signal_post_select(struct sched *s, __a_unused struct task *t) { - struct signal_task *st = container_of(t, struct signal_task, task); + int signum = para_next_signal(&s->rfds); - if (!FD_ISSET(st->fd, &s->rfds)) + switch (signum) { + case 0: return; - - st->signum = para_next_signal(); - switch (st->signum) { case SIGHUP: handle_sighup(); break; @@ -304,7 +305,7 @@ static void signal_post_select(struct sched *s, struct task *t) /* die on sigint/sigterm. Kill all children too. */ case SIGINT: case SIGTERM: - PARA_EMERG_LOG("terminating on signal %d\n", st->signum); + PARA_EMERG_LOG("terminating on signal %d\n", signum); kill(0, SIGTERM); /* * We must wait for afs because afs catches SIGINT/SIGTERM. @@ -348,7 +349,7 @@ static void init_signal_task(void) para_install_sighandler(SIGCHLD); para_sigaction(SIGPIPE, SIG_IGN); add_close_on_fork_list(st->fd); - register_task(&st->task); + register_task(&sched, &st->task); } static void command_pre_select(struct sched *s, struct task *t) @@ -366,12 +367,9 @@ static void command_post_select(struct sched *s, struct task *t) pid_t child_pid; uint32_t *chunk_table; - if (!FD_ISSET(sct->listen_fd, &s->rfds)) - return; - ret = para_accept(sct->listen_fd, NULL, 0); - if (ret < 0) + ret = para_accept(sct->listen_fd, &s->rfds, NULL, 0, &new_fd); + if (ret <= 0) goto out; - new_fd = ret; peer_name = remote_name(new_fd); PARA_INFO_LOG("got connection from %s, forking\n", peer_name); mmd->num_connects++; @@ -433,14 +431,15 @@ static void init_server_command_task(int argc, char **argv) if (ret < 0) goto err; add_close_on_fork_list(sct->listen_fd); /* child doesn't need the listener */ - register_task(&sct->task); + sprintf(sct->task.status, "server command task"); + register_task(&sched, &sct->task); return; err: PARA_EMERG_LOG("%s\n", para_strerror(-ret)); exit(EXIT_FAILURE); } -static int init_afs(void) +static int init_afs(int argc, char **argv) { int ret, afs_server_socket[2]; pid_t afs_pid; @@ -454,6 +453,10 @@ static int init_afs(void) if (afs_pid < 0) exit(EXIT_FAILURE); if (afs_pid == 0) { /* child (afs) */ + int i; + for (i = argc - 1; i >= 0; i--) + memset(argv[i], 0, strlen(argv[i])); + sprintf(argv[0], "para_server (afs)"); close(afs_server_socket[0]); afs_init(afs_socket_cookie, afs_server_socket[1]); } @@ -468,12 +471,6 @@ static int init_afs(void) return afs_server_socket[0]; } -__noreturn static void tmp_sigchld_handler(__a_unused int s) -{ - PARA_EMERG_LOG("caught early SIGCHLD\n"); - exit(EXIT_FAILURE); -} - static void server_init(int argc, char **argv) { struct server_cmdline_parser_params params = { @@ -497,11 +494,11 @@ static void server_init(int argc, char **argv) init_ipc_or_die(); /* init mmd struct and mmd->lock */ /* make sure, the global now pointer is uptodate */ gettimeofday(now, NULL); - server_uptime(UPTIME_SET); /* reset server uptime */ + set_server_start_time(now); init_user_list(user_list_file); /* become daemon */ if (conf.daemon_given) - daemonize(); + daemonize(true /* parent waits for SIGTERM */); PARA_NOTICE_LOG("initializing audio format handlers\n"); afh_init(); @@ -514,24 +511,28 @@ static void server_init(int argc, char **argv) */ 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. + * We have to block SIGCHLD before the afs process is being forked off. + * Otherwise, para_server does not notice if afs dies before the + * SIGCHLD handler has been installed for the parent process by + * init_signal_task() below. */ - para_sigaction(SIGCHLD, tmp_sigchld_handler); + para_block_signal(SIGCHLD); PARA_NOTICE_LOG("initializing the audio file selector\n"); - afs_socket = init_afs(); + afs_socket = init_afs(argc, argv); init_signal_task(); + para_unblock_signal(SIGCHLD); PARA_NOTICE_LOG("initializing virtual streaming system\n"); - init_vss_task(afs_socket); + init_vss_task(afs_socket, &sched); init_server_command_task(argc, argv); + if (conf.daemon_given) + kill(getppid(), SIGTERM); PARA_NOTICE_LOG("server init complete\n"); } static void status_refresh(void) { static int prev_uptime = -1, prev_events = -1; - int uptime = server_uptime(UPTIME_GET), ret = 1; + int uptime = get_server_uptime(now); if (prev_events != mmd->events) goto out; @@ -546,11 +547,8 @@ out: prev_uptime = uptime; prev_events = mmd->events; mmd->vss_status_flags = mmd->new_vss_status_flags; - if (ret) { - PARA_DEBUG_LOG("%d events, forcing status update\n", - mmd->events); - killpg(0, SIGUSR1); - } + PARA_DEBUG_LOG("%d events, forcing status update\n", mmd->events); + killpg(0, SIGUSR1); } static int server_select(int max_fileno, fd_set *readfds, fd_set *writefds, @@ -576,16 +574,13 @@ static int server_select(int max_fileno, fd_set *readfds, fd_set *writefds, int main(int argc, char *argv[]) { int ret; - static struct sched s = { - .default_timeout = { - .tv_sec = 1, - .tv_usec = 0 - }, - .select_function = server_select - }; + + sched.default_timeout.tv_sec = 1; + sched.select_function = server_select; + server_init(argc, argv); mutex_lock(mmd_mutex); - ret = schedule(&s); + ret = schedule(&sched); if (ret < 0) { PARA_EMERG_LOG("%s\n", para_strerror(-ret)); exit(EXIT_FAILURE);