int main(int argc, char **argv) { // temporarily set the syslog facilty to main and init it setup_syslog(LOG_USER, 0); // Parse the command line char *config_file = NULL; int parse_res = parse_cmd_line_args(argc, argv, &config_file); if (parse_res) return 1; // Parse the config file statsite_config *config = alloc_config(); int config_res = config_from_filename(config_file, config); if (config_res != 0) { syslog(LOG_ERR, "Failed to read the configuration file!"); return 1; } // Validate the config file if (validate_config(config)) { syslog(LOG_ERR, "Invalid configuration!"); return 1; } // close the initial syslog closelog(); // Initialize syslog with configured facility setup_syslog(config->syslog_log_facility, config->daemonize); // Set prefixes for each message type if (prepare_prefixes(config)) { syslog(LOG_ERR, "Failed to get prefixes!"); return 1; } // Build the prefix tree if (build_prefix_tree(config)) { syslog(LOG_ERR, "Failed to build prefix tree!"); return 1; } // Set the syslog mask setlogmask(config->syslog_log_level); // Initialize libcurl curl_global_init(CURL_GLOBAL_DEFAULT); // Daemonize if (config->daemonize) { pid_t pid, sid; int fd; syslog(LOG_INFO, "Daemonizing."); pid = fork(); // Exit if we failed to fork if (pid < 0) { syslog(LOG_ERR, "Failed to fork() daemon!"); return 1; } // Parent process returns if (pid) return 0; // Create a new session sid = setsid(); if (sid < 0) { syslog(LOG_ERR, "Failed to set daemon SID!"); return 1; } int write_pidfile_res = write_pidfile(config->pid_file, sid); if (write_pidfile_res) { syslog(LOG_ERR, "Failed to write pidfile. Terminating."); return 1; } if ((fd = open("/dev/null", O_RDWR, 0)) != -1) { dup2(fd, STDIN_FILENO); dup2(fd, STDOUT_FILENO); dup2(fd, STDERR_FILENO); if (fd > STDERR_FILENO) close(fd); } } // Log that we are starting up syslog(LOG_INFO, "Starting statsite."); // Build the sinks sink* sinks = NULL; init_sinks(&sinks, config); // Initialize the networking statsite_networking *netconf = NULL; int net_res = init_networking(config, &netconf, sinks); if (net_res != 0) { syslog(LOG_ERR, "Failed to initialize networking!"); return 1; } // Setup signal handlers signal(SIGPIPE, SIG_IGN); // Ignore SIG_IGN signal(SIGHUP, SIG_IGN); // Ignore SIG_IGN signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); // Join the networking loop, blocks until exit enter_networking_loop(netconf, &SIGNUM); if (SIGNUM != 0) { syslog(LOG_WARNING, "Received signal [%s]! Exiting...", strsignal(SIGNUM)); } // Begin the shutdown/cleanup shutdown_networking(netconf); // Do the final flush final_flush(sinks); // If daemonized, remove the pid file if (config->daemonize && unlink(config->pid_file)) { syslog(LOG_ERR, "Failed to delete pid file!"); } // Free our memory free_config(config); // Tear down libcurl curl_global_cleanup(); // Done return 0; }
int load_config (char * file) { if (!conf) conf = alloc_config(); if (!conf) return 1; /* * internal defaults */ if (!conf->verbosity) conf->verbosity = DEFAULT_VERBOSITY; conf->udev = udev_new(); dm_drv_version(conf->version, TGT_MPATH); conf->dev_type = DEV_NONE; conf->minio = DEFAULT_MINIO; conf->minio_rq = DEFAULT_MINIO_RQ; get_sys_max_fds(&conf->max_fds); conf->bindings_file = set_default(DEFAULT_BINDINGS_FILE); conf->wwids_file = set_default(DEFAULT_WWIDS_FILE); conf->bindings_read_only = 0; conf->multipath_dir = set_default(DEFAULT_MULTIPATHDIR); conf->features = set_default(DEFAULT_FEATURES); conf->flush_on_last_del = 0; conf->attribute_flags = 0; conf->reassign_maps = DEFAULT_REASSIGN_MAPS; conf->checkint = DEFAULT_CHECKINT; conf->max_checkint = MAX_CHECKINT(conf->checkint); conf->fast_io_fail = DEFAULT_FAST_IO_FAIL; conf->retain_hwhandler = DEFAULT_RETAIN_HWHANDLER; conf->detect_prio = DEFAULT_DETECT_PRIO; /* * preload default hwtable */ if (conf->hwtable == NULL) { conf->hwtable = vector_alloc(); if (!conf->hwtable) goto out; } if (setup_default_hwtable(conf->hwtable)) goto out; /* * read the config file */ set_current_keywords(&conf->keywords); alloc_keywords(); if (filepresent(file)) { int builtin_hwtable_size; builtin_hwtable_size = VECTOR_SIZE(conf->hwtable); if (init_data(file, init_keywords)) { condlog(0, "error parsing config file"); goto out; } if (VECTOR_SIZE(conf->hwtable) > builtin_hwtable_size) { /* * remove duplica in hwtable. config file * takes precedence over build-in hwtable */ factorize_hwtable(conf->hwtable, builtin_hwtable_size); } } else { init_keywords(); } /* * fill the voids left in the config file */ if (conf->blist_devnode == NULL) { conf->blist_devnode = vector_alloc(); if (!conf->blist_devnode) goto out; } if (conf->blist_wwid == NULL) { conf->blist_wwid = vector_alloc(); if (!conf->blist_wwid) goto out; } if (conf->blist_device == NULL) { conf->blist_device = vector_alloc(); if (!conf->blist_device) goto out; } if (setup_default_blist(conf)) goto out; if (conf->elist_devnode == NULL) { conf->elist_devnode = vector_alloc(); if (!conf->elist_devnode) goto out; } if (conf->elist_wwid == NULL) { conf->elist_wwid = vector_alloc(); if (!conf->elist_wwid) goto out; } if (conf->elist_device == NULL) { conf->elist_device = vector_alloc(); if (!conf->elist_device) goto out; } if (conf->mptable == NULL) { conf->mptable = vector_alloc(); if (!conf->mptable) goto out; } if (conf->bindings_file == NULL) conf->bindings_file = set_default(DEFAULT_BINDINGS_FILE); if (!conf->multipath_dir || !conf->bindings_file || !conf->wwids_file) goto out; return 0; out: free_config(conf); return 1; }