void ns_os_init(const char *progname) { ns_paths_init(); setup_syslog(progname); ntservice_init(); version_check(progname); }
void ns_os_init(const char *progname) { setup_syslog(progname); #ifdef HAVE_LINUX_CAPABILITY_H linux_initialprivs(); #endif #ifdef HAVE_LINUXTHREADS mainpid = getpid(); #endif #ifdef SIGXFSZ signal(SIGXFSZ, SIG_IGN); #endif }
void ns_os_init(const char *progname) { ns_paths_init(); setup_syslog(progname); /* * XXXMPA. We may need to split ntservice_init() in two and * just mark as running in ns_os_started(). If we do that * this is where the first part of ntservice_init() should be * called from. * * XXX970 Remove comment if no problems by 9.7.0. * * ntservice_init(); */ version_check(progname); }
int main(int argc, char **argv) { // Initialize syslog setup_syslog(); // 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_proxy_config *config = calloc(1, sizeof(statsite_proxy_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 int validate_res = validate_config(config); if (validate_res != 0) { syslog(LOG_ERR, "Invalid configuration!"); return 1; } // Set the syslog mask setlogmask(config->syslog_log_level); // Daemonize if (config->daemonize) { pid_t pid, sid; 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; } close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); } // Log that we are starting up syslog(LOG_INFO, "Starting statsite-proxy."); syslog(LOG_INFO, "Loaded Servers config: %s", config->servers); // Initialize proxy proxy *proxy = NULL; int proxy_res = proxy_init(&proxy, config->servers); if (proxy_res != 0) { syslog(LOG_ERR, "Failed to initialize proxy!"); return 1; } // Initialize the networking statsite_proxy_networking *netconf = NULL; int net_res = init_networking(config, &netconf, proxy); if (net_res != 0) { syslog(LOG_ERR, "Failed to initialize networking!"); return 1; } // Start the network workers pthread_t thread; pthread_create(&thread, NULL, (void*(*)(void*))start_networking_worker, netconf); /** * Loop forever, until we get a signal that * indicates we should shutdown. */ signal(SIGPIPE, SIG_IGN); // Ignore SIG_IGN signal(SIGHUP, SIG_IGN); // Ignore SIG_IGN signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); while (SHOULD_RUN) { sleep(1); } // Begin the shutdown/cleanup shutdown_networking(netconf); // If daemonized, remove the pid file if (config->daemonize && unlink(config->pid_file)) { syslog(LOG_ERR, "Failed to delete pid file!"); } // Free our memory proxy_destroy(proxy); free(config); // Done return 0; }
int main(int argc, char **argv) { // Initialize syslog setup_syslog(); // 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 = calloc(1, sizeof(statsite_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; } // 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); // 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."); // Initialize the networking statsite_networking *netconf = NULL; int net_res = init_networking(config, &netconf); 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, &SHOULD_RUN); // Begin the shutdown/cleanup shutdown_networking(netconf); // Do the final flush final_flush(); // 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); // Done return 0; }
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; }