diff --git a/src/daapd.h b/src/daapd.h index 72ba086d..118ea405 100644 --- a/src/daapd.h +++ b/src/daapd.h @@ -33,6 +33,7 @@ typedef struct tag_stats { typedef struct tag_config { int use_mdns; int stop; + int reload; char *configfile; char *web_root; int port; diff --git a/src/main.c b/src/main.c index 209ca5c2..66836976 100644 --- a/src/main.c +++ b/src/main.c @@ -432,6 +432,74 @@ int drop_privs(char *user) { return 0; } +/* + * signal_handler + * + * This thread merely spins waiting for signals + */ +void *signal_handler(void *arg) { + int error; + sigset_t intmask; + int sig; + + config.stop=0; + config.reload=0; + + DPRINTF(ERR_WARN,"Signal handler started\n"); + + while(!config.stop) { + if((sigemptyset(&intmask) == -1) || + (sigaddset(&intmask, SIGINT) == -1) || + (sigaddset(&intmask, SIGHUP) == -1) || + (sigwait(&intmask, &sig) == -1)) { + DPRINTF(ERR_FATAL,"Error waiting for signals. Aborting\n"); + } else { + /* process the signal */ + switch(sig) { + case SIGINT: + DPRINTF(ERR_LOG,"Got INT signal. Notifying daap server.\n"); + config.stop=1; + return NULL; + break; + case SIGHUP: + DPRINTF(ERR_LOG,"Got HUP signal. Notifying daap server.\n"); + config.reload=1; + break; + } + } + } + + return NULL; +} + +/* + * start_signal_handler + * + * Block signals and set up the signal handler + */ +int start_signal_handler(void) { + int error; + sigset_t set; + pthread_t handler_tid; + + if((sigemptyset(&set) == -1) || + (sigaddset(&set,SIGINT) == -1) || + (sigaddset(&set,SIGHUP) == -1) || + (sigprocmask(SIG_BLOCK, &set, NULL) == -1)) { + DPRINTF(ERR_LOG,"Error setting signal set\n"); + return -1; + } + + if(error=pthread_create(&handler_tid, NULL, signal_handler, NULL)) { + errno=error; + DPRINTF(ERR_LOG,"Error creating signal_handler thread\n"); + return -1; + } + + pthread_detach(handler_tid); + return 0; +} + int main(int argc, char *argv[]) { int option; char *configfile=DEFAULT_CONFIGFILE; @@ -491,9 +559,6 @@ int main(int argc, char *argv[]) { } } - /* block signals and set up the signal handling thread */ - - if((config.use_mdns) && (!parseonly)) { DPRINTF(ERR_LOG,"Starting rendezvous daemon\n"); @@ -502,7 +567,6 @@ int main(int argc, char *argv[]) { } } - if(db_open(config.dbdir)) { DPRINTF(ERR_FATAL,"Error in db_open: %s\n",strerror(errno)); } @@ -512,6 +576,12 @@ int main(int argc, char *argv[]) { DPRINTF(ERR_FATAL,"Error in drop_privs: %s\n",strerror(errno)); } + /* block signals and set up the signal handling thread */ + DPRINTF(ERR_LOG,"Starting signal handler\n"); + if(start_signal_handler()) { + DPRINTF(ERR_FATAL,"Error starting signal handler %s\n",strerror(errno)); + } + DPRINTF(ERR_LOG,"Loading playlists\n"); if(config.playlist) @@ -574,8 +644,13 @@ int main(int argc, char *argv[]) { config.stop=0; - while(!config.stop) + while(!config.stop) { + if(config.reload) { + config.reload=0; + DPRINTF(ERR_LOG,"Reloading configuration\n"); + } sleep(10); + } DPRINTF(ERR_LOG,"Stopping gracefully\n"); diff --git a/src/mp3-scanner.c b/src/mp3-scanner.c index d907a1b0..967dad8f 100644 --- a/src/mp3-scanner.c +++ b/src/mp3-scanner.c @@ -318,6 +318,11 @@ int scan_path(char *path) { } while(1) { + if(config.stop) { + DPRINTF(ERR_WARN,"Stop detected. Aborting scan of %s.\n",path); + return 0; + } + pde=(struct dirent *)&de; err=readdir_r(current_dir,(struct dirent *)de,&pde); diff --git a/src/rend-posix.c b/src/rend-posix.c index 864a5673..66c44c8b 100644 --- a/src/rend-posix.c +++ b/src/rend-posix.c @@ -89,6 +89,9 @@ Change History (most recent first): $Log$ + Revision 1.18 2004/04/19 06:19:46 rpedde + Starting to fix signal stuff + Revision 1.17 2004/03/29 19:44:58 rpedde Move mdns stuff out of mdns subdir to help compile on older automakes @@ -437,6 +440,7 @@ int rend_private_init(char *user) { signal(SIGINT, HandleSigInt); // SIGINT is what you get for a Ctrl-C signal(SIGQUIT, HandleSigQuit); // SIGQUIT is what you get for a Ctrl-\ (indeed) + signal(SIGHUP, SIG_IGN); // SIGHUP might happen from a request to reload the daap server while (!gStopNow) { int nfds = 1; diff --git a/src/rend-unix.c b/src/rend-unix.c index a3459b6f..2a71a3e2 100644 --- a/src/rend-unix.c +++ b/src/rend-unix.c @@ -98,6 +98,7 @@ int rend_running(void) { int result; DPRINTF(ERR_DEBUG,"Status inquiry\n"); + memset((void*)&msg,0x00,sizeof(msg)); msg.cmd=REND_MSG_TYPE_STATUS; result=rend_send_message(&msg); DPRINTF(ERR_DEBUG,"Returning status %d\n",result);