Rework error handling and cleanup

This commit is contained in:
Julien BLACHE 2009-06-05 18:07:36 +02:00
parent 0ff33f12d6
commit 149d5432c6

View File

@ -409,12 +409,12 @@ main(int argc, char **argv)
case 'v': case 'v':
fprintf(stdout, "Firefly Media Server: Version %s\n",VERSION); fprintf(stdout, "Firefly Media Server: Version %s\n",VERSION);
exit(EXIT_SUCCESS); return EXIT_SUCCESS;
break; break;
default: default:
usage(argv[0]); usage(argv[0]);
exit(EXIT_FAILURE); return EXIT_FAILURE;
break; break;
} }
} }
@ -424,7 +424,7 @@ main(int argc, char **argv)
{ {
fprintf(stderr, "Could not initialize log facility\n"); fprintf(stderr, "Could not initialize log facility\n");
exit(EXIT_FAILURE); return EXIT_FAILURE;
} }
ret = conffile_load(configfile); ret = conffile_load(configfile);
@ -433,7 +433,7 @@ main(int argc, char **argv)
DPRINTF(E_FATAL, L_MAIN, "Config file errors; please fix your config\n"); DPRINTF(E_FATAL, L_MAIN, "Config file errors; please fix your config\n");
logger_deinit(); logger_deinit();
exit(EXIT_FAILURE); return EXIT_FAILURE;
} }
logger_deinit(); logger_deinit();
@ -450,7 +450,7 @@ main(int argc, char **argv)
fprintf(stderr, "Could not reinitialize log facility with config file settings\n"); fprintf(stderr, "Could not reinitialize log facility with config file settings\n");
conffile_unload(); conffile_unload();
exit(EXIT_FAILURE); return EXIT_FAILURE;
} }
/* Set up libevent logging callback */ /* Set up libevent logging callback */
@ -458,7 +458,7 @@ main(int argc, char **argv)
DPRINTF(E_LOG, L_MAIN, "Firefly Version %s taking off\n", VERSION); DPRINTF(E_LOG, L_MAIN, "Firefly Version %s taking off\n", VERSION);
/* initialize ffmpeg */ /* Initialize ffmpeg */
av_register_all(); av_register_all();
/* Block signals for all threads except the main one */ /* Block signals for all threads except the main one */
@ -473,9 +473,8 @@ main(int argc, char **argv)
{ {
DPRINTF(E_LOG, L_MAIN, "Error setting signal set\n"); DPRINTF(E_LOG, L_MAIN, "Error setting signal set\n");
conffile_unload(); ret = EXIT_FAILURE;
logger_deinit(); goto signal_block_fail;
exit(EXIT_FAILURE);
} }
/* Daemonize and drop privileges */ /* Daemonize and drop privileges */
@ -484,9 +483,8 @@ main(int argc, char **argv)
{ {
DPRINTF(E_LOG, L_MAIN, "Could not initialize server\n"); DPRINTF(E_LOG, L_MAIN, "Could not initialize server\n");
conffile_unload(); ret = EXIT_FAILURE;
logger_deinit(); goto daemon_fail;
exit(EXIT_FAILURE);
} }
/* Initialize libevent (after forking) */ /* Initialize libevent (after forking) */
@ -498,22 +496,18 @@ main(int argc, char **argv)
{ {
DPRINTF(E_FATAL, L_MAIN, "mDNS init failed\n"); DPRINTF(E_FATAL, L_MAIN, "mDNS init failed\n");
conffile_unload(); ret = EXIT_FAILURE;
logger_deinit(); goto mdns_fail;
exit(EXIT_FAILURE);
} }
/* this will require that the db be readable by the runas user */ /* this will require that the db be readable by the runas user */
ret = db_open(&perr, "sqlite3", "/var/cache/mt-daapd"); /* FIXME */ ret = db_open(&perr, "sqlite3", "/var/cache/mt-daapd"); /* FIXME */
if (ret != 0) if (ret != 0)
{ {
DPRINTF(E_FATAL, L_MAIN, "Error opening db: %s\n", perr); DPRINTF(E_FATAL, L_MAIN, "Error opening db: %s\n", perr);
mdns_deinit(); ret = EXIT_FAILURE;
conffile_unload(); goto db_fail;
logger_deinit();
exit(EXIT_FAILURE);
} }
/* Initialize the database before starting */ /* Initialize the database before starting */
@ -529,11 +523,8 @@ main(int argc, char **argv)
{ {
DPRINTF(E_FATAL, L_MAIN, "File scanner thread failed to start\n"); DPRINTF(E_FATAL, L_MAIN, "File scanner thread failed to start\n");
mdns_deinit(); ret = EXIT_FAILURE;
db_deinit(); goto filescanner_fail;
conffile_unload();
logger_deinit();
exit(EXIT_FAILURE);
} }
/* Spawn HTTPd thread */ /* Spawn HTTPd thread */
@ -542,25 +533,16 @@ main(int argc, char **argv)
{ {
DPRINTF(E_FATAL, L_MAIN, "HTTPd thread failed to start\n"); DPRINTF(E_FATAL, L_MAIN, "HTTPd thread failed to start\n");
filescanner_deinit(); ret = EXIT_FAILURE;
mdns_deinit(); goto httpd_fail;
db_deinit();
conffile_unload();
logger_deinit();
exit(EXIT_FAILURE);
} }
/* Register mDNS services */ /* Register mDNS services */
ret = register_services(ffid, mdns_no_rsp, mdns_no_daap); ret = register_services(ffid, mdns_no_rsp, mdns_no_daap);
if (ret < 0) if (ret < 0)
{ {
httpd_deinit(); ret = EXIT_FAILURE;
filescanner_deinit(); goto mdns_reg_fail;
mdns_deinit();
db_deinit();
conffile_unload();
logger_deinit();
exit(EXIT_FAILURE);
} }
/* Set up signal fd */ /* Set up signal fd */
@ -569,13 +551,8 @@ main(int argc, char **argv)
{ {
DPRINTF(E_FATAL, L_MAIN, "Could not setup signalfd: %s\n", strerror(errno)); DPRINTF(E_FATAL, L_MAIN, "Could not setup signalfd: %s\n", strerror(errno));
httpd_deinit(); ret = EXIT_FAILURE;
filescanner_deinit(); goto signalfd_fail;
mdns_deinit();
db_deinit();
conffile_unload();
logger_deinit();
exit(EXIT_FAILURE);
} }
event_set(&sig_event, sigfd, EV_READ, signal_cb, NULL); event_set(&sig_event, sigfd, EV_READ, signal_cb, NULL);
@ -586,21 +563,37 @@ main(int argc, char **argv)
event_base_dispatch(evbase_main); event_base_dispatch(evbase_main);
DPRINTF(E_LOG, L_MAIN, "Stopping gracefully\n"); DPRINTF(E_LOG, L_MAIN, "Stopping gracefully\n");
ret = EXIT_SUCCESS;
DPRINTF(E_LOG, L_MAIN, "HTTPd deinit\n"); /*
httpd_deinit(); * On a clean shutdown, bring mDNS down first to give a chance
* to the clients to perform a clean shutdown on their end
DPRINTF(E_LOG, L_MAIN, "File scanner deinit\n"); */
filescanner_deinit();
DPRINTF(E_LOG, L_MAIN, "mDNS deinit\n"); DPRINTF(E_LOG, L_MAIN, "mDNS deinit\n");
mdns_deinit(); mdns_deinit();
conffile_unload(); signalfd_fail:
mdns_reg_fail:
DPRINTF(E_LOG, L_MAIN, "HTTPd deinit\n");
httpd_deinit();
httpd_fail:
DPRINTF(E_LOG, L_MAIN, "File scanner deinit\n");
filescanner_deinit();
filescanner_fail:
DPRINTF(E_LOG, L_MAIN, "Closing database\n"); DPRINTF(E_LOG, L_MAIN, "Closing database\n");
db_deinit(); db_deinit();
db_fail:
if (ret == EXIT_FAILURE)
{
DPRINTF(E_LOG, L_MAIN, "mDNS deinit\n");
mdns_deinit();
}
mdns_fail:
daemon_fail:
if (background) if (background)
{ {
ret = unlink(pidfile); ret = unlink(pidfile);
@ -608,9 +601,10 @@ main(int argc, char **argv)
DPRINTF(E_WARN, L_MAIN, "Could not unlink PID file %s: %s\n", pidfile, strerror(errno)); DPRINTF(E_WARN, L_MAIN, "Could not unlink PID file %s: %s\n", pidfile, strerror(errno));
} }
signal_block_fail:
DPRINTF(E_LOG, L_MAIN, "Exiting.\n"); DPRINTF(E_LOG, L_MAIN, "Exiting.\n");
conffile_unload();
logger_deinit(); logger_deinit();
return EXIT_SUCCESS; return ret;
} }