static void test_fingerprint(void *z) { struct Worker *server = NULL, *client = NULL; tt_assert(tls_init() == 0); /* both server & client with cert */ str_check(create_worker(&server, true, SERVER1, CA2, "verify-client=1", "peer-sha1=ssl/ca2_client2.crt.sha1", "peer-sha256=ssl/ca2_client2.crt.sha256", NULL), "OK"); str_check(create_worker(&client, false, CLIENT2, CA1, "host=server1.com", "peer-sha1=ssl/ca1_server1.crt.sha1", "peer-sha256=ssl/ca1_server1.crt.sha256", NULL), "OK"); str_check(run_case(client, server), "OK"); /* client without cert */ str_check(create_worker(&server, true, SERVER1, CA1, "verify-client=1", "peer-sha1=ssl/ca2_client2.crt.sha1", "peer-sha256=ssl/ca2_client2.crt.sha256", NULL), "OK"); str_check(create_worker(&client, false, CA1, "host=server1.com", NULL), "OK"); str_any2(run_case(client, server), "C:sslv3 alert handshake failure - S:peer did not return a certificate", "C:sslv3 alert handshake failure,C:shutdown while in init - S:peer did not return a certificate"); end:; }
static void test_verify(void *z) { struct Worker *server = NULL, *client = NULL; tt_assert(tls_init() == 0); /* default: client checks server cert, succeeds */ str_check(create_worker(&server, true, SERVER1, NULL), "OK"); str_check(create_worker(&client, false, CA1, "host=server1.com", NULL), "OK"); str_check(run_case(client, server), "OK"); /* default: client checks server cert, fails due to bad ca */ str_check(create_worker(&server, true, SERVER1, NULL), "OK"); str_check(create_worker(&client, false, CA2, "host=example.com", NULL), "OK"); str_check(run_case(client, server), "C:certificate verify failed - S:tlsv1 alert unknown ca"); /* default: client checks server cert, fails due to bad hostname */ str_check(create_worker(&server, true, SERVER1, NULL), "OK"); str_check(create_worker(&client, false, CA1, "host=example2.com", NULL), "OK"); str_check(run_case(client, server), "C:name `example2.com' not present in server certificate"); #if 0 /* client: aggressive close */ str_check(create_worker(&server, true, SERVER1, NULL), "OK"); str_check(create_worker(&client, false, CA1, "aggressive-close=1", "host=server1.com", NULL), "OK"); str_check(run_case(client, server), "S:bad pkt: res=-1 err=read failed: EOF,S:close error: res=-1 err=shutdown failed: Broken pipe"); /* server: aggressive close */ str_check(create_worker(&server, true, SERVER1, "aggressive-close=1", NULL), "OK"); str_check(create_worker(&client, false, CA1, "host=server1.com", NULL), "OK"); str_check(run_case(client, server), "C:write failed: Broken pipe,C:close error: res=-1 err=shutdown failed: Success"); #endif end:; }
static void test_fingerprint(void *z) { struct Worker *server = NULL, *client = NULL; tt_assert(tls_init() == 0); /* both server & client with cert */ str_check(create_worker(&server, true, SERVER1, CA2, "peer-sha1=ssl/ca2_client2.crt.sha1", "peer-sha256=ssl/ca2_client2.crt.sha256", NULL), "OK"); str_check(create_worker(&client, false, CLIENT2, CA1, "host=server1.com", "peer-sha1=ssl/ca1_server1.crt.sha1", "peer-sha256=ssl/ca1_server1.crt.sha256", NULL), "OK"); str_check(run_case(client, server), "OK"); /* client without cert */ str_check(create_worker(&server, true, SERVER1, CA1, "peer-sha1=ssl/ca2_client2.crt.sha1", "peer-sha256=ssl/ca2_client2.crt.sha256", NULL), "OK"); str_check(create_worker(&client, false, CA1, "host=server1.com", NULL), "OK"); str_check(run_case(client, server), "C:write!=3 - S:FP-sha1-fail"); end:; }
static void test_set_mem(void *z) { struct Worker *server = NULL, *client = NULL; tt_assert(tls_init() == 0); /* both server & client with cert */ str_check(create_worker(&server, true, "mem=1", SERVER1, CA2, NULL), "OK"); str_check(create_worker(&client, false, "mem=1", CLIENT2, CA1, "host=server1.com", NULL), "OK"); str_check(run_case(client, server), "OK"); end:; }
/* One group of senders and receivers */ static unsigned int group(pthread_t *pth, unsigned int num_fds, int ready_out, int wakefd) { unsigned int i; struct sender_context *snd_ctx = malloc(sizeof(struct sender_context) + num_fds * sizeof(int)); if (!snd_ctx) barf("malloc()"); for (i = 0; i < num_fds; i++) { int fds[2]; struct receiver_context *ctx = malloc(sizeof(*ctx)); if (!ctx) barf("malloc()"); /* Create the pipe between client and server */ fdpair(fds); ctx->num_packets = num_fds * loops; ctx->in_fds[0] = fds[0]; ctx->in_fds[1] = fds[1]; ctx->ready_out = ready_out; ctx->wakefd = wakefd; pth[i] = create_worker(ctx, (void *)receiver); snd_ctx->out_fds[i] = fds[1]; if (!thread_mode) close(fds[0]); } /* Now we have all the fds, fork the senders */ for (i = 0; i < num_fds; i++) { snd_ctx->ready_out = ready_out; snd_ctx->wakefd = wakefd; snd_ctx->num_fds = num_fds; pth[num_fds+i] = create_worker(snd_ctx, (void *)sender); } /* Close the fds we have left */ if (!thread_mode) for (i = 0; i < num_fds; i++) close(snd_ctx->out_fds[i]); /* Return number of children to reap */ return num_fds * 2; }
static void test_clientcert(void *z) { struct Worker *server = NULL, *client = NULL; tt_assert(tls_init() == 0); /* ok: server checks server cert */ str_check(create_worker(&server, true, SERVER1, CA2, NULL), "OK"); str_check(create_worker(&client, false, CLIENT2, CA1, "host=server1.com", NULL), "OK"); str_check(run_case(client, server), "OK"); /* fail: server rejects invalid cert */ str_check(create_worker(&server, true, SERVER1, CA1, NULL), "OK"); str_check(create_worker(&client, false, CLIENT2, CA1, "host=server1.com", NULL), "OK"); str_check(run_case(client, server), "C:tlsv1 alert unknown ca - S:handshake failure"); /* noverifycert: server allow invalid cert */ str_check(create_worker(&server, true, SERVER1, CA1, "noverifycert=1", NULL), "OK"); str_check(create_worker(&client, false, CLIENT2, CA1, "host=server1.com", NULL), "OK"); str_check(run_case(client, server), "OK"); /* allow client without cert */ str_check(create_worker(&server, true, SERVER1, CA2, NULL), "OK"); str_check(create_worker(&client, false, CA1, "host=server1.com", NULL), "OK"); str_check(run_case(client, server), "OK"); end:; }
void thread_init(int t_num, struct event_base *main_base) { dispatch_thread.base = main_base; dispatch_thread.thread_id = pthread_self(); int i; threads = calloc(t_num, sizeof(wk_thread)); if (!threads) { perror("Can't alloc so many thread\n"); exit(1); } for (i = 0; i < t_num; i++) { int fds[2]; if (pipe(fds)) { perror("can't pipe\n"); exit(1); } threads[i].notify_receive_fd = fds[0]; threads[i].notify_send_fd = fds[1]; setup_thread(&threads[i]); } for (i = 0; i < t_num; i++) { create_worker(worker_libevent, &threads[i]); } }
static void thread_init(global_conf_st * g_conf) { int i; pthread_mutex_init(&init_lock,NULL); pthread_cond_init(&init_cond,NULL); int nthreads = g_conf->max_thread; threads = calloc(nthreads,sizeof(work_thread)); if(!threads){ fprintf(stderr,"%s:%d :Cant allocate threads info",__FILE__,__LINE__); exit(-1); } for(i = 0 ;i < nthreads;i++){ threads[i].g_conf = g_conf; threads[i].msg_queue = g_async_queue_new(); } for(i = 0 ;i < nthreads;i++){ create_worker(worker_thread,&threads[i]); } pthread_mutex_lock(&init_lock); while(init_count < nthreads){ pthread_cond_wait(&init_cond,&init_lock); } pthread_mutex_unlock(&init_lock); }
int thr_pool_queue(thr_pool_t *pool, void *(*func)(void *), void *arg) { job_t *job; if ((job = malloc(sizeof (*job))) == NULL) { errno = ENOMEM; return (-1); } job->job_next = NULL; job->job_func = func; job->job_arg = arg; (void) pthread_mutex_lock(&pool->pool_mutex); if (pool->pool_head == NULL) pool->pool_head = job; else pool->pool_tail->job_next = job; pool->pool_tail = job; if (pool->pool_idle > 0) (void) pthread_cond_signal(&pool->pool_workcv); else if (pool->pool_nthreads < pool->pool_maximum && create_worker(pool) == 0) pool->pool_nthreads++; (void) pthread_mutex_unlock(&pool->pool_mutex); return (0); }
/* * Dispatch a work request to the thread pool. * If there are idle workers, awaken one. * Else, if the maximum number of workers has * not been reached, spawn a new worker thread. * Else just return with the job added to the queue. */ int tpool_dispatch(tpool_t *tpool, void (*func)(void *), void *arg) { tpool_job_t *job; if ((job = malloc(sizeof (*job))) == NULL) return (-1); bzero(job, sizeof(*job)); job->tpj_next = NULL; job->tpj_func = func; job->tpj_arg = arg; pthread_mutex_lock(&tpool->tp_mutex); if (tpool->tp_head == NULL) tpool->tp_head = job; else tpool->tp_tail->tpj_next = job; tpool->tp_tail = job; tpool->tp_njobs++; if (!(tpool->tp_flags & TP_SUSPEND)) { if (tpool->tp_idle > 0) (void) pthread_cond_signal(&tpool->tp_workcv); else if (tpool->tp_current < tpool->tp_maximum && create_worker(tpool) == 0) tpool->tp_current++; } pthread_mutex_unlock(&tpool->tp_mutex); return (0); }
static void test_clientcert(void *z) { struct Worker *server = NULL, *client = NULL; tt_assert(tls_init() == 0); /* ok: server checks client cert */ str_check(create_worker(&server, true, SERVER1, CA2, "verify-client=1", NULL), "OK"); str_check(create_worker(&client, false, CLIENT2, CA1, "host=server1.com", NULL), "OK"); str_check(run_case(client, server), "OK"); /* fail: server rejects invalid cert */ str_check(create_worker(&server, true, SERVER1, CA1, "verify-client=1", NULL), "OK"); str_check(create_worker(&client, false, CLIENT2, CA1, "host=server1.com", NULL), "OK"); str_any3(run_case(client, server), "C:tlsv1 alert unknown ca - S:no certificate returned", "C:tlsv1 alert unknown ca,C:shutdown while in init - S:certificate verify failed", "C:tlsv1 alert unknown ca - S:certificate verify failed"); /* noverifycert: server allow invalid cert */ str_check(create_worker(&server, true, SERVER1, CA1, "noverifycert=1", NULL), "OK"); str_check(create_worker(&client, false, CLIENT2, CA1, "host=server1.com", NULL), "OK"); str_check(run_case(client, server), "OK"); /* verify-client: don't allow client without cert */ str_check(create_worker(&server, true, SERVER1, CA2, "verify-client=1", NULL), "OK"); str_check(create_worker(&client, false, CA1, "host=server1.com", NULL), "OK"); str_any2(run_case(client, server), "C:sslv3 alert handshake failure - S:peer did not return a certificate", "C:sslv3 alert handshake failure,C:shutdown while in init - S:peer did not return a certificate"); /* verify-client-optional: allow client without cert */ str_check(create_worker(&server, true, SERVER1, CA2, "verify-client-optional=1", NULL), "OK"); str_check(create_worker(&client, false, CA1, "host=server1.com", NULL), "OK"); str_check(run_case(client, server), "OK"); end:; }
void create_modules() { ok(create_client( mod_gm_opt->server_list, &client ) == GM_OK, "created test client"); ok(create_worker( mod_gm_opt->server_list, &worker ) == GM_OK, "created test worker"); ok(worker_add_function( &worker, GM_DEFAULT_RESULT_QUEUE, get_results ) == GM_OK, "added result worker"); ok(worker_add_function( &worker, "dummy", dummy ) == GM_OK, "added dummy worker"); //gearman_worker_add_options(&worker, GEARMAN_WORKER_NON_BLOCKING); gearman_worker_set_timeout(&worker, 5000); return; }
static void test_cipher_nego(void *z) { struct Worker *server = NULL, *client = NULL; tt_assert(tls_init() == 0); /* server key is EC:secp384r1 - ECDHE-ECDSA */ str_check(create_worker(&server, true, "show=ciphers", SERVER1, NULL), "OK"); str_check(create_worker(&client, false, CA1, "ciphers=AESGCM", "host=server1.com", NULL), "OK"); str_any3(run_case(client, server), "TLSv1.2/ECDHE-ECDSA-AES256-GCM-SHA384/ECDH=secp384r1", "TLSv1.2/ECDHE-ECDSA-AES256-GCM-SHA384/ECDH=X25519", "TLSv1.2/ECDHE-ECDSA-AES256-GCM-SHA384"); /* server key is RSA - ECDHE-RSA */ str_check(create_worker(&server, true, "show=ciphers", SERVER2, NULL), "OK"); str_check(create_worker(&client, false, CA2, "ciphers=AESGCM", "host=server2.com", NULL), "OK"); str_any3(run_case(client, server), "TLSv1.2/ECDHE-RSA-AES256-GCM-SHA384/ECDH=prime256v1", "TLSv1.2/ECDHE-RSA-AES256-GCM-SHA384/ECDH=X25519", "TLSv1.2/ECDHE-RSA-AES256-GCM-SHA384"); /* server key is RSA - DHE-RSA */ str_check(create_worker(&server, true, SERVER2, "show=ciphers", "dheparams=auto", NULL), "OK"); str_check(create_worker(&client, false, CA2, "ciphers=EDH+AESGCM", "host=server2.com", NULL), "OK"); str_check(run_case(client, server), "TLSv1.2/DHE-RSA-AES256-GCM-SHA384/DH=2048"); /* server key is RSA - ECDHE-RSA */ str_check(create_worker(&server, true, SERVER2, "show=ciphers", NULL), "OK"); str_check(create_worker(&client, false, CA2, "ciphers=EECDH+AES", "host=server2.com", NULL), "OK"); str_any3(run_case(client, server), "TLSv1.2/ECDHE-RSA-AES256-GCM-SHA384/ECDH=prime256v1", "TLSv1.2/ECDHE-RSA-AES256-GCM-SHA384/ECDH=X25519", "TLSv1.2/ECDHE-RSA-AES256-GCM-SHA384"); end:; }
static void test_noverifyname(void *z) { struct Worker *server = NULL, *client = NULL; tt_assert(tls_init() == 0); /* noverifyname: client checks server cert, ignore bad hostname */ str_check(create_worker(&server, true, SERVER1, NULL), "OK"); str_check(create_worker(&client, false, CA1, "host=example2.com", "noverifyname=1", NULL), "OK"); str_check(run_case(client, server), "OK"); /* noverifyname: client checks server cert, ignore NULL hostname */ str_check(create_worker(&server, true, SERVER1, NULL), "OK"); str_check(create_worker(&client, false, CA1, "noverifyname=1", NULL), "OK"); str_check(run_case(client, server), "OK"); end:; }
/* * Initializes the thread subsystem, creating various worker threads. * * nthreads Number of worker event handler threads to spawn * main_base Event base for main thread */ void thread_init(int nthreads, struct event_base *main_base) { int i; pthread_mutex_init(&cache_lock, NULL);/*Lock for cache operations (item_*, assoc_*)*/ pthread_mutex_init(&stats_lock, NULL);/*Lock for global stats*/ pthread_mutex_init(&init_lock, NULL); pthread_cond_init(&init_cond, NULL); pthread_mutex_init(&cqi_freelist_lock, NULL); cqi_freelist = NULL; threads = calloc(nthreads, sizeof(LIBEVENT_THREAD)); /* 分配一个数组 ,数据大小为nthreads , * 数据的的每个元素的内存空间为 sizeof(LIEVENT_THREAD) 并且,返回一个指针, * 指向该数组*/ /* Allocate nthreads elements of sizeof(LIBEVENT_THREAD) bytes each, all initialized to 0. */ if (! threads) {//很明显这是是提示说tmd竟然分配内存失败了! perror("Can't allocate thread descriptors"); exit(1); } dispatcher_thread.base = main_base; /*main_base这个是主线程的libevent句柄,这个在main()方法里面初始化*/ dispatcher_thread.thread_id = pthread_self();/*获取当前线程的id 。*/ for (i = 0; i < nthreads; i++) { int fds[2]; if (pipe(fds)) {//创建一个管道符,用于两个进程之间的联系 perror("Can't create notify pipe"); exit(1); } threads[i].notify_receive_fd = fds[0]; threads[i].notify_send_fd = fds[1]; setup_thread(&threads[i]);//设置一些线程信息 } /* Create threads after we've done all the libevent setup. */ for (i = 0; i < nthreads; i++) { //顾名思义,每个线程的信息初始化完毕,开始创建线程 create_worker(worker_libevent, &threads[i]); } /* * 主线程等所有线程创建完毕之后再结束 * Wait for all the threads to set themselves up before returning. */ pthread_mutex_lock(&init_lock); while (init_count < nthreads) { pthread_cond_wait(&init_cond, &init_lock); } pthread_mutex_unlock(&init_lock); }
/* * Initializes the thread subsystem, creating various worker threads. * * nthreads Number of worker event handler threads to spawn * main_base Event base for main thread */ void thread_init(int nthr, struct event_base *main_base, void (*dispatcher_callback)(int, short, void *)) { int i; nthreads = nthr + 1; pthread_mutex_init(&stats_lock, NULL); pthread_mutex_init(&init_lock, NULL); pthread_cond_init(&init_cond, NULL); pthread_mutex_init(&cqi_freelist_lock, NULL); cqi_freelist = NULL; threads = calloc(nthreads, sizeof(LIBEVENT_THREAD)); if (! threads) { settings.extensions.logger->log(EXTENSION_LOG_WARNING, NULL, "Can't allocate thread descriptors: %s", strerror(errno)); exit(1); } thread_ids = calloc(nthreads, sizeof(pthread_t)); if (! thread_ids) { perror("Can't allocate thread descriptors"); exit(1); } setup_dispatcher(main_base, dispatcher_callback); for (i = 0; i < nthreads; i++) { if (!create_notification_pipe(&threads[i])) { exit(1); } threads[i].index = i; setup_thread(&threads[i], i == (nthreads - 1)); } /* Create threads after we've done all the libevent setup. */ for (i = 0; i < nthreads; i++) { create_worker(worker_libevent, &threads[i], &thread_ids[i]); threads[i].thread_id = thread_ids[i]; } tap_thread = &threads[nthreads - 1]; /* Wait for all the threads to set themselves up before returning. */ pthread_mutex_lock(&init_lock); while (init_count < nthreads) { pthread_cond_wait(&init_cond, &init_lock); } pthread_mutex_unlock(&init_lock); }
/* * Worker thread is terminating. Possible reasons: * - excess idle thread is terminating because there is no work. * - thread was cancelled (pool is being destroyed). * - the job function called pthread_exit(). * In the last case, create another worker thread * if necessary to keep the pool populated. */ void worker_cleanup(thr_pool_t *pool) { --pool->pool_nthreads; if (pool->pool_flags & POOL_DESTROY) { if (pool->pool_nthreads == 0) (void) pthread_cond_broadcast(&pool->pool_busycv); } else if (pool->pool_head != NULL && pool->pool_nthreads < pool->pool_maximum && create_worker(pool) == 0) { pool->pool_nthreads++; } (void) pthread_mutex_unlock(&pool->pool_mutex); }
/* * Initializes the thread subsystem, creating various worker threads. * * nthreads Number of worker event handler threads to spawn * main_base Event base for main thread */ void thread_init(int nthreads, struct event_base *main_base) { int i; int power; pthread_mutex_init(&cache_lock, NULL); pthread_mutex_init(&init_lock, NULL); pthread_cond_init(&init_cond, NULL); pthread_mutex_init(&cqi_freelist_lock, NULL); cqi_freelist = NULL; threads = (LIBEVENT_THREAD *)calloc(nthreads, sizeof(LIBEVENT_THREAD)); if (! threads) { log_debug(LOG_ERR, "Can't allocate thread descriptors, error:%s\n", strerror(errno)); exit(1); } dispatcher_thread.base = main_base; dispatcher_thread.thread_id = pthread_self(); for (i = 0; i < nthreads; i++) { int fds[2]; if (pipe(fds)) { log_debug(LOG_ERR, "Can't create notify pipe, error:%s\n", strerror(errno)); exit(1); } threads[i].notify_receive_fd = fds[0]; threads[i].notify_send_fd = fds[1]; setup_thread(&threads[i]); /* Reserve three fds for the libevent base, and two for the pipe */ } /* Create threads after we've done all the libevent setup. */ for (i = 0; i < nthreads; i++) { create_worker(worker_libevent, &threads[i]); } /* Wait for all the threads to set themselves up before returning. */ pthread_mutex_lock(&init_lock); wait_for_thread_registration(nthreads); pthread_mutex_unlock(&init_lock); }
/* * Initializes the thread subsystem, creating various worker threads. * * nthreads Number of worker event handler threads to spawn * main_base Event base for main thread */ void thread_init(int nthreads, struct event_base *main_base) { int i; pthread_mutex_init(&cache_lock, NULL); pthread_mutex_init(&stats_lock, NULL); pthread_mutex_init(&init_lock, NULL); pthread_cond_init(&init_cond, NULL); pthread_mutex_init(&cqi_freelist_lock, NULL); cqi_freelist = NULL; threads = calloc(nthreads, sizeof(LIBEVENT_THREAD)); if (! threads) { perror("Can't allocate thread descriptors"); exit(1); } dispatcher_thread.base = main_base; dispatcher_thread.thread_id = pthread_self(); for (i = 0; i < nthreads; i++) { int fds[2]; if (pipe(fds)) { perror("Can't create notify pipe"); exit(1); } threads[i].notify_receive_fd = fds[0]; threads[i].notify_send_fd = fds[1]; setup_thread(&threads[i]); } /* Create threads after we've done all the libevent setup. */ for (i = 0; i < nthreads; i++) { create_worker(worker_libevent, &threads[i]); } /* Wait for all the threads to set themselves up before returning. */ pthread_mutex_lock(&init_lock); while (init_count < nthreads) { pthread_cond_wait(&init_cond, &init_lock); } pthread_mutex_unlock(&init_lock); }
static void start_threads(struct ev_loop *loop, int tcp_sd, int udp_sd) { printf("tcp_sd=%d udp_sd=%d\n", tcp_sd, udp_sd); // Block all signals using the pthreads interface while starting threads, // which causes them to inherit the same mask. sigset_t sigmask_all, sigmask_prev; sigfillset(&sigmask_all); pthread_sigmask(SIG_SETMASK, &sigmask_all, &sigmask_prev); // system scope scheduling, joinable threads pthread_attr_t attribs; pthread_attr_init(&attribs); pthread_attr_setdetachstate(&attribs, PTHREAD_CREATE_JOINABLE); pthread_attr_setscope(&attribs, PTHREAD_SCOPE_SYSTEM); //unsigned num_addrs = 1; //threads = threadids = calloc(NUM_THREADS, sizeof(pthread_t)); threads = calloc(NUM_THREADS, sizeof(LIBEV_THREAD)); // Start UDP threads /*for(uintptr_t i = 0; i < num_addrs; i++) { const dns_addr_t* addrconf = &gconfig.dns_addrs[i]; int pthread_err = pthread_create(&threadids[addrconf->udp_threadnum], &attribs, &dnsio_udp_start, (void*)addrconf); if(pthread_err) log_fatal("pthread_create() of UDP DNS thread failed: %s", logf_errnum(pthread_err)); }*/ // Start TCP threads uintptr_t i = 0; for(i = 0; i < NUM_THREADS; i++) { //const dns_addr_t* addrconf = &gconfig.dns_addrs[i]; //int pthread_err = pthread_create(&threadids[i], &attribs, &dnsio_tcp_start, (void*)addrconf); //if(pthread_err) log_fatal("pthread_create() of TCP DNS thread failed: %d", pthread_err); threads[i].loop = loop; threads[i].tcp_sd = tcp_sd; threads[i].udp_sd = udp_sd; create_worker(worker_libev, &threads[i]); } // Invoke thread cleanup handlers at exit time //if(atexit(threads_cleanup)) // log_fatal("atexit(threads_cleanup) failed: %s", logf_errno()); // Restore the original mask in the main thread, so // we can continue handling signals like normal pthread_sigmask(SIG_SETMASK, &sigmask_prev, NULL); pthread_attr_destroy(&attribs); }
/* create the worker */ int set_worker( gearman_worker_st *w ) { int x = 0; gm_log( GM_LOG_TRACE, "set_worker()\n" ); create_worker( mod_gm_opt->server_list, w ); if(worker_run_mode == GM_WORKER_STATUS) { /* register status function */ char status_queue[GM_BUFFERSIZE]; snprintf(status_queue, GM_BUFFERSIZE, "worker_%s", mod_gm_opt->identifier ); worker_add_function( w, status_queue, return_status ); } else { /* normal worker */ if(mod_gm_opt->hosts == GM_ENABLED) worker_add_function( w, "host", get_job ); if(mod_gm_opt->services == GM_ENABLED) worker_add_function( w, "service", get_job ); if(mod_gm_opt->events == GM_ENABLED) worker_add_function( w, "eventhandler", get_job ); while ( mod_gm_opt->hostgroups_list[x] != NULL ) { char buffer[GM_BUFFERSIZE]; snprintf( buffer, (sizeof(buffer)-1), "hostgroup_%s", mod_gm_opt->hostgroups_list[x] ); worker_add_function( w, buffer, get_job ); x++; } x = 0; while ( mod_gm_opt->servicegroups_list[x] != NULL ) { char buffer[GM_BUFFERSIZE]; snprintf( buffer, (sizeof(buffer)-1), "servicegroup_%s", mod_gm_opt->servicegroups_list[x] ); worker_add_function( w, buffer, get_job ); x++; } } /* add our dummy queue, gearman sometimes forgets the last added queue */ worker_add_function( w, "dummy", dummy); return GM_OK; }
int main( int argc, char *argv[] ) { Huint i; argument *arg; hthread_t tid[WORKERS + DISPATCHERS]; arg = create_args(); for(i = 0;i<WORKERS;i++) tid[i] = create_worker( arg ); for(i = 0;i<DISPATCHERS;i++) tid[i+WORKERS] = create_dispatcher( arg ); for(i = 0;i<WORKERS;i++) hthread_join(tid[i], NULL); for(i = 0;i<DISPATCHERS;i++) hthread_join(tid[i+WORKERS], NULL); destroy_args( arg ); printf( "--QED--\n" ); return 1; }
/* get the worker */ int set_worker( gearman_worker_st *worker ) { create_worker( mod_gm_opt->server_list, worker ); if ( mod_gm_opt->result_queue == NULL ) { gm_log( GM_LOG_ERROR, "got no result queue!\n" ); return GM_ERROR; } gm_log( GM_LOG_DEBUG, "started result_worker thread for queue: %s\n", mod_gm_opt->result_queue ); if(worker_add_function( worker, mod_gm_opt->result_queue, get_results ) != GM_OK) { return GM_ERROR; } /* add our dummy queue, gearman sometimes forgets the last added queue */ worker_add_function( worker, "dummy", dummy); return GM_OK; }
int init_supervisor(supervisor_thread_t *thiz, tesr_config_t *config) { LOG_LOC; int ret = 0; if(thiz && config) { thiz->config = config; if(thiz->config->num_workers == 0) { LOG_WARN("you are running in single threaded mode (num_workers=0)\n"); } ret = bind_dgram_socket(&thiz->sd, &thiz->addr, thiz->config->recv_port); if (ret == 0) { LOG_ERROR("could not bind dgram socket\n"); exit(EXIT_FAILURE); } else { thiz->queue = create_queue(); init_queue(thiz->queue); connect_pipe(&thiz->int_fd, &thiz->ext_fd); thiz->event_loop = EV_DEFAULT; //or ev_default_loop (0); //Set up rate limiting thiz->rate_limiter = create_rate_limiter(); init_rate_limiter(thiz->rate_limiter, thiz->config->irl_max, thiz->config->irl_inactivity_timeout, thiz->config->irl_garbage_collect_count); //Initialize pthread thiz->next_thread_idx = 0; thiz->worker_threads = create_worker_array(thiz->config->num_workers); //We must initialize workers last as we pass the supervisor data to them int th=0; for(th=0; th < thiz->config->num_workers; th++) { thiz->worker_threads[th] = create_worker(); init_worker(thiz->worker_threads[th], thiz, th); pthread_attr_t attr; pthread_attr_init(&attr); pthread_create(&thiz->worker_threads[th]->thread, &attr, worker_thread_run, thiz->worker_threads[th]); } ev_io_init(&thiz->udp_read_watcher, udp_read_cb, thiz->sd, EV_READ); ev_io_init(&thiz->inbox_watcher, udp_write_cb, thiz->int_fd, EV_READ); ev_signal_init(&thiz->sigint_watcher, sigint_cb, SIGINT); ev_signal_init(&thiz->sigchld_watcher, sigchld_cb, SIGCHLD); } } return ret; }
static void test_noverifycert(void *z) { struct Worker *server = NULL, *client = NULL; tt_assert(tls_init() == 0); /* noverifycert: client ignores cert */ str_check(create_worker(&server, true, SERVER1, NULL), "OK"); str_check(create_worker(&client, false, CA2, "host=server1.com", "noverifycert=1", NULL), "OK"); str_check(run_case(client, server), "OK"); /* noverifycert: client ignores cert, but checks hostname */ str_check(create_worker(&server, true, SERVER1, NULL), "OK"); str_check(create_worker(&client, false, CA2, "host=server2.com", "noverifycert=1", NULL), "OK"); str_check(run_case(client, server), "C:name `server2.com' not present in server certificate"); /* noverifycert: client ignores both cert, hostname */ str_check(create_worker(&server, true, SERVER1, NULL), "OK"); str_check(create_worker(&client, false, CA2, "host=server2.com", "noverifycert=1", "noverifyname=1", NULL), "OK"); str_check(run_case(client, server), "OK"); /* noverifycert: client ignores both cert, hostname (=NULL) */ str_check(create_worker(&server, true, SERVER1, NULL), "OK"); str_check(create_worker(&client, false, CA2, "noverifycert=1", "noverifyname=1", NULL), "OK"); str_check(run_case(client, server), "OK"); end:; }
void thread_init(struct event_base *main_base, int nthreads, pthread_t *th) { int i; pthread_mutex_init(&init_lock, NULL); pthread_cond_init(&init_cond, NULL); pthread_mutex_init(&cqi_freelist_lock, NULL); cqi_freelist = NULL; dispatcher_thread.base = main_base; dispatcher_thread.thread_id = pthread_self(); threads = (LIBEVENT_THREAD *)calloc(nthreads, sizeof(LIBEVENT_THREAD)); if (NULL == threads) { mfatal("allocate threads failed!"); exit(1); } for (i = 0; i < nthreads; i++) { int fds[2]; if (pipe(fds)) { mfatal("can't create notify pipe!"); exit(1); } threads[i].notify_receive_fd = fds[0]; threads[i].notify_send_fd = fds[1]; setup_thread(&threads[i]); } for (i = 0; i < nthreads; i++) { create_worker(worker_libevent, &threads[i], th + i); } pthread_mutex_lock(&init_lock); wait_for_thread_registration(nthreads); pthread_mutex_unlock(&init_lock); num_threads = nthreads; }
void tpool_resume(tpool_t *tpool) { int excess; pthread_mutex_lock(&tpool->tp_mutex); if (!(tpool->tp_flags & TP_SUSPEND)) { pthread_mutex_unlock(&tpool->tp_mutex); return; } tpool->tp_flags &= ~TP_SUSPEND; (void) pthread_cond_broadcast(&tpool->tp_workcv); excess = tpool->tp_njobs - tpool->tp_idle; while (excess-- > 0 && tpool->tp_current < tpool->tp_maximum) { if (create_worker(tpool) != 0) break; /* pthread_create() failed */ tpool->tp_current++; } pthread_mutex_unlock(&tpool->tp_mutex); }
void WorkerThreads::initiate() { int i; threads=(LIBEVENT_THREAD*)calloc(nthreads,sizeof(LIBEVENT_THREAD)); //LIBEVENT_THREAD,加入libevent元素的thread结构 “数组” if(!threads) { perror("can't allocate thread des"); exit(1); } for(i=0;i<nthreads;i++) //设置thread和thread中的libevent所需属性 { int fds[2]; if(pipe(fds)) //thread和主线程的通信pipe { perror("can't create notify pipe"); exit(1); } threads[i].notify_receive_fd=fds[0]; threads[i].notify_send_fd=fds[1]; setup_event_thread(&threads[i]); //设置thread和thread中的libevent所需属性 printf("init thread:%d\n",i); } for(i=0;i<nthreads;i++) { create_worker(worker_libevent,&threads[i]); //启动thread } pthread_mutex_lock(&init_lock); while( init_count < nthreads) { pthread_cond_wait(&init_cond,&init_lock); } pthread_mutex_unlock(&init_lock); printf("finish\n"); }
static void test_cert_info(void *z) { struct Worker *server = NULL, *client = NULL; tt_assert(tls_init() == 0); /* server shows client cert */ str_check(create_worker(&server, true, "show=peer-cert", SERVER1, CA2, "peer-sha1=ssl/ca2_client2.crt.sha1", "peer-sha256=ssl/ca2_client2.crt.sha256", "verify-client=1", NULL), "OK"); str_check(create_worker(&client, false, CLIENT2, CA1, "host=server1.com", "peer-sha1=ssl/ca1_server1.crt.sha1", "peer-sha256=ssl/ca1_server1.crt.sha256", NULL), "OK"); str_check(run_case(client, server), "Subject: /CN=client2/C=XX/ST=State2/L=City2/O=Org2" " Issuer: /CN=TestCA2" " Serial: 1387724136048036785122419970010419099185643835502" " NotBefore: 2010-01-01T08:05:00Z" " NotAfter: 2060-12-31T23:55:00Z"); /* client shows server cert - utf8 */ str_check(create_worker(&server, true, COMPLEX1, NULL), "OK"); str_check(create_worker(&client, false, CA1, "show=peer-cert", "host=complex1.com", NULL), "OK"); str_check(run_case(client, server), "Subject: /CN=complex1.com/ST=様々な論争を引き起こしてきた。/L=Kõzzä" " Issuer: /CN=TestCA1/C=AA/ST=State1/L=City1/O=Org1" " Serial: 1113692385315072860785465640275941003895485612482" " NotBefore: 2010-01-01T08:05:00Z" " NotAfter: 2060-12-31T23:55:00Z"); /* client shows server cert - t61/bmp */ str_check(create_worker(&server, true, COMPLEX2, NULL), "OK"); str_check(create_worker(&client, false, CA2, "show=peer-cert", "host=complex2.com", NULL), "OK"); str_check(run_case(client, server), "Subject: /CN=complex2.com/ST=様々な論争を引き起こしてきた。/L=Kõzzä" " Issuer: /CN=TestCA2" " Serial: 344032136906054686761742495217219742691739762030" " NotBefore: 2010-01-01T08:05:00Z" " NotAfter: 2060-12-31T23:55:00Z"); end:; }
void work_thread_init(int nthreads){ int i; work_threads = (LIBEVENT_WORK_THREAD *)calloc(nthreads, sizeof(LIBEVENT_WORK_THREAD)); if(!work_threads){ DEBUG("work_threads create error"); exit(1); } wtq_queue_tail = wtq_queue_head = NULL; pthread_mutex_init(&init_work_lock, NULL); pthread_cond_init(&init_work_cond, NULL); for(i=0; i< nthreads; i++){ int fds[2]; if(pipe(fds)){ } work_threads[i].notify_read_fd = fds[0]; work_threads[i].notify_write_fd = fds[1]; work_threads[i].no = i; work_threads[i].tdbp = initdbp(); setup_thread(&work_threads[i]); } for(i=0; i<nthreads; i++){ create_worker(worker_libevent, &work_threads[i]); } pthread_mutex_lock(&init_work_lock); while (init_count < nthreads) { pthread_cond_wait(&init_work_cond, &init_work_lock); } wtq_queue_head->next = wtq_queue_tail; pthread_mutex_unlock(&init_work_lock); }