void server_evbase_init() { server.evconf = event_config_new(); event_config_avoid_method(server.evconf, "select"); event_config_avoid_method(server.evconf, "poll"); server.evbase = event_base_new_with_config(server.evconf); }
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; }
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); }
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 }
void EventLoop::initialize() { // We need to initialize Libevent differently depending on the // operating system threading support. #if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED) if (evthread_use_pthreads() < 0) { LOG(FATAL) << "Failed to initialize, evthread_use_pthreads"; } #elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED) if (evthread_use_windows_threads() < 0) { LOG(FATAL) << "Failed to initialize, evthread_use_windows_threads"; } #else #error "Libevent must be compiled with either pthread or Windows thread support" #endif // This enables debugging of libevent calls. We can remove this // when the implementation settles and after we gain confidence. event_enable_debug_mode(); // TODO(jmlvanre): Allow support for 'epoll' once SSL related // issues are resolved. struct event_config* config = event_config_new(); event_config_avoid_method(config, "epoll"); base = event_base_new_with_config(config); if (base == NULL) { LOG(FATAL) << "Failed to initialize, event_base_new"; } }
struct event_base *get_fd_rdy_event_base(void) { struct event_config *evcfg = event_config_new(); event_config_require_features(evcfg, EV_FEATURE_FDS); struct event_base *base = event_base_new_with_config(evcfg); event_config_free(evcfg); return base; }
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; }
opal_event_base_t* opal_event_base_create(void) { opal_event_base_t *base; base = event_base_new_with_config(config); if (NULL == base) { /* there is no backend method that does what we want */ opal_output(0, "No event method available"); } return base; }
void select_feature_test() { struct event_config *config = event_config_new(); event_config_avoid_method (config, "poll"); event_config_avoid_method (config, "epoll"); struct event_base *base = event_base_new_with_config (config); print_features(base); event_base_free(base); }
/* * Set up a thread's information. */ static void setup_thread(LIBEVENT_THREAD *me) { #if defined(LIBEVENT_VERSION_NUMBER) && LIBEVENT_VERSION_NUMBER >= 0x02000101 struct event_config *ev_config; ev_config = event_config_new(); event_config_set_flag(ev_config, EVENT_BASE_FLAG_NOLOCK); me->base = event_base_new_with_config(ev_config); event_config_free(ev_config); #else me->base = event_init(); #endif if (! me->base) { fprintf(stderr, "Can't allocate event base\n"); exit(1); } /* Listen for notifications from other threads */ event_set(&me->notify_event, me->notify_receive_fd, EV_READ | EV_PERSIST, thread_libevent_process, me); event_base_set(me->base, &me->notify_event); if (event_add(&me->notify_event, 0) == -1) { fprintf(stderr, "Can't monitor libevent notify pipe\n"); exit(1); } me->new_conn_queue = malloc(sizeof(struct conn_queue)); if (me->new_conn_queue == NULL) { perror("Failed to allocate memory for connection queue"); exit(EXIT_FAILURE); } cq_init(me->new_conn_queue); if (pthread_mutex_init(&me->stats.mutex, NULL) != 0) { perror("Failed to initialize mutex"); exit(EXIT_FAILURE); } me->suffix_cache = cache_create("suffix", SUFFIX_SIZE, sizeof(char*), NULL, NULL); if (me->suffix_cache == NULL) { fprintf(stderr, "Failed to create suffix cache\n"); exit(EXIT_FAILURE); } #ifdef EXTSTORE me->io_cache = cache_create("io", sizeof(io_wrap), sizeof(char*), NULL, NULL); if (me->io_cache == NULL) { fprintf(stderr, "Failed to create IO object cache\n"); exit(EXIT_FAILURE); } #endif }
/** Initialize the Libevent library and set up the event base. */ void tor_libevent_initialize(void) { tor_assert(the_event_base == NULL); #ifdef __APPLE__ if (MACOSX_KQUEUE_IS_BROKEN || tor_get_libevent_version(NULL) < V_OLD(1,1,'b')) { setenv("EVENT_NOKQUEUE","1",1); } #endif #ifdef HAVE_EVENT2_EVENT_H { struct event_config *cfg = event_config_new(); tor_assert(cfg); /* In 0.2.2, we don't use locking at all. Telling Libevent not to try to * turn it on can avoid a needless socketpair() attempt. */ event_config_set_flag(cfg, EVENT_BASE_FLAG_NOLOCK); the_event_base = event_base_new_with_config(cfg); event_config_free(cfg); } #else the_event_base = event_init(); #endif if (!the_event_base) { log_err(LD_GENERAL, "Unable to initialize Libevent: cannot continue."); exit(1); } #if defined(HAVE_EVENT_GET_VERSION) && defined(HAVE_EVENT_GET_METHOD) /* Making this a NOTICE for now so we can link bugs to a libevent versions * or methods better. */ log(LOG_NOTICE, LD_GENERAL, "Initialized libevent version %s using method %s. Good.", event_get_version(), tor_libevent_get_method()); #else log(LOG_NOTICE, LD_GENERAL, "Initialized old libevent (version 1.0b or earlier)."); log(LOG_WARN, LD_GENERAL, "You have a *VERY* old version of libevent. It is likely to be buggy; " "please build Tor with a more recent version."); #endif }
/* * main loop - construct the event_base and start the loop */ int run_server(char* ip, short port, int timeout_s, int backlog) { struct event_base *ev_base; struct event_config *ev_cfg; struct evhttp *ev_httpd; evutil_socket_t socket_fd; int socket_opts_flag = 1; int worker_id, socket_opts_results; openlog("Backend", LOG_PID|LOG_NDELAY, LOG_LOCAL0); ev_cfg = event_config_new(); //event_config_set_flag(ev_cfg, EV_TIMEOUT|EV_PERSIST); ev_base = event_base_new_with_config(ev_cfg); if (!ev_base) { printf("ERROR: Failed to initialize HTTP event loop!\n"); return -1; } // Set up httpd interface event ev_httpd = evhttp_new(ev_base); evhttp_set_timeout(ev_httpd, timeout_s); routes(ev_httpd, ev_base); socket_fd = create_socket(ip, port, timeout_s, backlog); socket_opts_results = setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, (char *) socket_opts_flag, sizeof(int)); if ( socket_opts_results < 0 ) { syslog( LOG_INFO, "Nagle DISABLED"); } else { syslog( LOG_INFO, "Nagle ENABLED"); } evhttp_accept_socket(ev_httpd, socket_fd); for (worker_id = 0; worker_id < WORKERS; worker_id++) { if (fork() == 0) { printf("Starting worker_id %d ... ", worker_id); worker(ev_base); printf("done.\n"); exit(0); } } closelog(); return 0; }
ClientImpl::ClientImpl(ClientConfig config) throw(voltdb::Exception, voltdb::LibEventException) : m_nextRequestId(INT64_MIN), m_nextConnectionIndex(0), m_listener(config.m_listener), m_invocationBlockedOnBackpressure(false), m_loopBreakRequested(false), m_isDraining(false), m_instanceIdIsSet(false), m_outstandingRequests(0), m_username(config.m_username), m_maxOutstandingRequests(config.m_maxOutstandingRequests), m_ignoreBackpressure(false), m_useClientAffinity(false),m_updateHashinator(false), m_pendingConnectionSize(0) , m_pLogger(0) { pthread_once(&once_initLibevent, initLibevent); #ifdef DEBUG if (!voltdb_clientimpl_debug_init_libevent) { event_enable_debug_mode(); voltdb_clientimpl_debug_init_libevent = true; } #endif struct event_config *cfg = event_config_new(); event_config_set_flag(cfg, EVENT_BASE_FLAG_NO_CACHE_TIME);//, EVENT_BASE_FLAG_NOLOCK); m_base = event_base_new_with_config(cfg); assert(m_base); if (!m_base) { throw voltdb::LibEventException(); } m_hashScheme = config.m_hashScheme; if (m_hashScheme == HASH_SHA1) { SHA1_CTX context; SHA1_Init(&context); SHA1_Update( &context, reinterpret_cast<const unsigned char*>(config.m_password.data()), config.m_password.size()); m_passwordHash = (unsigned char *)malloc(20*sizeof(char)); SHA1_Final ( &context, m_passwordHash); } else if (config.m_hashScheme == HASH_SHA256) { m_passwordHash = (unsigned char *)malloc(32*sizeof(char)); computeSHA256(config.m_password.c_str(), config.m_password.size(), m_passwordHash); } else { throw voltdb::LibEventException(); } if (0 == pipe(m_wakeupPipe)) { struct event *ev = event_new(m_base, m_wakeupPipe[0], EV_READ|EV_PERSIST, wakeupPipeCallback, this); event_add(ev, NULL); } else { m_wakeupPipe[1] = -1; } SHA1_CTX context; SHA1_Init(&context); SHA1_Update( &context, reinterpret_cast<const unsigned char*>(config.m_password.data()), config.m_password.size()); SHA1_Final(&context, m_passwordHash); }
struct event_base* server_event_base_init() { struct event_config *config = event_config_new(); event_config_avoid_method(config, "select"); event_config_avoid_method(config, "poll"); struct event_base *base = event_base_new_with_config(config); event_config_free(config); if(base == NULL) { log_err(__FILE__, __LINE__, "Cannot initialize event_base. error : %s", evutil_socket_error_to_string(errno)); return NULL; } return base; }
static void thread_deferred_cb_skew(void *arg) { struct timeval tv_timer = {1, 0}; struct event_base *base = NULL; struct event_config *cfg = NULL; struct timeval elapsed; int elapsed_usec; int i; cfg = event_config_new(); tt_assert(cfg); event_config_set_max_dispatch_interval(cfg, NULL, 16, 0); base = event_base_new_with_config(cfg); tt_assert(base); for (i = 0; i < QUEUE_THREAD_COUNT; ++i) deferred_data[i].queue = base; evutil_gettimeofday(&timer_start, NULL); event_base_once(base, -1, EV_TIMEOUT, timer_callback, NULL, &tv_timer); event_base_once(base, -1, EV_TIMEOUT, start_threads_callback, NULL, NULL); event_base_dispatch(base); evutil_timersub(&timer_end, &timer_start, &elapsed); TT_BLATHER(("callback count, %u", callback_count)); elapsed_usec = (unsigned)(elapsed.tv_sec*1000000 + elapsed.tv_usec); TT_BLATHER(("elapsed time, %u usec", elapsed_usec)); /* XXX be more intelligent here. just make sure skew is * within .4 seconds for now. */ tt_assert(elapsed_usec >= 600000 && elapsed_usec <= 1400000); end: for (i = 0; i < QUEUE_THREAD_COUNT; ++i) THREAD_JOIN(load_threads[i]); if (base) event_base_free(base); if (cfg) event_config_free(cfg); }
int CXtReactor::IocpInit(void) { if ( m_pEvtBaseCfg == NULL ) { new_event_base_cfg; if ( m_pEvtBaseCfg == NULL ) { return XT_REACT_ERR_MEM_FAIL; } } evthread_use_windows_threads(); event_config_set_flag( m_pEvtBaseCfg, EVENT_BASE_FLAG_STARTUP_IOCP ); m_pEvtBase = event_base_new_with_config(m_pEvtBaseCfg); event_config_free(m_pEvtBaseCfg); return FUN_RET_OK; }
/** Initialize the Libevent library and set up the event base. */ void tor_libevent_initialize(tor_libevent_cfg *torcfg) { tor_assert(the_event_base == NULL); /* some paths below don't use torcfg, so avoid unused variable warnings */ (void)torcfg; { int attempts = 0; struct event_config *cfg; ++attempts; cfg = event_config_new(); tor_assert(cfg); /* Telling Libevent not to try to turn locking on can avoid a needless * socketpair() attempt. */ event_config_set_flag(cfg, EVENT_BASE_FLAG_NOLOCK); if (torcfg->num_cpus > 0) event_config_set_num_cpus_hint(cfg, torcfg->num_cpus); /* We can enable changelist support with epoll, since we don't give * Libevent any dup'd fds. This lets us avoid some syscalls. */ event_config_set_flag(cfg, EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST); the_event_base = event_base_new_with_config(cfg); event_config_free(cfg); } if (!the_event_base) { /* LCOV_EXCL_START */ log_err(LD_GENERAL, "Unable to initialize Libevent: cannot continue."); exit(1); // exit ok: libevent is broken. /* LCOV_EXCL_STOP */ } log_info(LD_GENERAL, "Initialized libevent version %s using method %s. Good.", event_get_version(), tor_libevent_get_method()); }
void attach_namespace (int client, struct arguments arguments) { w(evutil_make_socket_closeonexec(client)); w(evutil_make_socket_nonblocking(client)); struct event_config *ev_config = p(event_config_new()); w(event_config_require_features(ev_config, EV_FEATURE_FDS)); struct event_base *ev_base = p(event_base_new_with_config(ev_config)); event_config_free(ev_config); struct event *ev_client = p(event_new(ev_base, client, EV_READ|EV_PERSIST, client_func, NULL)); struct arg arg = { .event = ev_client, .command = arguments.command }; w(event_assign(ev_client, ev_base, client, EV_READ|EV_PERSIST, client_func, &arg)); event_add(ev_client, NULL); w(event_base_dispatch(ev_base)); event_base_free(ev_base); }
int main(int argc, char **argv) { struct event_base *base; struct event_config *cfg; struct event *ev; const char *test = "test string"; evutil_socket_t pair[2]; /* Initialize the library and check if the backend supports EV_FEATURE_EARLY_CLOSE */ cfg = event_config_new(); event_config_require_features(cfg, EV_FEATURE_EARLY_CLOSE); base = event_base_new_with_config(cfg); event_config_free(cfg); if (!base) { /* Backend doesn't support EV_FEATURE_EARLY_CLOSE */ return 0; } /* Create a pair of sockets */ if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) return (1); /* Send some data on socket 0 and immediately close it */ if (send(pair[0], test, (int)strlen(test)+1, 0) < 0) return (1); shutdown(pair[0], SHUT_WR); /* Dispatch */ ev = event_new(base, pair[1], EV_CLOSED | EV_TIMEOUT, closed_cb, event_self_cbarg()); event_add(ev, &timeout); event_base_dispatch(base); /* Finalize library */ event_base_free(base); return 0; }
int main(int argc, char **argv) { struct event_config *evcfg; struct event_base *base; struct evconnlistener *listener; struct sockaddr_in sin; WSADATA wsa_data; WSAStartup(0x0201, &wsa_data); evcfg = event_config_new(); event_config_set_flag(evcfg, EVENT_BASE_FLAG_STARTUP_IOCP); base = event_base_new_with_config(evcfg); //base = event_base_new(); if (!base) { fprintf(stderr, "Could not initialize libevent!\n"); return 1; } 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; } event_base_dispatch(base); return 0; }
static int test_ratelimiting(void) { struct event_base *base; struct sockaddr_in sin; struct evconnlistener *listener; struct sockaddr_storage ss; ev_socklen_t slen; struct bufferevent **bevs; struct client_state *states; struct bufferevent_rate_limit_group *group = NULL; int i; struct timeval tv; ev_uint64_t total_received; double total_sq_persec, total_persec; double variance; double expected_total_persec = -1.0, expected_avg_persec = -1.0; int ok = 1; struct event_config *base_cfg; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */ sin.sin_port = 0; /* unspecified port */ if (0) event_enable_debug_mode(); base_cfg = event_config_new(); #ifdef _WIN32 if (cfg_enable_iocp) { evthread_use_windows_threads(); event_config_set_flag(base_cfg, EVENT_BASE_FLAG_STARTUP_IOCP); } #endif base = event_base_new_with_config(base_cfg); listener = evconnlistener_new_bind(base, echo_listenercb, base, LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1, (struct sockaddr *)&sin, sizeof(sin)); slen = sizeof(ss); if (getsockname(evconnlistener_get_fd(listener), (struct sockaddr *)&ss, &slen) < 0) { perror("getsockname"); return 1; } if (cfg_connlimit > 0) { conn_bucket_cfg = ev_token_bucket_cfg_new( cfg_connlimit, cfg_connlimit * 4, cfg_connlimit, cfg_connlimit * 4, &cfg_tick); assert(conn_bucket_cfg); } if (cfg_grouplimit > 0) { group_bucket_cfg = ev_token_bucket_cfg_new( cfg_grouplimit, cfg_grouplimit * 4, cfg_grouplimit, cfg_grouplimit * 4, &cfg_tick); group = ratelim_group = bufferevent_rate_limit_group_new( base, group_bucket_cfg); expected_total_persec = cfg_grouplimit; expected_avg_persec = cfg_grouplimit / cfg_n_connections; if (cfg_connlimit > 0 && expected_avg_persec > cfg_connlimit) expected_avg_persec = cfg_connlimit; if (cfg_min_share >= 0) bufferevent_rate_limit_group_set_min_share( ratelim_group, cfg_min_share); } if (expected_avg_persec < 0 && cfg_connlimit > 0) expected_avg_persec = cfg_connlimit; if (expected_avg_persec > 0) expected_avg_persec /= seconds_per_tick; if (expected_total_persec > 0) expected_total_persec /= seconds_per_tick; bevs = calloc(cfg_n_connections, sizeof(struct bufferevent *)); states = calloc(cfg_n_connections, sizeof(struct client_state)); for (i = 0; i < cfg_n_connections; ++i) { bevs[i] = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE|BEV_OPT_THREADSAFE); assert(bevs[i]); bufferevent_setcb(bevs[i], discard_readcb, loud_writecb, write_on_connectedcb, &states[i]); bufferevent_enable(bevs[i], EV_READ|EV_WRITE); bufferevent_socket_connect(bevs[i], (struct sockaddr *)&ss, slen); } tv.tv_sec = cfg_duration - 1; tv.tv_usec = 995000; event_base_loopexit(base, &tv); event_base_dispatch(base); ratelim_group = NULL; /* So no more responders get added */ for (i = 0; i < cfg_n_connections; ++i) { bufferevent_free(bevs[i]); } evconnlistener_free(listener); /* Make sure no new echo_conns get added to the group. */ ratelim_group = NULL; /* This should get _everybody_ freed */ while (n_echo_conns_open) { printf("waiting for %d conns\n", n_echo_conns_open); tv.tv_sec = 0; tv.tv_usec = 300000; event_base_loopexit(base, &tv); event_base_dispatch(base); } if (group) bufferevent_rate_limit_group_free(group); total_received = 0; total_persec = 0.0; total_sq_persec = 0.0; for (i=0; i < cfg_n_connections; ++i) { double persec = states[i].received; persec /= cfg_duration; total_received += states[i].received; total_persec += persec; total_sq_persec += persec*persec; printf("%d: %f per second\n", i+1, persec); } printf(" total: %f per second\n", ((double)total_received)/cfg_duration); if (expected_total_persec > 0) { double diff = expected_total_persec - ((double)total_received/cfg_duration); printf(" [Off by %lf]\n", diff); if (cfg_grouplimit_tolerance > 0 && fabs(diff) > cfg_grouplimit_tolerance) { fprintf(stderr, "Group bandwidth out of bounds\n"); ok = 0; } } printf(" average: %f per second\n", (((double)total_received)/cfg_duration)/cfg_n_connections); if (expected_avg_persec > 0) { double diff = expected_avg_persec - (((double)total_received)/cfg_duration)/cfg_n_connections; printf(" [Off by %lf]\n", diff); if (cfg_connlimit_tolerance > 0 && fabs(diff) > cfg_connlimit_tolerance) { fprintf(stderr, "Connection bandwidth out of bounds\n"); ok = 0; } } variance = total_sq_persec/cfg_n_connections - total_persec*total_persec/(cfg_n_connections*cfg_n_connections); printf(" stddev: %f per second\n", sqrt(variance)); if (cfg_stddev_tolerance > 0 && sqrt(variance) > cfg_stddev_tolerance) { fprintf(stderr, "Connection variance out of bounds\n"); ok = 0; } event_base_free(base); free(bevs); free(states); return ok ? 0 : 1; }
int game_event_init() { int ret = 0; struct event_config *config = NULL; // struct event *event_signal1 = NULL; // struct event *event_signal2 = NULL; event_set_log_callback(libevent_log); config = event_config_new(); if (!config) { log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: event_config_new failed[%d]", __FUNCTION__, __LINE__, errno); ret = -10; goto fail; } event_config_require_features(config, EV_FEATURE_ET | EV_FEATURE_O1 /*| EV_FEATURE_FDS*/); event_config_set_flag(config, EVENT_BASE_FLAG_NOLOCK | EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST); base = event_base_new_with_config(config); if (!base) { log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: event_base_new_with_config failed[%d]", __FUNCTION__, __LINE__, errno); ret = -20; goto fail; } /* event_signal1 = evsignal_new(base, SIGUSR1, cb_signal, NULL); if (!event_signal1) { log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: evsignal_new failed[%d]", __FUNCTION__, __LINE__, errno); ret = -30; goto fail; } event_signal2 = evsignal_new(base, SIGUSR2, cb_signal, NULL); if (!event_signal2) { log4c_category_log(mycat, LOG4C_PRIORITY_ERROR, "%s %d: evsignal_new failed[%d]", __FUNCTION__, __LINE__, errno); ret = -30; goto fail; } // struct event event_signal, event_timer; // evsignal_assign(&event_signal, base, SIGUSR1, cb_signal, NULL); // evtimer_assign(&event_timer, base, cb_timer, NULL); evsignal_add(event_signal1, NULL); evsignal_add(event_signal2, NULL); */ return (0); fail: /* if (event_signal1) { event_free(event_signal1); event_signal1 = NULL; } if (event_signal2) { event_free(event_signal2); event_signal2 = NULL; } */ if (config) { event_config_free(config); config = NULL; } if (base) { event_base_free(base); base = NULL; } return (ret); }
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); }
/** Initialize the Libevent library and set up the event base. */ void tor_libevent_initialize(tor_libevent_cfg *torcfg) { tor_assert(the_event_base == NULL); /* some paths below don't use torcfg, so avoid unused variable warnings */ (void)torcfg; #ifdef HAVE_EVENT2_EVENT_H { int attempts = 0; int using_threads; struct event_config *cfg; retry: ++attempts; using_threads = 0; cfg = event_config_new(); tor_assert(cfg); #if defined(_WIN32) && defined(USE_BUFFEREVENTS) if (! torcfg->disable_iocp) { evthread_use_windows_threads(); event_config_set_flag(cfg, EVENT_BASE_FLAG_STARTUP_IOCP); using_iocp_bufferevents = 1; using_threads = 1; } else { using_iocp_bufferevents = 0; } #elif defined(__COVERITY__) /* Avoid a 'dead code' warning below. */ using_threads = ! torcfg->disable_iocp; #endif if (!using_threads) { /* Telling Libevent not to try to turn locking on can avoid a needless * socketpair() attempt. */ event_config_set_flag(cfg, EVENT_BASE_FLAG_NOLOCK); } #if defined(LIBEVENT_VERSION_NUMBER) && LIBEVENT_VERSION_NUMBER >= V(2,0,7) if (torcfg->num_cpus > 0) event_config_set_num_cpus_hint(cfg, torcfg->num_cpus); #endif #if LIBEVENT_VERSION_NUMBER >= V(2,0,9) /* We can enable changelist support with epoll, since we don't give * Libevent any dup'd fds. This lets us avoid some syscalls. */ event_config_set_flag(cfg, EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST); #endif the_event_base = event_base_new_with_config(cfg); event_config_free(cfg); if (using_threads && the_event_base == NULL && attempts < 2) { /* This could be a socketpair() failure, which can happen sometimes on * windows boxes with obnoxious firewall rules. Downgrade and try * again. */ #if defined(_WIN32) && defined(USE_BUFFEREVENTS) if (torcfg->disable_iocp == 0) { log_warn(LD_GENERAL, "Unable to initialize Libevent. Trying again " "with IOCP disabled."); } else #endif { log_warn(LD_GENERAL, "Unable to initialize Libevent. Trying again."); } torcfg->disable_iocp = 1; goto retry; } } #else the_event_base = event_init(); #endif if (!the_event_base) { log_err(LD_GENERAL, "Unable to initialize Libevent: cannot continue."); exit(1); } /* Making this a NOTICE for now so we can link bugs to a libevent versions * or methods better. */ log_info(LD_GENERAL, "Initialized libevent version %s using method %s. Good.", event_get_version(), tor_libevent_get_method()); #ifdef USE_BUFFEREVENTS tor_libevent_set_tick_timeout(torcfg->msec_per_tick); #endif }
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; }
/* * The actual main function. */ int sntp_main ( int argc, char **argv, const char *sntpVersion ) { int i; int exitcode; int optct; struct event_config * evcfg; /* Initialize logging system - sets up progname */ sntp_init_logging(argv[0]); if (!libevent_version_ok()) exit(EX_SOFTWARE); init_lib(); init_auth(); optct = ntpOptionProcess(&sntpOptions, argc, argv); argc -= optct; argv += optct; debug = OPT_VALUE_SET_DEBUG_LEVEL; TRACE(2, ("init_lib() done, %s%s\n", (ipv4_works) ? "ipv4_works " : "", (ipv6_works) ? "ipv6_works " : "")); ntpver = OPT_VALUE_NTPVERSION; steplimit = OPT_VALUE_STEPLIMIT / 1e3; gap.tv_usec = max(0, OPT_VALUE_GAP * 1000); gap.tv_usec = min(gap.tv_usec, 999999); if (HAVE_OPT(LOGFILE)) open_logfile(OPT_ARG(LOGFILE)); msyslog(LOG_INFO, "%s", sntpVersion); if (0 == argc && !HAVE_OPT(BROADCAST) && !HAVE_OPT(CONCURRENT)) { printf("%s: Must supply at least one of -b hostname, -c hostname, or hostname.\n", progname); exit(EX_USAGE); } /* ** Eventually, we probably want: ** - separate bcst and ucst timeouts (why?) ** - multiple --timeout values in the commandline */ response_timeout = OPT_VALUE_TIMEOUT; response_tv.tv_sec = response_timeout; response_tv.tv_usec = 0; /* IPv6 available? */ if (isc_net_probeipv6() != ISC_R_SUCCESS) { ai_fam_pref = AF_INET; TRACE(1, ("No ipv6 support available, forcing ipv4\n")); } else { /* Check for options -4 and -6 */ if (HAVE_OPT(IPV4)) ai_fam_pref = AF_INET; else if (HAVE_OPT(IPV6)) ai_fam_pref = AF_INET6; } /* TODO: Parse config file if declared */ /* ** Init the KOD system. ** For embedded systems with no writable filesystem, ** -K /dev/null can be used to disable KoD storage. */ kod_init_kod_db(OPT_ARG(KOD), FALSE); // HMS: Should we use arg-defalt for this too? if (HAVE_OPT(KEYFILE)) auth_init(OPT_ARG(KEYFILE), &keys); /* ** Considering employing a variable that prevents functions of doing ** anything until everything is initialized properly ** ** HMS: What exactly does the above mean? */ event_set_log_callback(&sntp_libevent_log_cb); if (debug > 0) event_enable_debug_mode(); #ifdef WORK_THREAD evthread_use_pthreads(); /* we use libevent from main thread only, locks should be academic */ if (debug > 0) evthread_enable_lock_debuging(); #endif evcfg = event_config_new(); if (NULL == evcfg) { printf("%s: event_config_new() failed!\n", progname); return -1; } #ifndef HAVE_SOCKETPAIR event_config_require_features(evcfg, EV_FEATURE_FDS); #endif /* all libevent calls are from main thread */ /* event_config_set_flag(evcfg, EVENT_BASE_FLAG_NOLOCK); */ base = event_base_new_with_config(evcfg); event_config_free(evcfg); if (NULL == base) { printf("%s: event_base_new() failed!\n", progname); return -1; } /* wire into intres resolver */ worker_per_query = TRUE; addremove_io_fd = &sntp_addremove_fd; open_sockets(); if (HAVE_OPT(BROADCAST)) { int cn = STACKCT_OPT( BROADCAST ); const char ** cp = STACKLST_OPT( BROADCAST ); while (cn-- > 0) { handle_lookup(*cp, CTX_BCST); cp++; } } if (HAVE_OPT(CONCURRENT)) { int cn = STACKCT_OPT( CONCURRENT ); const char ** cp = STACKLST_OPT( CONCURRENT ); while (cn-- > 0) { handle_lookup(*cp, CTX_UCST | CTX_CONC); cp++; } } for (i = 0; i < argc; ++i) handle_lookup(argv[i], CTX_UCST); gettimeofday_cached(base, &start_tv); event_base_dispatch(base); event_base_free(base); if (!time_adjusted && (ENABLED_OPT(STEP) || ENABLED_OPT(SLEW))) exitcode = 1; else exitcode = 0; return exitcode; }
/*{{{ 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[]) { #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; }
void *http_server(apr_thread_t* t, void* d) { http_server_data_t *server_data = d; struct event_config *cfg = event_config_new(); struct event_base *base = NULL; struct evhttp *http = NULL; int i = 0; event_config_set_flag(cfg, EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST); base = event_base_new_with_config(cfg); if (!base) { LOG4C_ERROR(logger, "Couldn't create an event_base: exiting"); goto done; } event_config_free(cfg); /* Create a new evhttp object to handle requests. */ http = evhttp_new(base); if (!http) { LOG4C_ERROR(logger, "couldn't create evhttp. Exiting."); goto done; } evhttp_add_server_alias(http, "TVersity-Virtual-STB"); for (i = 0; i < server_data->num_handlers; i++) { evhttp_set_cb(http, server_data->handlers[i].uri, http_request_cb, &(server_data->handlers[i])); } //set up static file handler if (server_data->static_root) { evhttp_set_gencb(http, static_files_cb, (void *) server_data->static_root); } struct evhttp_bound_socket *handle = evhttp_accept_socket_with_handle(http, server_data->sock); if (!handle) { LOG4C_ERROR(logger, "could not accept on socket"); goto done; } server_base[server_data->server_index] = base; event_base_dispatch(base); done: if(server_data) free(server_data); if(http) { evhttp_free(http); } if (base) { server_base[server_data->server_index] = NULL; event_base_free(base); } apr_atomic_dec32(&num_servers); return NULL; }
int tnt_fork(int imsg_fds[2]) { pid_t pid; struct imsgbuf ibuf; struct imsg_data data; struct passwd *pw; struct event_base *evbase = NULL; struct event *sigterm = NULL; struct event *sigint = NULL; struct event *imsg_event = NULL; struct server server; struct event_config *evcfg; switch ((pid = fork())) { case -1: log_err(TNT_OSERR, "fork"); break; case 0: tnt_setproctitle("[unpriv]"); log_set_prefix("unpriv"); break; default: tnt_setproctitle("[priv]"); log_set_prefix("priv"); return pid; } if ((pw = getpwnam(TNETACLE_USER)) == NULL) { log_errx(1, "unknown user " TNETACLE_USER); return TNT_NOUSER; } /*Allocate the event config*/ evcfg = event_config_new(); /* Initialize the OpenSSL library */ SSL_library_init(); SSL_load_error_strings(); /* We MUST have entropy, or else there's no point to crypto. */ if (!RAND_poll()) { log_errx(TNT_SOFTWARE, "[INIT] failed to find an entropy source"); /* never returns */ } if (serv_opts.encryption) server.server_ctx = evssl_init(); else server.server_ctx = NULL; tnt_priv_drop(pw); #if defined(Darwin) /* It's sad isn't it ?*/ event_config_avoid_method(evcfg, "kqueue"); event_config_avoid_method(evcfg, "poll"); event_config_avoid_method(evcfg, "devpoll"); if ((evbase = event_base_new_with_config(evcfg)) == NULL) { log_err(1, "libevent"); } #else if ((evbase = event_base_new_with_config(evcfg)) == NULL) { log_err(1, "libevent"); } #endif sigterm = event_new(evbase, SIGTERM, EV_SIGNAL, &chld_sighdlr, evbase); sigint = event_new(evbase, SIGINT, EV_SIGNAL, &chld_sighdlr, evbase); signal(SIGPIPE, SIG_IGN); signal(SIGHUP, SIG_IGN); signal(SIGCHLD, SIG_DFL); if (server_init(&server, evbase) == -1) log_errx(1, "failed to init the server socket"); data.ibuf = &ibuf; data.evbase = evbase; data.server = &server; imsg_event = init_pipe_endpoint(imsg_fds, &data); event_add(sigterm, NULL); event_add(sigint, NULL); event_add(imsg_event, NULL); log_info("tnetacle ready"); /* Immediately request the creation of a tun interface */ imsg_compose(&ibuf, IMSG_CREATE_DEV, 0, 0, -1, NULL, 0); log_info("starting event loop"); event_base_dispatch(evbase); /* cleanely exit */ msgbuf_write(&ibuf.w); msgbuf_clear(&ibuf.w); /* Shutdown the server */ server_delete(&server); /* * It may look like we freed this one twice, * once here and once in tnetacled.c but this is not the case. * Please don't erase this ! */ event_free(sigterm); event_free(sigint); close(event_get_fd(imsg_event)); event_free(imsg_event); event_base_free(evbase); event_config_free(evcfg); log_info("tnetacle exiting"); exit(TNT_OK); }