From e9e305586e71127f06f798be8a95dcd975abccda Mon Sep 17 00:00:00 2001 From: Ron Pedde Date: Wed, 22 Dec 2004 04:59:34 +0000 Subject: [PATCH] reorder startup again --- src/main.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main.c b/src/main.c index 3c2a15d9..56e2dff6 100644 --- a/src/main.c +++ b/src/main.c @@ -490,7 +490,7 @@ int daemon_start(void) { if((fd = open("/dev/null", O_RDWR, 0)) != -1) { dup2(fd, STDIN_FILENO); dup2(fd, STDOUT_FILENO); - dup2(fd, STDERR_FILENO); + dup2(fd, STDERR_FILENO); if (fd > 2) close(fd); } @@ -753,7 +753,6 @@ int main(int argc, char *argv[]) { exit(EXIT_FAILURE); } - if(!foreground) { if(config.logfile) { err_setdest(config.logfile,LOGDEST_LOGFILE); @@ -771,19 +770,25 @@ int main(int argc, char *argv[]) { } #endif - /* DWB: shouldn't this be done after dropping privs? */ - if(db_open(config.dbdir, reload)) - DPRINTF(E_FATAL,L_MAIN|L_DB,"Error in db_open: %s\n",strerror(errno)); - /* open the pidfile, so it can be written once we detach */ if(!foreground) { + daemon_start(); + if(-1 == (pid_fd = open(PIDFILE,O_CREAT | O_WRONLY | O_TRUNC, 0644))) DPRINTF(E_FATAL,L_MAIN,"Error opening pidfile (%s): %s\n",PIDFILE,strerror(errno)); if(0 == (pid_fp = fdopen(pid_fd, "w"))) DPRINTF(E_FATAL,L_MAIN,"fdopen: %s\n",strerror(errno)); + + fprintf(pid_fp,"%d\n",getpid()); + fclose(pid_fp); } + /* DWB: shouldn't this be done after dropping privs? */ + if(db_open(config.dbdir, reload)) + DPRINTF(E_FATAL,L_MAIN|L_DB,"Error in db_open: %s\n",strerror(errno)); + + // Drop privs here if(drop_privs(config.runas)) { DPRINTF(E_FATAL,L_MAIN,"Error in drop_privs: %s\n",strerror(errno)); @@ -814,13 +819,6 @@ int main(int argc, char *argv[]) { DPRINTF(E_FATAL,L_MAIN|L_DB,"Error in db_init: %s\n",strerror(errno)); } - /* Wait as long as we can to detach */ - if(!foreground) { - daemon_start(); - fprintf(pid_fp,"%d\n",getpid()); - fclose(pid_fp); - } - DPRINTF(E_LOG,L_MAIN|L_SCAN,"Starting mp3 scan\n"); if(scan_init(config.mp3dir)) { DPRINTF(E_FATAL,L_MAIN|L_SCAN,"Error scanning MP3 files: %s\n",strerror(errno));