//--------------------------------------------------------------------- // 初始化 devpoll描述符 //--------------------------------------------------------------------- static int apu_init_pd(apolld ipd, int param) { PSTRUCT *ps = PDESC(ipd); int flags = O_RDWR; struct rlimit rl; #ifdef O_CLOEXEC flags |= O_CLOEXEC; #endif ps->dpfd = open("/dev/poll", flags); if (ps->dpfd < 0) return -1; #if (!defined(O_CLOEXEC)) && defined(FD_CLOEXEC) if (fcntl(ps->dpfd, F_SETFD, FD_CLOEXEC) < 0) { close(ps->dpfd); ps->dpfd = -1; return -2; } #endif iv_init(&ps->vresult, NULL); iv_init(&ps->vchange, NULL); apr_poll_fvinit(&ps->fv, NULL); ps->max_fd = 0; ps->num_fd = 0; ps->max_chg = 0; ps->num_chg = 0; ps->usr_len = 0; ps->limit = 32000; if (getrlimit(RLIMIT_NOFILE, &rl) == 0) { if (rl.rlim_cur != RLIM_INFINITY) { if (rl.rlim_cur < 32000) { ps->limit = rl.rlim_cur; } } } if (apu_grow(ps, 4, 4)) { apu_destroy_pd(ipd); return -3; } param = param + 1; return 0; }
//--------------------------------------------------------------------- // itm_wlist_record - 记录到发送列表 //--------------------------------------------------------------------- int itm_wlist_record(long hid) { static struct IVECTOR wlist; static int inited = 0; struct ITMD *itmd; int retval; if (inited == 0) { iv_init(&wlist, NULL); retval = iv_resize(&wlist, 8); assert(retval == 0); itm_wlist = (long*)wlist.data; itm_wsize = 0; inited = 1; } if ((itm_wsize * sizeof(long)) >= (unsigned long)wlist.length) { retval = iv_resize(&wlist, wlist.length * 2); assert(retval == 0); itm_wlist = (long*)wlist.data; } itmd = itm_hid_itmd(hid); if (itmd == NULL) return -1; if (itmd->inwlist) return 1; itmd->inwlist = 1; itm_wlist[itm_wsize++] = hid; return 0; }
int main() { struct skaidrus_instance si; fprintf(stderr, "skaidrus, an example transparent proxy application\n"); fprintf(stderr, "\n"); if (geteuid() != 0) { fprintf(stderr, "error: need root privileges for performing " "transparent proxy functions\n"); return 1; } if (detect_tproxy_v2()) { fprintf(stderr, "error: version >= 2.0.0 of linux transparent " "proxy patches not detected\n"); return 1; } iv_init(); openlog("skaidrus", LOG_NDELAY | LOG_PERROR, LOG_USER); si.local_addr.sin_family = AF_INET; si.local_addr.sin_addr.s_addr = htonl(INADDR_ANY); si.local_addr.sin_port = htons(9841); if (!register_skaidrus_instance(&si)) { fprintf(stderr, "can't register skaidrus instance\n"); return 1; } fprintf(stderr, "up and running\n"); iv_main(); return 0; }
void app_startup(void) { msg_init(FALSE); iv_set_fatal_msg_handler(app_fatal); iv_init(); g_thread_init(NULL); crypto_init(); hostname_global_init(); dns_caching_global_init(); dns_caching_thread_init(); afinter_global_init(); child_manager_init(); alarm_init(); stats_init(); tzset(); log_msg_global_init(); log_tags_global_init(); log_source_global_init(); log_template_global_init(); value_pairs_global_init(); service_management_init(); scratch_buffers_allocator_init(); main_loop_thread_resource_init(); nondumpable_setlogger(nondumpable_allocator_logger); secret_storage_init(); transport_factory_id_global_init(); }
static void iv_work_thread(void *_thr) { struct work_pool_thread *thr = _thr; struct work_pool_priv *pool = thr->pool; iv_init(); INIT_IV_LIST_HEAD(&thr->list); thr->kicked = 0; IV_EVENT_INIT(&thr->kick); thr->kick.cookie = thr; thr->kick.handler = iv_work_thread_got_event; iv_event_register(&thr->kick); IV_TIMER_INIT(&thr->idle_timer); thr->idle_timer.cookie = thr; thr->idle_timer.handler = iv_work_thread_idle_timeout; if (pool->thread_start != NULL) pool->thread_start(pool->cookie); iv_event_post(&thr->kick); iv_main(); iv_deinit(); }
static void log_threaded_dest_driver_worker_thread_main(gpointer arg) { LogThrDestDriver *self = (LogThrDestDriver *)arg; iv_init(); msg_debug("Worker thread started", evt_tag_str("driver", self->super.super.id), NULL); log_queue_set_use_backlog(self->queue, TRUE); log_threaded_dest_driver_init_watches(self); log_threaded_dest_driver_start_watches(self); if (self->worker.thread_init) self->worker.thread_init(self); iv_main(); __disconnect(self); if (self->worker.thread_deinit) self->worker.thread_deinit(self); msg_debug("Worker thread finished", evt_tag_str("driver", self->super.super.id), NULL); iv_deinit(); }
static gpointer _threaded_consume(gpointer st) { LogQueue *q = (LogQueue *) st; LogMessage *msg; LogPathOptions path_options = LOG_PATH_OPTIONS_INIT; gint loops = 0; gint msg_count = 0; /* just to make sure time is properly cached */ iv_init(); while (msg_count < MESSAGES_SUM) { gint slept = 0; msg = NULL; while((msg = log_queue_pop_head(q, &path_options)) == NULL) { struct timespec ns; /* sleep 1 msec */ ns.tv_sec = 0; ns.tv_nsec = 1000000; nanosleep(&ns, NULL); slept++; if (slept > 10000) { /* slept for more than 10 seconds */ fprintf(stderr, "The wait for messages took too much time, loops=%d, msg_count=%d\n", loops, msg_count); return GUINT_TO_POINTER(1); } } if ((loops % 10) == 0) { /* push the message back to the queue */ log_queue_push_head(q, msg, &path_options); } else { log_msg_ack(msg, &path_options, AT_PROCESSED); log_msg_unref(msg); msg_count++; } loops++; } iv_deinit(); return NULL; }
int main() { iv_init(); IV_EVENT_RAW_INIT(&ev0); ev0.handler = gotev0; iv_event_raw_register(&ev0); iv_event_raw_post(&ev0); iv_main(); iv_deinit(); return 0; }
int main() { struct req req[NUM]; int i; iv_init(); for (i = 0; i < NUM; i++) open_child_request(&req[i]); iv_main(); iv_deinit(); return 0; }
static void thr_child(void *_dummy) { iv_init(); IV_EVENT_INIT(&ev_child); ev_child.handler = got_ev_child; iv_event_register(&ev_child); iv_validate_now(); tim_start = iv_now; iv_event_post(&ev_parent); iv_main(); iv_deinit(); }
static gpointer _output_thread(gpointer args) { WorkerOptions wo; iv_init(); wo.is_output_thread = TRUE; main_loop_worker_thread_start(&wo); struct timespec ns; /* sleep 1 msec */ ns.tv_sec = 0; ns.tv_nsec = 1000000; nanosleep(&ns, NULL); main_loop_worker_thread_stop(); return NULL; }
gpointer threaded_feed(gpointer args) { LogQueue *q = args; char *msg_str = "<155>2006-02-11T10:34:56+01:00 bzorp syslog-ng[23323]: árvíztűrőtükörfúrógép"; gint msg_len = strlen(msg_str); gint i; LogPathOptions path_options = LOG_PATH_OPTIONS_INIT; LogMessage *msg, *tmpl; GTimeVal start, end; GSockAddr *sa; glong diff; iv_init(); /* emulate main loop for LogQueue */ main_loop_worker_thread_start(NULL); sa = g_sockaddr_inet_new("10.10.10.10", 1010); tmpl = log_msg_new(msg_str, msg_len, sa, &parse_options); g_sockaddr_unref(sa); g_get_current_time(&start); for (i = 0; i < MESSAGES_PER_FEEDER; i++) { msg = log_msg_clone_cow(tmpl, &path_options); log_msg_add_ack(msg, &path_options); msg->ack_func = test_ack; log_queue_push_tail(q, msg, &path_options); if ((i & 0xFF) == 0) main_loop_worker_invoke_batch_callbacks(); } main_loop_worker_invoke_batch_callbacks(); g_get_current_time(&end); diff = g_time_val_diff(&end, &start); g_static_mutex_lock(&tlock); sum_time += diff; g_static_mutex_unlock(&tlock); log_msg_unref(tmpl); iv_deinit(); main_loop_worker_thread_stop(); return NULL; }
int main() { iv_init(); iv_thread_set_debug_state(1); iv_thread_create("return", thr_return, NULL); iv_thread_create("selfcancel", thr_selfcancel, NULL); iv_thread_create("exit", thr_exit, NULL); iv_thread_list_children(); iv_main(); iv_deinit(); return 0; }
void app_startup(void) { main_thread_handle = g_thread_self(); msg_init(FALSE); iv_init(); g_thread_init(NULL); afinter_global_init(); child_manager_init(); dns_cache_init(); alarm_init(); stats_init(); tzset(); log_msg_global_init(); log_tags_init(); log_source_global_init(); log_template_global_init(); }
int main() { iv_init(); iv_thread_set_debug_state(1); IV_WORK_POOL_INIT(&pool); pool.max_threads = 8; iv_work_pool_create(&pool); IV_WORK_ITEM_INIT(&item_a); item_a.cookie = "a"; item_a.work = work; item_a.completion = work_complete; iv_work_pool_submit_work(&pool, &item_a); IV_WORK_ITEM_INIT(&item_b); item_b.cookie = "b"; item_b.work = work; item_b.completion = work_complete; iv_work_pool_submit_work(&pool, &item_b); IV_WORK_ITEM_INIT(&item_c); item_c.cookie = "c"; item_c.work = work; item_c.completion = work_complete; iv_work_pool_submit_work(&pool, &item_c); IV_WORK_ITEM_INIT(&item_d); item_d.cookie = "d"; item_d.work = work; item_d.completion = work_complete; iv_work_pool_submit_work(&pool, &item_d); item_count = 4; iv_main(); iv_deinit(); return 0; }
int main() { long long nsec; iv_init(); #ifdef USE_SIGNAL signal(SIGALRM, got_signal_timeout); alarm(5); #else IV_TIMER_INIT(&timeout); iv_validate_now(); timeout.expires = iv_now; timeout.expires.tv_sec += 5; timeout.handler = got_timer_timeout; iv_timer_register(&timeout); #endif IV_SIGNAL_INIT(&is); is.signum = SIGUSR1; is.handler = got_sig; iv_signal_register(&is); iv_validate_now(); tim_start = iv_now; raise(SIGUSR1); iv_main(); iv_deinit(); nsec = 1000000000ULL * (tim_end.tv_sec - tim_start.tv_sec) + (tim_end.tv_nsec - tim_start.tv_nsec); printf("%s: %d in %ld nsec => %d/sec\n", iv_poll_method_name(), sig_received, (long)nsec, (int)(1000000000ULL * sig_received / nsec)); return 0; }
void app_startup(void) { msg_init(FALSE); iv_set_fatal_msg_handler(app_fatal); iv_init(); g_thread_init(NULL); hostname_global_init(); dns_cache_global_init(); dns_cache_thread_init(); afinter_global_init(); child_manager_init(); alarm_init(); stats_init(); tzset(); log_msg_global_init(); log_tags_global_init(); log_source_global_init(); log_template_global_init(); service_management_init(); }
int main() { long long nsec; iv_init(); #ifdef USE_SIGNAL signal(SIGALRM, got_signal_timeout); alarm(5); #else IV_TIMER_INIT(&timeout); iv_validate_now(); timeout.expires = iv_now; timeout.expires.tv_sec += 5; timeout.handler = got_timer_timeout; iv_timer_register(&timeout); #endif IV_EVENT_RAW_INIT(&ev); ev.handler = got_ev; iv_event_raw_register(&ev); iv_validate_now(); tim_start = iv_now; iv_event_raw_post(&ev); iv_main(); iv_deinit(); nsec = 1000000000ULL * (tim_end.tv_sec - tim_start.tv_sec) + (tim_end.tv_nsec - tim_start.tv_nsec); printf("%s: %d in %ld nsec => %d/sec\n", iv_poll_method_name(), ev_received, (long)nsec, (int)(1000000000ULL * ev_received / nsec)); return 0; }
static gpointer _threaded_feed(gpointer args) { LogQueue *q = args; gint i; LogPathOptions path_options = LOG_PATH_OPTIONS_INIT; LogMessage *msg, *tmpl; GTimeVal start, end; glong diff; iv_init(); /* emulate main loop for LogQueue */ main_loop_worker_thread_start(NULL); tmpl = log_msg_new_empty(); g_get_current_time(&start); for (i = 0; i < MESSAGES_PER_FEEDER; i++) { msg = log_msg_clone_cow(tmpl, &path_options); log_msg_add_ack(msg, &path_options); msg->ack_func = test_ack; log_queue_push_tail(q, msg, &path_options); if ((i & 0xFF) == 0) main_loop_worker_invoke_batch_callbacks(); } main_loop_worker_invoke_batch_callbacks(); g_get_current_time(&end); diff = g_time_val_diff(&end, &start); g_static_mutex_lock(&tlock); sum_time += diff; g_static_mutex_unlock(&tlock); log_msg_unref(tmpl); main_loop_worker_thread_stop(); iv_deinit(); return NULL; }
int main(int argc, char **argv) { struct iv_inotify inotify; int i; if (argc < 2) { fprintf(stderr, "Usage: %s FILE...\n", argv[0]); return 1; } iv_init(); IV_INOTIFY_INIT(&inotify); iv_inotify_register(&inotify); for (i = 1; i < argc; i++) { struct iv_inotify_watch *w; w = malloc(sizeof(*w)); if (w == NULL) { perror("malloc"); return 1; } IV_INOTIFY_WATCH_INIT(w); w->inotify = &inotify; w->pathname = argv[i]; w->mask = IN_ALL_EVENTS; w->cookie = w; w->handler = test_handler; iv_inotify_watch_register(w); } iv_main(); iv_deinit(); return 0; }
int main() { alarm(5); iv_init(); IV_TASK_INIT(&task); task.handler = handler_task; iv_task_register(&task); IV_TIMER_INIT(&timer); iv_validate_now(); timer.expires = iv_now; timer.expires.tv_sec--; timer.handler = handler_timer; iv_main(); iv_deinit(); return !success; }
//--------------------------------------------------------------------- // 初始化描述列表 //--------------------------------------------------------------------- void apr_poll_fvinit(struct APOLLFV *fv, struct IALLOCATOR *allocator) { iv_init(&fv->vec, allocator); fv->count = 0; fv->fds = NULL; }
//--------------------------------------------------------------------- // itm_startup //--------------------------------------------------------------------- int itm_startup(void) { long retval, i; if (itm_mode) return 0; // 异步I/O驱动初始化 retval = apr_poll_install(APDEVICE_AUTO); if (retval) { itm_log(ITML_BASE, "service starting failed: poll device error %d", retval); return -10 + retval; } // 内存节点管理器初始化 imp_init(&itm_fds, sizeof(struct ITMD), NULL); imp_init(&itm_mem, itm_psize, NULL); // 初始化内存增长限制 itm_mem.grow_limit = 4096; // 套接字初始化 retval = itm_socket_create(); if (retval) { itm_log(ITML_BASE, "service starting failed: listen error code=%d errno=%d", retval, apr_errno()); return -20 + retval; } retval = apr_poll_init(&itm_polld, 0x20000); if (retval) { itm_socket_release(); itm_log(ITML_BASE, "service starting failed: poll init error %d", retval); return -30 + retval; } retval = apr_poll_add(itm_polld, itm_outer_sock4, APOLL_IN, &itmd_outer4); if (retval) { itm_socket_release(); apr_poll_destroy(itm_polld); itm_polld = NULL; itm_log(ITML_BASE, "service starting failed: poll event error %d", retval); return -40 + retval; } retval = apr_poll_add(itm_polld, itm_inner_sock4, APOLL_IN, &itmd_inner4); if (retval) { itm_socket_release(); apr_poll_destroy(itm_polld); itm_polld = NULL; itm_log(ITML_BASE, "service starting failed: poll event error %d", retval); return -50 + retval; } retval = apr_poll_add(itm_polld, itm_dgram_sock4, 0, &itmd_dgram4); if (retval) { itm_socket_release(); apr_poll_destroy(itm_polld); itm_polld = NULL; itm_log(ITML_BASE, "service starting failed: poll event error %d", retval); return -60 + retval; } itm_mask(&itmd_dgram4, APOLL_IN, 0); #ifdef AF_INET if (itm_outer_sock6 >= 0) { retval = apr_poll_add(itm_polld, itm_outer_sock6, APOLL_IN, &itmd_outer6); if (retval) { itm_socket_release(); apr_poll_destroy(itm_polld); itm_polld = NULL; itm_log(ITML_BASE, "service starting failed: poll event error %d", retval); return -70 + retval; } } if (itm_inner_sock6 >= 0) { retval = apr_poll_add(itm_polld, itm_inner_sock6, APOLL_IN, &itmd_inner6); if (retval) { itm_socket_release(); apr_poll_destroy(itm_polld); itm_polld = NULL; itm_log(ITML_BASE, "service starting failed: poll event error %d", retval); return -80 + retval; } } if (itm_dgram_sock6 >= 0) { retval = apr_poll_add(itm_polld, itm_dgram_sock6, 0, &itmd_dgram6); if (retval) { itm_socket_release(); apr_poll_destroy(itm_polld); itm_polld = NULL; itm_log(ITML_BASE, "service starting failed: poll event error %d", retval); return -90 + retval; } itm_mask(&itmd_dgram6, APOLL_IN, 0); } #endif idt_init(&itm_timeu, itm_outer_time, NULL); idt_init(&itm_timec, itm_inner_time, NULL); iv_init(&itm_hostv, NULL); iv_init(&itm_datav, NULL); if (itm_datamax < 0x100000) itm_datamax = 0x100000; itm_dsize(itm_datamax + 0x10000); itm_wchannel(32000, NULL); ims_init(&itm_dgramdat4, &itm_mem); ims_init(&itm_dgramdat6, &itm_mem); itm_state = 0; itm_outer_cnt = 0; itm_inner_cnt = 0; itm_wtime = 0; itm_stime = (int)time(NULL); itm_time_start = apr_timex() / 1000; itm_time_current = itm_time_start; itm_time_slap = itm_time_start + ITMD_TIME_CYCLE; itm_notice_slap = 0; itm_notice_cycle = -1; itm_notice_count = 0; itm_version = 0; itm_docsize = 0; itm_document[0] = 0; imp_init(&itm_msg_mem, 4096, NULL); ims_init(&itm_msg_n0, &itm_msg_mem); ims_init(&itm_msg_n1, &itm_msg_mem); apr_mutex_init(&itm_msg_lock); itm_msg_cnt0 = 0; itm_msg_cnt1 = 0; itm_wsize = 0; switch (itm_headmod) { case ITMH_WORDLSB: case ITMH_WORDMSB: case ITMH_EWORDLSB: case ITMH_EWORDMSB: itm_headlen = 12; itm_hdrsize = 2; break; case ITMH_DWORDLSB: case ITMH_DWORDMSB: case ITMH_EDWORDLSB: case ITMH_EDWORDMSB: case ITMH_DWORDMASK: itm_headlen = 14; itm_hdrsize = 4; break; case ITMH_BYTELSB: case ITMH_BYTEMSB: case ITMH_EBYTELSB: case ITMH_EBYTEMSB: itm_headlen = 11; itm_hdrsize = 1; break; case ITMH_RAWDATA: itm_headlen = 14; itm_hdrsize = 4; break; case ITMH_LINESPLIT: itm_headlen = 14; itm_hdrsize = 4; break; default: itm_headmod = ITMH_WORDLSB; itm_headlen = 12; itm_hdrsize = 2; break; } if (itm_headmod < ITMH_EWORDLSB) { itm_headint = itm_headmod; itm_headinc = 0; itm_headmsk = 0; } else if (itm_headmod < ITMH_DWORDMASK) { itm_headint = itm_headmod - 6; itm_headinc = itm_hdrsize; itm_headmsk = 0; } else if (itm_headmod == ITMH_DWORDMASK) { itm_headint = ITMH_DWORDLSB; itm_headinc = 0; itm_headmsk = 1; } else if (itm_headmod == ITMH_RAWDATA) { itm_headint = ITMH_DWORDLSB; itm_headinc = 0; itm_headmsk = 0; } else if (itm_headmod == ITMH_LINESPLIT) { itm_headint = ITMH_DWORDLSB; itm_headinc = 0; itm_headmsk = 0; } else { itm_headint = ITMH_DWORDLSB; itm_headinc = 0; itm_headmsk = 0; } for (i = 0; i < 512; i++) { itm_book[i] = NULL; itm_booklen[i] = 0; iv_init(&itm_bookv[i], NULL); } itm_mode = 1; itm_stat_send = 0; itm_stat_recv = 0; itm_stat_discard = 0; if (itm_limit < itm_inner_blimit * 2) { itm_limit = itm_inner_blimit * 2; } if (itm_dhcp_base < 100) itm_dhcp_base = 100; if (itm_dhcp_high < itm_dhcp_base) itm_dhcp_high = itm_dhcp_base; itm_log(ITML_BASE, "Transmod %x.%d%d (%s, %s) started ....", ITMV_VERSION >> 8, (ITMV_VERSION & 0xf0) >> 4, ITMV_VERSION & 0x0f, __DATE__, __TIME__); return 0; }