Exemplo n.º 1
0
static void
stormfs_destroy(void *data)
{
  cache_destroy();
  proxy_destroy();
  free(stormfs.bucket);
  free(stormfs.mountpoint);
  free(stormfs.access_key);
  free(stormfs.secret_key);
  free(stormfs.virtual_url);
  g_hash_table_destroy(stormfs.mime_types);
}
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;
}