/** Return a newly allocated, ready-for-use mutex. */ tor_mutex_t * tor_mutex_new(void) { tor_mutex_t *m = tor_malloc_zero(sizeof(tor_mutex_t)); tor_mutex_init(m); return m; }
/** Initialize the global logging facility */ void init_logging(void) { if (!log_mutex_initialized) { tor_mutex_init(&log_mutex); log_mutex_initialized = 1; } }
/** Allocate a new reply queue. Reply queues are used to pass results from * worker threads to the main thread. Since the main thread is running an * IO-centric event loop, it needs to get woken up with means other than a * condition variable. */ replyqueue_t * replyqueue_new(uint32_t alertsocks_flags) { replyqueue_t *rq; rq = tor_malloc_zero(sizeof(replyqueue_t)); if (alert_sockets_create(&rq->alert, alertsocks_flags) < 0) { tor_free(rq); return NULL; } tor_mutex_init(&rq->lock); TOR_TAILQ_INIT(&rq->answers); return rq; }
/** Install signal handlers as needed so that when we crash, we produce a * useful stack trace. Return 0 on success, -1 on failure. */ static int install_bt_handler(void) { int trap_signals[] = { SIGSEGV, SIGILL, SIGFPE, SIGBUS, SIGSYS, SIGIO, -1 }; int i, rv=0; struct sigaction sa; tor_mutex_init(&cb_buf_mutex); memset(&sa, 0, sizeof(sa)); sa.sa_sigaction = crash_handler; sa.sa_flags = SA_SIGINFO; sigfillset(&sa.sa_mask); for (i = 0; trap_signals[i] >= 0; ++i) { if (sigaction(trap_signals[i], &sa, NULL) == -1) { /* LCOV_EXCL_START */ log_warn(LD_BUG, "Sigaction failed: %s", strerror(errno)); rv = -1; /* LCOV_EXCL_STOP */ } } { /* Now, generate (but do not log) a backtrace. This ensures that * libc has pre-loaded the symbols we need to dump things, so that later * reads won't be denied by the sandbox code */ char **symbols; size_t depth = backtrace(cb_buf, MAX_DEPTH); symbols = backtrace_symbols(cb_buf, (int) depth); if (symbols) raw_free(symbols); } return rv; }
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; } }