int server_enqueue(struct connection *server, struct command *cmd) { if (server == NULL) { mbuf_range_clear(cmd->ctx, cmd->rep_buf); cmd_mark_fail(cmd, rep_server_err); return SERVER_NULL; } if (conn_register(server) == CORVUS_ERR) { return SERVER_REGISTER_ERROR; } server->info->last_active = time(NULL); mbuf_range_clear(cmd->ctx, cmd->rep_buf); cmd->server = server; STAILQ_INSERT_TAIL(&server->info->ready_queue, cmd, ready_next); return CORVUS_OK; }
int server_write(struct connection *server) { struct conn_info *info = server->info; if (!STAILQ_EMPTY(&info->ready_queue)) { server_make_iov(info); } if (info->iov.len <= 0) { cmd_iov_reset(&info->iov); return CORVUS_OK; } int status = conn_write(server, 0); if (status == CORVUS_ERR) { LOG(ERROR, "server_write: server %d fail to write iov", server->fd); return CORVUS_ERR; } if (status == CORVUS_AGAIN) return CORVUS_OK; ATOMIC_INC(info->send_bytes, status); if (info->iov.cursor >= info->iov.len) { cmd_iov_free(&info->iov); } if (!STAILQ_EMPTY(&info->ready_queue) || info->iov.cursor < info->iov.len) { if (conn_register(server) == CORVUS_ERR) { LOG(ERROR, "server_write: fail to reregister server %d", server->fd); return CORVUS_ERR; } } info->last_active = time(NULL); return CORVUS_OK; }
/* point-of-entry */ int main(int argc, char **argv) { int ret = -1, c, index, action = ACTION_NONE, tmp; char *action_param = NULL; processor_config_t *config = 0, *config2 = 0; char *conf_file = 0; int load_autoreg = 0; int flag_quiet = 0; int log_to_file = 0; int console_only = 0; int do_test = 0; // do whatever the test of the day is xmlInitParser(); xmlInitThreads(); //initGenericErrorDefaultFunc((xmlGenericErrorFunc *)xml_error_func); /* the getopt values */ static struct option long_options[] = { {"verbose", no_argument, 0, 0}, {"iface", required_argument, 0, 0}, {"threads", required_argument, 0, 0}, {"help", no_argument, 0, 0}, {"version", no_argument, 0, 0}, {"console", no_argument, 0, 0}, #ifdef CONFIG_PYTHON_ENABLED {"shell", no_argument, 0, 0}, {"run", required_argument, 0, 0}, {"no-scripts", no_argument, 0, 0}, #endif #ifdef CONFIG_SIP_ENABLED {"proxy-iface", required_argument, 0, 0}, {"no-mp", no_argument, 0, 0}, {"tunnel-mp", no_argument, 0, 0}, {"force-mp", no_argument, 0, 0}, {"allow-unknown", no_argument, 0, 0}, {"allow-untrusted", no_argument, 0, 0}, #endif {"list-ca", no_argument, 0, 0}, {"import-ca", required_argument, 0, 0}, {"remove-ca", required_argument, 0, 0}, {"list", no_argument, 0, 0}, {"import", required_argument, 0, 0}, {"remove", required_argument, 0, 0}, {"idents", required_argument, 0, 0}, {"log", required_argument, 0, 0}, #ifdef CONFIG_BROADCAST_ENABLED {"bc-addr", required_argument, 0, 0}, #endif #ifdef CONFIG_WEBCONF_ENABLED {"webconf", required_argument, 0, 0}, #endif #ifdef CONFIG_OPENDHT_ENABLED {"opendht", required_argument, 0, 0}, #endif #ifdef CONFIG_HIP_ENABLED {"list-hits", no_argument, 0, 0}, {"rvs", required_argument, 0, 0}, {"provide-rvs", no_argument, 0, 0}, #endif {0, 0, 0, 0} }; #ifdef LOCK_DEBUG debug2_init(); #endif #ifdef REF_DEBUG2 ship_debug_initref(); #endif if (!(config = processor_config_new()) || !(config2 = processor_config_new())) { USER_ERROR("Error loading application\n"); goto err; } if (processor_config_load_defaults(config2)) { USER_ERROR("Error loading default configurations\n"); goto err; } processor_config_get_string(config2, P2PSHIP_CONF_CONF_FILE, &conf_file); opterr = 0; while ((c = getopt_long(argc, argv, "LqvhDVs:c:p:i:Rr:kKt", long_options, &index)) != -1) { if (!c) { if (!strcmp(long_options[index].name, "threads")) { processor_config_set_int(config, P2PSHIP_CONF_WORKER_THREADS, atoi(optarg)); } else if (!strcmp(long_options[index].name, "help")) { c = '?'; } else if (!strcmp(long_options[index].name, "version")) { c = 'V'; } else if (!strcmp(long_options[index].name, "verbose")) { c = 'v'; } else if (!strcmp(long_options[index].name, "iface")) { c = 'i'; } else if (!strcmp(long_options[index].name, "console")) { console_only = 1; #ifdef CONFIG_PYTHON_ENABLED } else if (!strcmp(long_options[index].name, "shell")) { processor_config_set_true(config, P2PSHIP_CONF_START_SHELL); } else if (!strcmp(long_options[index].name, "run")) { processor_config_set_string(config, P2PSHIP_CONF_RUN_SCRIPT, optarg); } else if (!strcmp(long_options[index].name, "no-scripts")) { processor_config_set_false(config, P2PSHIP_CONF_STARTUP_SCRIPTS); #endif #ifdef CONFIG_SIP_ENABLED } else if (!strcmp(long_options[index].name, "proxy-iface")) { processor_config_set_string(config, P2PSHIP_CONF_SIPP_PROXY_IFACES, optarg); } else if (!strcmp(long_options[index].name, "no-mp")) { processor_config_set_string(config, P2PSHIP_CONF_SIPP_MEDIA_PROXY, "no"); } else if (!strcmp(long_options[index].name, "tunnel-mp")) { processor_config_set_string(config, P2PSHIP_CONF_SIPP_TUNNEL_PROXY, "yes"); } else if (!strcmp(long_options[index].name, "force-mp")) { processor_config_set_string(config, P2PSHIP_CONF_SIPP_FORCE_PROXY, "yes"); } else if (!strcmp(long_options[index].name, "allow-unknown")) { processor_config_set_string(config, P2PSHIP_CONF_IDENT_ALLOW_UNKNOWN_REGISTRATIONS, "yes"); } else if (!strcmp(long_options[index].name, "allow-untrusted")) { processor_config_set_string(config, P2PSHIP_CONF_IDENT_ALLOW_UNTRUSTED, "yes"); #endif } else if (!action && !strcmp(long_options[index].name, "list-ca")) { action = ACTION_LIST_CA; } else if (!action && !strcmp(long_options[index].name, "remove-ca")) { action = ACTION_REMOVE_CA; if (!action_param) action_param = strdup(optarg); } else if (!action && !strcmp(long_options[index].name, "import-ca")) { action = ACTION_IMPORT_CA; if (!action_param) action_param = strdup(optarg); } else if (!action && !strcmp(long_options[index].name, "list")) { action = ACTION_LIST; } else if (!action && !strcmp(long_options[index].name, "remove")) { action = ACTION_REMOVE; if (!action_param) action_param = strdup(optarg); } else if (!action && !strcmp(long_options[index].name, "import")) { action = ACTION_IMPORT; if (!action_param) action_param = strdup(optarg); } else if (!strcmp(long_options[index].name, "idents")) { processor_config_set_string(config, P2PSHIP_CONF_IDENTS_FILE, optarg); } else if (!strcmp(long_options[index].name, "log")) { processor_config_set_string(config, P2PSHIP_CONF_LOG_FILE, optarg); #ifdef CONFIG_BROADCAST_ENABLED } else if (!strcmp(long_options[index].name, "bc-addr")) { processor_config_set_string(config, P2PSHIP_CONF_BC_ADDR, optarg); #endif #ifdef CONFIG_OPENDHT_ENABLED } else if (!strcmp(long_options[index].name, "opendht")) { processor_config_set_string(config, P2PSHIP_CONF_OPENDHT_PROXY, optarg); #endif #ifdef CONFIG_HIP_ENABLED } else if (!strcmp(long_options[index].name, "list-hits")) { action = ACTION_LIST_HITS; } else if (!strcmp(long_options[index].name, "rvs")) { processor_config_set_string(config, P2PSHIP_CONF_RVS, optarg); } else if (!strcmp(long_options[index].name, "provide-rvs")) { processor_config_set_string(config, P2PSHIP_CONF_PROVIDE_RVS, "yes"); #endif #ifdef CONFIG_WEBCONF_ENABLED } else if (!strcmp(long_options[index].name, "webconf")) { processor_config_set_string(config, P2PSHIP_CONF_WEBCONF_SS, optarg); #endif } else { c = '?'; } } switch (c) { case 0: /* already processed */ break; case 'v': if (p2pship_log_level > -1) p2pship_log_level++; break; case 'q': flag_quiet = 1; p2pship_log_level = -1; break; case 'D': log_to_file = 1; processor_config_set_string(config, P2PSHIP_CONF_DAEMON, "yes"); break; case 'c': conf_file = optarg; processor_config_set_string(config, P2PSHIP_CONF_CONF_FILE, conf_file); break; case 'i': processor_config_set_string(config, P2PSHIP_CONF_IFACES, optarg); break; #ifdef CONFIG_SIP_ENABLED case 'p': if (sscanf(optarg, "%u", &tmp) != 1) { USER_ERROR("Invalid port %s\n", optarg); return 1; } else { processor_config_set_int(config, P2PSHIP_CONF_SIPP_PROXY_PORT, tmp); } break; #endif case 's': if (sscanf(optarg, "%u", &tmp) != 1) { USER_ERROR("Invalid port %s\n", optarg); return 1; } else { processor_config_set_int(config, P2PSHIP_CONF_SHIP_PORT, tmp); } break; case 'V': print_version(); return 0; case 'R': load_autoreg = 1; break; case 'r': processor_config_set_string(config, P2PSHIP_CONF_AUTOREG_FILE, optarg); break; case 'L': log_to_file = 1; break; case 'k': processor_kill_existing_pid(); break; case 'K': processor_kill_all_existing(); break; case 't': do_test = 1; break; case 'h': case '?': default: print_version(); print_usage(); return 1; } } if (!flag_quiet) print_version(); /* 1. load the defaults (done already), 2. load the conf file, 3. put on the manual overrides */ /* ensure that we have a config file! */ if (ship_ensure_file(conf_file, "# Autocreated\n\n") || processor_config_load(config2, conf_file)) { USER_ERROR("Error processing config file %s\n", conf_file); goto err; } if (processor_config_transfer(config2, config)) { USER_ERROR("Error processing configs\n"); goto err; } /* transfer */ processor_config_free(config); config = config2; config2 = NULL; /* ok, ready to rock! */ processor_config_ensure_configs(config); if (log_to_file) { p2pship_log_file = processor_config_string(config, P2PSHIP_CONF_LOG_FILE); } #ifdef CONFIG_START_GLIB_MAIN_LOOP if (!g_thread_supported()) g_thread_init(NULL); #endif #ifdef CONFIG_START_GTK gdk_threads_init(); gdk_threads_enter(); gtk_init(&argc, &argv); #endif #ifdef CALL_DEBUG calldebug_init(); #endif /* mark starttime for uptime calcs */ time(&p2pship_start); /* register each modules */ ASSERT_ZERO(processor_init(config), err); #ifdef CONFIG_HIP_ENABLED hipapi_register(); #endif #ifdef CONFIG_SIP_ENABLED sipp_register(); #endif ident_addr_register(); ident_register(); #ifdef CONFIG_WEBCONF_ENABLED webconf_register(); #endif #ifdef CONFIG_EXTAPI_ENABLED extapi_register(); #endif #ifdef CONFIG_WEBCACHE_ENABLED webcache_register(); #endif resourceman_register(); olclient_register(); conn_register(); netio_register(); netio_events_register(); netio_ff_register(); netio_man_register(); netio_http_register(); ui_register(); ui_stdin_register(); processor_init_module("ui_stdin", config); #ifdef CONFIG_DBUS_ENABLED dbus_register(); #endif if (!console_only) { #ifdef CONFIG_GTKUI_ENABLED ui_gtk_register(); processor_init_module("ui_gtk", config); #endif } addrbook_register(); #ifdef CONFIG_PYTHON_ENABLED pymod_register(); #endif #ifdef CONFIG_MEDIA_ENABLED media_register(); #endif /* check what we actually should do */ switch (action) { case ACTION_LIST_CA: { /* list ca */ if (processor_init_module("ident", config)) { USER_ERROR("Error initializing system\n"); } else { ident_data_print_cas(ident_get_cas()); } break; } case ACTION_REMOVE_CA: { /* remove ca */ if (processor_init_module("ident", config)) { USER_ERROR("Error initializing system\n"); } else { ident_remove_ca(action_param); } break; } case ACTION_LIST: { /* list */ if (processor_init_module("ident", config)) { USER_ERROR("Error initializing system\n"); } else { ident_data_print_idents(ident_get_identities()); } break; } case ACTION_REMOVE: { /* remove */ if (processor_init_module("ident", config)) { USER_ERROR("Error initializing system\n"); } else { ident_remove_ident(action_param); } break; } case ACTION_IMPORT: /* import */ case ACTION_IMPORT_CA: { /* import ca */ if (processor_init_module("ident", config)) { USER_ERROR("Error initializing system\n"); } else { if (ident_import_file(action_param, 1)) { USER_ERROR("Error loading processing file %s\n", action_param); } } break; } #ifdef CONFIG_HIP_ENABLED case ACTION_LIST_HITS: { if (processor_init_module("hipapi", config)) { USER_ERROR("Error initializing system\n"); } else { hipapi_list_hits(); } break; } #endif case ACTION_NONE: default: { struct rlimit rl; int result; #ifdef CONFIG_PYTHON_ENABLED if (processor_config_is_true(config, P2PSHIP_CONF_START_SHELL)) processor_config_set_false(config, P2PSHIP_CONF_DAEMON); #endif /* go daemon (..whee!) */ if (processor_config_is_true(config, P2PSHIP_CONF_DAEMON)) { if (fork()) goto err; } /* check the stack size */ if (!(result = getrlimit(RLIMIT_STACK, &rl))) { const rlim_t stacksize = 32L * 1024L * 1024L; if (rl.rlim_cur < stacksize) { LOG_INFO("increasing stack size to %d\n", stacksize); rl.rlim_cur = stacksize; if (setrlimit(RLIMIT_STACK, &rl)) { LOG_ERROR("could not set new stack size!\n"); } } } else { LOG_ERROR("error checking stack size\n"); } if (processor_init_modules(config) || (load_autoreg && ident_autoreg_load())) { USER_ERROR("Error initializing system\n"); } else { #ifdef REMOTE_DEBUG ship_remote_debug_init(config); #endif /* start the main loop, blocks */ #ifdef REF_DEBUG2 // processor_tasks_add_periodic(ship_debug_reportref, 10000); #endif if (do_test) { LOG_DEBUG("Run tests..\n"); } processor_run(); } } } ret = 0; err: #ifdef REF_DEBUG2 ship_debug_reportref(); #endif processor_close(); freez(action_param); processor_config_free(config); processor_config_free(config2); #ifdef LOCK_DEBUG debug2_close(); #endif #ifdef REF_DEBUG2 ship_debug_closeref(); #endif xmlCleanupThreads(); if (!ret && !flag_quiet) USER_ERROR("ending ok\n"); processor_cleanup_pid(); return ret; }