Try to wait as long as possible before detaching -- catch more startup errors that way

This commit is contained in:
Ron Pedde 2004-12-07 00:24:39 +00:00
parent ce0cde027f
commit 007fb3725c

View File

@ -522,6 +522,7 @@ void usage(char *program) {
printf(" -c <file> Use configfile specified"); printf(" -c <file> Use configfile specified");
printf(" -p Parse playlist file\n"); printf(" -p Parse playlist file\n");
printf(" -f Run in foreground\n"); printf(" -f Run in foreground\n");
printf(" -y Yes, go ahead and run as non-root user");
printf("\n\n"); printf("\n\n");
printf("Valid debug modules:\n"); printf("Valid debug modules:\n");
printf(" config,webserver,database,scan,query,index,browse\n"); printf(" config,webserver,database,scan,query,index,browse\n");
@ -685,12 +686,13 @@ int main(int argc, char *argv[]) {
int end_time; int end_time;
int rescan_counter=0; int rescan_counter=0;
int old_song_count; int old_song_count;
int force_non_root=0;
pthread_t signal_tid; pthread_t signal_tid;
config.use_mdns=1; config.use_mdns=1;
err_debuglevel=1; err_debuglevel=1;
while((option=getopt(argc,argv,"D:d:c:mpfr")) != -1) { while((option=getopt(argc,argv,"D:d:c:mpfry")) != -1) {
switch(option) { switch(option) {
case 'd': case 'd':
err_debuglevel=atoi(optarg); err_debuglevel=atoi(optarg);
@ -698,7 +700,7 @@ int main(int argc, char *argv[]) {
case 'D': case 'D':
if(err_setdebugmask(optarg)) { if(err_setdebugmask(optarg)) {
usage(argv[0]); usage(argv[0]);
exit(-1); exit(EXIT_FAILURE);
} }
break; break;
case 'f': case 'f':
@ -722,6 +724,10 @@ int main(int argc, char *argv[]) {
reload=1; reload=1;
break; break;
case 'y':
force_non_root=1;
break;
default: default:
usage(argv[0]); usage(argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -729,16 +735,22 @@ int main(int argc, char *argv[]) {
} }
} }
if((getuid()) && (!force_non_root)) {
fprintf(stderr,"You are not root. This is almost certainly wrong. If you are\n"
"sure you want to do this, use the -y command-line switch\n");
exit(EXIT_FAILURE);
}
/* read the configfile, if specified, otherwise /* read the configfile, if specified, otherwise
* try defaults */ * try defaults */
config.stats.start_time=start_time=time(NULL); config.stats.start_time=start_time=time(NULL);
if(config_read(configfile)) { if(config_read(configfile)) {
if(errno) perror("config_read"); fprintf(stderr,"Error reading config file (%s)\n",configfile);
else fprintf(stderr,"Error reading config file (%s)\n",configfile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if((config.logfile) && (!parseonly) && (!foreground)) { if((config.logfile) && (!parseonly) && (!foreground)) {
err_setdest(config.logfile,LOGDEST_LOGFILE); err_setdest(config.logfile,LOGDEST_LOGFILE);
} else { } else {
@ -747,15 +759,10 @@ int main(int argc, char *argv[]) {
} }
} }
/* DWB: shouldn't this be done after dropping privs? */
#ifndef WITHOUT_MDNS if(db_open(config.dbdir, reload)) {
if((config.use_mdns) && (!parseonly)) { DPRINTF(E_FATAL,L_MAIN|L_DB,"Error in db_open: %s\n",strerror(errno));
DPRINTF(E_LOG,L_MAIN,"Starting rendezvous daemon\n");
if(rend_init(config.runas)) {
DPRINTF(E_FATAL,L_MAIN|L_REND,"Error in rend_init: %s\n",strerror(errno));
} }
}
#endif
/* DWB: we want to detach before we drop privs so the pid file can /* DWB: we want to detach before we drop privs so the pid file can
be created with the original permissions. This has the be created with the original permissions. This has the
@ -763,19 +770,10 @@ int main(int argc, char *argv[]) {
we're attached, but if is much better when being automatically we're attached, but if is much better when being automatically
started as a system service. */ started as a system service. */
if(!foreground) { if(!foreground) {
daemon_start();
write_pid_file(); write_pid_file();
} }
/* 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));
}
/* block signals and set up the signal handling thread */ /* block signals and set up the signal handling thread */
DPRINTF(E_LOG,L_MAIN,"Starting signal handler\n"); DPRINTF(E_LOG,L_MAIN,"Starting signal handler\n");
@ -796,12 +794,34 @@ int main(int argc, char *argv[]) {
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
/* Initialize the database before starting */ /* Initialize the database before starting */
DPRINTF(E_LOG,L_MAIN|L_DB,"Initializing database\n"); DPRINTF(E_LOG,L_MAIN|L_DB,"Initializing database\n");
if(db_init()) { if(db_init()) {
DPRINTF(E_FATAL,L_MAIN|L_DB,"Error in db_init: %s\n",strerror(errno)); DPRINTF(E_FATAL,L_MAIN|L_DB,"Error in db_init: %s\n",strerror(errno));
} }
#ifndef WITHOUT_MDNS
if((config.use_mdns) && (!parseonly)) {
DPRINTF(E_LOG,L_MAIN,"Starting rendezvous daemon\n");
if(rend_init(config.runas)) {
DPRINTF(E_FATAL,L_MAIN|L_REND,"Error in rend_init: %s\n",strerror(errno));
}
}
#endif
/* Wait as long as we can to detach */
if(!foreground) {
daemon_start();
}
// Drop privs here
if(drop_privs(config.runas)) {
DPRINTF(E_FATAL,L_MAIN,"Error in drop_privs: %s\n",strerror(errno));
}
DPRINTF(E_LOG,L_MAIN|L_SCAN,"Starting mp3 scan\n"); DPRINTF(E_LOG,L_MAIN|L_SCAN,"Starting mp3 scan\n");
if(scan_init(config.mp3dir)) { if(scan_init(config.mp3dir)) {
DPRINTF(E_FATAL,L_MAIN|L_SCAN,"Error scanning MP3 files: %s\n",strerror(errno)); DPRINTF(E_FATAL,L_MAIN|L_SCAN,"Error scanning MP3 files: %s\n",strerror(errno));