static void snmpmgr_thread_finally_called( const lagopus_thread_t *thd, bool is_canceled, void *arg) { bool is_valid = false; lagopus_result_t ret; (void) is_canceled; (void) arg; if (*thd != snmpmgr) { lagopus_exit_fatal("other threads worked, argument is :%p, snmpmgr is :%p\n", *thd, snmpmgr); } ret = lagopus_thread_is_valid(&snmpmgr, &is_valid); if (ret != LAGOPUS_RESULT_OK || is_valid == false) { lagopus_perror(ret); return; } (void)lagopus_mutex_unlock(&snmp_lock); (void)lagopus_mutex_unlock(&snmp_state_lock); (void)lagopus_mutex_lock(&snmp_state_lock); state = SNMPMGR_NONE; (void)lagopus_mutex_unlock(&snmp_state_lock); global_state_cancel_janitor(); }
lagopus_result_t snmpmgr_start(void) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; (void)lagopus_mutex_lock(&snmp_state_lock); if (state == SNMPMGR_RUNNABLE) { if (is_thread == false) { state = SNMPMGR_RUNNING; (void)lagopus_mutex_unlock(&snmp_state_lock); /* open the session to AgentX master agent here. */ init_snmp(SNMP_TYPE); /* send_*_trap always return SNMP_ERR_NOERROR */ (void)send_coldStart_trap(); ret = LAGOPUS_RESULT_OK; lagopus_msg_info("SNMP manager started.\n"); } else { /* Note: * If is_thread is true, init_snmp and send_coldStart_trap * are called inside snmpmgr_thread_loop */ (void)lagopus_mutex_unlock(&snmp_state_lock); ret = lagopus_thread_start(&snmpmgr, false); } } else { (void)lagopus_mutex_unlock(&snmp_state_lock); ret = LAGOPUS_RESULT_NOT_STARTED; } return ret; }
static lagopus_result_t s_main(const lagopus_pipeline_stage_t *sptr, size_t idx, void *buf, size_t n) { test_stage_t t = (test_stage_t)(*sptr); (void)buf; if (t->m_counts[idx] < t->m_n_max_count) { #if 0 if (t->m_mod > 0 && (t->m_counts[idx] % t->m_mod) == 0) { (void)lagopus_mutex_lock(&(t->m_lock)); fprintf(stderr, "idx " PFSZ(u) ": " PFSZ(u) "\n", idx, t->m_counts[idx]); (void)lagopus_mutex_unlock(&(t->m_lock)); } #endif t->m_counts[idx]++; return (lagopus_result_t)n; } else { (void)lagopus_mutex_lock(&(t->m_lock)); fprintf(stderr, "idx " PFSZ(u) ": " PFSZ(u) "\n", idx, t->m_counts[idx]); (void)lagopus_mutex_unlock(&(t->m_lock)); return 0LL; } }
static lagopus_result_t snmpmgr_thread_loop(const lagopus_thread_t *selfptr, void *arg) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; lagopus_chrono_t interval = DEFAULT_SNMPMGR_LOOP_INTERVAL_NSEC; global_state_t global_state; shutdown_grace_level_t l; (void)selfptr; (void)arg; /* open the session to AgentX master agent here. */ init_snmp(SNMP_TYPE); /* wait all modules start */ if ((ret = global_state_wait_for(GLOBAL_STATE_STARTED, &global_state, &l, -1 /* forever! */)) != LAGOPUS_RESULT_OK) { lagopus_perror(ret); } else { if (global_state != GLOBAL_STATE_STARTED) { lagopus_exit_fatal("must not happen. die!\n"); } } /* all modules have started, then send a coldStart trap */ (void)send_coldStart_trap(); lagopus_msg_info("SNMP manager started (as a thread).\n"); /* main loop */ while (keep_running) { (void)lagopus_mutex_lock(&snmp_lock); ret = internal_snmpmgr_poll(interval); (void)lagopus_mutex_unlock(&snmp_lock); if (ret != LAGOPUS_RESULT_OK && ret != LAGOPUS_RESULT_TIMEDOUT) { lagopus_msg_warning("failed to poll SNMP AgentX request: %s", lagopus_error_get_string(ret)); } check_status_and_send_traps(); } /* stop SNMP */ snmp_shutdown(SNMP_TYPE); (void)lagopus_mutex_lock(&snmp_state_lock); if (state != SNMPMGR_RUNNABLE) { state = SNMPMGR_NONE; } (void)lagopus_mutex_unlock(&snmp_state_lock); return LAGOPUS_RESULT_OK; }
static inline lagopus_result_t snmpmgr_stop_single(void) { (void)lagopus_mutex_lock(&snmp_state_lock); if (state == SNMPMGR_RUNNING) { snmp_shutdown(SNMP_TYPE); state = SNMPMGR_RUNNABLE; (void)lagopus_mutex_unlock(&snmp_state_lock); return LAGOPUS_RESULT_OK; } else { (void)lagopus_mutex_unlock(&snmp_state_lock); return LAGOPUS_RESULT_NOT_STARTED; } }
static inline void bridgeq_mgr_unlock(void) { assert(lock != NULL); if (lock != NULL) { lagopus_mutex_unlock(&lock); } }
lagopus_result_t snmpmgr_poll(lagopus_chrono_t nsec) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; (void)lagopus_mutex_lock(&snmp_state_lock); if (state == SNMPMGR_RUNNING) { (void)lagopus_mutex_unlock(&snmp_state_lock); (void)lagopus_mutex_lock(&snmp_lock); ret = internal_snmpmgr_poll(nsec); (void)lagopus_mutex_unlock(&snmp_lock); return ret; } else { (void)lagopus_mutex_unlock(&snmp_state_lock); lagopus_msg_error("SNMP agent is not started.\n"); return LAGOPUS_RESULT_NOT_STARTED; } }
static inline void s_stop_all(void) { if (is_child == true) { if (stop_lock != NULL) { (void)lagopus_mutex_lock(&stop_lock); { if (is_stopped == false) { size_t i; is_stopped = true; /* * Shutdown the queues first. This should make all the * thread exited. */ if (bbqs != NULL) { for (i = 0; i < n_created_bbqs; i++) { lagopus_bbq_shutdown(&(bbqs[i]), true); } } } } (void)lagopus_mutex_unlock(&stop_lock); } } }
static inline lagopus_result_t s_parse_begin_call(datastore_interp_t *iptr, lagopus_dstring_t *result) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; char file_name[PATH_MAX]; /* copy file name. */ lagopus_mutex_lock(&lock); if (strlen(save_tmp_dir) + strlen(TMP_FILE) < PATH_MAX) { snprintf(file_name, sizeof(file_name), "%s%s", save_tmp_dir, TMP_FILE); ret = datastore_interp_atomic_begin(iptr, file_name, result); if (ret != LAGOPUS_RESULT_OK) { ret = datastore_json_result_set(result, ret, NULL); } } else { ret = datastore_json_result_string_setf( result, LAGOPUS_RESULT_TOO_LONG, "Bad file name."); } lagopus_mutex_unlock(&lock); return ret; }
lagopus_result_t snmpmgr_initialize(void *arg, lagopus_thread_t **thdptr) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; if (state == SNMPMGR_NONE) { /* don't check `arg` is NULL */ /* there may be no difference between NULL and false! */ is_thread_dummy = (bool) arg; /* ONLY checking whether are they valid argments or not */ if (is_thread_dummy == false || thdptr != NULL) { int pthd_once_ret; thdptr_dummy = thdptr; if ((pthd_once_ret = pthread_once(&initialized, initialize_internal)) == 0) { ret = initialize_ret; if (ret == LAGOPUS_RESULT_OK) { (void)lagopus_mutex_lock(&snmp_state_lock); state = SNMPMGR_RUNNABLE; (void)lagopus_mutex_unlock(&snmp_state_lock); } } else { errno = pthd_once_ret; perror("pthread_once"); ret = LAGOPUS_RESULT_POSIX_API_ERROR; } } else { /* if is_thread_dummy is true and thdptr is NULL */ ret = LAGOPUS_RESULT_INVALID_ARGS; } } else { ret = initialize_ret; } return ret; }
void session_tls_certcheck_set(lagopus_result_t (*func)(const char *, const char *)) { lagopus_mutex_lock(&lock); check_certificates = func; lagopus_mutex_unlock(&lock); }
static inline void s_destroy_all(void) { if (is_child == true) { s_stop_all(); (void)lagopus_mutex_lock(&stop_lock); { size_t i; test_thread_t tt; /* * Wait the threads done and delete it. */ if (tts != NULL) { for (i = 0; i < n_created_thds; i++) { tt = &(tts[i]); (void)lagopus_thread_wait((lagopus_thread_t *)&tt, -1LL); (void)lagopus_thread_destroy((lagopus_thread_t *)&tt); } free((void *)tts); tts = NULL; } if (polls != NULL) { for (i = 0; i < n_created_polls; i++) { lagopus_qmuxer_poll_destroy(&(polls[i])); } free((void *)polls); polls = NULL; } if (bbqs != NULL) { for (i = 0; i < n_created_bbqs; i++) { lagopus_bbq_destroy(&(bbqs[i]), true); } free((void *)bbqs); bbqs = NULL; } if (qmx != NULL) { lagopus_qmuxer_destroy(&qmx); qmx = NULL; } if (start_lock != NULL) { lagopus_mutex_destroy(&start_lock); start_lock = NULL; } } (void)lagopus_mutex_unlock(&stop_lock); if (stop_lock != NULL) { lagopus_mutex_destroy(&stop_lock); stop_lock = NULL; } } }
static lagopus_result_t s_test_thread_main(const lagopus_thread_t *tptr, void *arg) { lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; lagopus_chrono_t *dates = NULL; (void)arg; if (tptr != NULL) { test_thread_t tt = (test_thread_t)*tptr; if (tt != NULL) { ssize_t i; lagopus_chrono_t now; dates = (lagopus_chrono_t *) malloc(sizeof(lagopus_chrono_t) * (size_t)tt->m_n_puts); if (dates == NULL) { goto done; } /* * Ready, steady, */ (void)lagopus_mutex_lock(&(tt->m_start_lock)); /* * go. */ (void)lagopus_mutex_unlock(&(tt->m_start_lock)); for (i = 0; i < tt->m_n_puts; i++) { WHAT_TIME_IS_IT_NOW_IN_NSEC(dates[i]); } if (lagopus_bbq_put_n(&(tt->m_q), (void **)dates, (size_t)tt->m_n_puts, lagopus_chrono_t, -1LL, NULL) != (lagopus_result_t)tt->m_n_puts) { goto done; } now = -1LL; if (lagopus_bbq_put(&(tt->m_q), &now, lagopus_chrono_t, -1LL) != LAGOPUS_RESULT_OK) { goto done; } ret = tt->m_n_puts; lagopus_msg_debug(1, "Done, ret = " PFSZS(020, d) ", req = " PFSZS(020, u) ".\n", (size_t)ret, (size_t)tt->m_n_puts); } } done: free((void *)dates); return ret; }
static inline void test_thread_request_stop(test_thread_t tt) { if (tt != NULL) { (void)lagopus_mutex_lock(&(tt->m_lock)); { tt->m_stop_requested = true; } (void)lagopus_mutex_unlock(&(tt->m_lock)); } }
void session_tls_key_get(char **c) { lagopus_mutex_lock(&lock); if (client_key == NULL) { *c = NULL; } else { *c = strdup(client_key); } lagopus_mutex_unlock(&lock); }
void session_tls_key_set(const char *c) { if (c == NULL) { return; } lagopus_mutex_lock(&lock); free(client_key); client_key = strdup(c); lagopus_mutex_unlock(&lock); }
static inline void s_final(void) { if (s_cond != NULL) { lagopus_cond_destroy(&s_cond); } if (s_lck != NULL) { (void)lagopus_mutex_lock(&s_lck); (void)lagopus_mutex_unlock(&s_lck); lagopus_mutex_destroy(&s_lck); } }
struct session * session_tls_init(struct session *s) { pthread_once(&initialized, initialize_internal); s->ctx = malloc(sizeof(struct tls_ctx)); if (s->ctx == NULL) { lagopus_msg_warning("no memory.\n"); return NULL; } lagopus_mutex_lock(&lock); if (client_ca_dir == NULL || client_ca_dir == NULL || client_key == NULL) { lagopus_msg_warning("certificate files not initilized.\n"); lagopus_mutex_unlock(&lock); free(s->ctx); s->ctx = NULL; return NULL; } GET_TLS_CTX(s)->ca_dir = strdup(client_ca_dir); GET_TLS_CTX(s)->cert = strdup(client_cert); GET_TLS_CTX(s)->key = strdup(client_key); lagopus_mutex_unlock(&lock); GET_TLS_CTX(s)->ctx = NULL; GET_TLS_CTX(s)->ssl = NULL; GET_TLS_CTX(s)->verified = false; s->read = read_tls; s->write = write_tls; s->connect = connect_tls; s->close = close_tls; s->destroy = destroy_tls; s->connect_check = connect_check_tls; return s; }
static inline void a_obj_destroy(a_obj_t o) { lagopus_msg_debug(1, "enter.\n"); if (o != NULL) { if (o->m_lock != NULL) { (void)lagopus_mutex_lock(&(o->m_lock)); (void)lagopus_mutex_unlock(&(o->m_lock)); (void)lagopus_mutex_destroy(&(o->m_lock)); } free((void *)o); } lagopus_msg_debug(1, "leave.\n"); }
lagopus_result_t dp_thread_shutdown(lagopus_thread_t *threadptr, lagopus_mutex_t *lockptr, bool *runptr, shutdown_grace_level_t level) { bool is_valid = false; lagopus_chrono_t nsec; lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES; lagopus_msg_info("shutdown called\n"); lagopus_mutex_lock(lockptr); if (*runptr == false) { goto done; } ret = lagopus_thread_is_valid(threadptr, &is_valid); if (ret != LAGOPUS_RESULT_OK || is_valid == false) { lagopus_perror(ret); goto done; } /* XXX UP TO main() */ switch (level) { case SHUTDOWN_RIGHT_NOW: nsec = TIMEOUT_SHUTDOWN_RIGHT_NOW; break; case SHUTDOWN_GRACEFULLY: nsec = TIMEOUT_SHUTDOWN_GRACEFULLY; break; default: lagopus_msg_fatal("unknown shutdown level %d\n", level); ret = LAGOPUS_RESULT_ANY_FAILURES; goto done; } *runptr = false; ret = lagopus_thread_wait(threadptr, nsec); if (ret != LAGOPUS_RESULT_OK) { lagopus_perror(ret); goto done; } done: lagopus_mutex_unlock(lockptr); return ret; }
static inline size_t s_wait_freeup_arg(callout_arg_t arg) { size_t ret = 0; if (likely(arg != NULL)) { (void)lagopus_mutex_lock(&(arg->m_lock)); { while (arg->m_is_freeuped != true) { (void)lagopus_cond_wait(&(arg->m_cond), &(arg->m_lock), -1LL); } ret = __sync_fetch_and_add(&(arg->m_n_exec), 0); } (void)lagopus_mutex_unlock(&(arg->m_lock)); } return ret; }
static inline void s_freeup_arg(void *arg) { lagopus_msg_debug(1, "enter.\n"); if (likely(arg != NULL)) { callout_arg_t carg = (callout_arg_t)arg; (void)lagopus_mutex_lock(&(carg->m_lock)); { carg->m_is_freeuped = true; (void)lagopus_cond_notify(&(carg->m_cond), true); } (void)lagopus_mutex_unlock(&(carg->m_lock)); } lagopus_msg_debug(1, "leave.\n"); }
static void s_test_thread_finalize(const lagopus_thread_t *tptr, bool is_canceled, void *arg) { (void)arg; if (tptr != NULL) { test_thread_t tt = (test_thread_t)*tptr; if (tt != NULL) { lagopus_mutex_lock(&(tt->m_lock)); { tt->m_is_operational = false; } lagopus_mutex_unlock(&(tt->m_lock)); lagopus_msg_debug(1, "enter (%s).\n", (is_canceled == true) ? "canceled" : "exited"); } } }
lagopus_result_t dp_thread_start(lagopus_thread_t *threadptr, lagopus_mutex_t *lockptr, bool *runptr) { lagopus_result_t ret; lagopus_mutex_lock(lockptr); if (*runptr == true) { ret = LAGOPUS_RESULT_ALREADY_EXISTS; goto done; } *runptr = true; ret = lagopus_thread_start(threadptr, false); if (ret != LAGOPUS_RESULT_OK) { lagopus_perror(ret); } done: lagopus_mutex_unlock(lockptr); return ret; }
static void s_test_thread_destroy(const lagopus_thread_t *tptr, void *arg) { (void)arg; if (tptr != NULL) { test_thread_t tt = (test_thread_t)*tptr; if (tt != NULL) { (void)lagopus_mutex_lock(&(tt->m_lock)); { lagopus_msg_debug(1, "enter.\n"); if (tt->m_is_operational == false) { lagopus_msg_debug(1, "freeup.\n"); free((void *)tt->m_data); tt->m_data = NULL; } s_is_deleted = true; } (void)lagopus_mutex_unlock(&(tt->m_lock)); } lagopus_mutex_destroy(&(tt->m_lock)); } }
static inline void s_unlock_global(void) { if (likely(s_lck != NULL)) { (void)lagopus_mutex_unlock(&s_lck); } }
static inline void s_unlock(void) { (void)lagopus_mutex_unlock(&s_lck); }
static inline void s_unlock(void) { if (s_lck != NULL) { (void)lagopus_mutex_unlock(&s_lck); } }
static inline void s_pause_unlock_stage(lagopus_pipeline_stage_t ps) { if (ps != NULL && ps->m_pause_lock != NULL) { (void)lagopus_mutex_unlock(&(ps->m_pause_lock)); } }
static inline void s_unlock_sched(void) { if (likely(s_sched_lck != NULL)) { (void)lagopus_mutex_unlock(&s_sched_lck); } }