int main(int argc, char **argv) { replyqueue_t *rq; threadpool_t *tp; int i; tor_libevent_cfg evcfg; struct event *ev; uint32_t as_flags = 0; for (i = 1; i < argc; ++i) { if (!strcmp(argv[i], "-v")) { opt_verbose = 1; } else if (!strcmp(argv[i], "-T") && i+1<argc) { opt_n_threads = atoi(argv[++i]); } else if (!strcmp(argv[i], "-N") && i+1<argc) { opt_n_items = atoi(argv[++i]); } else if (!strcmp(argv[i], "-I") && i+1<argc) { opt_n_inflight = atoi(argv[++i]); } else if (!strcmp(argv[i], "-L") && i+1<argc) { opt_n_lowwater = atoi(argv[++i]); } else if (!strcmp(argv[i], "-R") && i+1<argc) { opt_ratio_rsa = atoi(argv[++i]); } else if (!strcmp(argv[i], "-C") && i+1<argc) { opt_n_cancel = atoi(argv[++i]); } else if (!strcmp(argv[i], "--no-eventfd2")) { as_flags |= ASOCKS_NOEVENTFD2; } else if (!strcmp(argv[i], "--no-eventfd")) { as_flags |= ASOCKS_NOEVENTFD; } else if (!strcmp(argv[i], "--no-pipe2")) { as_flags |= ASOCKS_NOPIPE2; } else if (!strcmp(argv[i], "--no-pipe")) { as_flags |= ASOCKS_NOPIPE; } else if (!strcmp(argv[i], "--no-socketpair")) { as_flags |= ASOCKS_NOSOCKETPAIR; } else if (!strcmp(argv[i], "-h")) { help(); return 0; } else { help(); return 1; } } if (opt_n_threads < 1 || opt_n_items < 1 || opt_n_inflight < 1 || opt_n_lowwater < 0 || opt_n_cancel > opt_n_inflight || opt_n_inflight > MAX_INFLIGHT || opt_ratio_rsa < 0) { help(); return 1; } init_logging(1); crypto_global_init(1, NULL, NULL); crypto_seed_rng(1); rq = replyqueue_new(as_flags); tor_assert(rq); tp = threadpool_new(opt_n_threads, rq, new_state, free_state, NULL); tor_assert(tp); crypto_seed_weak_rng(&weak_rng); memset(&evcfg, 0, sizeof(evcfg)); tor_libevent_initialize(&evcfg); ev = tor_event_new(tor_libevent_get_base(), replyqueue_get_socket(rq), EV_READ|EV_PERSIST, replysock_readable_cb, tp); event_add(ev, NULL); #ifdef TRACK_RESPONSES handled = bitarray_init_zero(opt_n_items); received = bitarray_init_zero(opt_n_items); tor_mutex_init(&bitmap_mutex); handled_len = opt_n_items; #endif for (i = 0; i < opt_n_inflight; ++i) { if (! add_work(tp)) { puts("Couldn't add work."); return 1; } } { struct timeval limit = { 30, 0 }; tor_event_base_loopexit(tor_libevent_get_base(), &limit); } event_base_loop(tor_libevent_get_base(), 0); if (n_sent != opt_n_items || n_received+n_successful_cancel != n_sent) { printf("%d vs %d\n", n_sent, opt_n_items); printf("%d+%d vs %d\n", n_received, n_successful_cancel, n_sent); puts("FAIL"); return 1; } else { puts("OK"); return 0; } }
replyqueue_process_cb(evutil_socket_t sock, short events, void *arg) { replyqueue_t *rq = arg; (void) sock; (void) events; replyqueue_process(rq); } /** Initialize the cpuworker subsystem. It is OK to call this more than once * during Tor's lifetime. */ MOCK_IMPL(void, cpu_init, (void)) { if (!replyqueue) { replyqueue = replyqueue_new(0); } if (!reply_event) { reply_event = tor_event_new(tor_libevent_get_base(), replyqueue_get_socket(replyqueue), EV_READ|EV_PERSIST, replyqueue_process_cb, replyqueue); event_add(reply_event, NULL); } if (!threadpool) { threadpool = threadpool_new(get_num_cpus(get_options()), replyqueue, worker_state_new, worker_state_free, NULL);