bool IOEventLoop::EnsureInit() { if (ebase_ == nullptr) { event_config* cfg = event_config_new(); if (cfg != nullptr) { if (use_precise_timer_) { event_config_set_flag(cfg, EVENT_BASE_FLAG_PRECISE_TIMER); } if (event_config_avoid_method(cfg, "epoll") != 0) { LOG(ERROR) << "event_config_avoid_method"; return false; } ebase_ = event_base_new_with_config(cfg); // perf event files support reporting available data via poll methods. However, it doesn't // work well with epoll. Because perf_poll() in kernel/events/core.c uses a report and reset // way to report poll events. If perf_poll() is called twice, it may return POLLIN for the // first time, and no events for the second time. And epoll may call perf_poll() more than // once to confirm events. A failed situation is below: // When profiling SimpleperfExampleOfKotlin on Pixel device with `-g --duration 10`, the // kernel fills up the buffer before we call epoll_ctl(EPOLL_CTL_ADD). Then the POLLIN event // is returned when calling epoll_ctl(), while no events are returned when calling // epoll_wait(). As a result, simpleperf doesn't receive any poll wakeup events. if (strcmp(event_base_get_method(ebase_), "poll") != 0) { LOG(ERROR) << "event_base_get_method isn't poll: " << event_base_get_method(ebase_); return false; } event_config_free(cfg); } if (ebase_ == nullptr) { LOG(ERROR) << "failed to create event_base"; return false; } } return true; }
static int luaevent_method(lua_State* L) { #ifdef _EVENT_VERSION le_base *base = event_base_get(L, 1); if(strcmp(_EVENT_VERSION, "1.3")<0) lua_pushstring(L, event_base_get_method(base->base)); else #endif lua_pushstring(L, event_base_get_method(base->base)); return 1; }
static void test_edgetriggered(void *et) { struct event *ev = NULL; struct event_base *base = NULL; const char *test = "test string"; evutil_socket_t pair[2] = {-1,-1}; int supports_et; if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair) == -1) { tt_abort_perror("socketpair"); } called = was_et = 0; send(pair[0], test, (int)strlen(test)+1, 0); shutdown(pair[0], SHUT_WR); /* Initalize the event library */ base = event_base_new(); if (!strcmp(event_base_get_method(base), "epoll") || !strcmp(event_base_get_method(base), "epoll (with changelist)") || !strcmp(event_base_get_method(base), "kqueue")) supports_et = 1; else supports_et = 0; TT_BLATHER(("Checking for edge-triggered events with %s, which should %s" "support edge-triggering", event_base_get_method(base), supports_et?"":"not ")); /* Initalize one event */ ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST, read_cb, &ev); event_add(ev, NULL); /* We're going to call the dispatch function twice. The first invocation * will read a single byte from pair[1] in either case. If we're edge * triggered, we'll only see the event once (since we only see transitions * from no data to data), so the second invocation of event_base_loop will * do nothing. If we're level triggered, the second invocation of * event_base_loop will also activate the event (because there's still * data to read). */ event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE); event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE); if (supports_et) { tt_int_op(called, ==, 1); tt_assert(was_et); } else {
CIOServer::CIOServer() : m_evbase(event_base_new()) , m_status(status_null) , m_binit(false) , m_id(gen_ioserverid()) , m_curpost( 0 ) , m_haspostdata( false ) { //static CNetInit netinit; if ( !m_evbase ) { XH_LOG_ERROR(::xhnet::logname_base, "Create EventBase Failed, Check OS config"); } else { XH_LOG_DEBUG(::xhnet::logname_base, "Using LibEvent with backend method " << event_base_get_method(m_evbase)); // 设置m_evbase的优先级范围0-10 //event_base_priority_init(m_evbase, 10); //event_priority_set 设置event的优先级 //event_base_get_npriorities // 2.1.1 #ifdef _DEBUG int f = event_base_get_features(m_evbase); if ((f & EV_FEATURE_ET)) XH_LOG_DEBUG(::xhnet::logname_base, " Edge-triggered events are supported."); if ((f & EV_FEATURE_O1)) XH_LOG_DEBUG(::xhnet::logname_base, " O(1) event notification is supported."); if ((f & EV_FEATURE_FDS)) XH_LOG_DEBUG(::xhnet::logname_base, " All FD types are supported."); #endif } }
int setupListeners() { evthread_use_pthreads(); evBase = event_base_new(); const char **methods = event_get_supported_methods(); lprintf("Starting Libevent %s. Supported methods are:\n", event_get_version()); for (int i=0;methods[i] != NULL; i++){ lprintf("\t%s\n",methods[i]); } free((char**)methods); lprintf("Using %s.\n",event_base_get_method(evBase)); struct event *signalEvent; signalEvent = evsignal_new(evBase, SIGINT, signalCallback, (void*) evBase); if (!signalEvent || event_add(signalEvent, NULL) < 0){ lprintf("Could not create / add a signal event!\n"); return -1; } lprintf("done\n"); try{ contConnection = new ControllerLink(); mtc = new MTCModel(); for (int i=0;i<MAX_XL3_CON;i++){ xl3s[i] = new XL3Model(i); } tubii = new TUBIIModel(); } catch(int e){ return -1; } return 0; }
void print_features (struct event_base *base) { const char *method=event_base_get_method(base); printf ("--- method <%s> ---\n", method); int cnt = event_base_get_features(base); if (cnt & EV_FEATURE_ET) { /* see man for more information about edge-triggered and level-triggered */ printf("support edge-triggered events with EV_ET\n"); } if(cnt & EV_FEATURE_O1) { /* poll and select is O(n) */ printf("having one event triggered among many is an O(1) operation.\n"); } if(cnt & EV_FEATURE_FDS) { /* epoll not support it */ printf("allows file descriptors as well as sockets. \n"); } if (cnt & EV_FEATURE_EARLY_CLOSE) { /* detect connection close without the necessity of reading all the pending data. */ printf ("EV_FEATURE_EARLY_CLOSE\n"); } printf ("that is all\n\n"); }
void CInitBase::Init(void) { m_pCfg = event_config_new(); if (NULL == m_pCfg) { Q_EXCEPTION(Q_RTN_FAILE, "%s", "event_config_new error."); } #ifdef Q_IOCP evthread_use_windows_threads(); event_config_set_flag(m_pCfg, EVENT_BASE_FLAG_STARTUP_IOCP); #endif m_pBase = event_base_new_with_config(m_pCfg); if (NULL == m_pBase) { Q_EXCEPTION(Q_RTN_FAILE, "%s", "event_base_new error."); } #ifdef Q_IOCP Q_Printf("event version %s, using %s", event_get_version(), "IOCP"); #else Q_Printf("event version %s, using %s", event_get_version(), event_base_get_method(m_pBase)); #endif }
EventBase::EventBase(const char* method) { // enable locking for libevent structure if (evthread_use_pthreads()) { throw std::exception(); } #ifdef DEBUG evthread_enable_lock_debuging(); event_enable_debug_mode(); #endif struct event_config *config; config = event_config_new(); int i = 0; const char** availMethods = event_get_supported_methods(); for (i = 0; availMethods[i] != NULL; i++) { if (strcmp(availMethods[i], method)) { event_config_avoid_method(config, availMethods[i]); } } base_ = event_base_new_with_config(config); if (!base_) { throw BadBaseException(); } event_base_get_method(base_); event_config_free(config); }
/** get the event system in use */ static void get_event_sys(const char** n, const char** s, const char** m) { #ifdef USE_WINSOCK *n = "event"; *s = "winsock"; *m = "WSAWaitForMultipleEvents"; #elif defined(USE_MINI_EVENT) *n = "mini-event"; *s = "internal"; *m = "select"; #else struct event_base* b; *s = event_get_version(); # ifdef HAVE_EVENT_BASE_GET_METHOD *n = "libevent"; b = event_base_new(); *m = event_base_get_method(b); # elif defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP) *n = "libev"; b = (struct event_base*)ev_default_loop(EVFLAG_AUTO); *m = ev_backend2str(ev_backend((struct ev_loop*)b)); # else *n = "unknown"; *m = "not obtainable"; b = NULL; # endif # ifdef HAVE_EVENT_BASE_FREE event_base_free(b); # endif #endif }
static void levent_init(struct lldpd *cfg) { /* Setup libevent */ log_debug("event", "initialize libevent"); event_set_log_callback(levent_log_cb); if (!(cfg->g_base = event_base_new())) fatalx("unable to create a new libevent base"); log_info("event", "libevent %s initialized with %s method", event_get_version(), event_base_get_method(cfg->g_base)); /* Setup SNMP */ #ifdef USE_SNMP if (cfg->g_snmp) { agent_init(cfg, cfg->g_snmp_agentx); cfg->g_snmp_timeout = evtimer_new(cfg->g_base, levent_snmp_timeout, cfg); if (!cfg->g_snmp_timeout) fatalx("unable to setup timeout function for SNMP"); if ((cfg->g_snmp_fds = malloc(sizeof(struct ev_l))) == NULL) fatalx("unable to allocate memory for SNMP events"); TAILQ_INIT(levent_snmp_fds(cfg)); } #endif /* Setup loop that will run every X seconds. */ log_debug("event", "register loop timer"); if (!(cfg->g_main_loop = event_new(cfg->g_base, -1, 0, levent_update_and_send, cfg))) fatalx("unable to setup main timer"); event_active(cfg->g_main_loop, EV_TIMEOUT, 1); /* Setup unix socket */ log_debug("event", "register Unix socket"); TAILQ_INIT(&lldpd_clients); evutil_make_socket_nonblocking(cfg->g_ctl); if ((cfg->g_ctl_event = event_new(cfg->g_base, cfg->g_ctl, EV_READ|EV_PERSIST, levent_ctl_accept, cfg)) == NULL) fatalx("unable to setup control socket event"); event_add(cfg->g_ctl_event, NULL); /* Signals */ log_debug("event", "register signals"); signal(SIGHUP, SIG_IGN); evsignal_add(evsignal_new(cfg->g_base, SIGUSR1, levent_dump, cfg->g_base), NULL); evsignal_add(evsignal_new(cfg->g_base, SIGINT, levent_stop, cfg->g_base), NULL); evsignal_add(evsignal_new(cfg->g_base, SIGTERM, levent_stop, cfg->g_base), NULL); }
Bool HawkGateThread::Init(UInt32 iBaseId) { HawkAssert(!m_pThread && !m_pBase && !m_pZmq); m_iBaseSid = iBaseId; //创建通用缓冲 if (!m_pOctets) m_pOctets = new OctetsStream(m_pGateway->GetBufSize()); //创建线程 if (!m_pThread) m_pThread = new HawkThread(hawk_GateThreadRoutine); //创建事件基础对象 if (!m_pBase) { event_config* pCfg = event_config_new(); if (!pCfg) { HawkPrint("Create EventConfig Failed."); return false; } #ifdef PLATFORM_LINUX event_config_require_features(pCfg, EV_FEATURE_ET); #endif event_config_set_flag(pCfg, EVENT_BASE_FLAG_NOLOCK); m_pBase = (void*)event_base_new_with_config(pCfg); event_config_free(pCfg); if (!m_pBase) { HawkPrint("Create EventBase Failed."); return false; } if (m_iBaseSid == 1) { const Char* pszMethod = event_base_get_method((event_base*)m_pBase); if (pszMethod && strlen(pszMethod)) { HawkFmtPrint("Kernel Event Notification Mechanism: %s", pszMethod); } } } //创建ZMQ对象 if (!m_pZmq) { m_pZmq = P_ZmqManager->CreateZmq(HawkZmq::HZMQ_DEALER); m_pZmq->SetIdentity(&m_iBaseSid, sizeof(m_iBaseSid)); m_pZmq->Connect(m_pGateway->GetThreadZmqAddr()); } return true; }
/** Return the name of the Libevent backend we're using. */ const char * tor_libevent_get_method(void) { #ifdef HAVE_EVENT2_EVENT_H return event_base_get_method(the_event_base); #elif defined(HAVE_EVENT_GET_METHOD) return event_get_method(); #else return "<unknown>"; #endif }
int main(int argc, char **argv) { struct event_base *base; struct evconnlistener *listener; struct event *signal_event; struct sockaddr_in sin; #ifdef WIN32 WSADATA wsa_data; WSAStartup(0x0201, &wsa_data); #endif base = event_base_new(); if (!base) { fprintf(stderr, "Could not initialize libevent!\n"); return 1; } const char* method = event_base_get_method(base); printf("method:%s\n",method); memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(PORT); listener = evconnlistener_new_bind(base, listener_cb, (void *)base, LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1, (struct sockaddr*)&sin, sizeof(sin)); if (!listener) { fprintf(stderr, "Could not create a listener!\n"); return 1; } signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base); if (!signal_event || event_add(signal_event, NULL)<0) { fprintf(stderr, "Could not create/add a signal event!\n"); return 1; } event_base_dispatch(base); evconnlistener_free(listener); event_free(signal_event); event_base_free(base); printf("done\n"); return 0; }
static int init_server(dtls_listener_relay_server_type* server, const char* ifname, const char *local_address, int port, int verbose, ioa_engine_handle e, turn_turnserver *ts) { if(!server) return -1; server->dtls_ctx = e->dtls_ctx; server->ts = ts; server->children_ss = ur_addr_map_create(65535); if(ifname) STRCPY(server->ifname,ifname); if(make_ioa_addr((const u08bits*)local_address, port, &server->addr)<0) { TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR,"Cannot create a UDP/DTLS listener for address: %s\n",local_address); return -1; } server->slen0 = get_ioa_addr_len(&(server->addr)); server->verbose=verbose; server->e = e; TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO,"IO method: %s\n",event_base_get_method(server->e->event_base)); if(server->dtls_ctx) { #if defined(REQUEST_CLIENT_CERT) /* If client has to authenticate, then */ SSL_CTX_set_verify(server->dtls_ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, dtls_verify_callback); #endif SSL_CTX_set_read_ahead(server->dtls_ctx, 1); #if !defined(TURN_NO_DTLS) SSL_CTX_set_cookie_generate_cb(server->dtls_ctx, generate_cookie); SSL_CTX_set_cookie_verify_cb(server->dtls_ctx, verify_cookie); #endif } return create_server_socket(server); }
void ub_get_event_sys(struct ub_event_base* base, const char** n, const char** s, const char** m) { #ifdef USE_WINSOCK (void)base; *n = "event"; *s = "winsock"; *m = "WSAWaitForMultipleEvents"; #elif defined(USE_MINI_EVENT) (void)base; *n = "mini-event"; *s = "internal"; *m = "select"; #else struct event_base* b = AS_EVENT_BASE(base); *s = event_get_version(); # if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP) *n = "libev"; if (!b) b = (struct event_base*)ev_default_loop(EVFLAG_AUTO); # ifdef EVBACKEND_SELECT *m = ub_ev_backend2str(ev_backend((struct ev_loop*)b)); # else *m = "not obtainable"; # endif # elif defined(HAVE_EVENT_BASE_GET_METHOD) *n = "libevent"; if (!b) b = event_base_new(); *m = event_base_get_method(b); # else *n = "unknown"; *m = "not obtainable"; (void)b; # endif # ifdef HAVE_EVENT_BASE_FREE if (b && b != AS_EVENT_BASE(base)) event_base_free(b); # endif #endif }
void feature_test () { /* supported async */ const char ** methods = event_get_supported_methods(); if (methods) { int i = 0; while(methods[i]) { printf ("method %d : <%s>\n", i+1, methods[i]); i++; } } /* get the default async method */ base = event_base_new(); const char * bb = event_base_get_method(base); printf("default method is <%s>\n", bb); /* epoll */ print_features(base); event_base_free(base); }
void TNonblockingIOThread::run() { threadId_ = Thread::get_current(); assert(eventBase_ == 0); eventBase_ = event_base_new(); // Print some libevent stats if (number_ == 0) { GlobalOutput.printf("TNonblockingServer: using libevent %s method %s", event_get_version(), event_base_get_method(eventBase_)); } registerEvents(); GlobalOutput.printf("TNonblockingServer: IO thread #%d entering loop...", number_); if (useHighPriority_) { setCurrentThreadHighPriority(true); } // Run libevent engine, never returns, invokes calls to eventHandler event_base_loop(eventBase_, 0); if (useHighPriority_) { setCurrentThreadHighPriority(false); } // cleans up our registered events cleanupEvents(); GlobalOutput.printf("TNonblockingServer: IO thread #%d run() done!", number_); }
/*{{{ main */ int main (int argc, char *argv[]) { Application *app; gboolean verbose = FALSE; gboolean version = FALSE; GError *error = NULL; GOptionContext *context; gchar **s_params = NULL; gchar **s_config = NULL; gboolean foreground = FALSE; gchar conf_str[1023]; struct stat st; gchar **cache_dir = NULL; gchar **s_fuse_opts = NULL; gchar **s_log_file = NULL; guint32 part_size = 0; gboolean disable_syslog = FALSE; gboolean disable_stats = FALSE; gboolean force_head_requests = FALSE; gint uid = -1; gint gid = -1; gint fmode = -1; gint dmode = -1; struct event_config *ev_config; srand (time (NULL)); app = g_new0 (Application, 1); app->conf_path = g_build_filename (SYSCONFDIR, "riofs.conf.xml", NULL); g_snprintf (conf_str, sizeof (conf_str), "Path to configuration file. Default: %s", app->conf_path); GOptionEntry entries[] = { { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &s_params, NULL, NULL }, { "config", 'c', 0, G_OPTION_ARG_FILENAME_ARRAY, &s_config, conf_str, NULL}, { "uid", 0, 0, G_OPTION_ARG_INT, &uid, "Set UID of filesystem owner.", NULL }, { "gid", 0, 0, G_OPTION_ARG_INT, &gid, "Set GID of filesystem owner.", NULL }, { "fmode", 0, 0, G_OPTION_ARG_INT, &fmode, "Set mode for all files.", NULL }, { "dmode", 0, 0, G_OPTION_ARG_INT, &dmode, "Set mode for all directories.", NULL }, { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground, "Flag. Do not daemonize process.", NULL }, { "cache-dir", 0, 0, G_OPTION_ARG_STRING_ARRAY, &cache_dir, "Set cache directory.", NULL }, { "fuse-options", 'o', 0, G_OPTION_ARG_STRING_ARRAY, &s_fuse_opts, "Fuse options.", "\"opt[,opt...]\"" }, { "disable-syslog", 0, 0, G_OPTION_ARG_NONE, &disable_syslog, "Flag. Disable logging to syslog.", NULL }, { "disable-stats", 0, 0, G_OPTION_ARG_NONE, &disable_stats, "Flag. Disable Statistics HTTP interface.", NULL }, { "part-size", 0, 0, G_OPTION_ARG_INT, &part_size, "Set file part size (in bytes).", NULL }, { "log-file", 'l', 0, G_OPTION_ARG_STRING_ARRAY, &s_log_file, "File to write output.", NULL }, { "force-head-requests", 0, 0, G_OPTION_ARG_NONE, &force_head_requests, "Flag. Send HEAD request for each file.", NULL }, { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Verbose output.", NULL }, { "version", 'V', 0, G_OPTION_ARG_NONE, &version, "Show application version and exit.", NULL }, { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL } }; // init 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 (); #ifdef SSL_ENABLED SSL_load_error_strings (); SSL_library_init (); #endif g_random_set_seed (time (NULL)); // init main app structure ev_config = event_config_new (); #if defined(__APPLE__) // method select is the preferred method on OS X. kqueue and poll are not supported. event_config_avoid_method (ev_config, "kqueue"); event_config_avoid_method (ev_config, "poll"); #endif app->evbase = event_base_new_with_config (ev_config); event_config_free (ev_config); if (!app->evbase) { LOG_err (APP_LOG, "Failed to create event base !"); application_destroy (app); return -1; } app->dns_base = evdns_base_new (app->evbase, 1); if (!app->dns_base) { LOG_err (APP_LOG, "Failed to create DNS base !"); application_destroy (app); return -1; } app->f_log = NULL; app->log_file_name = NULL; /*{{{ cmd line args */ // parse command line options context = g_option_context_new ("[bucketname] [mountpoint]"); g_option_context_add_main_entries (context, entries, NULL); g_option_context_set_description (context, "Please set both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables!"); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_fprintf (stderr, "Failed to parse command line options: %s\n", error->message); application_destroy (app); g_option_context_free (context); return -1; } g_option_context_free (context); // check if --version is specified if (version) { g_fprintf (stdout, "RioFS File System v%s\n", VERSION); g_fprintf (stdout, "Copyright (C) 2012-2014 Paul Ionkin <*****@*****.**>\n"); g_fprintf (stdout, "Copyright (C) 2012-2014 Skoobe GmbH. All rights reserved.\n"); g_fprintf (stdout, "Libraries:\n"); g_fprintf (stdout, " GLib: %d.%d.%d libevent: %s fuse: %d.%d", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION, LIBEVENT_VERSION, FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION ); #if defined(__APPLE__) || defined(__FreeBSD__) || !defined(__GLIBC__) g_fprintf (stdout, "\n"); #else g_fprintf (stdout, " glibc: %s\n", gnu_get_libc_version ()); #endif g_fprintf (stdout, "Features:\n"); g_fprintf (stdout, " libevent backend method: %s\n", event_base_get_method(app->evbase)); #ifdef SSL_ENABLED g_fprintf (stdout, " SSL enabled\n"); #endif /* { int i; const char **methods = event_get_supported_methods (); g_fprintf (stdout, " Available libevent backend methods:\n"); for (i = 0; methods[i] != NULL; ++i) { g_fprintf (stdout, " %s\n", methods[i]); } } */ return 0; } if (!s_params || g_strv_length (s_params) != 2) { LOG_err (APP_LOG, "Wrong number of provided arguments!\nTry `%s --help' for more information.", argv[0]); application_destroy (app); return -1; } if (verbose) log_level = LOG_debug; else log_level = LOG_msg; /*}}}*/ /*{{{ parse config file */ // user provided alternative config path if (s_config && g_strv_length (s_config) > 0) { g_free (app->conf_path); app->conf_path = g_strdup (s_config[0]); g_strfreev (s_config); } app->conf = conf_create (); if (access (app->conf_path, R_OK) == 0) { LOG_debug (APP_LOG, "Using config file: %s", app->conf_path); if (!conf_parse_file (app->conf, app->conf_path)) { LOG_err (APP_LOG, "Failed to parse configuration file: %s", app->conf_path); application_destroy (app); return -1; } } else { LOG_err (APP_LOG, "Configuration file is not found !"); application_destroy (app); return -1; } if (!conf_check_keys (app->conf, conf_keys_str, conf_keys_len)) { LOG_err (APP_LOG, "Configuration file is missing keys, please re-check your configuration file: %s", app->conf_path); application_destroy (app); return -1; } if (disable_syslog) { conf_set_boolean (app->conf, "log.use_syslog", FALSE); } // update logging settings logger_set_syslog (conf_get_boolean (app->conf, "log.use_syslog")); logger_set_color (conf_get_boolean (app->conf, "log.use_color")); if (cache_dir && g_strv_length (cache_dir) > 0) { conf_set_string (app->conf, "filesystem.cache_dir", cache_dir[0]); g_strfreev (cache_dir); } if (!verbose) log_level = conf_get_int (app->conf, "log.level"); if (uid >= 0) conf_set_int (app->conf, "filesystem.uid", uid); if (gid >= 0) conf_set_int (app->conf, "filesystem.gid", gid); if (fmode >= 0) conf_set_int (app->conf, "filesystem.file_mode", fmode); if (dmode >= 0) conf_set_int (app->conf, "filesystem.dir_mode", dmode); /*}}}*/ // try to get access parameters from the environment if (getenv ("AWS_ACCESS_KEY_ID")) { conf_set_string (app->conf, "s3.access_key_id", getenv ("AWS_ACCESS_KEY_ID")); // else check if it's set it the config file } else { if (!conf_node_exists (app->conf, "s3.access_key_id")) { LOG_err (APP_LOG, "Environment variables are not set!\nTry `%s --help' for more information.", argv[0]); application_destroy (app); return -1; } } if (getenv ("AWS_SECRET_ACCESS_KEY")) { conf_set_string (app->conf, "s3.secret_access_key", getenv ("AWS_SECRET_ACCESS_KEY")); } else { if (!conf_node_exists (app->conf, "s3.secret_access_key")) { LOG_err (APP_LOG, "Environment variables are not set!\nTry `%s --help' for more information.", argv[0]); application_destroy (app); return -1; } } // check if both strings are set if (!conf_get_string (app->conf, "s3.access_key_id") || !conf_get_string (app->conf, "s3.secret_access_key")) { LOG_err (APP_LOG, "Environment variables are not set!\nTry `%s --help' for more information.", argv[0]); application_destroy (app); return -1; } // foreground is set if (foreground) conf_set_boolean (app->conf, "app.foreground", foreground); if (part_size) conf_set_uint (app->conf, "s3.part_size", part_size); if (disable_stats) conf_set_boolean (app->conf, "statistics.enabled", FALSE); if (force_head_requests) conf_set_boolean (app->conf, "s3.force_head_requests_on_lookup", TRUE); else conf_set_boolean (app->conf, "s3.force_head_requests_on_lookup", FALSE); conf_set_string (app->conf, "s3.bucket_name", s_params[0]); if (!application_set_url (app, conf_get_string (app->conf, "s3.endpoint"))) { application_destroy (app); return -1; } if (s_fuse_opts && g_strv_length (s_fuse_opts) > 0) { app->fuse_opts = g_strdup (s_fuse_opts[0]); g_strfreev (s_fuse_opts); } if (s_log_file && g_strv_length (s_log_file) > 0) { app->log_file_name = g_strdup (s_log_file[0]); app->f_log = fopen (s_log_file[0], "a+"); if (!app->f_log) { LOG_err (APP_LOG, "Failed to open log file: %s Error: %s", s_log_file[0], strerror (errno)); application_destroy (app); return -1; } LOG_debug (APP_LOG, "Using %s for storing application logs.", s_log_file[0]); logger_set_file (app->f_log); g_strfreev (s_log_file); } conf_set_string (app->conf, "app.mountpoint", s_params[1]); // check if directory exists if (stat (conf_get_string (app->conf, "app.mountpoint"), &st) == -1) { LOG_err (APP_LOG, "Mountpoint %s does not exist! Please check directory permissions!", conf_get_string (app->conf, "app.mountpoint")); application_destroy (app); return -1; } // check if it's a directory if (!S_ISDIR (st.st_mode)) { LOG_err (APP_LOG, "Mountpoint %s is not a directory!", conf_get_string (app->conf, "app.mountpoint")); application_destroy (app); return -1; } g_strfreev (s_params); #ifdef SSL_ENABLED app->ssl_ctx = SSL_CTX_new (SSLv23_client_method ()); if (!app->ssl_ctx) { LOG_err (APP_LOG, "Failed to initialize SSL engine !"); application_exit (app); return -1; } SSL_CTX_set_options (app->ssl_ctx, SSL_OP_ALL); #endif #ifdef MAGIC_ENABLED app->magic_ctx = magic_open(MAGIC_MIME_TYPE); if (!app->magic_ctx) { LOG_err(APP_LOG, "Failed to initialize magic library\n"); return -1; } if (magic_load(app->magic_ctx, NULL)) { LOG_err(APP_LOG, "Failed to load magic database: %s\n", magic_error(app->magic_ctx)); magic_close(app->magic_ctx); return -1; } #endif app->stat_srv = stat_srv_create (app); if (!app->stat_srv) { application_exit (app); return -1; } // perform the initial request to get bucket ACL (handles redirect as well) app->service_con = http_connection_create (app); if (!app->service_con) { application_destroy (app); return -1; } bucket_client_get (app->service_con, "/?acl", application_on_bucket_acl_cb, app); // start the loop event_base_dispatch (app->evbase); application_destroy (app); return 0; }
int main(int argc, char **argv) { struct event_config *cfg = event_config_new(); struct event_base *base; struct evhttp *http; int i; int c; int use_iocp = 0; unsigned short port = 8080; char *endptr = NULL; #ifdef _WIN32 WSADATA WSAData; WSAStartup(0x101, &WSAData); #else if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) return (1); #endif for (i = 1; i < argc; ++i) { if (*argv[i] != '-') continue; c = argv[i][1]; if ((c == 'p' || c == 'l') && i + 1 >= argc) { fprintf(stderr, "-%c requires argument.\n", c); exit(1); } switch (c) { case 'p': if (i+1 >= argc || !argv[i+1]) { fprintf(stderr, "Missing port\n"); exit(1); } port = (int)strtol(argv[i+1], &endptr, 10); if (*endptr != '\0') { fprintf(stderr, "Bad port\n"); exit(1); } break; case 'l': if (i+1 >= argc || !argv[i+1]) { fprintf(stderr, "Missing content length\n"); exit(1); } content_len = (size_t)strtol(argv[i+1], &endptr, 10); if (*endptr != '\0' || content_len == 0) { fprintf(stderr, "Bad content length\n"); exit(1); } break; #ifdef _WIN32 case 'i': use_iocp = 1; evthread_use_windows_threads(); event_config_set_flag(cfg,EVENT_BASE_FLAG_STARTUP_IOCP); break; #endif default: fprintf(stderr, "Illegal argument \"%c\"\n", c); exit(1); } } base = event_base_new_with_config(cfg); if (!base) { fprintf(stderr, "creating event_base failed. Exiting.\n"); return 1; } http = evhttp_new(base); content = malloc(content_len); if (content == NULL) { fprintf(stderr, "Cannot allocate content\n"); exit(1); } else { int i = 0; for (i = 0; i < (int)content_len; ++i) content[i] = (i & 255); } evhttp_set_cb(http, "/ind", http_basic_cb, NULL); fprintf(stderr, "/ind - basic content (memory copy)\n"); evhttp_set_cb(http, "/ref", http_ref_cb, NULL); fprintf(stderr, "/ref - basic content (reference)\n"); fprintf(stderr, "Serving %d bytes on port %d using %s\n", (int)content_len, port, use_iocp? "IOCP" : event_base_get_method(base)); evhttp_bind_socket(http, "0.0.0.0", port); #ifdef _WIN32 if (use_iocp) { struct timeval tv={99999999,0}; event_base_loopexit(base, &tv); } #endif event_base_dispatch(base); /* NOTREACHED */ return (0); }
const char* EventBase::getMethod() { return event_base_get_method(base_); }
void TNonblockingIOThread::createNotificationPipe() { if(evutil_socketpair(AF_LOCAL, SOCK_STREAM, 0, notificationPipeFDs_) == -1) { GlobalOutput.perror("TNonblockingServer::createNotificationPipe ", EVUTIL_SOCKET_ERROR()); throw TException("can't create notification pipe"); } if(evutil_make_socket_nonblocking(notificationPipeFDs_[0])<0 || evutil_make_socket_nonblocking(notificationPipeFDs_[1])<0) { ::THRIFT_CLOSESOCKET(notificationPipeFDs_[0]); ::THRIFT_CLOSESOCKET(notificationPipeFDs_[1]); throw TException("TNonblockingServer::createNotificationPipe() THRIFT_O_NONBLOCK"); } for (int i = 0; i < 2; ++i) { #if LIBEVENT_VERSION_NUMBER < 0x02000000 int flags; if ((flags = THRIFT_FCNTL(notificationPipeFDs_[i], F_GETFD, 0)) < 0 || THRIFT_FCNTL(notificationPipeFDs_[i], F_SETFD, flags | FD_CLOEXEC) < 0) { #else if (evutil_make_socket_closeonexec(notificationPipeFDs_[i]) < 0) { #endif ::THRIFT_CLOSESOCKET(notificationPipeFDs_[0]); ::THRIFT_CLOSESOCKET(notificationPipeFDs_[1]); throw TException("TNonblockingServer::createNotificationPipe() " "FD_CLOEXEC"); } } } /** * Register the core libevent events onto the proper base. */ void TNonblockingIOThread::registerEvents() { threadId_ = Thread::get_current(); assert(eventBase_ == 0); eventBase_ = getServer()->getUserEventBase(); if (eventBase_ == NULL) { eventBase_ = event_base_new(); ownEventBase_ = true; } // Print some libevent stats if (number_ == 0) { GlobalOutput.printf("TNonblockingServer: using libevent %s method %s", event_get_version(), event_base_get_method(eventBase_)); } if (listenSocket_ >= 0) { // Register the server event event_set(&serverEvent_, listenSocket_, EV_READ | EV_PERSIST, TNonblockingIOThread::listenHandler, server_); event_base_set(eventBase_, &serverEvent_); // Add the event and start up the server if (-1 == event_add(&serverEvent_, 0)) { throw TException("TNonblockingServer::serve(): " "event_add() failed on server listen event"); } GlobalOutput.printf("TNonblocking: IO thread #%d registered for listen.", number_); } createNotificationPipe(); // Create an event to be notified when a task finishes event_set(¬ificationEvent_, getNotificationRecvFD(), EV_READ | EV_PERSIST, TNonblockingIOThread::notifyHandler, this); // Attach to the base event_base_set(eventBase_, ¬ificationEvent_); // Add the event and start up the server if (-1 == event_add(¬ificationEvent_, 0)) { throw TException("TNonblockingServer::serve(): " "event_add() failed on task-done notification event"); } GlobalOutput.printf("TNonblocking: IO thread #%d registered for notify.", number_); }
int main(int argc, char **argv) { int listen_fd, nZero = 0; struct sockaddr_in listen_addr; int reuseaddr_on = 1; /* The socket accept event. */ struct event ev_accept; struct event_base * evbase = NULL; WSADATA wsaData; int err = WSAStartup( MAKEWORD( 2, 0 ), &wsaData ); if ( err != 0 ) { printf( "Couldn't find a useable winsock.dll.\n" ); return -1; } /* Initialize libevent. */ evbase = event_init(); printf( "method : %s\n", event_base_get_method (evbase) ); /* Create our listening socket. This is largely boiler plate * code that I¡¯ll abstract away in the future. */ listen_fd = socket(AF_INET, SOCK_STREAM, 0); if (listen_fd < 0) event_err(1, "listen failed"); if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&reuseaddr_on, sizeof(reuseaddr_on)) == -1) event_err(1, "setsockopt failed"); memset(&listen_addr, 0, sizeof(listen_addr)); listen_addr.sin_family = AF_INET; listen_addr.sin_addr.s_addr = INADDR_ANY; listen_addr.sin_port = htons(SERVER_PORT); if (bind(listen_fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0) event_err(1, "bind failed"); if (listen(listen_fd, 1024) < 0) event_err(1, "listen failed"); /* Set the socket to non-blocking, this is essential in event * based programming with libevent. */ if (setnonblock(listen_fd) < 0) event_err(1, "failed to set server socket to non-blocking"); // Disable send buffering on the socket. Setting SO_SNDBUF // to 0 causes winsock to stop bufferring sends and perform // sends directly from our buffers, thereby reducing CPU usage. nZero = 0; if (SOCKET_ERROR == setsockopt(listen_fd, SOL_SOCKET, SO_SNDBUF, (char *)&nZero, sizeof(nZero))) { printf("setsockopt(SNDBUF): %d\n", WSAGetLastError()); return -1; } // Disable receive buffering on the socket. Setting SO_RCVBUF // to 0 causes winsock to stop bufferring receive and perform // receives directly from our buffers, thereby reducing CPU usage. nZero = 0; if (SOCKET_ERROR == setsockopt(listen_fd, SOL_SOCKET, SO_RCVBUF, (char *)&nZero, sizeof(nZero))) { printf("setsockopt(SO_RCVBUF): %d\n", WSAGetLastError()); return(FALSE); } /* We now have a listening socket, we create a read event to * be notified when a client connects. */ event_set(&ev_accept, listen_fd, EV_ACCEPT|EV_READ|EV_PERSIST, on_accept, NULL); event_add(&ev_accept, NULL); /* Start the libevent event loop. */ event_dispatch(); return 0; }
int main(int argc, char*argv[]) { printf("Hello, world!\n"); event_config *conf = event_config_new(); #ifdef WIN32 WORD wVersionRequested; WSADATA wsaData; int err; /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { /* Tell the user that we could not find a usable */ /* Winsock DLL. */ printf("WSAStartup failed with error: %d\n", err); return 1; } evthread_use_windows_threads(); event_config_set_flag(conf, EVENT_BASE_FLAG_STARTUP_IOCP); #endif base = event_base_new_with_config(conf); const char ** methods = event_get_supported_methods(); int loop; std::cout << "Version: " << event_get_version() << std::endl; std::cout << "Method: " << event_base_get_method(base) << std::endl; std::cout << "Features: 0x" << std::hex << event_base_get_features(base) << std::endl; std::cout << "Base: " << base << std::endl; while (*methods) { std::cout << "Method: " << *methods++ << std::endl; } event_set_log_callback(_log_cb); /* The caller has already set up fd1, fd2 somehow, and make them nonblocking. */ if (0) { evutil_socket_t fd1 = 1; evutil_socket_t fd2 = 1; struct timeval five_seconds = { 5, 0 }; struct event *ev1 = event_new(base, fd1, EV_TIMEOUT | EV_READ/*|EV_PERSIST*/, cb_func, (char*) "Reading event"); struct event *ev2 = event_new(base, fd2, EV_WRITE/*|EV_PERSIST*/, cb_func, (char*) "Writing event"); event_add(ev1, &five_seconds); event_add(ev2, NULL); std::cout << "\nEntering loop" << std::endl; loop = event_base_loop(base, 0); std::cout << "Exiting loop: " << loop << std::endl; } // http server evhttp *ev_http = evhttp_new(base); int http_port = 9090; // evhttp_bind_socket expects its PORT param in host byte order. Sigh. int r = evhttp_bind_socket(ev_http, "0.0.0.0", http_port); // This return value is undocumented (!), but this seems to work. if (r == -1) { std::cerr << "could not open port " << http_port << std::endl; return 3; } evhttp_set_gencb(ev_http, http_handle_generic, 0); //evhttp_set_cb(ev_http, "/", http_handle_root); std::cout << "\nEntering loop" << std::endl; loop = event_base_loop(base, 0); std::cout << "Exiting loop: " << loop << std::endl; evhttp_free(ev_http); event_base_free(base); event_config_free(conf); return 0; }
int main(int argc, char * * argv) { struct sigaction sa; upnpc_t upnp; char * multicast_if = NULL; if(argc > 1) { multicast_if = argv[1]; } memset(&sa, 0, sizeof(struct sigaction)); sa.sa_handler = sighandler; if(sigaction(SIGINT, &sa, NULL) < 0) { perror("sigaction"); } if(find_local_address() < 0) { fprintf(stderr, "failed to get local address\n"); return 1; } #ifdef DEBUG event_enable_debug_mode(); #if LIBEVENT_VERSION_NUMBER >= 0x02010100 event_enable_debug_logging(EVENT_DBG_ALL); /* Libevent 2.1.1 */ #endif /* LIBEVENT_VERSION_NUMBER >= 0x02010100 */ #endif /* DEBUG */ printf("Using libevent %s\n", event_get_version()); if(LIBEVENT_VERSION_NUMBER != event_get_version_number()) { fprintf(stderr, "WARNING build using libevent %s", LIBEVENT_VERSION); } base = event_base_new(); if(base == NULL) { fprintf(stderr, "event_base_new() failed\n"); return 1; } #ifdef DEBUG printf("Using Libevent with backend method %s.\n", event_base_get_method(base)); #endif /* DEBUG */ if(upnpc_init(&upnp, base, multicast_if, ready, soap, &upnp) != UPNPC_OK) { fprintf(stderr, "upnpc_init() failed\n"); return 1; } upnpc_set_local_address(&upnp, local_address, 50000); #ifdef ENABLE_UPNP_EVENTS upnpc_set_event_callback(&upnp, event_callback); #endif /* ENABLE_UPNP_EVENTS */ if(upnpc_start(&upnp) != UPNPC_OK) { fprintf(stderr, "upnp_start() failed\n"); return 1; } event_base_dispatch(base); /* TODO : check return value */ printf("finishing...\n"); upnpc_finalize(&upnp); event_base_free(base); #if LIBEVENT_VERSION_NUMBER >= 0x02010100 libevent_global_shutdown(); /* Libevent 2.1.1 */ #endif return 0; }
int main(int argc, char **argv) { init_config(argc, argv); open_authorizations("r"); init_webfinger(); /** OPEN MAGIC DATABASE **/ magic_cookie = magic_open(MAGIC_MIME); if(magic_load(magic_cookie, RS_MAGIC_DATABASE) != 0) { log_error("Failed to load magic database: %s", magic_error(magic_cookie)); exit(EXIT_FAILURE); } log_info("starting process: main"); if(prctl(PR_SET_NAME, "rs-serve [main]", 0, 0, 0) != 0) { log_error("Failed to set process name: %s", strerror(errno)); } /** SETUP EVENT BASE **/ rs_event_base = event_base_new(); ASSERT_NOT_NULL(rs_event_base, "event_base_new()"); log_debug("libevent method: %s", event_base_get_method(rs_event_base)); event_set_log_callback(log_event_base_message); // TODO: add error cb to base /** SETUP AUTH TOKENS HASH TABLE **/ auth_sessions = sm_new(1024); // FIXME: this a hardcoded value. /** SETUP MAIN LISTENER **/ struct sockaddr_in sin; memset(&sin, 0, sizeof(struct sockaddr_in)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(0); sin.sin_port = htons(RS_PORT); evhtp_t *server = evhtp_new(rs_event_base, NULL); if(RS_USE_SSL) { evhtp_ssl_cfg_t ssl_config = { .pemfile = RS_SSL_CERT_PATH, .privfile = RS_SSL_KEY_PATH, .cafile = RS_SSL_CA_PATH, // what's this??? .capath = NULL, .ciphers = "RC4+RSA:HIGH:+MEDIUM:+LOW", .ssl_opts = SSL_OP_NO_SSLv2, .ssl_ctx_timeout = 60*60*48, .verify_peer = SSL_VERIFY_PEER, .verify_depth = 42, .x509_verify_cb = dummy_ssl_verify_callback, .x509_chk_issued_cb = dummy_check_issued_cb, .scache_type = evhtp_ssl_scache_type_internal, .scache_size = 1024, .scache_timeout = 1024, .scache_init = NULL, .scache_add = NULL, .scache_get = NULL, .scache_del = NULL }; if(evhtp_ssl_init(server, &ssl_config) != 0) { log_error("evhtp_ssl_init() failed"); exit(EXIT_FAILURE); } } /* WEBFINGER */ evhtp_callback_cb webfinger_cb = (RS_WEBFINGER_ENABLED ? handle_webfinger : reject_webfinger); evhtp_set_cb(server, "/.well-known/webfinger", webfinger_cb, NULL); // support legacy webfinger clients (we don't support XRD though): evhtp_set_cb(server, "/.well-known/host-meta", webfinger_cb, NULL); evhtp_set_cb(server, "/.well-known/host-meta.json", webfinger_cb, NULL); /* REMOTESTORAGE */ evhtp_callback_t *storage_cb = evhtp_set_regex_cb(server, "^/storage/([^/]+)/.*$", handle_storage, NULL); evhtp_set_hook(&storage_cb->hooks, evhtp_hook_on_request_fini, finish_request, NULL); if(evhtp_bind_sockaddr(server, (struct sockaddr*)&sin, sizeof(sin), 1024) != 0) { log_error("evhtp_bind_sockaddr() failed: %s", strerror(errno)); exit(EXIT_FAILURE); } /** SETUP AUTH LISTENER **/ memset(&sin, 0, sizeof(struct sockaddr_in)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(0); sin.sin_port = htons(RS_AUTH_PORT); evhtp_t *server_auth = evhtp_new(rs_event_base, NULL); if(RS_USE_SSL) { evhtp_ssl_cfg_t ssl_config = { .pemfile = RS_SSL_CERT_PATH, .privfile = RS_SSL_KEY_PATH, .cafile = RS_SSL_CA_PATH, // what's this??? .capath = NULL, .ciphers = "RC4+RSA:HIGH:+MEDIUM:+LOW", .ssl_opts = SSL_OP_NO_SSLv2, .ssl_ctx_timeout = 60*60*48, .verify_peer = SSL_VERIFY_PEER, .verify_depth = 42, .x509_verify_cb = dummy_ssl_verify_callback, .x509_chk_issued_cb = dummy_check_issued_cb, .scache_type = evhtp_ssl_scache_type_internal, .scache_size = 1024, .scache_timeout = 1024, .scache_init = NULL, .scache_add = NULL, .scache_get = NULL, .scache_del = NULL }; if(evhtp_ssl_init(server_auth, &ssl_config) != 0) { log_error("evhtp_ssl_init() failed"); exit(EXIT_FAILURE); } } /* AUTH */ evhtp_set_cb(server_auth, "/authenticate", handle_authenticate, NULL); evhtp_set_cb(server_auth, "/authorizations", handle_authorizations, NULL); evhtp_set_hook(&storage_cb->hooks, evhtp_hook_on_request_fini, finish_request, NULL); if(evhtp_bind_sockaddr(server_auth, (struct sockaddr*)&sin, sizeof(sin), 1024) != 0) { log_error("evhtp_bind_sockaddr() failed: %s", strerror(errno)); exit(EXIT_FAILURE); } /** SETUP SIGNALS **/ sigset_t sigmask; sigemptyset(&sigmask); sigaddset(&sigmask, SIGINT); sigaddset(&sigmask, SIGTERM); sigaddset(&sigmask, SIGCHLD); ASSERT_ZERO(sigprocmask(SIG_BLOCK, &sigmask, NULL), "sigprocmask()"); int sfd = signalfd(-1, &sigmask, SFD_NONBLOCK); ASSERT_NOT_EQ(sfd, -1, "signalfd()"); struct event *signal_event = event_new(rs_event_base, sfd, EV_READ | EV_PERSIST, handle_signal, NULL); event_add(signal_event, NULL); /** RUN EVENT LOOP **/ if(RS_DETACH) { int pid = fork(); if(pid == 0) { event_reinit(rs_event_base); if(RS_LOG_FILE == stdout) { log_warn("No --log-file option given. Future output will be lost."); freopen("/dev/null", "r", stdout); freopen("/dev/null", "r", stderr); } return event_base_dispatch(rs_event_base); } else { printf("rs-serve detached with pid %d\n", pid); if(RS_PID_FILE) { fprintf(RS_PID_FILE, "%d", pid); fflush(RS_PID_FILE); } _exit(EXIT_SUCCESS); } } else { if(RS_PID_FILE) { fprintf(RS_PID_FILE, "%d", getpid()); fflush(RS_PID_FILE); } return event_base_dispatch(rs_event_base); } }
int main(int argc, char* argv[]) { #if 1 // For debug with segment fault struct sigaction sa; sa.sa_handler = backtrace_info; sigaction(SIGSEGV, &sa, NULL); // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGCHLD, SIG_IGN); signal(SIGABRT, SIG_IGN); #endif int opt_g = 0; memset(&cltopt, 0, sizeof(CLT_OPT)); cltopt.C_TYPE = C_USR; while( (opt_g = getopt(argc, argv, "Dh")) != -1 ) { switch(opt_g) { case 'D': cltopt.C_TYPE = C_DAEMON; break; case 'h': default: usage(); exit(EXIT_SUCCESS); } } if(load_settings_client(&cltopt) == RET_NO) { st_d_error("加载配置文件settings.json出错!"); exit(EXIT_FAILURE); } OpenSSL_add_ssl_algorithms(); SSL_load_error_strings(); SSL_library_init(); //SSL_library_init() always returns "1" //int sd_id128_from_string(const char *s, sd_id128_t *ret); sd_id128_get_machine(&cltopt.mach_uuid); gethostname(cltopt.hostname, sizeof(cltopt.hostname)); st_d_print("CURRENT MACH_ID:%s, HOSTNAME:%s", SD_ID128_CONST_STR(cltopt.mach_uuid), cltopt.hostname); if (cltopt.C_TYPE == C_DAEMON) { cltopt.session_uuid = cltopt.mach_uuid; st_d_print("PLEASE REMEMEBER SET MACH_ID FOR USER TYPE!"); } dump_clt_opts(&cltopt); /*带配置产生event_base对象*/ struct event_config *cfg; cfg = event_config_new(); event_config_avoid_method(cfg, "select"); //避免使用select event_config_require_features(cfg, EV_FEATURE_ET); //使用边沿触发类型 base = event_base_new_with_config(cfg); event_config_free(cfg); st_d_print("当前复用Event模式: %s", event_base_get_method(base)); // epoll /*连接服务器*/ int srv_fd = socket(AF_INET, SOCK_STREAM, 0); unsigned int optval = 1; setsockopt(srv_fd, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval));//禁用NAGLE算法 if(sc_connect_srv(srv_fd) != RET_YES) { SYS_ABORT("连接服务器失败!"); } if(cltopt.C_TYPE == C_DAEMON) { if (sc_daemon_init_srv(srv_fd) != RET_YES) SYS_ABORT("(Daemon) 服务器返回错误!"); } else { if (sc_usr_init_srv(srv_fd) != RET_YES) SYS_ABORT("(Usr) 服务器返回错误!"); } st_d_print("客户端连接服务器OK!"); /** * USR 建立本地Listen侦听套接字 */ if (cltopt.C_TYPE == C_USR) { int i = 0; for (i=0; i<MAX_PORT_NUM; i++) { if (cltopt.maps[i].usrport) { struct evconnlistener *listener; struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(0); sin.sin_port = htons(cltopt.maps[i].usrport); /* Port Num */ listener = evconnlistener_new_bind(base, accept_conn_cb, &cltopt.maps[i], LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1/*backlog 连接无限制*/, (struct sockaddr*)&sin, sizeof(sin)); if (!listener) { st_d_error("[USR]创建侦听套接字失败 %d:%d", cltopt.maps[i].usrport, cltopt.maps[i].daemonport); continue; } evconnlistener_set_error_cb(listener, accept_error_cb); st_d_print("[USR]创建侦听套接字 %d:%d OK", cltopt.maps[i].usrport, cltopt.maps[i].daemonport); } else break; } } encrypt_init(SD_ID128_CONST_STR(cltopt.mach_uuid), cltopt.enc_key); if (cltopt.C_TYPE == C_DAEMON && cltopt.ss5_port ) { /** * 目前只考虑将sockets5代理使用线程池来处理,其它的端口暴露 * 基本都是长连接,不单独处理 */ cltopt.thread_num = 5; cltopt.main_thread_id = pthread_self(); cltopt.thread_objs = (P_THREAD_OBJ)calloc(sizeof(THREAD_OBJ), cltopt.thread_num); if (!cltopt.thread_objs) { SYS_ABORT("申请THREAD_OBJ出错"); } sc_create_ss5_worker_threads(cltopt.thread_num, cltopt.thread_objs); st_d_print("[DAEMON]创建sockets5代理端口:%d", cltopt.ss5_port); struct evconnlistener *listener; struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(0); sin.sin_port = htons(cltopt.ss5_port); /* Port Num */ listener = evconnlistener_new_bind(base, ss5_accept_conn_cb, NULL, LEV_OPT_LEAVE_SOCKETS_BLOCKING/* 阻塞 */|LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1/*backlog 连接无限制*/, (struct sockaddr*)&sin, sizeof(sin)); if (!listener) { st_d_error("[DAEMON]sockets5代理创建侦听套接字失败 %d", cltopt.ss5_port); exit(EXIT_FAILURE); } evconnlistener_set_error_cb(listener, accept_error_cb); st_d_print("[DAEMON]sockets5代理创建侦听套接字OK %d", cltopt.ss5_port); } if (cltopt.C_TYPE == C_DAEMON && cltopt.dns_port) { st_d_print("[DAEMON]创建DNS代理端口:%d", cltopt.dns_port); if (cltopt.dns_port != 53) { st_d_print("[DAEMON]请注意标准DNS侦听#53端口!"); } int dns_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (dns_socket < 0 ) { st_d_error("Create DNS socket error!"); exit(EXIT_FAILURE); } unsigned int optval = 1; setsockopt(dns_socket, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof(optval));//禁用NAGLE算法 setsockopt(dns_socket, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)); evutil_make_socket_closeonexec(dns_socket); evutil_make_socket_nonblocking(dns_socket); struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(0); sin.sin_port = htons(cltopt.dns_port); /* Port Num */ if (bind(dns_socket, (struct sockaddr *)&sin, sizeof(sin))) { st_d_error("Bind DNS socket error!"); exit(EXIT_FAILURE); } cltopt.dns_transid_port_map = (unsigned short*)malloc(sizeof(unsigned short) * 0xFFFF); if (!cltopt.dns_transid_port_map) { st_d_error("Malloc for requestid-port failed!"); exit(EXIT_FAILURE); } P_PORTTRANS p_trans = sc_create_trans(cltopt.dns_port); if (!p_trans) { st_d_error("本地无空闲TRANS!"); exit(EXIT_FAILURE); } p_trans->is_enc = 1; p_trans->l_port = cltopt.dns_port; encrypt_ctx_init(&p_trans->ctx_enc, p_trans->l_port, cltopt.enc_key, 1); encrypt_ctx_init(&p_trans->ctx_dec, p_trans->l_port, cltopt.enc_key, 0); // 建立DNS UDP事件侦听 p_trans->extra_ev = event_new(base, dns_socket, EV_READ | EV_PERSIST, dns_client_to_proxy_cb, p_trans); int dns_srv_fd = socket(AF_INET, SOCK_STREAM, 0); if(sc_connect_srv(dns_srv_fd) != RET_YES) { SYS_ABORT("连接服务器失败!"); } sc_daemon_dns_init_srv(dns_srv_fd, p_trans->l_port, 12333); evutil_make_socket_nonblocking(dns_srv_fd); // later enabled //event_add(p_trans->extra_ev, NULL) != 0); p_trans->srv_bev = bufferevent_socket_new(base, dns_srv_fd, BEV_OPT_CLOSE_ON_FREE); bufferevent_setcb(p_trans->srv_bev, dns_bufferread_cb_enc, NULL, dns_bufferevent_cb, p_trans); st_d_print("[DAEMON]DNS代理创建侦听套接字OK %d", cltopt.dns_port); } sc_set_eventcb_srv(srv_fd, base); /** * Main Loop Here */ event_base_loop(base, 0); event_base_free(base); st_d_print("程序退出!!!!"); return 0; }
static void test_edgetriggered(void *et) { struct event *ev = NULL; struct event_base *base = NULL; const char *test = "test string"; evutil_socket_t pair[2] = {-1,-1}; int supports_et; /* On Linux 3.2.1 (at least, as patched by Fedora and tested by Nick), * doing a "recv" on an AF_UNIX socket resets the readability of the * socket, even though there is no state change, so we don't actually * get edge-triggered behavior. Yuck! Linux 3.1.9 didn't have this * problem. */ #ifdef __linux__ if (evutil_ersatz_socketpair_(AF_INET, SOCK_STREAM, 0, pair) == -1) { tt_abort_perror("socketpair"); } #else if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair) == -1) { tt_abort_perror("socketpair"); } #endif called = was_et = 0; tt_int_op(send(pair[0], test, (int)strlen(test)+1, 0), >, 0); shutdown(pair[0], SHUT_WR); /* Initalize the event library */ base = event_base_new(); if (!strcmp(event_base_get_method(base), "epoll") || !strcmp(event_base_get_method(base), "epoll (with changelist)") || !strcmp(event_base_get_method(base), "kqueue")) supports_et = 1; else supports_et = 0; TT_BLATHER(("Checking for edge-triggered events with %s, which should %s" "support edge-triggering", event_base_get_method(base), supports_et?"":"not ")); /* Initalize one event */ ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST, read_cb, &ev); event_add(ev, NULL); /* We're going to call the dispatch function twice. The first invocation * will read a single byte from pair[1] in either case. If we're edge * triggered, we'll only see the event once (since we only see transitions * from no data to data), so the second invocation of event_base_loop will * do nothing. If we're level triggered, the second invocation of * event_base_loop will also activate the event (because there's still * data to read). */ event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE); event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE); if (supports_et) { tt_int_op(called, ==, 1); tt_assert(was_et); } else {
bool EventLoopL::Prepare() { _DBG("EventLoopL::Prepare"); if(!EventConfig_) { EventConfig_ = event_config_new(); } if(!EventConfig_) { throwSystemError("event_config_new allocate fail!"); } //event_config_avoid_method(EventConfig_, "select");//avoid select //event_config_avoid_method(EventConfig_, "poll");//avoid poll if(0 != event_config_require_features(EventConfig_, EV_FEATURE_O1))//EV_FEATURE_ET ? { _ERR("set event config feature fail!"); return false; } if(0 != event_config_set_flag(EventConfig_, EVENT_BASE_FLAG_NOLOCK)) { _ERR("set event config flag fail!"); return false; } if(!EventBase_) { EventBase_ = event_base_new_with_config(EventConfig_); } if(!EventBase_) { throwSystemError("event_base_new_with_config allocate fail!"); return false; } _DBG("Current used method : %s", event_base_get_method(EventBase_)); for(auto &data : Datas_) { if(data) { std::shared_ptr<IO> io(data->io.lock()); if(!io) { _WRN("This io %d is not exist!", io->Index()); continue; } if(io->Index() < 0) { _ERR("This IO has been removed!"); continue; } if(data->ev) { event_del(data->ev); event_free(data->ev); data->ev = NULL; } event* ev = event_new(EventBase_, io->Fd(), io->Condition(), CEventCallBack, (void*)&(data->fn)); _DBG("Add io %p(index : %d fd : %d cond : %d)", ev, io->Index(), io->Fd(), io->Condition()); if(ev == NULL) { _ERR("event_new fail, index %d", io->Index()); continue; } data->ev = ev; if(event_add(ev, NULL)) { _ERR("event_add fail, index %d!", io->Index()); continue; } } } int evtfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (evtfd < 0) { _ERR("Get eventfd fail!"); return false; } Wakeup_.reset(new IO(evtfd, EV_READ | EV_PERSIST)); Wakeup_->SetCallback(std::bind(&EventLoopL::quitAsync, this, std::placeholders::_1)); AddIO(Wakeup_); return true; }