/* rotate the log file */ static switch_status_t mod_logfile_rotate(logfile_profile_t *profile) { unsigned int i = 0; char *filename = NULL; switch_status_t stat = 0; int64_t offset = 0; switch_memory_pool_t *pool = NULL; switch_time_exp_t tm; char date[80] = ""; switch_size_t retsize; switch_status_t status = SWITCH_STATUS_SUCCESS; switch_mutex_lock(globals.mutex); switch_time_exp_lt(&tm, switch_micro_time_now()); switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm); profile->log_size = 0; stat = switch_file_seek(profile->log_afd, SWITCH_SEEK_SET, &offset); if (stat != SWITCH_STATUS_SUCCESS) { status = SWITCH_STATUS_FALSE; goto end; } switch_core_new_memory_pool(&pool); filename = switch_core_alloc(pool, strlen(profile->logfile) + WARM_FUZZY_OFFSET); for (i = 1; i < MAX_ROT; i++) { sprintf((char *) filename, "%s.%s.%i", profile->logfile, date, i); if (switch_file_exists(filename, pool) == SWITCH_STATUS_SUCCESS) { continue; } switch_file_close(profile->log_afd); switch_file_rename(profile->logfile, filename, pool); if ((status = mod_logfile_openlogfile(profile, SWITCH_FALSE)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error Rotating Log!\n"); goto end; } break; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New log started.\n"); end: if (pool) { switch_core_destroy_memory_pool(&pool); } switch_mutex_unlock(globals.mutex); return status; }
SWITCH_DECLARE(switch_status_t) switch_core_speech_open(switch_speech_handle_t *sh, const char *module_name, const char *voice_name, unsigned int rate, unsigned int interval, switch_speech_flag_t *flags, switch_memory_pool_t *pool) { switch_status_t status; char buf[256] = ""; char *param = NULL; if (!sh || !flags || zstr(module_name)) { return SWITCH_STATUS_FALSE; } if (strchr(module_name, ':')) { switch_set_string(buf, module_name); if ((param = strchr(buf, ':'))) { *param++ = '\0'; module_name = buf; } } if ((sh->speech_interface = switch_loadable_module_get_speech_interface(module_name)) == 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid speech module [%s]!\n", module_name); return SWITCH_STATUS_GENERR; } sh->flags = *flags; if (pool) { sh->memory_pool = pool; } else { if ((status = switch_core_new_memory_pool(&sh->memory_pool)) != SWITCH_STATUS_SUCCESS) { UNPROTECT_INTERFACE(sh->speech_interface); return status; } switch_set_flag(sh, SWITCH_SPEECH_FLAG_FREE_POOL); } sh->engine = switch_core_strdup(sh->memory_pool, module_name); if (param) { sh->param = switch_core_strdup(sh->memory_pool, param); } sh->rate = rate; sh->name = switch_core_strdup(sh->memory_pool, module_name); sh->samples = switch_samples_per_packet(rate, interval); sh->samplerate = rate; sh->native_rate = rate; if ((status = sh->speech_interface->speech_open(sh, voice_name, rate, flags)) == SWITCH_STATUS_SUCCESS) { switch_set_flag(sh, SWITCH_SPEECH_FLAG_OPEN); } else { UNPROTECT_INTERFACE(sh->speech_interface); } return status; }
SimpleLockBasic(switch_memory_pool_t *pool = NULL) : _pool(pool), _can_delete_pool( (_pool == NULL) ) { if(!_pool) switch_core_new_memory_pool(&_pool); //switch_mutex_init(&_mutex, SWITCH_MUTEX_DEFAULT, _pool); switch_mutex_init(&_mutex, SWITCH_MUTEX_NESTED, _pool); }
static dir_profile_t *load_profile(const char *profile_name) { dir_profile_t *profile = NULL; switch_xml_t x_profiles, x_profile, cfg, xml = NULL; switch_event_t *event = NULL; if (!(xml = switch_xml_open_cfg(global_cf, &cfg, NULL))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", global_cf); return profile; } if (!(x_profiles = switch_xml_child(cfg, "profiles"))) { goto end; } if ((x_profile = switch_xml_find_child(x_profiles, "profile", "name", profile_name))) { switch_memory_pool_t *pool; int count; if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Pool Failure\n"); goto end; } if (!(profile = switch_core_alloc(pool, sizeof(dir_profile_t)))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Alloc Failure\n"); switch_core_destroy_memory_pool(&pool); goto end; } profile->pool = pool; profile_set_config(profile); /* Add the params to the event structure */ count = (int)switch_event_import_xml(switch_xml_child(x_profile, "param"), "name", "value", &event); if (switch_xml_config_parse_event(event, count, SWITCH_FALSE, profile->config) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to process configuration\n"); switch_core_destroy_memory_pool(&pool); goto end; } switch_thread_rwlock_create(&profile->rwlock, pool); profile->name = switch_core_strdup(pool, profile_name); switch_mutex_init(&profile->mutex, SWITCH_MUTEX_NESTED, profile->pool); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Added Profile %s\n", profile->name); switch_core_hash_insert(globals.profile_hash, profile->name, profile); } end: switch_xml_free(xml); return profile; }
SWITCH_DECLARE_CONSTRUCTOR EventConsumer::EventConsumer(const char *event_name, const char *subclass_name) { switch_core_new_memory_pool(&pool); switch_queue_create(&events, 5000, pool); node_index = 0; if (!zstr(event_name)) { bind(event_name, subclass_name); } }
SWITCH_DECLARE(void) switch_scheduler_task_thread_start(void) { switch_threadattr_t *thd_attr; switch_core_new_memory_pool(&globals.memory_pool); switch_threadattr_create(&thd_attr, globals.memory_pool); switch_mutex_init(&globals.task_mutex, SWITCH_MUTEX_NESTED, globals.memory_pool); switch_threadattr_detach_set(thd_attr, 1); switch_thread_create(&task_thread_p, thd_attr, switch_scheduler_task_thread, NULL, globals.memory_pool); }
SWITCH_DECLARE(switch_status_t) switch_core_asr_open(switch_asr_handle_t *ah, const char *module_name, const char *codec, int rate, const char *dest, switch_asr_flag_t *flags, switch_memory_pool_t *pool) { switch_status_t status; char buf[256] = ""; char *param = NULL; if (strchr(module_name, ':')) { switch_set_string(buf, module_name); if ((param = strchr(buf, ':'))) { *param++ = '\0'; module_name = buf; } } switch_assert(ah != NULL); if ((ah->asr_interface = switch_loadable_module_get_asr_interface(module_name)) == 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid ASR module [%s]!\n", module_name); return SWITCH_STATUS_GENERR; } ah->flags = *flags; if (pool) { ah->memory_pool = pool; } else { if ((status = switch_core_new_memory_pool(&ah->memory_pool)) != SWITCH_STATUS_SUCCESS) { UNPROTECT_INTERFACE(ah->asr_interface); return status; } switch_set_flag(ah, SWITCH_ASR_FLAG_FREE_POOL); } if (param) { ah->param = switch_core_strdup(ah->memory_pool, param); } ah->rate = rate; ah->name = switch_core_strdup(ah->memory_pool, module_name); ah->samplerate = rate; ah->native_rate = rate; status = ah->asr_interface->asr_open(ah, codec, rate, dest, flags); if (status != SWITCH_STATUS_SUCCESS) { UNPROTECT_INTERFACE(ah->asr_interface); } return status; }
SavedCondition(switch_memory_pool_t *pool=NULL): _pool(pool), _can_delete_pool(false) { if(!_pool) { switch_core_new_memory_pool(&_pool); _can_delete_pool = true; } switch_thread_cond_create(&_condition, _pool); switch_mutex_init(&_mutex, SWITCH_MUTEX_DEFAULT, _pool); }
/** * Start execution of prompt component */ static iks *start_call_prompt_component(struct rayo_actor *call, struct rayo_message *msg, void *session_data) { iks *iq = msg->payload; switch_core_session_t *session = (switch_core_session_t *)session_data; switch_memory_pool_t *pool; struct prompt_component *prompt_component = NULL; iks *prompt = iks_find(iq, "prompt"); iks *input; iks *output; iks *cmd; if (!VALIDATE_RAYO_PROMPT(prompt)) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Bad <prompt> attrib\n"); return iks_new_error_detailed(iq, STANZA_ERROR_BAD_REQUEST, "Bad <prompt> attrib value"); } output = iks_find(prompt, "output"); if (!output) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Missing <output>\n"); return iks_new_error_detailed(iq, STANZA_ERROR_BAD_REQUEST, "Missing <output>"); } input = iks_find(prompt, "input"); if (!input) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Missing <input>\n"); return iks_new_error_detailed(iq, STANZA_ERROR_BAD_REQUEST, "Missing <input>"); } /* create prompt component, linked to call */ switch_core_new_memory_pool(&pool); prompt_component = switch_core_alloc(pool, sizeof(*prompt_component)); rayo_component_init(RAYO_COMPONENT(prompt_component), pool, RAT_CALL_COMPONENT, "prompt", NULL, call, iks_find_attrib(iq, "from")); prompt_component->iq = iks_copy(iq); /* start output */ if (iks_find_bool_attrib(prompt, "barge-in")) { prompt_component->state = PCS_START_OUTPUT_BARGE; } else { prompt_component->state = PCS_START_OUTPUT; } cmd = iks_new("iq"); iks_insert_attrib(cmd, "from", RAYO_JID(prompt_component)); iks_insert_attrib(cmd, "to", RAYO_JID(call)); iks_insert_attrib(cmd, "id", iks_find_attrib(iq, "id")); iks_insert_attrib(cmd, "type", "set"); output = iks_copy_within(output, iks_stack(cmd)); iks_insert_node(cmd, output); RAYO_SEND_MESSAGE(prompt_component, RAYO_JID(call), cmd); return NULL; }
switch_status_t mongo_connection_pool_create(mongo_connection_pool_t **conn_pool, switch_size_t min_connections, switch_size_t max_connections, const char *conn_str) { switch_memory_pool_t *pool = NULL; switch_status_t status = SWITCH_STATUS_SUCCESS; mongo_connection_pool_t *cpool = NULL; DBClientBase *conn = NULL; if ((status = switch_core_new_memory_pool(&pool)) != SWITCH_STATUS_SUCCESS) { return status; } if (!(cpool = (mongo_connection_pool_t *)switch_core_alloc(pool, sizeof(mongo_connection_pool_t)))) { switch_goto_status(SWITCH_STATUS_MEMERR, done); } if ((status = switch_mutex_init(&cpool->mutex, SWITCH_MUTEX_NESTED, pool)) != SWITCH_STATUS_SUCCESS) { goto done; } if ((status = switch_queue_create(&cpool->connections, max_connections, pool)) != SWITCH_STATUS_SUCCESS) { goto done; } cpool->min_connections = min_connections; cpool->max_connections = max_connections; cpool->conn_str = switch_core_strdup(pool, conn_str); cpool->pool = pool; for (cpool->size = 0; cpool->size < min_connections; cpool->size++) { if (mongo_connection_create(&conn, conn_str) == SWITCH_STATUS_SUCCESS) { mongo_connection_pool_put(cpool, conn); } else { break; } } done: if (status == SWITCH_STATUS_SUCCESS) { *conn_pool = cpool; } else { switch_core_destroy_memory_pool(&pool); } return status; }
static void do_load(void) { switch_mutex_lock(MUTEX); if (globals.pool) { switch_core_destroy_memory_pool(&globals.pool); } memset(&globals, 0, sizeof(globals)); switch_core_new_memory_pool(&globals.pool); globals.timeout = 10; load_config(); switch_mutex_unlock(MUTEX); }
blacklist_t *blacklist_create(const char *name) { switch_memory_pool_t *pool = NULL; blacklist_t *bl = NULL; switch_core_new_memory_pool(&pool); bl = switch_core_alloc(pool, sizeof(*bl)); switch_assert(bl); bl->pool = pool; switch_core_hash_init(&bl->list); switch_mutex_init(&bl->list_mutex, SWITCH_MUTEX_NESTED, pool); return bl; }
/** * Create new output component */ static struct rayo_component *create_output_component(struct rayo_actor *actor, const char *type, iks *output, const char *client_jid) { switch_memory_pool_t *pool; struct output_component *output_component = NULL; switch_core_new_memory_pool(&pool); output_component = switch_core_alloc(pool, sizeof(*output_component)); rayo_component_init((struct rayo_component *)output_component, pool, type, "output", NULL, actor, client_jid); output_component->document = iks_copy(output); output_component->repeat_interval = iks_find_int_attrib(output, "repeat-interval"); output_component->repeat_times = iks_find_int_attrib(output, "repeat-times"); output_component->max_time = iks_find_int_attrib(output, "max-time"); output_component->start_paused = iks_find_bool_attrib(output, "start-paused"); return (struct rayo_component *)output_component; }
/** * Create new output component */ static struct rayo_component *create_output_component(struct rayo_actor *actor, const char *type, iks *output, const char *client_jid) { switch_memory_pool_t *pool; struct output_component *output_component = NULL; switch_core_new_memory_pool(&pool); output_component = switch_core_alloc(pool, sizeof(*output_component)); output_component = OUTPUT_COMPONENT(rayo_component_init((struct rayo_component *)output_component, pool, type, "output", NULL, actor, client_jid)); if (output_component) { output_component->document = iks_copy(output); output_component->start_offset_ms = iks_find_int_attrib(output, "start-offset"); output_component->repeat_interval_ms = iks_find_int_attrib(output, "repeat-interval"); output_component->repeat_times = iks_find_int_attrib(output, "repeat-times"); output_component->max_time_ms = iks_find_int_attrib(output, "max-time"); output_component->start_paused = iks_find_bool_attrib(output, "start-paused"); output_component->renderer = switch_core_strdup(RAYO_POOL(output_component), iks_find_attrib_soft(output, "renderer")); /* get custom headers */ { switch_stream_handle_t headers = { 0 }; iks *header = NULL; int first = 1; SWITCH_STANDARD_STREAM(headers); for (header = iks_find(output, "header"); header; header = iks_next_tag(header)) { if (!strcmp("header", iks_name(header))) { const char *name = iks_find_attrib_soft(header, "name"); const char *value = iks_find_attrib_soft(header, "value"); if (!zstr(name) && !zstr(value)) { headers.write_function(&headers, "%s%s=%s", first ? "{" : ",", name, value); first = 0; } } } if (headers.data) { headers.write_function(&headers, "}"); output_component->headers = switch_core_strdup(RAYO_POOL(output_component), (char *)headers.data); free(headers.data); } } } else { switch_core_destroy_memory_pool(&pool); } return RAYO_COMPONENT(output_component); }
/** * Create a record component */ static struct rayo_component *record_component_create(struct rayo_actor *actor, const char *client_jid, iks *record) { switch_memory_pool_t *pool; struct record_component *record_component = NULL; char *local_file_path; char *fs_file_path; switch_bool_t start_paused; /* validate record attributes */ if (!VALIDATE_RAYO_RECORD(record)) { return NULL; } start_paused = iks_find_bool_attrib(record, "start-paused"); /* create record filename from session UUID and ref */ /* for example: prefix/1234-1234-1234-1234-30.wav */ local_file_path = switch_mprintf("%s%s-%i.%s", globals.record_file_prefix, actor->id, rayo_actor_seq_next(actor), iks_find_attrib(record, "format")); fs_file_path = switch_mprintf("{pause=%s}fileman://%s", start_paused ? "true" : "false", local_file_path); switch_core_new_memory_pool(&pool); record_component = switch_core_alloc(pool, sizeof(*record_component)); rayo_component_init(RAYO_COMPONENT(record_component), pool, "record", fs_file_path, actor, client_jid); record_component->max_duration = iks_find_int_attrib(record, "max-duration"); record_component->initial_timeout = iks_find_int_attrib(record, "initial-timeout"); record_component->final_timeout = iks_find_int_attrib(record, "final-timeout"); record_component->direction = switch_core_strdup(RAYO_POOL(record_component), iks_find_attrib_soft(record, "direction")); record_component->mix = iks_find_bool_attrib(record, "mix"); record_component->start_beep = iks_find_bool_attrib(record, "start-beep"); record_component->stop_beep = iks_find_bool_attrib(record, "stop-beep"); record_component->start_time = start_paused ? 0 : switch_micro_time_now(); record_component->local_file_path = switch_core_strdup(RAYO_POOL(record_component), local_file_path); switch_safe_free(local_file_path); switch_safe_free(fs_file_path); return RAYO_COMPONENT(record_component); }
SWITCH_DECLARE_CONSTRUCTOR EventConsumer::EventConsumer(const char *event_name, const char *subclass_name) { switch_name_event(event_name, &e_event_id); switch_core_new_memory_pool(&pool); if (!zstr(subclass_name)) { e_subclass_name = switch_core_strdup(pool, subclass_name); } else { e_subclass_name = NULL; } switch_queue_create(&events, 5000, pool); if (switch_event_bind_removable(__FILE__, e_event_id, e_subclass_name, event_handler, this, &node) == SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "bound to %s %s\n", event_name, switch_str_nil(e_subclass_name)); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot bind to %s %s\n", event_name, switch_str_nil(e_subclass_name)); } }
void switch_load_timezones(switch_bool_t reload) { switch_xml_t xml = NULL, x_lists = NULL, x_list = NULL, cfg = NULL; unsigned total = 0; if (TIMEZONES_LIST.hash) { switch_core_hash_destroy(&TIMEZONES_LIST.hash); } if (TIMEZONES_LIST.pool) { switch_core_destroy_memory_pool(&TIMEZONES_LIST.pool); } memset(&TIMEZONES_LIST, 0, sizeof(TIMEZONES_LIST)); switch_core_new_memory_pool(&TIMEZONES_LIST.pool); switch_core_hash_init(&TIMEZONES_LIST.hash, TIMEZONES_LIST.pool); if ((xml = switch_xml_open_cfg("timezones.conf", &cfg, NULL))) { if ((x_lists = switch_xml_child(cfg, "timezones"))) { for (x_list = switch_xml_child(x_lists, "zone"); x_list; x_list = x_list->next) { const char *name = switch_xml_attr(x_list, "name"); const char *value = switch_xml_attr(x_list, "value"); if (zstr(name)) { continue; } if (zstr(value)) { continue; } switch_core_hash_insert(TIMEZONES_LIST.hash, name, switch_core_strdup(TIMEZONES_LIST.pool, value)); total++; } } switch_xml_free(xml); } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Timezone %sloaded %d definitions\n", reload ? "re" : "", total); }
SWITCH_DECLARE(switch_status_t) switch_directory_exists(const char *dirname, switch_memory_pool_t *pool) { apr_dir_t *dir_handle; switch_memory_pool_t *our_pool = NULL; switch_status_t status; if (!pool) { switch_core_new_memory_pool(&our_pool); pool = our_pool; } if ((status = apr_dir_open(&dir_handle, dirname, pool)) == APR_SUCCESS) { apr_dir_close(dir_handle); } if (our_pool) { switch_core_destroy_memory_pool(&our_pool); } return status; }
static switch_status_t handle_msg_bgapi(listener_t *listener, erlang_msg * msg, int arity, ei_x_buff * buf, ei_x_buff * rbuf) { char api_cmd[MAXATOMLEN]; char arg[ARGLEN]; if (arity < 3 || ei_decode_atom(buf->buff, &buf->index, api_cmd) || ei_decode_string_or_binary(buf->buff, &buf->index, ARGLEN - 1, arg)) { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); } else { struct api_command_struct *acs = NULL; switch_memory_pool_t *pool; switch_thread_t *thread; switch_threadattr_t *thd_attr = NULL; switch_uuid_t uuid; switch_core_new_memory_pool(&pool); acs = switch_core_alloc(pool, sizeof(*acs)); switch_assert(acs); acs->pool = pool; acs->listener = listener; acs->api_cmd = switch_core_strdup(acs->pool, api_cmd); acs->arg = switch_core_strdup(acs->pool, arg); acs->bg = 1; acs->pid = msg->from; switch_threadattr_create(&thd_attr, acs->pool); switch_threadattr_detach_set(thd_attr, 1); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); switch_uuid_get(&uuid); switch_uuid_format(acs->uuid_str, &uuid); switch_thread_create(&thread, thd_attr, api_exec, acs, acs->pool); ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "ok"); _ei_x_encode_string(rbuf, acs->uuid_str); } return SWITCH_STATUS_SUCCESS; }
static JSBool socket_construct(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { js_socket_obj_t *js_socket_obj = 0; switch_memory_pool_t *pool; switch_socket_t *socket; switch_status_t ret; switch_core_new_memory_pool(&pool); ret = switch_socket_create(&socket, AF_INET, SOCK_STREAM, SWITCH_PROTO_TCP, pool); if (ret != SWITCH_STATUS_SUCCESS) { switch_core_destroy_memory_pool(&pool); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Failed to create socket, reason: %d.\n", ret); return JS_FALSE; } // allocate information needed by JS to be able to write to the log. // (needed since multitple js sessions can write to the same log) js_socket_obj = switch_core_alloc(pool, sizeof(js_socket_obj_t)); js_socket_obj->pool = pool; js_socket_obj->socket = socket; JS_SetPrivate(cx, obj, js_socket_obj); return JS_TRUE; }
SWITCH_DECLARE(switch_status_t) switch_core_directory_open(switch_directory_handle_t *dh, char *module_name, char *source, char *dsn, char *passwd, switch_memory_pool_t *pool) { switch_status_t status; if ((dh->directory_interface = switch_loadable_module_get_directory_interface(module_name)) == 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Invalid directory module [%s]!\n", module_name); return SWITCH_STATUS_GENERR; } if (pool) { dh->memory_pool = pool; } else { if ((status = switch_core_new_memory_pool(&dh->memory_pool)) != SWITCH_STATUS_SUCCESS) { UNPROTECT_INTERFACE(dh->directory_interface); return status; } switch_set_flag(dh, SWITCH_DIRECTORY_FLAG_FREE_POOL); } return dh->directory_interface->directory_open(dh, source, dsn, passwd); }
static switch_status_t bind_fetch_agent(switch_xml_section_t section, switch_xml_binding_t **binding) { switch_memory_pool_t *pool = NULL; ei_xml_agent_t *agent; /* create memory pool for this xml search binging (lives for duration of mod_kazoo runtime) */ if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Out of memory: They're not people; they're hippies!\n"); return SWITCH_STATUS_MEMERR; } /* allocate some memory to store the fetch bindings for this section */ if (!(agent = switch_core_alloc(pool, sizeof (*agent)))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Out of memory: Oh, Jesus tap-dancing Christ!\n"); return SWITCH_STATUS_MEMERR; } /* try to bind to the switch */ if (switch_xml_bind_search_function_ret(fetch_handler, section, agent, binding) != SWITCH_STATUS_SUCCESS) { switch_core_destroy_memory_pool(&pool); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Could not bind to FreeSWITCH %s XML requests\n" ,xml_section_to_string(section)); return SWITCH_STATUS_GENERR; } agent->pool = pool; agent->section = section; switch_thread_rwlock_create(&agent->lock, pool); agent->clients = NULL; switch_mutex_init(&agent->current_client_mutex, SWITCH_MUTEX_DEFAULT, pool); agent->current_client = NULL; switch_mutex_init(&agent->replies_mutex, SWITCH_MUTEX_DEFAULT, pool); switch_thread_cond_create(&agent->new_reply, pool); agent->replies = NULL; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Bound to %s XML requests\n" ,xml_section_to_string(section)); return SWITCH_STATUS_SUCCESS; }
WSClientParser::WSClientParser() { // TODO Auto-generated constructor stub mState = WSClientState_UnKnow; mpCallback = NULL; mpClient = NULL; mpUser = NULL; mpDomain = NULL; mpDestNumber = NULL; mpSite = NULL; mpCustom = NULL; mpChannel = NULL; memset(mWebSocketKey, '\0', sizeof(mWebSocketKey)); // 创建内存池 switch_core_new_memory_pool(&mpPool); switch_mutex_init(&clientMutex, SWITCH_MUTEX_NESTED, mpPool); // 生成唯一标识 switch_uuid_t uuid; switch_uuid_get(&uuid); switch_uuid_format(this->uuid, &uuid); }
/** * Initialize NLSML parser. This function is not thread safe. */ int nlsml_init(void) { if (globals.init) { return 1; } globals.init = SWITCH_TRUE; switch_core_new_memory_pool(&globals.pool); switch_core_hash_init(&globals.tag_defs, globals.pool); add_root_tag_def("result", process_attribs_ignore, process_cdata_ignore, "interpretation"); add_tag_def("interpretation", process_attribs_ignore, process_cdata_ignore, "input,model,xf:model,instance,xf:instance"); add_tag_def("input", process_attribs_ignore, process_cdata_match, "input,nomatch,noinput"); add_tag_def("noinput", process_noinput, process_cdata_bad, ""); add_tag_def("nomatch", process_nomatch, process_cdata_ignore, ""); add_tag_def("model", process_attribs_ignore, process_cdata_ignore, "ANY"); add_tag_def("xf:model", process_attribs_ignore, process_cdata_ignore, "ANY"); add_tag_def("instance", process_attribs_ignore, process_cdata_ignore, "ANY"); add_tag_def("xf:instance", process_attribs_ignore, process_cdata_ignore, "ANY"); add_tag_def("ANY", process_attribs_ignore, process_cdata_ignore, "ANY"); return 1; }
int py_thread(const char *text) { switch_thread_t *thread; switch_threadattr_t *thd_attr = NULL; switch_memory_pool_t *pool; struct switch_py_thread *pt; switch_core_new_memory_pool(&pool); assert(pool != NULL); pt = switch_core_alloc(pool, sizeof(*pt)); assert(pt != NULL); pt->pool = pool; pt->args = switch_core_strdup(pt->pool, text); switch_threadattr_create(&thd_attr, pt->pool); switch_threadattr_detach_set(thd_attr, 1); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); switch_thread_create(&thread, thd_attr, py_thread_run, pt, pt->pool); return 0; }
SWITCH_DECLARE(switch_status_t) switch_file_exists(const char *filename, switch_memory_pool_t *pool) { int32_t wanted = APR_FINFO_TYPE; switch_memory_pool_t *our_pool = NULL; switch_status_t status = SWITCH_STATUS_FALSE; apr_finfo_t info = { 0 }; if (!pool) { switch_core_new_memory_pool(&our_pool); } if (filename) { apr_stat(&info, filename, wanted, pool ? pool : our_pool); if (info.filetype != APR_NOFILE) { status = SWITCH_STATUS_SUCCESS; } } if (our_pool) { switch_core_destroy_memory_pool(&our_pool); } return status; }
SWITCH_DECLARE(void) switch_ssl_init_ssl_locks(void) { int i, num; if (ssl_count == 0) { num = CRYPTO_num_locks(); ssl_mutexes = OPENSSL_malloc(CRYPTO_num_locks() * sizeof(switch_mutex_t*)); switch_assert(ssl_mutexes != NULL); switch_core_new_memory_pool(&ssl_pool); for (i = 0; i < num; i++) { switch_mutex_init(&(ssl_mutexes[i]), SWITCH_MUTEX_NESTED, ssl_pool); switch_assert(ssl_mutexes[i] != NULL); } CRYPTO_set_id_callback(switch_ssl_ssl_thread_id); CRYPTO_set_locking_callback((void (*)(int, int, const char*, int))switch_ssl_ssl_lock_callback); } ssl_count++; }
static int task_thread_loop(int done) { switch_scheduler_task_container_t *tofree, *tp, *last = NULL; switch_mutex_lock(globals.task_mutex); for (tp = globals.task_list; tp; tp = tp->next) { if (done) { tp->destroyed = 1; } else if (!tp->destroyed) { int64_t now = switch_epoch_time_now(NULL); if (now >= tp->task.runtime && !tp->in_thread) { int32_t diff = (int32_t) (now - tp->task.runtime); if (diff > 1) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Task was executed late by %d seconds %u %s (%s)\n", diff, tp->task.task_id, tp->desc, switch_str_nil(tp->task.group)); } tp->executed = now; if (switch_test_flag(tp, SSHF_OWN_THREAD)) { switch_thread_t *thread; switch_threadattr_t *thd_attr; switch_core_new_memory_pool(&tp->pool); switch_threadattr_create(&thd_attr, tp->pool); switch_threadattr_detach_set(thd_attr, 1); tp->in_thread = 1; switch_thread_create(&thread, thd_attr, task_own_thread, tp, tp->pool); } else { tp->running = 1; switch_mutex_unlock(globals.task_mutex); switch_scheduler_execute(tp); switch_mutex_lock(globals.task_mutex); tp->running = 0; } } } } switch_mutex_unlock(globals.task_mutex); switch_mutex_lock(globals.task_mutex); for (tp = globals.task_list; tp;) { if (tp->destroyed && !tp->in_thread) { switch_event_t *event; tofree = tp; tp = tp->next; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Deleting task %u %s (%s)\n", tofree->task.task_id, tofree->desc, switch_str_nil(tofree->task.group)); if (switch_event_create(&event, SWITCH_EVENT_DEL_SCHEDULE) == SWITCH_STATUS_SUCCESS) { switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Task-ID", "%u", tofree->task.task_id); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Task-Desc", tofree->desc); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Task-Group", switch_str_nil(tofree->task.group)); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Task-Runtime", "%" SWITCH_INT64_T_FMT, tofree->task.runtime); switch_queue_push(globals.event_queue, event); event = NULL; } if (last) { last->next = tofree->next; } else { globals.task_list = tofree->next; } switch_safe_free(tofree->task.group); if (tofree->task.cmd_arg && switch_test_flag(tofree, SSHF_FREE_ARG)) { free(tofree->task.cmd_arg); } switch_safe_free(tofree->desc); free(tofree); } else { last = tp; tp = tp->next; } } switch_mutex_unlock(globals.task_mutex); return done; }
/* rotate the log file */ static switch_status_t mod_logfile_rotate(logfile_profile_t *profile) { unsigned int i = 0; char *filename = NULL; switch_status_t stat = 0; int64_t offset = 0; switch_memory_pool_t *pool = NULL; switch_time_exp_t tm; char date[80] = ""; switch_size_t retsize; switch_status_t status = SWITCH_STATUS_SUCCESS; switch_mutex_lock(globals.mutex); switch_time_exp_lt(&tm, switch_micro_time_now()); switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm); profile->log_size = 0; stat = switch_file_seek(profile->log_afd, SWITCH_SEEK_SET, &offset); if (stat != SWITCH_STATUS_SUCCESS) { status = SWITCH_STATUS_FALSE; goto end; } switch_core_new_memory_pool(&pool); filename = switch_core_alloc(pool, strlen(profile->logfile) + WARM_FUZZY_OFFSET); if (profile->max_rot) { char *from_filename = NULL; char *to_filename = NULL; from_filename = switch_core_alloc(pool, strlen(profile->logfile) + WARM_FUZZY_OFFSET); to_filename = switch_core_alloc(pool, strlen(profile->logfile) + WARM_FUZZY_OFFSET); for (i=profile->suffix; i>1; i--) { sprintf((char *) to_filename, "%s.%i", profile->logfile, i); sprintf((char *) from_filename, "%s.%i", profile->logfile, i-1); if (switch_file_exists(to_filename, pool) == SWITCH_STATUS_SUCCESS) { if ((status = switch_file_remove(to_filename, pool)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error removing log %s\n",to_filename); goto end; } } if ((status = switch_file_rename(from_filename, to_filename, pool)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error renaming log from %s to %s [%s]\n", from_filename, to_filename, strerror(errno)); goto end; } } sprintf((char *) to_filename, "%s.%i", profile->logfile, i); if (switch_file_exists(to_filename, pool) == SWITCH_STATUS_SUCCESS) { if ((status = switch_file_remove(to_filename, pool)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error removing log %s [%s]\n", to_filename, strerror(errno)); goto end; } } switch_file_close(profile->log_afd); if ((status = switch_file_rename(profile->logfile, to_filename, pool)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error renaming log from %s to %s [%s]\n", profile->logfile, to_filename, strerror(errno)); goto end; } if ((status = mod_logfile_openlogfile(profile, SWITCH_FALSE)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error reopening log %s\n", profile->logfile); } if (profile->suffix < profile->max_rot) { profile->suffix++; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New log started.\n"); goto end; } /* XXX This have no real value EXCEPT making sure if we rotate within the same second, the end index will increase */ for (i = 1; i < MAX_ROT; i++) { sprintf((char *) filename, "%s.%s.%i", profile->logfile, date, i); if (switch_file_exists(filename, pool) == SWITCH_STATUS_SUCCESS) { continue; } switch_file_close(profile->log_afd); switch_file_rename(profile->logfile, filename, pool); if ((status = mod_logfile_openlogfile(profile, SWITCH_FALSE)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error Rotating Log!\n"); goto end; } break; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New log started.\n"); end: if (pool) { switch_core_destroy_memory_pool(&pool); } switch_mutex_unlock(globals.mutex); return status; }
static JSBool teletone_construct(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * rval) { JSObject *session_obj; struct teletone_obj *tto = NULL; struct js_session *jss = NULL; switch_memory_pool_t *pool; char *timer_name = NULL; switch_codec_implementation_t read_impl = { 0 }; if (argc > 0) { if (JS_ValueToObject(cx, argv[0], &session_obj)) { if (!(jss = JS_GetPrivate(cx, session_obj))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot Find Session [1]\n"); return JS_FALSE; } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot Find Session [2]\n"); return JS_FALSE; } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing Session Arg\n"); return JS_FALSE; } if (argc > 1) { timer_name = JS_GetStringBytes(JS_ValueToString(cx, argv[1])); } switch_core_new_memory_pool(&pool); if (!(tto = switch_core_alloc(pool, sizeof(*tto)))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error\n"); return JS_FALSE; } switch_core_session_get_read_impl(jss->session, &read_impl); if (switch_core_codec_init(&tto->codec, "L16", NULL, read_impl.actual_samples_per_second, read_impl.microseconds_per_packet / 1000, read_impl.number_of_channels, SWITCH_CODEC_FLAG_ENCODE | SWITCH_CODEC_FLAG_DECODE, NULL, pool) == SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Raw Codec Activated\n"); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Raw Codec Activation Failed\n"); return JS_FALSE; } if (timer_name) { unsigned int ms = read_impl.microseconds_per_packet / 1000; if (switch_core_timer_init(&tto->timer_base, timer_name, ms, (read_impl.samples_per_second / 50) * read_impl.number_of_channels, pool) == SWITCH_STATUS_SUCCESS) { tto->timer = &tto->timer_base; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Timer INIT Success %u\n", ms); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Timer INIT Failed\n"); } } switch_buffer_create_dynamic(&tto->audio_buffer, JS_BLOCK_SIZE, JS_BUFFER_SIZE, 0); tto->pool = pool; tto->obj = obj; tto->cx = cx; tto->session = jss->session; teletone_init_session(&tto->ts, 0, teletone_handler, tto); JS_SetPrivate(cx, obj, tto); return JS_TRUE; }