Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

switch_core.c File Reference


Data Structures

struct  switch_ip_list_t

Defines

#define BUFSIZE   1024

Functions

 SWITCH_STANDARD_SCHED_FUNC (heartbeat_callback)
switch_status_t switch_core_set_console (const char *console)
 Set the output console to the desired file.
FILE * switch_core_get_console (void)
 Get the output console.
FILE * switch_core_data_channel (switch_text_channel_t channel)
 Retrieve a FILE stream of a given text channel name.
int switch_core_add_state_handler (const switch_state_handler_table_t *state_handler)
const switch_state_handler_table_tswitch_core_get_state_handler (int index)
char * switch_core_get_variable (const char *varname)
void switch_core_set_variable (const char *varname, const char *value)
char * switch_core_get_uuid (void)
 Retrieve the unique identifier from the core.
void switch_core_thread_session_end (switch_core_thread_session_t *thread_session)
void switch_core_service_session (switch_core_session_t *session, switch_core_thread_session_t *thread_session, int stream_id)
void switch_core_launch_thread (switch_thread_start_t func, void *obj, switch_memory_pool_t *pool)
void switch_core_set_globals (void)
 Initiate Globals.
int32_t set_high_priority (void)
 Set the maximum priority the process can obtain.
int32_t change_user_group (const char *user, const char *group)
 Change user and/or group of the running process.
void switch_core_runtime_loop (int bg)
 Run endlessly until the system is shutdown.
const char * switch_core_mime_ext2type (const char *ext)
switch_hash_index_tswitch_core_mime_index (void)
switch_status_t switch_core_mime_add_type (const char *type, const char *ext)
void switch_core_setrlimits (void)
switch_bool_t switch_check_network_list_ip (const char *ip_str, const char *list_name)
void switch_load_network_lists (switch_bool_t reload)
uint32_t switch_core_max_dtmf_duration (uint32_t duration)
uint32_t switch_core_default_dtmf_duration (uint32_t duration)
switch_status_t switch_core_init (switch_core_flag_t flags, switch_bool_t console, const char **err)
switch_status_t switch_core_init_and_modload (switch_core_flag_t flags, switch_bool_t console, const char **err)
void switch_core_measure_time (switch_time_t total_ms, switch_core_time_duration_t *duration)
 Breakdown a number of milliseconds into various time spec.
switch_time_t switch_core_uptime (void)
 Number of microseconds the system has been up.
int32_t switch_core_session_ctl (switch_session_ctl_t cmd, int32_t *val)
 send a control message to the core
switch_core_flag_t switch_core_flags (void)
 return core flags
switch_bool_t switch_core_ready (void)
 Determines if the core is ready to take calls.
switch_status_t switch_core_destroy (void)
 Destroy the core.
switch_status_t switch_core_management_exec (char *relative_oid, switch_management_action_t action, char *data, switch_size_t datalen)
 Execute a management operation.
void switch_core_memory_reclaim_all (void)

Variables

SWITCH_DECLARE_DATA switch_directories SWITCH_GLOBAL_dirs = { 0 }
switch_runtime runtime


Define Documentation

#define BUFSIZE   1024
 


Function Documentation

int switch_core_add_state_handler const switch_state_handler_table_t state_handler  ) 
 

00120 {
00121         int index = runtime.state_handler_index++;
00122 
00123         if (runtime.state_handler_index >= SWITCH_MAX_STATE_HANDLERS) {
00124                 return -1;
00125         }
00126 
00127         runtime.state_handlers[index] = state_handler;
00128         return index;
00129 }

const switch_state_handler_table_t* switch_core_get_state_handler int  index  ) 
 

00132 {
00133 
00134         if (index >= SWITCH_MAX_STATE_HANDLERS || index > runtime.state_handler_index) {
00135                 return NULL;
00136         }
00137 
00138         return runtime.state_handlers[index];
00139 }

char* switch_core_get_variable const char *  varname  ) 
 

00142 {
00143         char *val;
00144         switch_mutex_lock(runtime.global_mutex);
00145         val = (char *) switch_core_hash_find(runtime.global_vars, varname);
00146         switch_mutex_unlock(runtime.global_mutex);
00147         return val;
00148 }

Here is the call graph for this function:

switch_status_t switch_core_init switch_core_flag_t  flags,
switch_bool_t  console,
const char **  err
 

00850 {
00851         switch_xml_t xml = NULL, cfg = NULL;
00852         switch_uuid_t uuid;
00853         char guess_ip[256];
00854 
00855         memset(&runtime, 0, sizeof(runtime));
00856 
00857         runtime.dummy_cng_frame.data = runtime.dummy_data;
00858         runtime.dummy_cng_frame.datalen= sizeof(runtime.dummy_data);
00859         runtime.dummy_cng_frame.buflen= sizeof(runtime.dummy_data);
00860         runtime.dummy_cng_frame.flags = SFF_CNG;
00861 
00862         switch_set_flag((&runtime), SCF_NO_NEW_SESSIONS);
00863         runtime.hard_log_level = SWITCH_LOG_DEBUG;
00864         runtime.mailer_app = "sendmail";
00865         runtime.mailer_app_args = "-t";
00866         runtime.max_dtmf_duration = SWITCH_MAX_DTMF_DURATION;
00867         runtime.default_dtmf_duration = SWITCH_DEFAULT_DTMF_DURATION;
00868 
00869         /* INIT APR and Create the pool context */
00870         if (apr_initialize() != SWITCH_STATUS_SUCCESS) {
00871                 *err = "FATAL ERROR! Could not initialize APR\n";
00872                 return SWITCH_STATUS_MEMERR;
00873         }
00874 
00875         if (!(runtime.memory_pool = switch_core_memory_init())) {
00876                 *err = "FATAL ERROR! Could not allocate memory pool\n";
00877                 return SWITCH_STATUS_MEMERR;
00878         }
00879         switch_assert(runtime.memory_pool != NULL);
00880         switch_mutex_init(&runtime.throttle_mutex, SWITCH_MUTEX_NESTED, runtime.memory_pool);
00881         switch_mutex_init(&runtime.global_mutex, SWITCH_MUTEX_NESTED, runtime.memory_pool);
00882         switch_core_set_globals();
00883         switch_core_session_init(runtime.memory_pool);
00884         switch_core_hash_init(&runtime.global_vars, runtime.memory_pool);
00885         switch_core_hash_init(&runtime.mime_types, runtime.memory_pool);
00886         load_mime_types();
00887         runtime.flags = flags;
00888         runtime.sps_total = 30;
00889 
00890         switch_find_local_ip(guess_ip, sizeof(guess_ip), AF_INET);
00891         switch_core_set_variable("local_ip_v4", guess_ip);
00892         switch_find_local_ip(guess_ip, sizeof(guess_ip), AF_INET6);
00893         switch_core_set_variable("local_ip_v6", guess_ip);
00894         switch_core_set_variable("base_dir", SWITCH_GLOBAL_dirs.base_dir);
00895 
00896         switch_log_init(runtime.memory_pool);
00897         switch_event_init(runtime.memory_pool);
00898 
00899         if (switch_xml_init(runtime.memory_pool, err) != SWITCH_STATUS_SUCCESS) {
00900                 apr_terminate();
00901                 return SWITCH_STATUS_MEMERR;
00902         }
00903 
00904         if ((xml = switch_xml_open_cfg("switch.conf", &cfg, NULL))) {
00905                 switch_xml_t settings, param;
00906 
00907                 if ((settings = switch_xml_child(cfg, "settings"))) {
00908                         for (param = switch_xml_child(settings, "param"); param; param = param->next) {
00909                                 const char *var = switch_xml_attr_soft(param, "name");
00910                                 const char *val = switch_xml_attr_soft(param, "value");
00911 
00912                                 if (!strcasecmp(var, "crash-protection")) {
00913                                         if (switch_true(val)) {
00914                                                 switch_set_flag((&runtime), SCF_CRASH_PROT);
00915                                         }
00916                                 } else if (!strcasecmp(var, "loglevel")) {
00917                     int level;                                                                                                                                  
00918                     if (*val > 47 && *val < 58) {
00919                         level = atoi(val);
00920                     } else {
00921                         level = switch_log_str2level(val);
00922                     }
00923 
00924                                         if (level != SWITCH_LOG_INVALID) {
00925                             switch_core_session_ctl(SCSC_LOGLEVEL, &level);
00926                                         }
00927                                         
00928 #ifdef HAVE_SETRLIMIT
00929                                 } else if (!strcasecmp(var, "dump-cores")) {
00930                                         struct rlimit rlp;
00931                                         memset(&rlp, 0, sizeof(rlp));
00932                                         rlp.rlim_cur = RLIM_INFINITY;
00933                                         rlp.rlim_max = RLIM_INFINITY;
00934                                         setrlimit(RLIMIT_CORE, &rlp);
00935 #endif
00936                                 } else if (!strcasecmp(var, "mailer-app")) {
00937                                         runtime.mailer_app = switch_core_strdup(runtime.memory_pool, val);
00938                                 } else if (!strcasecmp(var, "mailer-app-args")) {
00939                                         runtime.mailer_app_args = switch_core_strdup(runtime.memory_pool, val);
00940                                 } else if (!strcasecmp(var, "sessions-per-second")) {
00941                                         switch_core_sessions_per_second(atoi(val));
00942                                 } else if (!strcasecmp(var, "max_dtmf_duration")) {
00943                                         int tmp = atoi(val);
00944                                         if (tmp > 0) {
00945                                                 switch_core_max_dtmf_duration((uint32_t)tmp);
00946                                         }
00947                                 } else if (!strcasecmp(var, "default_dtmf_duration")) {
00948                                         int tmp = atoi(val);
00949                                         if (tmp > 0) {
00950                                                 switch_core_default_dtmf_duration((uint32_t)tmp);
00951                                         }
00952                                 } else if (!strcasecmp(var, "disable-monotonic-timing")) {
00953                                         switch_time_set_monotonic(SWITCH_FALSE);
00954                                 } else if (!strcasecmp(var, "max-sessions")) {
00955                                         switch_core_session_limit(atoi(val));
00956                                 }
00957                                 else if (!strcasecmp(var, "rtp-start-port")) {
00958                                         switch_rtp_set_start_port((switch_port_t)atoi(val));
00959                                 }
00960                                 else if (!strcasecmp(var, "rtp-end-port")) {
00961                                         switch_rtp_set_end_port((switch_port_t)atoi(val));
00962                                 }
00963                         }
00964                 }
00965 
00966                 if ((settings = switch_xml_child(cfg, "variables"))) {
00967                         for (param = switch_xml_child(settings, "variable"); param; param = param->next) {
00968                                 const char *var = switch_xml_attr_soft(param, "name");
00969                                 const char *val = switch_xml_attr_soft(param, "value");
00970                                 char *varr = NULL, *vall = NULL;
00971 
00972                                 varr = switch_core_strdup(runtime.memory_pool, var);
00973                                 vall = switch_core_strdup(runtime.memory_pool, val);
00974                                 switch_core_hash_insert(runtime.global_vars, varr, vall);
00975                         }
00976                 }
00977 
00978                 switch_xml_free(xml);
00979         }
00980 
00981         switch_core_state_machine_init(runtime.memory_pool);
00982 
00983         *err = NULL;
00984 
00985         if (console) {
00986                 runtime.console = stdout;
00987         }
00988 
00989         if (switch_test_flag((&runtime), SCF_USE_SQL)) {
00990                 switch_core_sqldb_start(runtime.memory_pool);
00991         }
00992         switch_rtp_init(runtime.memory_pool);
00993         runtime.running = 1;
00994 
00995         switch_scheduler_task_thread_start();
00996         runtime.initiated = switch_time_now();
00997 
00998         switch_scheduler_add_task(switch_timestamp(NULL), heartbeat_callback, "heartbeat", "core", 0, NULL, SSHF_NONE | SSHF_NO_DEL);
00999 
01000         switch_uuid_get(&uuid);
01001         switch_uuid_format(runtime.uuid_str, &uuid);
01002 
01003         return SWITCH_STATUS_SUCCESS;
01004 }

Here is the call graph for this function:

switch_status_t switch_core_init_and_modload switch_core_flag_t  flags,
switch_bool_t  console,
const char **  err
 

01072 {
01073         switch_event_t *event;
01074         if (switch_core_init(flags, console, err) != SWITCH_STATUS_SUCCESS) {
01075                 return SWITCH_STATUS_GENERR;
01076         }
01077 
01078         /* set signal handlers */
01079         signal(SIGINT, handle_SIGINT);
01080 #ifdef SIGPIPE
01081         signal(SIGPIPE, handle_SIGPIPE);
01082 #endif
01083 #ifdef SIGQUIT
01084         signal(SIGQUIT, handle_SIGQUIT);
01085 #endif
01086 #ifdef SIGPOLL
01087         signal(SIGPOLL, handle_SIGPOLL);
01088 #endif
01089 #ifdef SIGIO
01090         signal(SIGIO, handle_SIGIO);
01091 #endif
01092 #ifdef TRAP_BUS
01093         signal(SIGBUS, handle_SIGBUS);
01094 #endif
01095 
01096         signal(SIGHUP, handle_SIGHUP);
01097         switch_load_network_lists(SWITCH_FALSE);        
01098 
01099         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Bringing up environment.\n");
01100         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Loading Modules.\n");
01101         if (switch_loadable_module_init() != SWITCH_STATUS_SUCCESS) {
01102                 *err = "Cannot load modules";
01103                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Error: %s\n", *err);
01104                 return SWITCH_STATUS_GENERR;
01105         }
01106 
01107         if (switch_event_create(&event, SWITCH_EVENT_STARTUP) == SWITCH_STATUS_SUCCESS) {
01108                 switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Ready");
01109                 switch_event_fire(&event);
01110         }
01111 
01112         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE,
01113                                           "\nFreeSWITCH Version %s Started.\nCrash Protection [%s]\nMax Sessions[%u]\nSession Rate[%d]\nSQL [%s]\n", SWITCH_VERSION_FULL, 
01114                                           switch_test_flag((&runtime), SCF_CRASH_PROT) ? "Enabled" : "Disabled",
01115                                           switch_core_session_limit(0),
01116                                           switch_core_sessions_per_second(0),
01117                                           switch_test_flag((&runtime), SCF_USE_SQL) ? "Enabled" : "Disabled"
01118                                           );
01119 
01120         switch_clear_flag((&runtime), SCF_NO_NEW_SESSIONS);
01121 
01122         return SWITCH_STATUS_SUCCESS;
01123 
01124 }

Here is the call graph for this function:

void switch_core_launch_thread switch_thread_start_t  func,
void *  obj,
switch_memory_pool_t pool
 

00241 {
00242         switch_thread_t *thread;
00243         switch_threadattr_t *thd_attr = NULL;
00244         switch_core_thread_session_t *ts;
00245         int mypool;
00246 
00247         mypool = pool ? 0 : 1;
00248 
00249         if (!pool && switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) {
00250                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not allocate memory pool\n");
00251                 return;
00252         }
00253 
00254         switch_threadattr_create(&thd_attr, pool);
00255         switch_threadattr_detach_set(thd_attr, 1);
00256 
00257         if ((ts = switch_core_alloc(pool, sizeof(*ts))) == 0) {
00258                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not allocate memory\n");
00259         } else {
00260                 if (mypool) {
00261                         ts->pool = pool;
00262                 }
00263                 ts->objs[0] = obj;
00264                 switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
00265                 switch_thread_create(&thread, thd_attr, func, ts, pool);
00266         }
00267 }

Here is the call graph for this function:

void switch_core_service_session switch_core_session_t session,
switch_core_thread_session_t thread_session,
int  stream_id
 

00219 {
00220         thread_session->running = 1;
00221         thread_session->objs[0] = session;
00222         thread_session->objs[1] = &stream_id;
00223         switch_core_session_launch_thread(session, switch_core_service_thread, thread_session);
00224 }

Here is the call graph for this function:

void switch_core_set_variable const char *  varname,
const char *  value
 

00151 {
00152         char *val;
00153 
00154         if (varname) {
00155                 switch_mutex_lock(runtime.global_mutex);
00156                 val = (char *) switch_core_hash_find(runtime.global_vars, varname);
00157                 if (val) {
00158                         free(val);
00159                 }
00160                 if (value) {
00161                         switch_core_hash_insert(runtime.global_vars, varname, strdup(value));
00162                 } else {
00163                         switch_core_hash_delete(runtime.global_vars, varname);
00164                 }
00165                 switch_mutex_unlock(runtime.global_mutex);
00166         }
00167 }

Here is the call graph for this function:

void switch_core_thread_session_end switch_core_thread_session_t thread_session  ) 
 

00208 {
00209         if (thread_session->running > 0) {
00210                 thread_session->running = -1;
00211 
00212                 while (thread_session->running) {
00213                         switch_yield(1000);
00214                 }
00215         }
00216 }

SWITCH_STANDARD_SCHED_FUNC heartbeat_callback   ) 
 

00079 {
00080         send_heartbeat();
00081 
00082         /* reschedule this task */
00083         task->runtime = switch_timestamp(NULL) + 20;
00084 }

Here is the call graph for this function:


Variable Documentation

struct switch_runtime runtime
 

SWITCH_DECLARE_DATA switch_directories SWITCH_GLOBAL_dirs = { 0 }
 


Generated on Mon May 26 22:06:51 2008 for FreeSWITCH by  doxygen 1.3.9.1