#include <assert.h>
#include <errno.h>
#include <getopt.h>
+#include <limits.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
config_filename = argv[optind++];
}
+ // Canonicalize argv[0] and config_filename.
+ char argv0_canon[PATH_MAX];
+ char config_filename_canon[PATH_MAX];
+
+ if (realpath(argv[0], argv0_canon) == NULL) {
+ log_perror(argv[0]);
+ exit(1);
+ }
+ if (realpath(config_filename.c_str(), config_filename_canon) == NULL) {
+ log_perror(config_filename.c_str());
+ exit(1);
+ }
+
+ // Now parse the configuration file.
Config config;
- if (!parse_config(config_filename, &config)) {
+ if (!parse_config(config_filename_canon, &config)) {
exit(1);
}
if (test_config) {
exit(0);
}
+
+ // Ideally we'd like to daemonize only when we've started up all threads etc.,
+ // but daemon() forks, which is not good in multithreaded software, so we'll
+ // have to do it here.
+ if (config.daemonize) {
+ if (daemon(0, 0) == -1) {
+ log_perror("daemon");
+ exit(1);
+ }
+ }
start:
// Open logs as soon as possible.
}
// OK, so the signal was SIGHUP. Check that the new config is okay, then exec the new binary.
- if (!dry_run_config(argv[0], config_filename)) {
+ if (!dry_run_config(argv0_canon, config_filename_canon)) {
open_logs(config.log_destinations);
log(ERROR, "%s --test-config failed. Restarting old version instead of new.", argv[0]);
hupped = false;
sprintf(buf, "%d", state_fd);
for ( ;; ) {
- execlp(argv[0], argv[0], config_filename.c_str(), "--state", buf, NULL);
+ execlp(argv0_canon, argv0_canon, config_filename_canon, "--state", buf, NULL);
open_logs(config.log_destinations);
log_perror("execlp");
- log(ERROR, "re-exec of %s failed. Waiting 0.2 seconds and trying again...", argv[0]);
+ log(ERROR, "re-exec of %s failed. Waiting 0.2 seconds and trying again...", argv0_canon);
shut_down_logging();
usleep(200000);
}