/** @brief AutoBuilder Entry Point @param argc Number of arguments passed through argv @param argv String representation of arguments passed to the application @return EXIT_SUCCESS on success or a non-zero integer on failure. **/ int main (int argc, char ** argv) { conf_t conf; conf_err_t cerr; opt_t opt; opt_err_t oerr; const char *db_type, *db_host, *db_port, *db_user, *db_pass, *db_db; /* Parse the command line */ oerr = opt_init(&opt, argc, argv, 1); if (oerr != OPT_OK) { fprintf (stderr, "%s\n", SHORT_HELP); opt_destroy (&opt); return EXIT_FAILURE; } if (opt.help) { fprintf (stderr, "%s\n", HELP_TXT); return EXIT_SUCCESS; } /* Parse the configuration */ cerr = conf_init (&conf, opt.conf); if (cerr != CONF_OK) { fprintf (stderr, "Configuration Error: %s", conf_get_err (&conf)); conf_destroy (&conf); opt_destroy (&opt); return EXIT_FAILURE; } /* Read Database Options */ db_type = conf_get (&conf, "DB_TYPE"); db_host = conf_get (&conf, "DB_HOST"); db_port = conf_get (&conf, "DB_PORT"); db_user = conf_get (&conf, "DB_USER"); db_pass = conf_get (&conf, "DB_PASS"); db_db = conf_get (&conf, "DB_DB"); /* Connect to the database */ //db_connect (db_type, db_host, db_port, db_user, db_pass, db_db); /* Cleanup */ conf_destroy (&conf); opt_destroy (&opt); return EXIT_SUCCESS; }
static void zipper_init_command_server(zipper_t *zipper, const char *config_path) { config_t *command_server_conf = NULL; int ret = conf_get_sub_config(zipper->config_, "command_server_config", &command_server_conf); ASSERT(ret == 0, "get configure for command server failed\n"); char ip[MAX_IP_STR_LEN]; int port; conf_get_string(command_server_conf, "ip", ip); conf_get_integer(command_server_conf, "port", &port); char command_spec_file_path[PATH_MAX]; if (0 != conf_get_path(command_server_conf, "command_spec_file", command_spec_file_path)) ASSERT(0, "command specific file doesn't found\n"); zipper->command_server_ = command_server_create(zipper->base_event_, ip, port, command_spec_file_path, config_path); conf_destroy(command_server_conf); command_server_init_command_runner(zipper->command_server_, zipper->rule_store_, zipper->ip_store_); }
static void destroy(void) { /* Cleanup forward */ conf_destroy(); destroy_shmlock(); /* Close pres db */ pres_db_close(); }
static rmt_conf * conf_open(char *filename) { int ret; rmt_conf *cf = NULL; FILE *fh = NULL; sds path = NULL; if (filename == NULL) { log_error("ERROR: configuration file name is NULL."); return NULL; } path = getAbsolutePath(filename); if (path == NULL) { log_error("ERROR: configuration file name '%s' is error.", filename); goto error; } fh = fopen(path, "r"); if (fh == NULL) { log_error("ERROR: failed to open configuration '%s': %s", path, strerror(errno)); goto error; } cf = rmt_alloc(sizeof(*cf)); if (cf == NULL) { goto error; } ret = conf_init(cf); if(ret != RMT_OK){ goto error; } cf->fname = path; cf->fh = fh; return cf; error: if(fh != NULL) { fclose(fh); } if (cf != NULL) { conf_destroy(cf); } if (path != NULL) { sdsfree(path); } return NULL; }
static void zipper_init_log(zipper_t *zipper) { config_t *log_conf = NULL; int ret = conf_get_sub_config(zipper->config_, "log_config", &log_conf); ASSERT(ret == 0, "read log configure from configure file failed\n"); log_init(log_conf); conf_destroy(log_conf); }
static void core_ctx_destroy(struct context *ctx) { log_debug(LOG_VVERB, "destroy ctx %p id %"PRIu32"", ctx, ctx->id); proxy_deinit(ctx); server_pool_disconnect(ctx); event_deinit(ctx); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); }
static void zipper_delete(zipper_t *zipper) { log_fini(); conf_destroy(zipper->config_); dns_server_delete(zipper->dns_server_); dns_client_delete(zipper->dns_client_); rule_store_destroy(zipper->rule_store_); ipstore_destroy(zipper->ip_store_); command_server_delete(zipper->command_server_); memorypool_delete(zipper->query_session_pool_); free(zipper); zrb_finalize_ruby_env(); }
// USR1 signal: re-read configuration file static void sigusr1_cb (G_GNUC_UNUSED evutil_socket_t sig, G_GNUC_UNUSED short events, G_GNUC_UNUSED void *user_data) { ConfData *conf_new = conf_create(); ConfData *conf_old = _app->conf; LOG_err (APP_LOG, "Got SIGUSR1"); if (!conf_parse_file (conf_new, _app->conf_path)) { LOG_err (APP_LOG, "Failed to parse configuration file: %s", _app->conf_path); conf_destroy(conf_new); } else { const gchar *copy_entries[] = {"s3.host", "s3.port", "s3.access_key_id", "s3.secret_access_key", "s3.bucket_name", NULL}; int i; _app->conf = conf_new; for (i = 0; copy_entries[i]; i++) { conf_copy_entry (_app->conf, conf_old, copy_entries[i], FALSE); } conf_destroy (conf_old); log_level = conf_get_int(_app->conf, "log.level"); } }
//解析配置信息,并进行有效性判断 struct conf * conf_create(char *filename) { rstatus_t status; struct conf *cf; //打开配置文件,创建conf结构,并初始化 cf = conf_open(filename); if (cf == NULL) { return NULL; } /* validate configuration file before parsing */ status = conf_pre_validate(cf); //检查配置格式是否符合yml标准 if (status != NC_OK) { goto error; } /* parse the configuration file */ status = conf_parse(cf); //配置解析 if (status != NC_OK) { goto error; } /* validate parsed configuration */ status = conf_post_validate(cf); //重复性判断 if (status != NC_OK) { goto error; } conf_dump(cf); fclose(cf->fh); cf->fh = NULL; return cf; error: log_stderr("nutcracker: configuration file '%s' syntax is invalid", filename); fclose(cf->fh); cf->fh = NULL; conf_destroy(cf); return NULL; }
/* * Returns true if configuration file has a valid syntax, otherwise * returns false */ static bool dn_test_conf(struct instance *nci) { struct conf *cf; cf = conf_create(nci->conf_filename); if (cf == NULL) { log_stderr("dynomite: configuration file '%s' syntax is invalid", nci->conf_filename); return false; } conf_destroy(cf); log_stderr("dynomite: configuration file '%s' syntax is ok", nci->conf_filename); return true; }
/* * Returns true if configuration file has a valid syntax, otherwise * returns false */ static bool nc_test_conf(struct env_master *env) { struct conf *cf; cf = conf_create(env->conf_filename); if (cf == NULL) { log_stderr("nutcracker: configuration file '%s' syntax is invalid", env->conf_filename); return false; } conf_destroy(cf); log_stderr("nutcracker: configuration file '%s' syntax is ok", env->conf_filename); return true; }
int conf_get_key(char *key, char* line) { CONFIG *config; char *key_temp; config = conf_build(line, CONF_SEPARATOR); Assert(config->conf_opt_size == 2); /* Key = Value*/ key_temp = trim(config->conf_opt_infor[0].conf_value, ' '); // key_temp = config->conf_opt_infor[0].conf_value; MEMCPY(key, key_temp, STRLEN(key_temp)); conf_destroy(config); return TRUE; }
int conf_get_value(char *value, char* line) { CONFIG *config; char *value_temp; config = conf_build(line, CONF_SEPARATOR); Assert(config->conf_opt_size == 2); value_temp = trim(config->conf_opt_infor[1].conf_value, ' '); // value_temp = config->conf_opt_infor[1].conf_value; MEMCPY(value, value_temp, STRLEN(value_temp)); conf_destroy(config); return TRUE; }
rmt_conf * conf_create(char *filename) { int ret; rmt_conf *cf; cf = conf_open(filename); if (cf == NULL) { return NULL; } /* validate configuration file before parsing */ ret = conf_pre_validate(cf); if (ret != RMT_OK) { goto error; } conf_organizations_dump(cf); /* parse the configuration file */ ret = conf_parse(cf); if (ret != RMT_OK) { goto error; } /* validate parsed configuration */ ret = conf_post_validate(cf); if (ret != RMT_OK) { goto error; } conf_dump(cf); fclose(cf->fh); cf->fh = NULL; return cf; error: fclose(cf->fh); cf->fh = NULL; conf_destroy(cf); return NULL; }
/* if the target key has not been found, the method will return NULL */ int conf_get_value_by_key(char *value, char *file_path, char* target_key) { char *conf_opt; char key[TOKEN_NAME_MAX_LEN]; char *buffer; CONFIG *config; int i; int file_size; Assert(value != NULL); file_size = file_get_size(file_path); buffer = (char *)MEMALLOCHEAP(file_size); file_read(file_path, buffer, file_size); config = conf_build(buffer, LINE_SEPARATOR); for(i=0; i<config->conf_opt_size; i++) { conf_opt = config->conf_opt_infor[i].conf_value; Assert(STRLEN(conf_opt) != 0); MEMSET(key, TOKEN_NAME_MAX_LEN); conf_get_key(key, conf_opt); if(match(key, target_key)) { conf_get_value(value, conf_opt); break; } } conf_destroy(config); trim(value, ' '); MEMFREEHEAP(buffer); return TRUE; }
struct conf * conf_create(char *filename) { rstatus_t status; struct conf *cf; cf = conf_open(filename); if (cf == NULL) { return NULL; } /* validate configuration file before parsing */ status = conf_pre_validate(cf); if (status != NC_OK) { goto error; } /* parse the configuration file */ status = conf_parse(cf); if (status != NC_OK) { goto error; } /* validate parsed configuration */ status = conf_post_validate(cf); if (status != NC_OK) { goto error; } conf_dump(cf); fclose(cf->fh); cf->fh = NULL; return cf; error: fclose(cf->fh); cf->fh = NULL; conf_destroy(cf); return NULL; }
static void zipper_init_server(zipper_t *zipper) { config_t *zipper_server_config = NULL; int ret = conf_get_sub_config(zipper->config_, "server_config", &zipper_server_config); ASSERT(ret == 0, "get configure for server failed\n"); char ip[MAX_IP_STR_LEN]; int port; conf_get_string(zipper_server_config, "ip", ip); conf_get_integer(zipper_server_config, "port", &port); zipper->dns_server_ = dns_server_create(zipper->base_event_, UDP_SERVER, ip, port); conf_destroy(zipper_server_config); command_server_get_named_ip(zipper->command_server_, ip); port = command_server_get_named_port(zipper->command_server_); zipper->dns_client_ = dns_client_create(zipper->base_event_, UDP_CLIENT, ip, port); }
static struct context * core_ctx_create(struct instance *nci) { rstatus_t status; struct context *ctx; struct conn *sentinel_conn; ctx = nc_alloc(sizeof(*ctx)); if (ctx == NULL) { return NULL; } ctx->id = ++ctx_id; ctx->cf = NULL; ctx->stats = NULL; array_null(&ctx->pool); ctx->ep = -1; ctx->nevent = EVENT_SIZE_HINT; ctx->max_timeout = nci->stats_interval; ctx->timeout = ctx->max_timeout; ctx->event = NULL; ctx->server_reconnect_interval = nci->server_reconnect_interval; ctx->whitelist = nci->whitelist; /* parse and create configuration */ ctx->cf = conf_create(nci->conf_filename); if (ctx->cf == NULL) { nc_free(ctx); return NULL; } /* initialize server pool from configuration */ status = server_pool_init(&ctx->pool, &ctx->cf->pool, ctx); if (status != NC_OK) { conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* initialize sentinel server */ ctx->sentinel = sentinel_init(nci->sentinel_port, nci->sentinel_addr); if (ctx->sentinel == NULL) { server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* create stats per server pool */ ctx->stats = stats_create(nci->stats_port, nci->stats_addr, nci->stats_interval, nci->hostname, &ctx->pool); if (ctx->stats == NULL) { sentinel_deinit(ctx->sentinel); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* initialize event handling for client, proxy and server */ status = event_init(ctx, EVENT_SIZE_HINT); if (status != NC_OK) { stats_destroy(ctx->stats); sentinel_deinit(ctx->sentinel); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* preconnect? servers in server pool */ status = server_pool_preconnect(ctx); if (status != NC_OK) { server_pool_disconnect(ctx); event_deinit(ctx); stats_destroy(ctx->stats); sentinel_deinit(ctx->sentinel); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* initialize sentinel server conn */ sentinel_conn = sentinel_connect(ctx); if (sentinel_conn == NULL) { server_pool_disconnect(ctx); event_deinit(ctx); stats_destroy(ctx->stats); sentinel_deinit(ctx->sentinel); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); } /* initialize proxy per server pool */ status = proxy_init(ctx); if (status != NC_OK) { sentinel_conn->close(ctx, sentinel_conn); server_pool_disconnect(ctx); event_deinit(ctx); stats_destroy(ctx->stats); sentinel_deinit(ctx->sentinel); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } log_debug(LOG_VVERB, "created ctx %p id %"PRIu32"", ctx, ctx->id); return ctx; }
int main(int argc, char *argv[]) { CREW crew; int i; int count = 0; int res; BOOLEAN result; char ** keys; void * statusp; C = new_conf(); parse_cmdline_cfg(C, argc, argv); parse_cfgfile(C); parse_cmdline(C, argc, argv); RUNNER R = new_runner(conf_get_user(C), conf_get_group(C)); runas(R); runner_destroy(R); sigmasker(); if (is_daemon(C)) { res = fork(); if (res == -1 ){ // ERRROR NOTIFY(FATAL, "%s: [error] unable to run in the background\n", program_name); } else if (res == 0) { // CHILD PID P = new_pid(conf_get_pidfile(C)); HASH H = conf_get_items(C); count = conf_get_count(C); keys = hash_get_keys_delim(H, ':'); if ((crew = new_crew(count, count, FALSE)) == NULL) { NOTIFY(FATAL, "%s: [error] unable to allocate memory for %d log files", program_name, count); } set_pid(P, getpid()); pid_destroy(P); for (i = 0; i < count && crew_get_shutdown(crew) != TRUE; i++) { FIDO F = new_fido(C, keys[i]); result = crew_add(crew, (void*)start, F); if (result == FALSE) { NOTIFY(FATAL, "%s: [error] unable to spawn additional threads", program_name); } } crew_join(crew, TRUE, &statusp); conf_destroy(C); } else { // PARENT } } else { HASH H = conf_get_items(C); count = conf_get_count(C); keys = hash_get_keys_delim(H, ':'); if ((crew = new_crew(count, count, FALSE)) == NULL) { NOTIFY(FATAL, "%s: [error] unable to allocate memory for %d log files", program_name, count); } for (i = 0; i < count && crew_get_shutdown(crew) != TRUE; i++) { FIDO F = new_fido(C, keys[i]); result = crew_add(crew, (void*)start, F); } crew_join(crew, TRUE, &statusp); conf_destroy(C); } exit(EXIT_SUCCESS); } /* end of int main **/
int my_conf_destroy() { conf_destroy(); return 0; }
int main (int argc, char *argv[]) { gchar *in_dir; GList *tmp; struct evhttp_uri *uri; struct timeval tv; log_level = LOG_debug; event_set_mem_functions (g_malloc, g_realloc, g_free); // init SSL libraries CRYPTO_set_mem_functions (g_malloc0, g_realloc, g_free); ENGINE_load_builtin_engines (); ENGINE_register_all_complete (); ERR_load_crypto_strings (); OpenSSL_add_all_algorithms (); SSL_load_error_strings (); SSL_library_init (); if (!RAND_poll ()) { fprintf(stderr, "RAND_poll() failed.\n"); return 1; } g_random_set_seed (time (NULL)); in_dir = g_dir_make_tmp (NULL, NULL); g_assert (in_dir); app = g_new0 (Application, 1); app->files_count = 10; app->evbase = event_base_new (); app->dns_base = evdns_base_new (app->evbase, 1); app->conf = conf_create (); conf_add_boolean (app->conf, "log.use_syslog", TRUE); conf_add_uint (app->conf, "auth.ttl", 85800); conf_add_int (app->conf, "pool.writers", 2); conf_add_int (app->conf, "pool.readers", 2); conf_add_int (app->conf, "pool.operations", 4); conf_add_uint (app->conf, "pool.max_requests_per_pool", 100); conf_add_int (app->conf, "connection.timeout", 20); conf_add_int (app->conf, "connection.retries", -1); conf_add_uint (app->conf, "filesystem.dir_cache_max_time", 5); conf_add_boolean (app->conf, "filesystem.cache_enabled", TRUE); conf_add_string (app->conf, "filesystem.cache_dir", "/tmp/hydrafs"); conf_add_string (app->conf, "filesystem.cache_dir_max_size", "1Gb"); conf_add_boolean (app->conf, "statistics.enabled", TRUE); conf_add_int (app->conf, "statistics.port", 8011); conf_add_string (app->conf, "auth.user", "test:tester"); conf_add_string (app->conf, "auth.key", "testing"); uri = evhttp_uri_parse ("https://10.0.0.104:8080/auth/v1.0"); app->ssl_ctx = SSL_CTX_new (TLSv1_client_method ()); app->stats = hfs_stats_srv_create (app); app->auth_client = auth_client_create (app, uri); app->http = http_client_create (app); // start server start_srv (app->evbase, in_dir); app->timeout = evtimer_new (app->evbase, on_output_timer, NULL); evutil_timerclear(&tv); tv.tv_sec = 0; tv.tv_usec = 500; event_add (app->timeout, &tv); event_base_dispatch (app->evbase); evhttp_uri_free (uri); event_del (app->timeout); event_free (app->timeout); evhttp_free (app->http_srv); auth_client_destroy (app->auth_client); evdns_base_free (app->dns_base, 0); event_base_free (app->evbase); conf_destroy (app->conf); g_free (app); return 0; }
static struct context * core_ctx_create(struct instance *nci) { rstatus_t status; struct context *ctx; srand((unsigned) time(NULL)); ctx = dn_alloc(sizeof(*ctx)); if (ctx == NULL) { return NULL; } ctx->id = ++ctx_id; ctx->cf = NULL; ctx->stats = NULL; ctx->evb = NULL; array_null(&ctx->pool); ctx->max_timeout = nci->stats_interval; ctx->timeout = ctx->max_timeout; ctx->dyn_state = INIT; /* parse and create configuration */ ctx->cf = conf_create(nci->conf_filename); if (ctx->cf == NULL) { loga("Failed to create context!!!"); dn_free(ctx); return NULL; } /* initialize server pool from configuration */ status = server_pool_init(&ctx->pool, &ctx->cf->pool, ctx); if (status != DN_OK) { loga("Failed to initialize server pool!!!"); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* crypto init */ status = crypto_init(ctx); if (status != DN_OK) { loga("Failed to initialize crypto!!!"); dn_free(ctx); return NULL; } /* create stats per server pool */ ctx->stats = stats_create(nci->stats_port, nci->stats_addr, nci->stats_interval, nci->hostname, &ctx->pool, ctx); if (ctx->stats == NULL) { loga("Failed to create stats!!!"); crypto_deinit(); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* initialize event handling for client, proxy and server */ ctx->evb = event_base_create(EVENT_SIZE, &core_core); if (ctx->evb == NULL) { loga("Failed to create socket event handling!!!"); crypto_deinit(); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* preconnect? servers in server pool */ status = server_pool_preconnect(ctx); if (status != DN_OK) { loga("Failed to preconnect for server pool!!!"); crypto_deinit(); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* initialize proxy per server pool */ status = proxy_init(ctx); if (status != DN_OK) { loga("Failed to initialize proxy!!!"); crypto_deinit(); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } /* initialize dnode listener per server pool */ status = dnode_init(ctx); if (status != DN_OK) { loga("Failed to initialize dnode!!!"); crypto_deinit(); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } ctx->dyn_state = JOINING; //TODOS: change this to JOINING /* initialize peers */ status = dnode_peer_init(&ctx->pool, ctx); if (status != DN_OK) { loga("Failed to initialize dnode peers!!!"); crypto_deinit(); dnode_deinit(ctx); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } core_debug(ctx); /* preconntect peers - probably start gossip here */ status = dnode_peer_pool_preconnect(ctx); if (status != DN_OK) { loga("Failed to preconnect dnode peers!!!"); crypto_deinit(); dnode_peer_deinit(&ctx->pool); dnode_deinit(ctx); server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); dn_free(ctx); return NULL; } //init ring msg queue CBUF_Init(C2G_InQ); CBUF_Init(C2G_OutQ); //init stats msg queue CBUF_Init(C2S_InQ); CBUF_Init(C2S_OutQ); gossip_pool_init(ctx); log_debug(LOG_VVERB, "created ctx %p id %"PRIu32"", ctx, ctx->id); return ctx; }
int main(int argc, char **argv) { rstatus_t status; int i, count; //argv [0] = 'master'; if (nc_save_argv(argc, argv) != NC_OK) { exit(1); } nc_set_default_options(&env_global); env_global.worker_processes = sysconf(_SC_NPROCESSORS_ONLN); env_global.cpu_mask = (1<<env_global.worker_processes) - 1; status = nc_get_options(nc_argc, nc_argv, &env_global); if (status != NC_OK) { nc_show_usage(); exit(1); } if (show_version) { log_stderr("This is nutcracker-%s" CRLF, NC_VERSION_STRING); if (show_help) { nc_show_usage(); } if (describe_stats) { stats_describe(); } exit(0); } if (test_conf) { if (!nc_test_conf(&env_global)) { exit(1); } exit(0); } memcpy(&nci_global, &env_global, sizeof(struct instance)); // context will be initialized in later nc_run. if (nc_init_setproctitle() != NC_OK) { exit(1); } status = nc_master_pre_run(&env_global); if (status != NC_OK) { nc_master_post_run(&env_global); exit(1); } count = 0; for(i = 0; i<sysconf(_SC_NPROCESSORS_ONLN); i++) { count+= env_global.cpu_mask & (0x1<<i)?1:0; //log_error("count %d", count); } if (count < env_global.worker_processes) { log_error('cpu mask %x is not fit in worker processes %d', env_global.cpu_mask, env_global.worker_processes); nc_master_post_run(&env_global); exit(1); } /* parse and create configuration for master */ struct conf* cf = malloc(sizeof(struct conf)); memset(cf, 0, sizeof(struct conf)); cf = conf_create(env_global.conf_filename); if (status != NC_OK) { conf_destroy(cf); exit(1); } /* initialize server pool from configuration */ status = tw_master_conf_init(&env_global.pool, &cf->pool); if (status != NC_OK) { conf_destroy(cf); // release array server pool exit(1); } tw_master_cycle(&env_global); nc_master_post_run(&env_global); exit(1); }
int main(int argc, char **argv) { int ch; char conf_path[MAXPATHLEN]; int conf=0; st_cn_t conn; conn = st_connection_alloc(); conn->st_config = conf_create_with_defaults(); (void)strncpy(conf_path, STUNEL_CONFIG, MAXPATHLEN-1); /* Parse command line args */ /* TODO: Add options for port, destination ip, key and user so using conf file is not necessary*/ while ((ch = getopt(argc, argv, "hqvDAf:p:d:i:u:")) != -1) { switch (ch) { case 'A': conf_set_authtype(conn->st_config, STUNEL_AUTH_AGENT); break; case 'f': if (argc == 3) { memset(conf_path, 0, MAXPATHLEN); (void)strncpy(conf_path, optarg, MAXPATHLEN-1); conf_path[MAXPATHLEN - 1]='\0'; conf=1; } break; case 'p': conf_set_port(conn->st_config, atoi(optarg)); break; case 'd': conf_set_address(conn->st_config, optarg); break; case 'i': conf_set_sshkey(conn->st_config, optarg); break; case 'u': conf_set_login(conn->st_config, optarg); break; case 'q': conf_set_log_level(conn->st_config, STUNEL_NORMAL); break; case 'v': conf_set_log_level(conn->st_config, STUNEL_VERBOSE); break; case 'D': conf_set_log_level(conn->st_config, STUNEL_DEBUG); break; case '?': case 'h': default: usage(); } } argc -= optind; argv += optind; if ( conf ) { printf("Reading configuration from file: %s\n", conf_path); conf_destroy(conn->st_config); conn->st_config = conf_get_file(conf_path); } printf("Config file: \n%s\n", conf_dump(conn->st_config)); if (conf_check(conn->st_config)) { st_connection_destroy(conn); exit(1); } printf("User is set to %s, port to %d\n", conf_get_login(conn->st_config), conf_get_port(conn->st_config)); printf("Connecting with libssh\n"); st_ssh_connect(conn); st_connection_destroy(conn); return 0; }
static struct context * core_ctx_create(struct instance *nci) { rstatus_t status; struct context *ctx; ctx = nc_alloc(sizeof(*ctx)); if (ctx == NULL) { return NULL; } ctx->id = ++ctx_id; ctx->cf = NULL; ctx->stats = NULL; ctx->evb = NULL; array_null(&ctx->pool); ctx->max_timeout = nci->stats_interval; ctx->timeout = ctx->max_timeout; ctx->max_nfd = 0; ctx->max_ncconn = 0; ctx->max_nsconn = 0; /* parse and create configuration */ ctx->cf = conf_create(nci->conf_filename); if (ctx->cf == NULL) { nc_free(ctx); return NULL; } /* initialize server pool from configuration */ status = server_pool_init(&ctx->pool, &ctx->cf->pool, ctx); if (status != NC_OK) { conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* * Get rlimit and calculate max client connections after we have * calculated max server connections */ status = core_calc_connections(ctx); if (status != NC_OK) { server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* create stats per server pool */ ctx->stats = stats_create(nci->stats_port, nci->stats_addr, nci->stats_interval, nci->hostname, &ctx->pool); if (ctx->stats == NULL) { server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* initialize event handling for client, proxy and server */ ctx->evb = event_base_create(EVENT_SIZE, &core_core); if (ctx->evb == NULL) { stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* preconnect? servers in server pool */ status = server_pool_preconnect(ctx); if (status != NC_OK) { server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* initialize proxy per server pool */ status = proxy_init(ctx); if (status != NC_OK) { server_pool_disconnect(ctx); event_base_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } log_debug(LOG_VVERB, "created ctx %p id %"PRIu32"", ctx, ctx->id); return ctx; }
static struct context * core_ctx_create(struct instance *nci) { rstatus_t status; struct context *ctx; int64_t now; uint32_t npool; ctx = nc_alloc(sizeof(*ctx)); if (ctx == NULL) { return NULL; } now = nc_msec_now(); if (now < 0) { nc_free(ctx); return NULL; } ctx->id = ++ctx_id; ctx->cf = NULL; ctx->stats = NULL; ctx->evb = NULL; array_null(&ctx->pool); ctx->max_timeout = nci->stats_interval; ctx->timeout = ctx->max_timeout; ctx->next_tick = now + NC_TICK_INTERVAL; /* parse and create configuration */ ctx->cf = conf_create(nci->conf_filename); if (ctx->cf == NULL) { nc_free(ctx); return NULL; } npool = array_n(&ctx->cf->pool); /* initialize server pool from configuration */ if (npool != 0) { status = server_pool_init(&ctx->pool, &ctx->cf->pool, ctx); if (status != NC_OK) { conf_destroy(ctx->cf); nc_free(ctx); return NULL; } } /* create stats per server pool */ if (npool != 0) { ctx->stats = stats_create(nci->stats_port, nci->stats_addr, nci->stats_interval, nci->hostname, &ctx->pool); if (ctx->stats == NULL) { server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } } /* initialize event handling for client, proxy and server */ ctx->evb = evbase_create(NC_EVENT_SIZE, &core_core); if (ctx->evb == NULL) { stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } /* preconnect? servers in server pool */ if (npool != 0) { status = server_pool_preconnect(ctx); if (status != NC_OK) { server_pool_disconnect(ctx); evbase_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } } /* initialize proxy per server pool */ if (npool != 0) { status = proxy_init(ctx); if (status != NC_OK) { server_pool_disconnect(ctx); evbase_destroy(ctx->evb); stats_destroy(ctx->stats); server_pool_deinit(&ctx->pool); conf_destroy(ctx->cf); nc_free(ctx); return NULL; } } log_debug(LOG_VVERB, "created ctx %p id %"PRIu32"", ctx, ctx->id); return ctx; }
/*{{{ application_destroy */ static void application_destroy (Application *app) { LOG_debug (APP_LOG, "Destroying application !"); g_free (app->conf_path); if (app->read_client_pool) client_pool_destroy (app->read_client_pool); if (app->write_client_pool) client_pool_destroy (app->write_client_pool); if (app->ops_client_pool) client_pool_destroy (app->ops_client_pool); if (app->dir_tree) dir_tree_destroy (app->dir_tree); if (app->cmng) cache_mng_destroy (app->cmng); if (app->sigint_ev) event_free (app->sigint_ev); if (app->sigterm_ev) event_free (app->sigterm_ev); if (app->sigpipe_ev) event_free (app->sigpipe_ev); if (app->sigusr1_ev) event_free (app->sigusr1_ev); if (app->sigusr2_ev) event_free (app->sigusr2_ev); if (app->service_con) http_connection_destroy (app->service_con); if (app->stat_srv) stat_srv_destroy (app->stat_srv); // destroy Fuse if (app->rfuse) rfuse_destroy (app->rfuse); if (app->dns_base) evdns_base_free (app->dns_base, 0); if (app->evbase) event_base_free (app->evbase); if (app->uri) evhttp_uri_free (app->uri); if (app->conf) conf_destroy (app->conf); if (app->fuse_opts) g_free (app->fuse_opts); #ifdef SSL_ENABLED SSL_CTX_free (app->ssl_ctx); #endif #ifdef MAGIC_ENABLED magic_close(app->magic_ctx); #endif logger_destroy (); if (app->f_log) fclose (app->f_log); if (app->log_file_name) g_free (app->log_file_name); g_free (app); ENGINE_cleanup (); CRYPTO_cleanup_all_ex_data (); ERR_free_strings (); ERR_remove_thread_state (NULL); EVP_cleanup (); }