RTS API Documentation  1.10.11
switch_core.c
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  * Michael Jerris <mike@jerris.com>
28  * Paul D. Tinsley <pdt at jackhammer.org>
29  * Marcel Barbulescu <marcelbarbulescu@gmail.com>
30  * Joseph Sullivan <jossulli@amazon.com>
31  * Seven Du <dujinfang@gmail.com>
32  * Andrey Volk <andywolk@gmail.com>
33  *
34  * switch_core.c -- Main Core Library
35  *
36  */
37 
38 
39 
40 #include <switch.h>
41 #include <switch_ssl.h>
42 #include <switch_stun.h>
43 #include <switch_nat.h>
44 #include "private/switch_apr_pvt.h"
46 #include <switch_curl.h>
47 #include <switch_msrp.h>
48 #ifndef WIN32
49 #include <switch_private.h>
50 #ifdef HAVE_SETRLIMIT
51 #include <sys/resource.h>
52 #endif
53 #endif
54 #include <errno.h>
55 #include <sqlite3.h>
56 #ifdef HAVE_SYS_PRCTL_H
57 #include <sys/prctl.h>
58 #endif
59 #ifdef SOLARIS_PRIVILEGES
60 #include <priv.h>
61 #endif
62 
63 #ifdef __linux__
64 #include <sys/wait.h>
65 #ifndef _GNU_SOURCE
66 #define _GNU_SOURCE /* Required for POSIX_SPAWN_USEVFORK */
67 #endif
68 #include <spawn.h>
69 #include <poll.h>
70 #endif
71 
72 #ifdef WIN32
73 #define popen _popen
74 #define pclose _pclose
75 #endif
76 
77 #ifdef HAVE_SYSTEMD
78 #include <systemd/sd-daemon.h>
79 #endif
80 #include <xswitch.h> // xswitch functions and extensions
81 
84 
85 /* The main runtime obj we keep this hidden for ourselves */
86 struct switch_runtime runtime = { 0 };
87 static void switch_load_core_config(const char *file);
88 
89 static void send_heartbeat(void)
90 {
91  switch_event_t *event;
93 
95 
97  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Ready");
99  "%u year%s, "
100  "%u day%s, "
101  "%u hour%s, "
102  "%u minute%s, "
103  "%u second%s, "
104  "%u millisecond%s, "
105  "%u microsecond%s",
106  duration.yr, duration.yr == 1 ? "" : "s",
107  duration.day, duration.day == 1 ? "" : "s",
108  duration.hr, duration.hr == 1 ? "" : "s",
109  duration.min, duration.min == 1 ? "" : "s",
110  duration.sec, duration.sec == 1 ? "" : "s",
111  duration.ms, duration.ms == 1 ? "" : "s", duration.mms, duration.mms == 1 ? "" : "s");
112 
113  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "FreeSWITCH-Version", "%s", switch_version_full());
117  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Per-Sec", "%u", runtime.sps);
118  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Per-Sec-Last", "%u", runtime.sps_last);
119  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Per-Sec-Max", "%u", runtime.sps_peak);
120  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Per-Sec-FiveMin", "%u", runtime.sps_peak_fivemin);
121  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Since-Startup", "%" SWITCH_SIZE_T_FMT, switch_core_session_id() - 1);
122  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Peak-Max", "%u", runtime.sessions_peak);
123  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Peak-FiveMin", "%u", runtime.sessions_peak_fivemin);
125  switch_event_fire(&event);
126  }
127 }
128 
129 static char main_ip4[256] = "";
130 static char main_ip6[256] = "";
131 
132 static void check_ip(void)
133 {
134  char guess_ip4[256] = "";
135  char guess_ip6[256] = "";
136  char old_ip4[256] = "";
137  char old_ip6[256] = "";
138  int ok4 = 1, ok6 = 1;
139  int mask = 0;
140  switch_status_t check6, check4;
141  switch_event_t *event;
142  char *hostname = switch_core_get_variable("hostname");
143 
144  gethostname(runtime.hostname, sizeof(runtime.hostname));
145 
146  if (zstr(hostname)) {
147  switch_core_set_variable("hostname", runtime.hostname);
148  } else if (strcmp(hostname, runtime.hostname)) {
150  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "condition", "hostname-change");
151  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "old-hostname", hostname);
152  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "new-hostname", runtime.hostname);
153  switch_event_fire(&event);
154  }
155 
156  switch_core_set_variable("hostname", runtime.hostname);
157  }
158 
159  check4 = switch_find_local_ip(guess_ip4, sizeof(guess_ip4), &mask, AF_INET);
160  check6 = switch_find_local_ip(guess_ip6, sizeof(guess_ip6), NULL, AF_INET6);
161 
162  if (check6 != SWITCH_STATUS_SUCCESS && (zstr(main_ip6) || !strcasecmp(main_ip6, "::1"))) {
163  check6 = SWITCH_STATUS_SUCCESS;
164  }
165 
166  if (check4 != SWITCH_STATUS_SUCCESS) {
167  ok4 = 2;
168  } else if (!*main_ip4) {
169  switch_set_string(main_ip4, guess_ip4);
170  } else {
171  if (!(ok4 = !strcmp(main_ip4, guess_ip4))) {
172  struct in_addr in;
173 
174  in.s_addr = mask;
175  switch_set_string(old_ip4, main_ip4);
176  switch_set_string(main_ip4, guess_ip4);
177  switch_core_set_variable("local_ip_v4", guess_ip4);
178  switch_core_set_variable("local_mask_v4", inet_ntoa(in));
179  }
180  }
181 
182  if (check6 != SWITCH_STATUS_SUCCESS) {
183  ok6 = 2;
184  } else if (!*main_ip6) {
185  switch_set_string(main_ip6, guess_ip6);
186  } else {
187  if (!(ok6 = !strcmp(main_ip6, guess_ip6))) {
188  switch_set_string(old_ip6, main_ip6);
189  switch_set_string(main_ip6, guess_ip6);
190  switch_core_set_variable("local_ip_v6", guess_ip6);
191  }
192  }
193 
194  if (!ok4 || !ok6) {
196  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "condition", "network-address-change");
197  if (!ok4) {
198  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-previous-v4", old_ip4);
199  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-change-v4", main_ip4);
200  }
201  if (!ok6) {
202  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-previous-v6", old_ip6);
203  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-change-v6", main_ip6);
204  }
205  switch_event_fire(&event);
206  }
207  }
208 
209  if (ok4 == 2 || ok6 == 2) {
211  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "condition", "network-outage");
212 
213  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-status-v4", ok4 == 2 ? "disconnected" : "active");
214  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-v4", main_ip4);
215 
216  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-status-v6", ok6 == 2 ? "disconnected" : "active");
217  switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-v6", main_ip6);
218 
219  switch_event_fire(&event);
220  }
221  }
222 
223 }
224 
225 SWITCH_STANDARD_SCHED_FUNC(heartbeat_callback)
226 {
227  send_heartbeat();
228 
229  /* reschedule this task */
230  task->runtime = switch_epoch_time_now(NULL) + runtime.event_heartbeat_interval;
231 }
232 
233 
234 SWITCH_STANDARD_SCHED_FUNC(check_ip_callback)
235 {
236  check_ip();
237 
238  /* reschedule this task */
239  task->runtime = switch_epoch_time_now(NULL) + 60;
240 }
241 
242 
244 {
245  if ((runtime.console = fopen(console, "a")) == 0) {
246  fprintf(stderr, "Cannot open output file %s.\n", console);
247  return SWITCH_STATUS_FALSE;
248  }
249 
250  return SWITCH_STATUS_SUCCESS;
251 }
252 
254 {
255  return runtime.console;
256 }
257 
258 #ifdef HAVE_SYS_IOCTL_H
259 #include <sys/ioctl.h>
260 #endif
262 {
263 
264 #ifdef WIN32
265  CONSOLE_SCREEN_BUFFER_INFO csbi;
266  int ret;
267 
268  if ((ret = GetConsoleScreenBufferInfo(GetStdHandle( STD_OUTPUT_HANDLE ), &csbi))) {
269  if (x) *x = csbi.dwSize.X;
270  if (y) *y = csbi.dwSize.Y;
271  }
272 
273 #elif defined(TIOCGWINSZ)
274  struct winsize w;
275  ioctl(0, TIOCGWINSZ, &w);
276 
277  if (x) *x = w.ws_col;
278  if (y) *y = w.ws_row;
279 #else
280  if (x) *x = 80;
281  if (y) *y = 24;
282 #endif
283 
284 }
285 
287 {
288  return runtime.console;
289 }
290 
291 
293 {
294  int index, tmp_index = 0;
296 
298 
299  for (index = 0; index < runtime.state_handler_index; index++) {
301  runtime.state_handlers[index] = NULL;
302  if (cur == state_handler) {
303  continue;
304  }
305  tmp[tmp_index++] = cur;
306  }
307 
308  runtime.state_handler_index = 0;
309 
310  for (index = 0; index < tmp_index; index++) {
311  runtime.state_handlers[runtime.state_handler_index++] = tmp[index];
312  }
314 }
315 
316 
318 {
319  int index;
320 
322  index = runtime.state_handler_index;
323 
324  if (index > (SWITCH_MAX_STATE_HANDLERS - 1)) {
325  index = -1;
326  } else {
327  runtime.state_handlers[index] = state_handler;
328  runtime.state_handler_index++;
329  }
330 
332  return index;
333 }
334 
336 {
337 
338  if (index >= SWITCH_MAX_STATE_HANDLERS || index > runtime.state_handler_index) {
339  return NULL;
340  }
341 
342  return runtime.state_handlers[index];
343 }
344 
346 {
348 
350  for (hi = runtime.global_vars->headers; hi; hi = hi->next) {
351  stream->write_function(stream, "%s=%s\n", hi->name, hi->value);
352  }
354 }
355 
357 {
358  return runtime.hostname;
359 }
360 
362 {
363  if (!zstr(runtime.switchname)) return runtime.switchname;
364  return runtime.hostname;
365 }
366 
368 {
369  char *domain;
370  const char *var;
371 
373  if (!(var = switch_core_get_variable("domain"))) {
374  var = "freeswitch.local";
375  }
376  if (dup) {
377  domain = strdup(var);
378  } else {
379  domain = (char *) var;
380  }
382 
383  return domain;
384 }
385 
387 {
388  switch_status_t status;
390  status = switch_event_dup(event, runtime.global_vars);
392  return status;
393 }
394 
395 SWITCH_DECLARE(char *) switch_core_get_variable(const char *varname)
396 {
397  char *val;
399  val = (char *) switch_event_get_header(runtime.global_vars, varname);
401  return val;
402 }
403 
404 SWITCH_DECLARE(char *) switch_core_get_variable_dup(const char *varname)
405 {
406  char *val = NULL, *v;
407 
408  if (varname) {
410  if ((v = (char *) switch_event_get_header(runtime.global_vars, varname))) {
411  val = strdup(v);
412  }
414  }
415 
416  return val;
417 }
418 
420 {
421  char *val = NULL, *v;
422 
423  if (varname) {
425  if ((v = (char *) switch_event_get_header(runtime.global_vars, varname))) {
426  val = switch_core_strdup(pool, v);
427  }
429  }
430 
431  return val;
432 }
433 
435 {
440 }
441 
442 SWITCH_DECLARE(void) switch_core_set_variable(const char *varname, const char *value)
443 {
444  char *val;
445 
446  if (varname) {
448  val = (char *) switch_event_get_header(runtime.global_vars, varname);
449  if (val) {
450  switch_event_del_header(runtime.global_vars, varname);
451  }
452  if (value) {
453  char *v = strdup(value);
456  free(v);
457  } else {
458  switch_event_del_header(runtime.global_vars, varname);
459  }
461  }
462 }
463 
464 SWITCH_DECLARE(switch_bool_t) switch_core_set_var_conditional(const char *varname, const char *value, const char *val2)
465 {
466  char *val;
467 
468  if (varname) {
470  val = (char *) switch_event_get_header(runtime.global_vars, varname);
471 
472  if (val) {
473  if (!val2 || strcmp(val, val2) != 0) {
475  return SWITCH_FALSE;
476  }
477  switch_event_del_header(runtime.global_vars, varname);
478  } else if (!zstr(val2)) {
480  return SWITCH_FALSE;
481  }
482 
483  if (value) {
484  char *v = strdup(value);
487  } else {
488  switch_event_del_header(runtime.global_vars, varname);
489  }
491  }
492  return SWITCH_TRUE;
493 }
494 
496 {
497  return runtime.uuid_str;
498 }
499 
500 
502 {
503  switch_core_session_t *session = obj;
504  switch_channel_t *channel;
505  switch_frame_t *read_frame = NULL;
506 
507 // switch_assert(thread != NULL);
508 // switch_assert(session != NULL);
509 
511  return NULL;
512  }
513 
515 
516  channel = switch_core_session_get_channel(session);
517 
519  while (switch_channel_test_flag(channel, CF_SERVICE)) {
520 
522  switch (switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0)) {
525  case SWITCH_STATUS_BREAK:
526  break;
527  default:
529  break;
530  }
531  }
532 
534  switch (switch_core_session_read_video_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0)) {
537  case SWITCH_STATUS_BREAK:
538  break;
539  default:
541  break;
542  }
543  }
544  }
545 
547 
550 
552 
553  return NULL;
554 }
555 
556 /* Either add a timeout here or make damn sure the thread cannot get hung somehow (my preference) */
558 {
559  switch_channel_t *channel;
560  switch_assert(session);
561 
562  channel = switch_core_session_get_channel(session);
563  switch_assert(channel);
564 
568 
570 
571 }
572 
574 {
575  switch_channel_t *channel;
576  switch_assert(session);
577 
578  channel = switch_core_session_get_channel(session);
579  switch_assert(channel);
580 
581  if (audio) switch_channel_set_flag(channel, CF_SERVICE_AUDIO);
582  if (video) switch_channel_set_flag(channel, CF_SERVICE_VIDEO);
583 
584  switch_core_session_launch_thread(session, (void *(*)(switch_thread_t *,void *))switch_core_service_thread, session);
585 }
586 
587 /* This function abstracts the thread creation for modules by allowing you to pass a function ptr and
588  a void object and trust that that the function will be run in a thread with arg This lets
589  you request and activate a thread without giving up any knowledge about what is in the thread
590  neither the core nor the calling module know anything about each other.
591 
592  This thread is expected to never exit until the application exits so the func is responsible
593  to make sure that is the case.
594 
595  The typical use for this is so switch_loadable_module.c can start up a thread for each module
596  passing the table of module methods as a session obj into the core without actually allowing
597  the core to have any clue and keeping switch_loadable_module.c from needing any thread code.
598 
599 */
600 
602 {
603  switch_thread_t *thread = NULL;
604  switch_threadattr_t *thd_attr = NULL;
606  int mypool;
607 
608  mypool = pool ? 0 : 1;
609 
610  if (!pool && switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) {
611  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not allocate memory pool\n");
612  return NULL;
613  }
614 
615  switch_threadattr_create(&thd_attr, pool);
616 
617  if ((ts = switch_core_alloc(pool, sizeof(*ts))) == 0) {
618  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not allocate memory\n");
619  } else {
620  if (mypool) {
621  ts->pool = pool;
622  }
623  ts->objs[0] = obj;
624  ts->objs[1] = thread;
627  switch_thread_create(&thread, thd_attr, func, ts, pool);
628  }
629 
630  return thread;
631 }
632 
634 {
635 #define BUFSIZE 1024
636 #ifdef WIN32
637  char lpPathBuffer[BUFSIZE];
638  DWORD dwBufSize = BUFSIZE;
639  char base_dir[1024];
640  char *lastbacklash;
641  char *tmp;
642 
643  GetModuleFileName(NULL, base_dir, BUFSIZE);
644  lastbacklash = strrchr(base_dir, '\\');
645  base_dir[(lastbacklash - base_dir)] = '\0';
646  /* set base_dir as cwd, to be able to use relative paths in scripting languages (e.g. mod_lua) when FS is running as a service or while debugging FS using visual studio */
647  SetCurrentDirectory(base_dir);
648  tmp = switch_string_replace(base_dir, "\\", "/");
649  strcpy(base_dir, tmp);
650  free(tmp);
651 
652 #else
653  char base_dir[1024] = SWITCH_PREFIX_DIR;
654 #endif
655 
656  /* Order of precedence for, eg, rundir:
657  * -run
658  * -base
659  * --with-rundir
660  * --prefix
661  */
662 
663  if (!SWITCH_GLOBAL_dirs.mod_dir && (SWITCH_GLOBAL_dirs.mod_dir = (char *) malloc(BUFSIZE))) {
666  else
667 #ifdef SWITCH_MOD_DIR
668  switch_snprintf(SWITCH_GLOBAL_dirs.mod_dir, BUFSIZE, "%s", SWITCH_MOD_DIR);
669 #else
671 #endif
672  }
673 
674  if (!SWITCH_GLOBAL_dirs.lib_dir && (SWITCH_GLOBAL_dirs.lib_dir = (char *) malloc(BUFSIZE))) {
677  else
678 #ifdef SWITCH_LIB_DIR
679  switch_snprintf(SWITCH_GLOBAL_dirs.lib_dir, BUFSIZE, "%s", SWITCH_LIB_DIR);
680 #else
682 #endif
683  }
684 
685  if (!SWITCH_GLOBAL_dirs.conf_dir && (SWITCH_GLOBAL_dirs.conf_dir = (char *) malloc(BUFSIZE))) {
688  else
689 #ifdef SWITCH_CONF_DIR
690  switch_snprintf(SWITCH_GLOBAL_dirs.conf_dir, BUFSIZE, "%s", SWITCH_CONF_DIR);
691 #else
693 #endif
694  }
695 
696  if (!SWITCH_GLOBAL_dirs.log_dir && (SWITCH_GLOBAL_dirs.log_dir = (char *) malloc(BUFSIZE))) {
699  else
700 #ifdef SWITCH_LOG_DIR
701  switch_snprintf(SWITCH_GLOBAL_dirs.log_dir, BUFSIZE, "%s", SWITCH_LOG_DIR);
702 #else
704 #endif
705  }
706 
707  if (!SWITCH_GLOBAL_dirs.run_dir && (SWITCH_GLOBAL_dirs.run_dir = (char *) malloc(BUFSIZE))) {
710  else
711 #ifdef SWITCH_RUN_DIR
712  switch_snprintf(SWITCH_GLOBAL_dirs.run_dir, BUFSIZE, "%s", SWITCH_RUN_DIR);
713 #else
715 #endif
716  }
717 
721  else
722 #ifdef SWITCH_RECORDINGS_DIR
723  switch_snprintf(SWITCH_GLOBAL_dirs.recordings_dir, BUFSIZE, "%s", SWITCH_RECORDINGS_DIR);
724 #else
726 #endif
727  }
728 
729  if (!SWITCH_GLOBAL_dirs.sounds_dir && (SWITCH_GLOBAL_dirs.sounds_dir = (char *) malloc(BUFSIZE))) {
732  else
733 #ifdef SWITCH_SOUNDS_DIR
734  switch_snprintf(SWITCH_GLOBAL_dirs.sounds_dir, BUFSIZE, "%s", SWITCH_SOUNDS_DIR);
735 #else
737 #endif
738  }
739 
743  else
744 #ifdef SWITCH_STORAGE_DIR
745  switch_snprintf(SWITCH_GLOBAL_dirs.storage_dir, BUFSIZE, "%s", SWITCH_STORAGE_DIR);
746 #else
748 #endif
749  }
750 
751  if (!SWITCH_GLOBAL_dirs.cache_dir && (SWITCH_GLOBAL_dirs.cache_dir = (char *) malloc(BUFSIZE))) {
754  else
755 #ifdef SWITCH_CACHE_DIR
756  switch_snprintf(SWITCH_GLOBAL_dirs.cache_dir, BUFSIZE, "%s", SWITCH_CACHE_DIR);
757 #else
759 #endif
760  }
761 
762  if (!SWITCH_GLOBAL_dirs.db_dir && (SWITCH_GLOBAL_dirs.db_dir = (char *) malloc(BUFSIZE))) {
765  else
766 #ifdef SWITCH_DB_DIR
767  switch_snprintf(SWITCH_GLOBAL_dirs.db_dir, BUFSIZE, "%s", SWITCH_DB_DIR);
768 #else
770 #endif
771  }
772 
773  if (!SWITCH_GLOBAL_dirs.script_dir && (SWITCH_GLOBAL_dirs.script_dir = (char *) malloc(BUFSIZE))) {
776  else
777 #ifdef SWITCH_SCRIPT_DIR
778  switch_snprintf(SWITCH_GLOBAL_dirs.script_dir, BUFSIZE, "%s", SWITCH_SCRIPT_DIR);
779 #else
781 #endif
782  }
783 
784  if (!SWITCH_GLOBAL_dirs.htdocs_dir && (SWITCH_GLOBAL_dirs.htdocs_dir = (char *) malloc(BUFSIZE))) {
787  else
788 #ifdef SWITCH_HTDOCS_DIR
789  switch_snprintf(SWITCH_GLOBAL_dirs.htdocs_dir, BUFSIZE, "%s", SWITCH_HTDOCS_DIR);
790 #else
792 #endif
793  }
794 
798  else
799 #ifdef SWITCH_GRAMMAR_DIR
800  switch_snprintf(SWITCH_GLOBAL_dirs.grammar_dir, BUFSIZE, "%s", SWITCH_GRAMMAR_DIR);
801 #else
803 #endif
804  }
805 
806  if (!SWITCH_GLOBAL_dirs.fonts_dir && (SWITCH_GLOBAL_dirs.fonts_dir = (char *) malloc(BUFSIZE))) {
809  else
810 #ifdef SWITCH_FONTS_DIR
811  switch_snprintf(SWITCH_GLOBAL_dirs.fonts_dir, BUFSIZE, "%s", SWITCH_FONTS_DIR);
812 #else
814 #endif
815  }
816 
817  if (!SWITCH_GLOBAL_dirs.images_dir && (SWITCH_GLOBAL_dirs.images_dir = (char *) malloc(BUFSIZE))) {
820  else
821 #ifdef SWITCH_IMAGES_DIR
822  switch_snprintf(SWITCH_GLOBAL_dirs.images_dir, BUFSIZE, "%s", SWITCH_IMAGES_DIR);
823 #else
825 #endif
826  }
827 
828  if (!SWITCH_GLOBAL_dirs.data_dir && (SWITCH_GLOBAL_dirs.data_dir = (char *) malloc(BUFSIZE))) {
831  else
832 #ifdef SWITCH_DATA_DIR
833  switch_snprintf(SWITCH_GLOBAL_dirs.data_dir, BUFSIZE, "%s", SWITCH_DATA_DIR);
834 #else
836 #endif
837  }
838 
842  else
843 #ifdef SWITCH_LOCALSTATE_DIR
844  switch_snprintf(SWITCH_GLOBAL_dirs.localstate_dir, BUFSIZE, "%s", SWITCH_LOCALSTATE_DIR);
845 #else
847 #endif
848  }
849 
850  if (!SWITCH_GLOBAL_dirs.certs_dir && (SWITCH_GLOBAL_dirs.certs_dir = (char *) malloc(BUFSIZE))) {
853  else
854 #ifdef SWITCH_CERTS_DIR
855  switch_snprintf(SWITCH_GLOBAL_dirs.certs_dir, BUFSIZE, "%s", SWITCH_CERTS_DIR);
856 #else
858 #endif
859  }
860 
861  if (!SWITCH_GLOBAL_dirs.temp_dir && (SWITCH_GLOBAL_dirs.temp_dir = (char *) malloc(BUFSIZE))) {
862 #ifdef SWITCH_TEMP_DIR
863  switch_snprintf(SWITCH_GLOBAL_dirs.temp_dir, BUFSIZE, "%s", SWITCH_TEMP_DIR);
864 #else
865 #ifdef WIN32
866  GetTempPath(dwBufSize, lpPathBuffer);
867  lpPathBuffer[strlen(lpPathBuffer)-1] = 0;
868  tmp = switch_string_replace(lpPathBuffer, "\\", "/");
869  strcpy(lpPathBuffer, tmp);
870  free(tmp);
871  switch_snprintf(SWITCH_GLOBAL_dirs.temp_dir, BUFSIZE, "%s", lpPathBuffer);
872 #else
874 #endif
875 #endif
876  }
877 
879  switch_snprintf(SWITCH_GLOBAL_filenames.conf_name, BUFSIZE, "%s", "freeswitch.xml");
880  }
881 
882  /* Do this last because it being empty is part of the above logic */
883  if (!SWITCH_GLOBAL_dirs.base_dir && (SWITCH_GLOBAL_dirs.base_dir = (char *) malloc(BUFSIZE))) {
885  }
886 
905 
907 }
908 
909 
911 {
912 #ifdef SOLARIS_PRIVILEGES
913  priv_set_t *basicset;
914 
915  /* make the process privilege-aware */
916  setpflags(PRIV_AWARE, 1);
917 
918  /* reset the privileges to basic */
919  basicset = priv_str_to_set("basic", ",", NULL);
920  if (setppriv(PRIV_SET, PRIV_EFFECTIVE, basicset) != 0) {
921  fprintf(stderr, "ERROR: Failed to acquire basic privileges (%s)\n", strerror(errno));
922  }
923 
924  /* we need high-resolution clock, and this requires a non-basic privilege */
925  if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_CLOCK_HIGHRES, NULL) < 0) {
926  fprintf(stderr, "ERROR: Failed to acquire proc_clock_highres privilege (%s)\n", strerror(errno));
927  return -1;
928  }
929 
930  /* need this for setrlimit */
931  if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_SYS_RESOURCE, NULL) < 0) {
932  fprintf(stderr, "ERROR: Failed to acquire sys_resource privilege (%s)\n", strerror(errno));
933  return -1;
934  }
935 
936  /* we need to read directories belonging to other uid */
937  if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_FILE_DAC_SEARCH, NULL) < 0) {
938  fprintf(stderr, "ERROR: Failed to acquire file_dac_search privilege (%s)\n", strerror(errno));
939  return -1;
940  }
941 #endif
942  return 0;
943 }
944 
946 {
947 #ifdef WIN32
948  return SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
949 #else
950 #if defined(USE_SCHED_SETSCHEDULER) && ! defined(SOLARIS_PRIVILEGES)
951  /*
952  * Try to use a normal scheduler
953  */
954  struct sched_param sched = { 0 };
955  sched.sched_priority = 0;
956  if (sched_setscheduler(0, SCHED_OTHER, &sched) < 0) {
957  fprintf(stderr, "ERROR: Failed to set SCHED_OTHER scheduler (%s)\n", strerror(errno));
958  return -1;
959  }
960 #endif
961 
962 #ifdef HAVE_SETPRIORITY
963  /*
964  * setpriority() works on FreeBSD (6.2), nice() doesn't
965  */
966  if (setpriority(PRIO_PROCESS, getpid(), 19) < 0) {
967  fprintf(stderr, "ERROR: Could not set nice level\n");
968  return -1;
969  }
970 #else
971  if (nice(19) != 19) {
972  fprintf(stderr, "ERROR: Could not set nice level\n");
973  return -1;
974  }
975 #endif
976 
977  return 0;
978 #endif
979 }
980 
982 {
983 #ifdef WIN32
984  return SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
985 #else
986 #ifdef USE_SCHED_SETSCHEDULER
987  /*
988  * Try to use a round-robin scheduler
989  * with a fallback if that does not work
990  */
991  struct sched_param sched = { 0 };
992  sched.sched_priority = SWITCH_PRI_LOW;
993 #endif
994 
995 #ifdef SOLARIS_PRIVILEGES
996  /* request the privileges to elevate the priority */
997  if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_PRIOCNTL, NULL) < 0) {
998 #ifdef PRIV_PROC_PRIOUP
999  /* fallback to PRIV_PROC_PRIOUP on SmartOS */
1000  fprintf(stderr, "WARN: Failed to acquire proc_priocntl privilege (%s)\n", strerror(errno));
1001  if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_PRIOUP, NULL) < 0) {
1002  fprintf(stderr, "ERROR: Failed to acquire proc_prioup privilege (%s)\n", strerror(errno));
1003  return -1;
1004  }
1005 #else
1006  fprintf(stderr, "ERROR: Failed to acquire proc_priocntl privilege (%s)\n", strerror(errno));
1007  return -1;
1008 #endif
1009  }
1010 
1011  if (sched_setscheduler(0, SCHED_FIFO, &sched) < 0) {
1012  fprintf(stderr, "WARN: Failed to set SCHED_FIFO scheduler (%s)\n", strerror(errno));
1013  } else {
1014  return 0;
1015  }
1016 
1017  if (setpriority(PRIO_PROCESS, 0, -10) < 0) {
1018  fprintf(stderr, "ERROR: Could not set nice level\n");
1019  return -1;
1020  }
1021 
1022  return 0;
1023 #else
1024 
1025 #ifdef USE_SCHED_SETSCHEDULER
1026  if (sched_setscheduler(0, SCHED_FIFO, &sched) < 0) {
1027  fprintf(stderr, "ERROR: Failed to set SCHED_FIFO scheduler (%s)\n", strerror(errno));
1028  sched.sched_priority = 0;
1029  if (sched_setscheduler(0, SCHED_OTHER, &sched) < 0 ) {
1030  fprintf(stderr, "ERROR: Failed to set SCHED_OTHER scheduler (%s)\n", strerror(errno));
1031  return -1;
1032  }
1033  }
1034 #endif
1035 
1036 #ifdef HAVE_SETPRIORITY
1037  /*
1038  * setpriority() works on FreeBSD (6.2), nice() doesn't
1039  */
1040  if (setpriority(PRIO_PROCESS, getpid(), -10) < 0) {
1041  fprintf(stderr, "ERROR: Could not set nice level\n");
1042  return -1;
1043  }
1044 #else
1045  if (nice(-10) != -10) {
1046  fprintf(stderr, "ERROR: Could not set nice level\n");
1047  return -1;
1048  }
1049 #endif
1050 #endif
1051  return 0;
1052 #endif
1053 }
1054 
1056 {
1057  return runtime.cpu_count;
1058 }
1059 
1061 {
1062  return 0;
1063 }
1064 
1066 {
1067 #ifndef WIN32
1068  runtime.cpu_count = sysconf (_SC_NPROCESSORS_ONLN);
1069 #else
1070  SYSTEM_INFO sysinfo;
1071  GetSystemInfo( &sysinfo );
1072  runtime.cpu_count = sysinfo.dwNumberOfProcessors;
1073 #endif
1074 
1075  if (!runtime.cpu_count) runtime.cpu_count = 1;
1076 
1077  return set_realtime_priority();
1078 
1079 
1080  // ERROR: code not reachable on Windows Visual Studio Express 2008 return 0;
1081 }
1082 
1083 SWITCH_DECLARE(int32_t) change_user_group(const char *user, const char *group)
1084 {
1085 #ifndef WIN32
1086  uid_t runas_uid = 0;
1087  gid_t runas_gid = 0;
1088  struct passwd *runas_pw = NULL;
1089 
1090  if (user) {
1091  /*
1092  * Lookup user information in the system's db
1093  */
1094  runas_pw = getpwnam(user);
1095  if (!runas_pw) {
1096  fprintf(stderr, "ERROR: Unknown user \"%s\"\n", user);
1097  return -1;
1098  }
1099  runas_uid = runas_pw->pw_uid;
1100  }
1101 
1102  if (group) {
1103  struct group *gr = NULL;
1104 
1105  /*
1106  * Lookup group information in the system's db
1107  */
1108  gr = getgrnam(group);
1109  if (!gr) {
1110  fprintf(stderr, "ERROR: Unknown group \"%s\"\n", group);
1111  return -1;
1112  }
1113  runas_gid = gr->gr_gid;
1114  }
1115 
1116  if (runas_uid && getuid() == runas_uid && (!runas_gid || runas_gid == getgid())) {
1117  /* already running as the right user and group, nothing to do! */
1118  return 0;
1119  }
1120 
1121  if (runas_uid) {
1122 #ifdef SOLARIS_PRIVILEGES
1123  /* request the privilege to set the UID */
1124  if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_SETID, NULL) < 0) {
1125  fprintf(stderr, "ERROR: Failed to acquire proc_setid privilege (%s)\n", strerror(errno));
1126  return -1;
1127  }
1128 #endif
1129 #ifdef HAVE_SETGROUPS
1130  /*
1131  * Drop all group memberships prior to changing anything
1132  * or else we're going to inherit the parent's list of groups
1133  * (which is not what we want...)
1134  */
1135  if (setgroups(0, NULL) < 0) {
1136  fprintf(stderr, "ERROR: Failed to drop group access list\n");
1137  return -1;
1138  }
1139 #endif
1140  if (runas_gid) {
1141  /*
1142  * A group has been passed, switch to it
1143  * (without loading the user's other groups)
1144  */
1145  if (setgid(runas_gid) < 0) {
1146  fprintf(stderr, "ERROR: Failed to change gid!\n");
1147  return -1;
1148  }
1149  } else {
1150  /*
1151  * No group has been passed, use the user's primary group in this case
1152  */
1153  if (setgid(runas_pw->pw_gid) < 0) {
1154  fprintf(stderr, "ERROR: Failed to change gid!\n");
1155  return -1;
1156  }
1157 #ifdef HAVE_INITGROUPS
1158  /*
1159  * Set all the other groups the user is a member of
1160  * (This can be really useful for fine-grained access control)
1161  */
1162  if (initgroups(runas_pw->pw_name, runas_pw->pw_gid) < 0) {
1163  fprintf(stderr, "ERROR: Failed to set group access list for user\n");
1164  return -1;
1165  }
1166 #endif
1167  }
1168 
1169  /*
1170  * Finally drop all privileges by switching to the new userid
1171  */
1172  if (setuid(runas_uid) < 0) {
1173  fprintf(stderr, "ERROR: Failed to change uid!\n");
1174  return -1;
1175  }
1176 #ifdef HAVE_SYS_PRCTL_H
1177  if (prctl(PR_SET_DUMPABLE, 1) < 0) {
1178  fprintf(stderr, "ERROR: Failed to enable core dumps!\n");
1179  return -1;
1180  }
1181 #endif
1182  }
1183 #endif
1184  return 0;
1185 }
1186 
1188 {
1189 #ifdef WIN32
1190  HANDLE shutdown_event;
1191  char path[256] = "";
1192 #endif
1193  if (bg) {
1194 #ifdef WIN32
1195  switch_snprintf(path, sizeof(path), "Global\\Freeswitch.%d", getpid());
1196  shutdown_event = CreateEvent(NULL, FALSE, FALSE, path);
1197  if (shutdown_event) {
1198  WaitForSingleObject(shutdown_event, INFINITE);
1199  }
1200 #else
1201  while (runtime.running) {
1202  switch_yield(1000000);
1203  }
1204 #endif
1205  } else {
1206  /* wait for console input */
1208  }
1209 }
1210 
1211 SWITCH_DECLARE(const char *) switch_core_mime_ext2type(const char *ext)
1212 {
1213  if (!ext) {
1214  return NULL;
1215  }
1216  return (const char *) switch_core_hash_find(runtime.mime_types, ext);
1217 }
1218 
1219 SWITCH_DECLARE(const char *) switch_core_mime_type2ext(const char *mime)
1220 {
1221  if (!mime) {
1222  return NULL;
1223  }
1224  return (const char *) switch_core_hash_find(runtime.mime_type_exts, mime);
1225 }
1226 
1228 {
1229  return switch_core_hash_first(runtime.mime_types);
1230 }
1231 
1232 SWITCH_DECLARE(switch_status_t) switch_core_mime_add_type(const char *type, const char *ext)
1233 {
1234  char *ptype = NULL;
1235  char *ext_list = NULL;
1236  int argc = 0;
1237  char *argv[20] = { 0 };
1238  int x;
1240 
1241  switch_assert(type);
1242  switch_assert(ext);
1243 
1244  ptype = switch_core_permanent_strdup(type);
1245  ext_list = strdup(ext);
1246 
1247  switch_assert(ext_list);
1248 
1249  /* Map each file extension to this MIME type if not already mapped. Map the MIME type to the first file extension in the list if not already mapped. */
1250  if ((argc = switch_separate_string(ext_list, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
1251  int is_mapped_type = switch_core_hash_find(runtime.mime_type_exts, ptype) != NULL;
1252  for (x = 0; x < argc; x++) {
1253  if (argv[x] && ptype) {
1254  if (!switch_core_hash_find(runtime.mime_types, ext)) {
1255  switch_core_hash_insert(runtime.mime_types, argv[x], ptype);
1256  }
1257  if (!is_mapped_type) {
1259  is_mapped_type = 1;
1260  }
1261  }
1262  }
1263 
1264  status = SWITCH_STATUS_SUCCESS;
1265  }
1266 
1267  free(ext_list);
1268 
1269  return status;
1270 }
1271 
1272 static void load_mime_types(void)
1273 {
1274  char *cf = "mime.types";
1275  FILE *fd = NULL;
1276  char *line_buf = NULL;
1277  switch_size_t llen = 0;
1278  char *mime_path = NULL;
1279 
1280  mime_path = switch_mprintf("%s/%s", SWITCH_GLOBAL_dirs.conf_dir, cf);
1281  switch_assert(mime_path);
1282 
1283  fd = fopen(mime_path, "rb");
1284 
1285  if (fd == NULL) {
1286  goto end;
1287  }
1288 
1289  while ((switch_fp_read_dline(fd, &line_buf, &llen))) {
1290  char *p;
1291  char *type = line_buf;
1292 
1293  if (*line_buf == '#') {
1294  continue;
1295  }
1296 
1297  if ((p = strchr(line_buf, '\r')) || (p = strchr(line_buf, '\n'))) {
1298  *p = '\0';
1299  }
1300 
1301  if ((p = strchr(type, '\t')) || (p = strchr(type, ' '))) {
1302  *p++ = '\0';
1303 
1304  while (*p == ' ' || *p == '\t') {
1305  p++;
1306  }
1307 
1308  switch_core_mime_add_type(type, p);
1309  }
1310 
1311  }
1312 
1313  switch_safe_free(line_buf);
1314  fclose(fd);
1315 
1316  end:
1317 
1318  switch_safe_free(mime_path);
1319 
1320 }
1321 
1323 {
1324 #ifdef HAVE_SETRLIMIT
1325  struct rlimit rlp;
1326 
1327  /*
1328  Setting the stack size on FreeBSD results in an instant crash.
1329 
1330  If anyone knows how to fix this,
1331  feel free to submit a patch to https://github.com/signalwire/freeswitch
1332  */
1333 
1334 #ifndef __FreeBSD__
1335  memset(&rlp, 0, sizeof(rlp));
1336  rlp.rlim_cur = SWITCH_THREAD_STACKSIZE;
1337  rlp.rlim_max = SWITCH_SYSTEM_THREAD_STACKSIZE;
1338  setrlimit(RLIMIT_STACK, &rlp);
1339 #endif
1340 
1341  memset(&rlp, 0, sizeof(rlp));
1342  rlp.rlim_cur = 999999;
1343  rlp.rlim_max = 999999;
1344  setrlimit(RLIMIT_NOFILE, &rlp);
1345 
1346  memset(&rlp, 0, sizeof(rlp));
1347  rlp.rlim_cur = RLIM_INFINITY;
1348  rlp.rlim_max = RLIM_INFINITY;
1349 
1350  setrlimit(RLIMIT_CPU, &rlp);
1351  setrlimit(RLIMIT_DATA, &rlp);
1352  setrlimit(RLIMIT_FSIZE, &rlp);
1353 #ifdef RLIMIT_NPROC
1354  setrlimit(RLIMIT_NPROC, &rlp);
1355 #endif
1356 #ifdef RLIMIT_RTPRIO
1357  setrlimit(RLIMIT_RTPRIO, &rlp);
1358 #endif
1359 
1360 #if !defined(__OpenBSD__) && !defined(__NetBSD__)
1361  setrlimit(RLIMIT_AS, &rlp);
1362 #endif
1363 #endif
1364  return;
1365 }
1366 
1367 typedef struct {
1371 
1372 static switch_ip_list_t IP_LIST = { 0 };
1373 
1374 SWITCH_DECLARE(switch_bool_t) switch_check_network_list_ip_port_token(const char *ip_str, int port, const char *list_name, const char **token)
1375 {
1376  switch_network_list_t *list;
1377  ip_t ip, mask, net;
1378  uint32_t bits;
1379  char *ipv6 = strchr(ip_str,':');
1381  char *ipv4 = NULL;
1382 
1383  if (!list_name) {
1384  return SWITCH_FALSE;
1385  }
1386 
1387  if ((ipv4 = switch_network_ipv4_mapped_ipv6_addr(ip_str))) {
1388  ip_str = ipv4;
1389  ipv6 = NULL;
1390  }
1391 
1393  if (ipv6) {
1394  switch_inet_pton(AF_INET6, ip_str, &ip);
1395  } else {
1396  switch_inet_pton(AF_INET, ip_str, &ip);
1397  ip.v4 = htonl(ip.v4);
1398  }
1399 
1400  if ((list = switch_core_hash_find(IP_LIST.hash, list_name))) {
1401  if (ipv6) {
1402  ok = switch_network_list_validate_ip6_port_token(list, ip, port, token);
1403  } else {
1404  ok = switch_network_list_validate_ip_port_token(list, ip.v4, port, token);
1405  }
1406  } else if (strchr(list_name, '/')) {
1407  if (strchr(list_name, ',')) {
1408  char *list_name_dup = strdup(list_name);
1409  char *argv[100]; /* MAX ACL */
1410  int argc;
1411 
1412  switch_assert(list_name_dup);
1413 
1414  if ((argc = switch_separate_string(list_name_dup, ',', argv, (sizeof(argv) / sizeof(argv[0]))))) {
1415  int i;
1416  for (i = 0; i < argc; i++) {
1417  switch_parse_cidr(argv[i], &net, &mask, &bits);
1418  if (ipv6) {
1419  if ((ok = switch_testv6_subnet(ip, net, mask))){
1420  break;
1421  }
1422  } else {
1423  if ((ok = switch_test_subnet(ip.v4, net.v4, mask.v4))) {
1424  break;
1425  }
1426  }
1427  }
1428  }
1429  free(list_name_dup);
1430  } else {
1431  switch_parse_cidr(list_name, &net, &mask, &bits);
1432 
1433  if (ipv6) {
1434  ok = switch_testv6_subnet(ip, net, mask);
1435  } else {
1436  ok = switch_test_subnet(ip.v4, net.v4, mask.v4);
1437  }
1438  }
1439  }
1440 
1441  switch_safe_free(ipv4);
1443 
1444  return ok;
1445 }
1446 
1447 SWITCH_DECLARE(switch_bool_t) switch_check_network_list_ip_token(const char *ip_str, const char *list_name, const char **token)
1448 {
1449  return switch_check_network_list_ip_port_token(ip_str, 0, list_name, token);
1450 }
1451 
1453 {
1454  switch_xml_t xml = NULL, x_lists = NULL, x_list = NULL, x_node = NULL, cfg = NULL;
1455  switch_network_list_t *rfc_list, *list;
1456  char guess_ip[16] = "";
1457  int mask = 0;
1458  char guess_mask[16] = "";
1459  char *tmp_name;
1460  struct in_addr in;
1461 
1462  switch_find_local_ip(guess_ip, sizeof(guess_ip), &mask, AF_INET);
1463  in.s_addr = mask;
1464  switch_set_string(guess_mask, inet_ntoa(in));
1465 
1467 
1468  if (IP_LIST.hash) {
1469  switch_core_hash_destroy(&IP_LIST.hash);
1470  }
1471 
1472  if (IP_LIST.pool) {
1474  }
1475 
1476  memset(&IP_LIST, 0, sizeof(IP_LIST));
1478  switch_core_hash_init(&IP_LIST.hash);
1479 
1480 
1481  tmp_name = "rfc6598.auto";
1482  switch_network_list_create(&rfc_list, tmp_name, SWITCH_FALSE, IP_LIST.pool);
1483  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Created ip list %s default (deny)\n", tmp_name);
1484  switch_network_list_add_cidr(rfc_list, "100.64.0.0/10", SWITCH_TRUE);
1485  switch_core_hash_insert(IP_LIST.hash, tmp_name, rfc_list);
1486 
1487  tmp_name = "rfc1918.auto";
1488  switch_network_list_create(&rfc_list, tmp_name, SWITCH_FALSE, IP_LIST.pool);
1489  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Created ip list %s default (deny)\n", tmp_name);
1490  switch_network_list_add_cidr(rfc_list, "10.0.0.0/8", SWITCH_TRUE);
1491  switch_network_list_add_cidr(rfc_list, "172.16.0.0/12", SWITCH_TRUE);
1492  switch_network_list_add_cidr(rfc_list, "192.168.0.0/16", SWITCH_TRUE);
1493  switch_network_list_add_cidr(rfc_list, "fe80::/10", SWITCH_TRUE);
1494  switch_core_hash_insert(IP_LIST.hash, tmp_name, rfc_list);
1495 
1496  tmp_name = "wan.auto";
1497  switch_network_list_create(&rfc_list, tmp_name, SWITCH_TRUE, IP_LIST.pool);
1498  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Created ip list %s default (allow)\n", tmp_name);
1499  switch_network_list_add_cidr(rfc_list, "0.0.0.0/8", SWITCH_FALSE);
1500  switch_network_list_add_cidr(rfc_list, "10.0.0.0/8", SWITCH_FALSE);
1501  switch_network_list_add_cidr(rfc_list, "172.16.0.0/12", SWITCH_FALSE);
1502  switch_network_list_add_cidr(rfc_list, "192.168.0.0/16", SWITCH_FALSE);
1503  switch_network_list_add_cidr(rfc_list, "169.254.0.0/16", SWITCH_FALSE);
1504  switch_network_list_add_cidr(rfc_list, "100.64.0.0/10", SWITCH_FALSE);
1505  switch_network_list_add_cidr(rfc_list, "fe80::/10", SWITCH_FALSE);
1506  switch_core_hash_insert(IP_LIST.hash, tmp_name, rfc_list);
1507 
1508  tmp_name = "wan_v6.auto";
1509  switch_network_list_create(&rfc_list, tmp_name, SWITCH_TRUE, IP_LIST.pool);
1510  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Created ip list %s default (allow)\n", tmp_name);
1511  switch_network_list_add_cidr(rfc_list, "0.0.0.0/0", SWITCH_FALSE);
1512  switch_network_list_add_cidr(rfc_list, "fe80::/10", SWITCH_FALSE);
1513  switch_core_hash_insert(IP_LIST.hash, tmp_name, rfc_list);
1514 
1515 
1516  tmp_name = "wan_v4.auto";
1517  switch_network_list_create(&rfc_list, tmp_name, SWITCH_TRUE, IP_LIST.pool);
1518  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Created ip list %s default (allow)\n", tmp_name);
1519  switch_network_list_add_cidr(rfc_list, "0.0.0.0/8", SWITCH_FALSE);
1520  switch_network_list_add_cidr(rfc_list, "10.0.0.0/8", SWITCH_FALSE);
1521  switch_network_list_add_cidr(rfc_list, "172.16.0.0/12", SWITCH_FALSE);
1522  switch_network_list_add_cidr(rfc_list, "192.168.0.0/16", SWITCH_FALSE);
1523  switch_network_list_add_cidr(rfc_list, "169.254.0.0/16", SWITCH_FALSE);
1524  switch_network_list_add_cidr(rfc_list, "100.64.0.0/10", SWITCH_FALSE);
1525  switch_network_list_add_cidr(rfc_list, "::/0", SWITCH_FALSE);
1526  switch_core_hash_insert(IP_LIST.hash, tmp_name, rfc_list);
1527 
1528 
1529  tmp_name = "any_v6.auto";
1530  switch_network_list_create(&rfc_list, tmp_name, SWITCH_TRUE, IP_LIST.pool);
1531  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Created ip list %s default (allow)\n", tmp_name);
1532  switch_network_list_add_cidr(rfc_list, "0.0.0.0/0", SWITCH_FALSE);
1533  switch_core_hash_insert(IP_LIST.hash, tmp_name, rfc_list);
1534 
1535 
1536  tmp_name = "any_v4.auto";
1537  switch_network_list_create(&rfc_list, tmp_name, SWITCH_TRUE, IP_LIST.pool);
1538  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Created ip list %s default (allow)\n", tmp_name);
1539  switch_network_list_add_cidr(rfc_list, "::/0", SWITCH_FALSE);
1540  switch_core_hash_insert(IP_LIST.hash, tmp_name, rfc_list);
1541 
1542 
1543  tmp_name = "nat.auto";
1544  switch_network_list_create(&rfc_list, tmp_name, SWITCH_FALSE, IP_LIST.pool);
1545  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Created ip list %s default (deny)\n", tmp_name);
1546  if (switch_network_list_add_host_mask(rfc_list, guess_ip, guess_mask, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
1547  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Adding %s/%s (deny) to list %s\n", guess_ip, guess_mask, tmp_name);
1548  }
1549  switch_network_list_add_cidr(rfc_list, "10.0.0.0/8", SWITCH_TRUE);
1550  switch_network_list_add_cidr(rfc_list, "172.16.0.0/12", SWITCH_TRUE);
1551  switch_network_list_add_cidr(rfc_list, "192.168.0.0/16", SWITCH_TRUE);
1552  switch_network_list_add_cidr(rfc_list, "100.64.0.0/10", SWITCH_TRUE);
1553  switch_core_hash_insert(IP_LIST.hash, tmp_name, rfc_list);
1554 
1555  tmp_name = "loopback.auto";
1556  switch_network_list_create(&rfc_list, tmp_name, SWITCH_FALSE, IP_LIST.pool);
1557  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Created ip list %s default (deny)\n", tmp_name);
1558  switch_network_list_add_cidr(rfc_list, "127.0.0.0/8", SWITCH_TRUE);
1559  switch_network_list_add_cidr(rfc_list, "::1/128", SWITCH_TRUE);
1560  switch_core_hash_insert(IP_LIST.hash, tmp_name, rfc_list);
1561 
1562  tmp_name = "localnet.auto";
1563  switch_network_list_create(&list, tmp_name, SWITCH_FALSE, IP_LIST.pool);
1564  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Created ip list %s default (deny)\n", tmp_name);
1565 
1566  if (switch_network_list_add_host_mask(list, guess_ip, guess_mask, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) {
1567  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Adding %s/%s (allow) to list %s\n", guess_ip, guess_mask, tmp_name);
1568  }
1569  switch_core_hash_insert(IP_LIST.hash, tmp_name, list);
1570 
1571 
1572  if ((xml = switch_xml_open_cfg("acl.conf", &cfg, NULL))) {
1573  if ((x_lists = switch_xml_child(cfg, "network-lists"))) {
1574  for (x_list = switch_xml_child(x_lists, "list"); x_list; x_list = x_list->next) {
1575  const char *name = switch_xml_attr(x_list, "name");
1576  const char *dft = switch_xml_attr(x_list, "default");
1577  switch_bool_t default_type = SWITCH_TRUE;
1578 
1579  if (zstr(name)) {
1580  continue;
1581  }
1582 
1583  if (dft) {
1584  default_type = switch_true(dft);
1585  }
1586 
1587  if (switch_network_list_create(&list, name, default_type, IP_LIST.pool) != SWITCH_STATUS_SUCCESS) {
1588  abort();
1589  }
1590 
1591  if (reload) {
1592  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Created ip list %s default (%s)\n", name, default_type ? "allow" : "deny");
1593  } else {
1594  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Created ip list %s default (%s)\n", name, default_type ? "allow" : "deny");
1595  }
1596 
1597 
1598  for (x_node = switch_xml_child(x_list, "node"); x_node; x_node = x_node->next) {
1599  const char *cidr = NULL, *host = NULL, *mask = NULL, *domain = NULL, *port = NULL;
1600  switch_bool_t ok = default_type;
1601  const char *type = switch_xml_attr(x_node, "type");
1602  switch_network_port_range_t port_range;
1603  char *argv[MAX_NETWORK_PORTS] = { 0 };
1604  int argc = 0, i;
1605 
1606  if (type) {
1607  ok = switch_true(type);
1608  }
1609 
1610  cidr = switch_xml_attr(x_node, "cidr");
1611  host = switch_xml_attr(x_node, "host");
1612  mask = switch_xml_attr(x_node, "mask");
1613  domain = switch_xml_attr(x_node, "domain");
1614 
1615  memset(&port_range, 0, sizeof(switch_network_port_range_t));
1616 
1617  if( (port = switch_xml_attr(x_node, "port")) != NULL) {
1618  port_range.port = atoi(port);
1619  }
1620 
1621  if( (port = switch_xml_attr(x_node, "ports")) != NULL) {
1622  argc = switch_separate_string((char*)port, ',', argv, (sizeof(argv) / sizeof(argv[0])));
1623  for(i=0; i < argc; i++) {
1624  port_range.ports[i] = atoi(argv[i]);
1625  }
1626  }
1627  if( (port = switch_xml_attr(x_node, "port-min")) != NULL) {
1628  port_range.min_port = atoi(port);
1629  }
1630  if( (port = switch_xml_attr(x_node, "port-max")) != NULL) {
1631  port_range.max_port = atoi(port);
1632  }
1633 
1634  if (domain) {
1635  switch_event_t *my_params = NULL;
1636  switch_xml_t x_domain, xml_root;
1637  switch_xml_t gt, gts, ut, uts;
1638 
1640  switch_assert(my_params);
1641  switch_event_add_header_string(my_params, SWITCH_STACK_BOTTOM, "domain", domain);
1642  switch_event_add_header_string(my_params, SWITCH_STACK_BOTTOM, "purpose", "network-list");
1643 
1644  if (switch_xml_locate_domain(domain, my_params, &xml_root, &x_domain) != SWITCH_STATUS_SUCCESS) {
1645  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Cannot locate domain %s\n", domain);
1646  switch_event_destroy(&my_params);
1647  continue;
1648  }
1649 
1650  switch_event_destroy(&my_params);
1651 
1652  if ((ut = switch_xml_child(x_domain, "users"))) {
1653  x_domain = ut;
1654  }
1655 
1656  for (ut = switch_xml_child(x_domain, "user"); ut; ut = ut->next) {
1657  const char *user_cidr = switch_xml_attr(ut, "cidr");
1658  const char *id = switch_xml_attr(ut, "id");
1659 
1660  if (id && user_cidr) {
1661  char *token = switch_mprintf("%s@%s", id, domain);
1662  switch_assert(token);
1663  switch_network_list_add_cidr_token(list, user_cidr, ok, token);
1664  free(token);
1665  }
1666  }
1667 
1668  for (gts = switch_xml_child(x_domain, "groups"); gts; gts = gts->next) {
1669  for (gt = switch_xml_child(gts, "group"); gt; gt = gt->next) {
1670  for (uts = switch_xml_child(gt, "users"); uts; uts = uts->next) {
1671  for (ut = switch_xml_child(uts, "user"); ut; ut = ut->next) {
1672  const char *user_cidr = switch_xml_attr(ut, "cidr");
1673  const char *id = switch_xml_attr(ut, "id");
1674 
1675  if (id && user_cidr) {
1676  char *token = switch_mprintf("%s@%s", id, domain);
1677  switch_assert(token);
1678  switch_network_list_add_cidr_port_token(list, user_cidr, ok, token, &port_range);
1679  free(token);
1680  }
1681  }
1682  }
1683  }
1684  }
1685 
1686  switch_xml_free(xml_root);
1687  } else if (cidr) {
1688  switch_network_list_add_cidr_port_token(list, cidr, ok, NULL, &port_range);
1689  } else if (host && mask) {
1690  switch_network_list_add_host_port_mask(list, host, mask, ok, &port_range);
1691  }
1692  }
1693 
1694  switch_core_hash_insert(IP_LIST.hash, name, list);
1695  }
1696  }
1697 
1698  switch_xml_free(xml);
1699  }
1700 
1702 }
1703 
1704 SWITCH_DECLARE(uint32_t) switch_core_max_dtmf_duration(uint32_t duration)
1705 {
1706  if (duration) {
1707  if (duration > SWITCH_MAX_DTMF_DURATION) {
1708  duration = SWITCH_MAX_DTMF_DURATION;
1709  }
1710  if (duration < SWITCH_MIN_DTMF_DURATION) {
1711  duration = SWITCH_MIN_DTMF_DURATION;
1712  }
1713  runtime.max_dtmf_duration = duration;
1714  if (duration < runtime.min_dtmf_duration) {
1715  runtime.min_dtmf_duration = duration;
1716  }
1717  }
1718  return runtime.max_dtmf_duration;
1719 }
1720 
1722 {
1723  if (duration) {
1724  if (duration < SWITCH_MIN_DTMF_DURATION) {
1725  duration = SWITCH_MIN_DTMF_DURATION;
1726  }
1727  if (duration > SWITCH_MAX_DTMF_DURATION) {
1728  duration = SWITCH_MAX_DTMF_DURATION;
1729  }
1730  runtime.default_dtmf_duration = duration;
1731 
1732  if (duration < runtime.min_dtmf_duration) {
1733  runtime.min_dtmf_duration = duration;
1734  }
1735 
1736  if (duration > runtime.max_dtmf_duration) {
1737  runtime.max_dtmf_duration = duration;
1738  }
1739 
1740  }
1741  return runtime.default_dtmf_duration;
1742 }
1743 
1744 SWITCH_DECLARE(uint32_t) switch_core_min_dtmf_duration(uint32_t duration)
1745 {
1746  if (duration) {
1747  if (duration < SWITCH_MIN_DTMF_DURATION) {
1748  duration = SWITCH_MIN_DTMF_DURATION;
1749  }
1750  if (duration > SWITCH_MAX_DTMF_DURATION) {
1751  duration = SWITCH_MAX_DTMF_DURATION;
1752  }
1753 
1754  runtime.min_dtmf_duration = duration;
1755 
1756  if (duration > runtime.max_dtmf_duration) {
1757  runtime.max_dtmf_duration = duration;
1758  }
1759  }
1760  return runtime.min_dtmf_duration;
1761 }
1762 
1764 {
1766 
1767  if (cpu > -1) {
1768 
1769 #ifdef HAVE_CPU_SET_MACROS
1770  cpu_set_t set;
1771 
1772  CPU_ZERO(&set);
1773  CPU_SET(cpu, &set);
1774 
1775  if (!sched_setaffinity(0, sizeof(set), &set)) {
1776  status = SWITCH_STATUS_SUCCESS;
1777  }
1778 
1779 #else
1780 #if WIN32
1781  if (SetThreadAffinityMask(GetCurrentThread(), (DWORD_PTR) cpu)) {
1782  status = SWITCH_STATUS_SUCCESS;
1783  }
1784 #endif
1785 #endif
1786  }
1787 
1788  return status;
1789 }
1790 
1791 
1793 {
1794  return switch_test_flag((&runtime), flag);
1795 }
1796 
1797 
1799 {
1800  switch_uuid_t uuid;
1801  char guess_ip[256];
1802  int mask = 0;
1803  struct in_addr in;
1804 
1805 
1806  if (runtime.runlevel > 0) {
1807  /* one per customer */
1808  return SWITCH_STATUS_SUCCESS;
1809  }
1810 
1811  memset(&runtime, 0, sizeof(runtime));
1812  gethostname(runtime.hostname, sizeof(runtime.hostname));
1813 
1815  runtime.max_db_handles = 50;
1816  runtime.db_handle_timeout = 5000000;
1817  runtime.event_heartbeat_interval = 20;
1818 
1819  runtime.runlevel++;
1820  runtime.dummy_cng_frame.data = runtime.dummy_data;
1821  runtime.dummy_cng_frame.datalen = sizeof(runtime.dummy_data);
1822  runtime.dummy_cng_frame.buflen = sizeof(runtime.dummy_data);
1823  runtime.dbname = "core";
1825  switch_set_flag((&runtime), SCF_AUTO_SCHEMAS);
1826  switch_set_flag((&runtime), SCF_CLEAR_SQL);
1827  switch_set_flag((&runtime), SCF_API_EXPANSION);
1829 #ifdef WIN32
1831 #endif
1832  switch_set_flag((&runtime), SCF_NO_NEW_SESSIONS);
1833  if (flags & SCF_LOG_DISABLE) {
1835  flags &= ~SCF_LOG_DISABLE;
1836  } else {
1837  runtime.hard_log_level = SWITCH_LOG_DEBUG;
1838  }
1839  runtime.mailer_app = "sendmail";
1840  runtime.mailer_app_args = "-t";
1844  runtime.odbc_dbtype = DBTYPE_DEFAULT;
1845  runtime.dbname = NULL;
1846 #ifndef WIN32
1847  runtime.cpu_count = sysconf (_SC_NPROCESSORS_ONLN);
1848 #else
1849  {
1850  SYSTEM_INFO sysinfo;
1851  GetSystemInfo( &sysinfo );
1852  runtime.cpu_count = sysinfo.dwNumberOfProcessors;
1853  }
1854 #endif
1855 
1856  if (!runtime.cpu_count) runtime.cpu_count = 1;
1857 
1858  if (sqlite3_initialize() != SQLITE_OK) {
1859  *err = "FATAL ERROR! Could not initialize SQLite\n";
1860  return SWITCH_STATUS_MEMERR;
1861  }
1862 
1863  /* INIT APR and Create the pool context */
1864  if (fspr_initialize() != SWITCH_STATUS_SUCCESS) {
1865  *err = "FATAL ERROR! Could not initialize APR\n";
1866  return SWITCH_STATUS_MEMERR;
1867  }
1868 
1869  if (!(runtime.memory_pool = switch_core_memory_init())) {
1870  *err = "FATAL ERROR! Could not allocate memory pool\n";
1871  return SWITCH_STATUS_MEMERR;
1872  }
1873  switch_assert(runtime.memory_pool != NULL);
1874 
1890 
1892 
1894 
1897 
1905  load_mime_types();
1906  runtime.flags |= flags;
1907  runtime.sps_total = 30;
1908 
1909  *err = NULL;
1910 
1911  if (console) {
1912  runtime.console = stdout;
1913  }
1914 
1915  SSL_library_init();
1917  OpenSSL_add_all_algorithms();
1918  switch_curl_init();
1919 
1920  switch_core_set_variable("hostname", runtime.hostname);
1921  switch_find_local_ip(guess_ip, sizeof(guess_ip), &mask, AF_INET);
1922  switch_core_set_variable("local_ip_v4", guess_ip);
1923  in.s_addr = mask;
1924  switch_core_set_variable("local_mask_v4", inet_ntoa(in));
1925 
1926 
1927  switch_find_local_ip(guess_ip, sizeof(guess_ip), NULL, AF_INET6);
1928  switch_core_set_variable("local_ip_v6", guess_ip);
1950  switch_event_init(runtime.memory_pool);
1952 
1953  if (switch_xml_init(runtime.memory_pool, err) != SWITCH_STATUS_SUCCESS) {
1954  /* allow missing configuration if MINIMAL */
1955  if (!(flags & SCF_MINIMAL)) {
1956  fspr_terminate();
1957  return SWITCH_STATUS_MEMERR;
1958  }
1959  }
1960 
1961  if (switch_test_flag((&runtime), SCF_USE_AUTO_NAT)) {
1963  }
1964 
1965  switch_log_init(runtime.memory_pool, runtime.colorize_console);
1966 
1967  runtime.tipping_point = 0;
1968  runtime.timer_affinity = -1;
1969  runtime.microseconds_per_tick = 20000;
1970 
1971  if (flags & SCF_MINIMAL) return SWITCH_STATUS_SUCCESS;
1972 
1973  switch_load_core_config("switch.conf");
1974 
1976 
1979 
1981 
1982  switch_rtp_init(runtime.memory_pool);
1983 
1984  runtime.running = 1;
1986 
1987  switch_scheduler_add_task(switch_epoch_time_now(NULL), heartbeat_callback, "heartbeat", "core", 0, NULL, SSHF_NONE | SSHF_NO_DEL);
1988 
1989  switch_scheduler_add_task(switch_epoch_time_now(NULL), check_ip_callback, "check_ip", "core", 0, NULL, SSHF_NONE | SSHF_NO_DEL | SSHF_OWN_THREAD);
1990 
1991  switch_uuid_get(&uuid);
1992  switch_uuid_format(runtime.uuid_str, &uuid);
1993  switch_core_set_variable("core_uuid", runtime.uuid_str);
1994 
1995 
1996  return SWITCH_STATUS_SUCCESS;
1997 }
1998 
1999 
2000 #ifdef TRAP_BUS
2001 static void handle_SIGBUS(int sig)
2002 {
2004  return;
2005 }
2006 #endif
2007 
2008 static void handle_SIGHUP(int sig)
2009 {
2010  if (sig) {
2011  switch_event_t *event;
2012 
2014  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Trapped-Signal", "HUP");
2015  switch_event_fire(&event);
2016  }
2017  }
2018  return;
2019 }
2020 
2021 
2022 SWITCH_DECLARE(uint32_t) switch_default_ptime(const char *name, uint32_t number)
2023 {
2024  uint32_t *p;
2025 
2026  if ((p = switch_core_hash_find(runtime.ptimes, name))) {
2027  return *p;
2028  }
2029 
2030  return 20;
2031 }
2032 
2033 SWITCH_DECLARE(uint32_t) switch_default_rate(const char *name, uint32_t number)
2034 {
2035 
2036  if (!strcasecmp(name, "opus")) {
2037  return 48000;
2038  } else if (!strncasecmp(name, "h26", 3)) { // h26x
2039  return 90000;
2040  } else if (!strncasecmp(name, "vp", 2)) { // vp8, vp9
2041  return 90000;
2042  }
2043 
2044  return 8000;
2045 }
2046 
2047 static uint32_t d_30 = 30;
2048 
2049 static void switch_load_core_config(const char *file)
2050 {
2051  switch_xml_t xml = NULL, cfg = NULL;
2052 
2053  switch_core_hash_insert(runtime.ptimes, "ilbc", &d_30);
2054  switch_core_hash_insert(runtime.ptimes, "isac", &d_30);
2055  switch_core_hash_insert(runtime.ptimes, "G723", &d_30);
2056 
2057 
2058  if ((xml = switch_xml_open_cfg(file, &cfg, NULL))) {
2059  switch_xml_t settings, param;
2060 
2061  if ((settings = switch_xml_child(cfg, "default-ptimes"))) {
2062  for (param = switch_xml_child(settings, "codec"); param; param = param->next) {
2063  const char *var = switch_xml_attr_soft(param, "name");
2064  const char *val = switch_xml_attr_soft(param, "ptime");
2065 
2066  if (!zstr(var) && !zstr(val)) {
2067  uint32_t *p;
2068  uint32_t v = switch_atoul(val);
2069 
2070  if (!strcasecmp(var, "G723") || !strcasecmp(var, "iLBC")) {
2071  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error adding %s, defaults cannot be changed\n", var);
2072  continue;
2073  }
2074 
2075  if (v == 0) {
2076  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error adding %s, invalid ptime\n", var);
2077  continue;
2078  }
2079 
2080  p = switch_core_alloc(runtime.memory_pool, sizeof(*p));
2081  *p = v;
2082  switch_core_hash_insert(runtime.ptimes, var, p);
2083  }
2084 
2085  }
2086  }
2087 
2088  if ((settings = switch_xml_child(cfg, "settings"))) {
2089  for (param = switch_xml_child(settings, "param"); param; param = param->next) {
2090  const char *var = switch_xml_attr_soft(param, "name");
2091  const char *val = switch_xml_attr_soft(param, "value");
2092 
2093  if (!strcasecmp(var, "loglevel")) {
2094  int level;
2095  if (*val > 47 && *val < 58) {
2096  level = atoi(val);
2097  } else {
2098  level = switch_log_str2level(val);
2099  }
2100 
2101  if (level != SWITCH_LOG_INVALID) {
2103  }
2104 #ifdef HAVE_SETRLIMIT
2105  } else if (!strcasecmp(var, "dump-cores") && switch_true(val)) {
2106  struct rlimit rlp;
2107  memset(&rlp, 0, sizeof(rlp));
2108  rlp.rlim_cur = RLIM_INFINITY;
2109  rlp.rlim_max = RLIM_INFINITY;
2110  setrlimit(RLIMIT_CORE, &rlp);
2111 #endif
2112  } else if (!strcasecmp(var, "debug-level")) {
2113  int tmp = atoi(val);
2114  if (tmp > -1 && tmp < 11) {
2116  }
2117  } else if (!strcasecmp(var, "max-db-handles")) {
2118  long tmp = atol(val);
2119 
2120  if (tmp > 4 && tmp < 5001) {
2121  runtime.max_db_handles = (uint32_t) tmp;
2122  } else {
2123  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "max-db-handles must be between 5 and 5000\n");
2124  }
2125  } else if (!strcasecmp(var, "odbc-skip-autocommit-flip")) {
2126  if (switch_true(val)) {
2128  }
2129  } else if (!strcasecmp(var, "db-handle-timeout")) {
2130  long tmp = atol(val);
2131 
2132  if (tmp > 0 && tmp < 5001) {
2133  runtime.db_handle_timeout = (uint32_t) tmp * 1000000;
2134  } else {
2135  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "db-handle-timeout must be between 1 and 5000\n");
2136  }
2137 
2138  } else if (!strcasecmp(var, "event-heartbeat-interval")) {
2139  long tmp = atol(val);
2140 
2141  if (tmp > 0) {
2142  runtime.event_heartbeat_interval = (uint32_t) tmp;
2143  } else {
2144  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "heartbeat-interval must be a greater than 0\n");
2145  }
2146 
2147  } else if (!strcasecmp(var, "multiple-registrations")) {
2148  runtime.multiple_registrations = switch_true(val);
2149  } else if (!strcasecmp(var, "auto-create-schemas")) {
2150  if (switch_true(val)) {
2151  switch_set_flag((&runtime), SCF_AUTO_SCHEMAS);
2152  } else {
2153  switch_clear_flag((&runtime), SCF_AUTO_SCHEMAS);
2154  }
2155  } else if (!strcasecmp(var, "session-thread-pool")) {
2156  if (switch_true(val)) {
2158  } else {
2160  }
2161  } else if (!strcasecmp(var, "auto-clear-sql")) {
2162  if (switch_true(val)) {
2163  switch_set_flag((&runtime), SCF_CLEAR_SQL);
2164  } else {
2165  switch_clear_flag((&runtime), SCF_CLEAR_SQL);
2166  }
2167  } else if (!strcasecmp(var, "api-expansion")) {
2168  if (switch_true(val)) {
2169  switch_set_flag((&runtime), SCF_API_EXPANSION);
2170  } else {
2171  switch_clear_flag((&runtime), SCF_API_EXPANSION);
2172  }
2173  } else if (!strcasecmp(var, "enable-early-hangup") && switch_true(val)) {
2174  switch_set_flag((&runtime), SCF_EARLY_HANGUP);
2175  } else if (!strcasecmp(var, "colorize-console") && switch_true(val)) {
2176  runtime.colorize_console = SWITCH_TRUE;
2177  } else if (!strcasecmp(var, "core-db-pre-trans-execute") && !zstr(val)) {
2179  } else if (!strcasecmp(var, "core-db-post-trans-execute") && !zstr(val)) {
2181  } else if (!strcasecmp(var, "core-db-inner-pre-trans-execute") && !zstr(val)) {
2183  } else if (!strcasecmp(var, "core-db-inner-post-trans-execute") && !zstr(val)) {
2185  } else if (!strcasecmp(var, "dialplan-timestamps")) {
2186  if (switch_true(val)) {
2188  } else {
2190  }
2191  } else if (!strcasecmp(var, "mailer-app") && !zstr(val)) {
2192  runtime.mailer_app = switch_core_strdup(runtime.memory_pool, val);
2193  } else if (!strcasecmp(var, "mailer-app-args") && val) {
2194  runtime.mailer_app_args = switch_core_strdup(runtime.memory_pool, val);
2195  } else if (!strcasecmp(var, "sessions-per-second") && !zstr(val)) {
2197  } else if (!strcasecmp(var, "max-dtmf-duration") && !zstr(val)) {
2198  int tmp = atoi(val);
2199  if (tmp > 0) {
2200  switch_core_max_dtmf_duration((uint32_t) tmp);
2201  }
2202  } else if (!strcasecmp(var, "min-dtmf-duration") && !zstr(val)) {
2203  int tmp = atoi(val);
2204  if (tmp > 0) {
2205  switch_core_min_dtmf_duration((uint32_t) tmp);
2206  }
2207  } else if (!strcasecmp(var, "default-dtmf-duration") && !zstr(val)) {
2208  int tmp = atoi(val);
2209  if (tmp > 0) {
2210  switch_core_default_dtmf_duration((uint32_t) tmp);
2211  }
2212  } else if (!strcasecmp(var, "enable-use-system-time")) {
2214  } else if (!strcasecmp(var, "enable-monotonic-timing")) {
2216  } else if (!strcasecmp(var, "enable-softtimer-timerfd")) {
2217  int ival = 0;
2218  if (val) {
2219  if (switch_true(val)) {
2220  ival = 2;
2221  } else {
2222  if (strcasecmp(val, "broadcast")) {
2223  ival = 1;
2224  } else if (strcasecmp(val, "fd-per-timer")) {
2225  ival = 2;
2226  }
2227  }
2228  }
2230  } else if (!strcasecmp(var, "enable-clock-nanosleep")) {
2232  } else if (!strcasecmp(var, "enable-cond-yield")) {
2234  } else if (!strcasecmp(var, "enable-timer-matrix")) {
2236  } else if (!strcasecmp(var, "max-sessions") && !zstr(val)) {
2237  switch_core_session_limit(atoi(val));
2238  } else if (!strcasecmp(var, "verbose-channel-events") && !zstr(val)) {
2239  int v = switch_true(val);
2240  if (v) {
2241  switch_set_flag((&runtime), SCF_VERBOSE_EVENTS);
2242  } else {
2244  }
2245  } else if (!strcasecmp(var, "threaded-system-exec") && !zstr(val)) {
2246 #ifdef WIN32
2247  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "threaded-system-exec is not implemented on this platform\n");
2248 #else
2249  int v = switch_true(val);
2250  if (v) {
2252  } else {
2254  }
2255 #endif
2256  } else if (!strcasecmp(var, "spawn-instead-of-system") && !zstr(val)) {
2257 #ifdef WIN32
2258  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "spawn-instead-of-system is not implemented on this platform\n");
2259 #else
2260  int v = switch_true(val);
2261  if (v) {
2262  switch_core_set_variable("spawn_instead_of_system", "true");
2263  } else {
2264  switch_core_set_variable("spawn_instead_of_system", "false");
2265  }
2266 #endif
2267  } else if (!strcasecmp(var, "exclude-error-log-from-xml-cdr") && !zstr(val)) {
2268  int v = switch_true(val);
2269  if (v) {
2270  switch_core_set_variable("exclude_error_log_from_xml_cdr", "true");
2271  } else {
2272  switch_core_set_variable("exclude_error_log_from_xml_cdr", "false");
2273  }
2274  } else if (!strcasecmp(var, "min-idle-cpu") && !zstr(val)) {
2275  switch_core_min_idle_cpu(atof(val));
2276  } else if (!strcasecmp(var, "tipping-point") && !zstr(val)) {
2277  runtime.tipping_point = atoi(val);
2278  } else if (!strcasecmp(var, "cpu-idle-smoothing-depth") && !zstr(val)) {
2279  runtime.cpu_idle_smoothing_depth = atoi(val);
2280  } else if (!strcasecmp(var, "events-use-dispatch") && !zstr(val)) {
2281  runtime.events_use_dispatch = switch_true(val);
2282  } else if (!strcasecmp(var, "initial-event-threads") && !zstr(val)) {
2283  int tmp;
2284 
2285  if (!runtime.events_use_dispatch) {
2286  runtime.events_use_dispatch = 1;
2288  "Implicitly setting events-use-dispatch based on usage of this initial-event-threads parameter.\n");
2289  }
2290 
2291  tmp = atoi(val);
2292 
2293  if (tmp > runtime.cpu_count / 2) {
2294  tmp = runtime.cpu_count / 2;
2295  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "This value cannot be higher than %d so setting it to that value\n",
2296  runtime.cpu_count / 2);
2297  }
2298 
2299  if (tmp < 1) {
2300  tmp = 1;
2301  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "This value cannot be lower than 1 so setting it to that level\n");
2302  }
2303 
2305 
2306  } else if (!strcasecmp(var, "1ms-timer") && switch_true(val)) {
2307  runtime.microseconds_per_tick = 1000;
2308  } else if (!strcasecmp(var, "timer-affinity") && !zstr(val)) {
2309  if (!strcasecmp(val, "disabled")) {
2310  runtime.timer_affinity = -1;
2311  } else {
2312  runtime.timer_affinity = atoi(val);
2313  }
2314  } else if (!strcasecmp(var, "ice-resolve-candidate")) {
2316  } else if (!strcasecmp(var, "rtp-start-port") && !zstr(val)) {
2318  } else if (!strcasecmp(var, "rtp-end-port") && !zstr(val)) {
2320  } else if (!strcasecmp(var, "rtp-port-usage-robustness") && switch_true(val)) {
2321  runtime.port_alloc_flags |= SPF_ROBUST_UDP;
2322  } else if (!strcasecmp(var, "core-db-name") && !zstr(val)) {
2323  runtime.dbname = switch_core_strdup(runtime.memory_pool, val);
2324  } else if (!strcasecmp(var, "core-db-dsn") && !zstr(val)) {
2325  runtime.odbc_dsn = switch_core_strdup(runtime.memory_pool, val);
2326  } else if (!strcasecmp(var, "core-non-sqlite-db-required") && !zstr(val)) {
2328  } else if (!strcasecmp(var, "core-dbtype") && !zstr(val)) {
2329  if (!strcasecmp(val, "MSSQL")) {
2330  runtime.odbc_dbtype = DBTYPE_MSSQL;
2331  } else {
2332  runtime.odbc_dbtype = DBTYPE_DEFAULT;
2333  }
2334  } else if (!strcasecmp(var, "switchname") && !zstr(val)) {
2335  runtime.switchname = switch_core_strdup(runtime.memory_pool, val);
2336  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Set switchname to %s\n", runtime.switchname);
2337  } else if (!strcasecmp(var, "rtp-retain-crypto-keys")) {
2338  if (switch_true(val)) {
2340  "rtp-retain-crypto-keys enabled. Could be used to decrypt secure media.\n");
2341  }
2342  switch_core_set_variable("rtp_retain_crypto_keys", val);
2343  } else if (!strcasecmp(var, "caller-profile-soft-variables-uses-prefix") && !zstr(val)) {
2344  int v = switch_true(val);
2345  if (v) {
2346  switch_set_flag((&runtime), SCF_CPF_SOFT_PREFIX);
2347  } else {
2349  }
2350  } else if (!strcasecmp(var, "caller-profile-soft-lookup-values") && !zstr(val)) {
2351  int v = switch_true(val);
2352  if (v) {
2353  switch_set_flag((&runtime), SCF_CPF_SOFT_LOOKUP);
2354  } else {
2356  }
2357  } else if (!strcasecmp(var, "event-channel-key-separator") && !zstr(val)) {
2359  } else if (!strcasecmp(var, "event-channel-enable-hierarchy-deliver") && !zstr(val)) {
2360  int v = switch_true(val);
2361  if (v) {
2363  } else {
2365  }
2366  } else if (!strcasecmp(var, "event-channel-hierarchy-deliver-once") && !zstr(val)) {
2367  int v = switch_true(val);
2368  if (v) {
2370  } else {
2372  }
2373  } else if (!strcasecmp(var, "event-channel-log-undeliverable-json") && !zstr(val)) {
2374  int v = switch_true(val);
2375  if (v) {
2377  } else {
2379  }
2380  } else if (!strcasecmp(var, "max-audio-channels") && !zstr(val)) {
2381  switch_core_max_audio_channels(atoi(val));
2382  }
2383  }
2384  }
2385 
2386  if (runtime.event_channel_key_separator == NULL) {
2388  }
2389 
2390  if ((settings = switch_xml_child(cfg, "variables"))) {
2391  for (param = switch_xml_child(settings, "variable"); param; param = param->next) {
2392  const char *var = switch_xml_attr_soft(param, "name");
2393  const char *val = switch_xml_attr_soft(param, "value");
2394  if (var && val) {
2395  switch_core_set_variable(var, val);
2396  }
2397  }
2398  }
2399 
2400  switch_xml_free(xml);
2401  }
2402 
2403 
2404 }
2405 
2407 {
2408 
2409  return ("\n"
2410  ".=============================================================.\n"
2411  "| _____ ______ _____ _____ ____ _ _ |\n"
2412  "| | ___| __ ___ ___/ ___\\ \\ / /_ _|_ _/ ___| | | | |\n"
2413  "| | |_ | '__/ _ \\/ _ \\___ \\\\ \\ /\\ / / | | | || | | |_| | |\n"
2414  "| | _|| | | __/ __/___) |\\ V V / | | | || |___| _ | |\n"
2415  "| |_| |_| \\___|\\___|____/ \\_/\\_/ |___| |_| \\____|_| |_| |\n"
2416  "| |\n"
2417  ".=============================================================."
2418  "\n"
2419 
2420  "| Anthony Minessale II, Michael Jerris, Brian West, Others |\n"
2421  "| FreeSWITCH (http://www.freeswitch.org) |\n"
2422  "| Paypal Donations Appreciated: paypal@freeswitch.org |\n"
2423  "| Brought to you by ClueCon http://www.cluecon.com/ |\n"
2424  ".=============================================================.\n"
2425  "\n");
2426 }
2427 
2429 {
2431  *err = "NO SUITABLE DATABASE INTERFACE IS AVAILABLE TO SERVE 'core-db-dsn'!\n";
2432  return SWITCH_STATUS_GENERR;
2433  }
2434 
2436  *err = "Error activating database";
2437  return SWITCH_STATUS_GENERR;
2438  }
2439 
2440  return SWITCH_STATUS_SUCCESS;
2441 }
2442 
2444 {
2445  switch_event_t *event;
2446  char *cmd;
2447  int x = 0;
2448  const char *use = NULL;
2449 #include "cc.h"
2450 
2451 
2452  if (switch_core_init(flags, console, err) != SWITCH_STATUS_SUCCESS) {
2453  return SWITCH_STATUS_GENERR;
2454  }
2455 
2456  if (runtime.runlevel > 1) {
2457  /* one per customer */
2458  return SWITCH_STATUS_SUCCESS;
2459  }
2460 
2461  runtime.runlevel++;
2462  runtime.events_use_dispatch = 1;
2463 
2466 
2467  switch_msrp_init();
2468 
2469  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Bringing up environment.\n");
2472  *err = "Cannot load modules";
2474  return SWITCH_STATUS_GENERR;
2475  }
2476 
2478 
2479  switch_load_core_config("post_load_switch.conf");
2480 
2482 
2484  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Ready");
2485  switch_event_fire(&event);
2486  }
2487 
2488  switch_core_screen_size(&x, NULL);
2489 
2490  use = (x > 100) ? cc : cc_s;
2491 
2492 #ifdef WIN32
2494 #else
2500 
2501 #endif
2502 
2503 
2505  "\nFreeSWITCH Version %s (%s)\n\nFreeSWITCH Started\nMax Sessions [%u]\nSession Rate [%d]\nSQL [%s]\n",
2508  switch_core_sessions_per_second(0), switch_test_flag((&runtime), SCF_USE_SQL) ? "Enabled" : "Disabled");
2509 
2510 
2511  if (x < 160) {
2512  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "\n[This app Best viewed at 160x60 or more..]\n");
2513  }
2514 
2516 
2517  if ((cmd = switch_core_get_variable_dup("api_on_startup"))) {
2518  switch_stream_handle_t stream = { 0 };
2519  SWITCH_STANDARD_STREAM(stream);
2520  switch_console_execute(cmd, 0, &stream);
2521  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Startup command [%s] executed. Output:\n%s\n", cmd, (char *)stream.data);
2522  free(stream.data);
2523  free(cmd);
2524  }
2525 
2526 #ifdef HAVE_SYSTEMD
2527  sd_notifyf(0, "READY=1\n"
2528  "MAINPID=%lu\n", (unsigned long) getpid());
2529 #endif
2530 
2531  return SWITCH_STATUS_SUCCESS;
2532 
2533 }
2534 
2536 {
2537  switch_time_t temp = total_ms / 1000;
2538  memset(duration, 0, sizeof(*duration));
2539  duration->mms = (uint32_t) (total_ms % 1000);
2540  duration->ms = (uint32_t) (temp % 1000);
2541  temp = temp / 1000;
2542  duration->sec = (uint32_t) (temp % 60);
2543  temp = temp / 60;
2544  duration->min = (uint32_t) (temp % 60);
2545  temp = temp / 60;
2546  duration->hr = (uint32_t) (temp % 24);
2547  temp = temp / 24;
2548  duration->day = (uint32_t) (temp % 365);
2549  duration->yr = (uint32_t) (temp / 365);
2550 }
2551 
2553 {
2554  return switch_mono_micro_time_now() - runtime.initiated;
2555 }
2556 
2557 
2558 #ifdef _MSC_VER
2559 static void win_shutdown(void)
2560 {
2561 
2562  HANDLE shutdown_event;
2563  char path[512];
2564  /* for windows we need the event to signal for shutting down a background FreeSWITCH */
2565  snprintf(path, sizeof(path), "Global\\Freeswitch.%d", getpid());
2566 
2567  /* open the event so we can signal it */
2568  shutdown_event = OpenEvent(EVENT_MODIFY_STATE, FALSE, path);
2569 
2570  if (shutdown_event) {
2571  /* signal the event to shutdown */
2572  SetEvent(shutdown_event);
2573  /* cleanup */
2574  CloseHandle(shutdown_event);
2575  }
2576 }
2577 #endif
2578 
2580 {
2581  /* set signal handlers */
2582  signal(SIGINT, SIG_IGN);
2583 #ifdef SIGPIPE
2584  signal(SIGPIPE, SIG_IGN);
2585 #endif
2586 #ifdef SIGALRM
2587  signal(SIGALRM, SIG_IGN);
2588 #endif
2589 #ifdef SIGQUIT
2590  signal(SIGQUIT, SIG_IGN);
2591 #endif
2592 #ifdef SIGPOLL
2593  signal(SIGPOLL, SIG_IGN);
2594 #endif
2595 #ifdef SIGIO
2596  signal(SIGIO, SIG_IGN);
2597 #endif
2598 #ifdef TRAP_BUS
2599  signal(SIGBUS, handle_SIGBUS);
2600 #endif
2601 #ifdef SIGUSR1
2602  signal(SIGUSR1, handle_SIGHUP);
2603 #endif
2604  signal(SIGHUP, handle_SIGHUP);
2605 }
2606 
2608 {
2609  return runtime.debug_level;
2610 }
2611 
2613 {
2614  return runtime.sps;
2615 }
2616 
2618 {
2619  return runtime.sps_last;
2620 }
2621 
2623 {
2624  return runtime.sps_peak;
2625 }
2626 
2628 {
2629  return runtime.sps_peak_fivemin;
2630 }
2631 
2633 {
2634  return runtime.sessions_peak;
2635 }
2636 
2638 {
2639  return runtime.sessions_peak_fivemin;
2640 }
2641 
2643 {
2644  if (limit) {
2645  runtime.max_audio_channels = limit;
2646  }
2647 
2648  return runtime.max_audio_channels;
2649 }
2650 
2652 {
2653  int *intval = (int *) val;
2654  int oldintval = 0, newintval = 0;
2655 
2656  if (intval) {
2657  oldintval = *intval;
2658  }
2659 
2660  if (switch_test_flag((&runtime), SCF_SHUTTING_DOWN)) {
2661  return -1;
2662  }
2663 
2664  switch (cmd) {
2665  case SCSC_RECOVER:
2666  {
2667  char *arg = (char *) val;
2668  char *tech = NULL, *prof = NULL;
2669  int r, flush = 0;
2670 
2671  if (!zstr(arg)) {
2672  tech = strdup(arg);
2673  switch_assert(tech);
2674 
2675  if ((prof = strchr(tech, ':'))) {
2676  *prof++ = '\0';
2677  }
2678 
2679  if (!strcasecmp(tech, "flush")) {
2680  flush++;
2681 
2682  if (prof) {
2683  char *tech = prof;
2684  if ((prof = strchr(tech, ':'))) {
2685  *prof++ = '\0';
2686  }
2687  }
2688  }
2689 
2690  }
2691 
2692  if (flush) {
2693  switch_core_recovery_flush(tech, prof);
2694  r = -1;
2695  } else {
2696  r = switch_core_recovery_recover(tech, prof);
2697  }
2698 
2699  switch_safe_free(tech);
2700  return r;
2701 
2702  }
2703  break;
2704  case SCSC_DEBUG_SQL:
2705  {
2706  if (switch_test_flag((&runtime), SCF_DEBUG_SQL)) {
2707  switch_clear_flag((&runtime), SCF_DEBUG_SQL);
2708  newintval = 0;
2709  } else {
2710  switch_set_flag((&runtime), SCF_DEBUG_SQL);
2711  newintval = 1;
2712  }
2713  }
2714  break;
2715  case SCSC_VERBOSE_EVENTS:
2716  if (intval) {
2717  if (oldintval > -1) {
2718  if (oldintval) {
2719  switch_set_flag((&runtime), SCF_VERBOSE_EVENTS);
2720  } else {
2722  }
2723  }
2724  newintval = switch_test_flag((&runtime), SCF_VERBOSE_EVENTS);
2725  }
2726  break;
2727  case SCSC_API_EXPANSION:
2728  if (intval) {
2729  if (oldintval > -1) {
2730  if (oldintval) {
2731  switch_set_flag((&runtime), SCF_API_EXPANSION);
2732  } else {
2733  switch_clear_flag((&runtime), SCF_API_EXPANSION);
2734  }
2735  }
2736  newintval = switch_test_flag((&runtime), SCF_API_EXPANSION);
2737  }
2738  break;
2740  if (intval) {
2741  if (oldintval > -1) {
2742  if (oldintval) {
2744  } else {
2746  }
2747  }
2748  newintval = switch_test_flag((&runtime), SCF_THREADED_SYSTEM_EXEC);
2749  }
2750  break;
2751  case SCSC_CALIBRATE_CLOCK:
2753  break;
2754  case SCSC_FLUSH_DB_HANDLES:
2756  break;
2757  case SCSC_SEND_SIGHUP:
2758  handle_SIGHUP(1);
2759  break;
2760  case SCSC_SYNC_CLOCK:
2761  switch_time_sync();
2762  newintval = 0;
2763  break;
2765  newintval = switch_core_session_sync_clock();
2766  break;
2767  case SCSC_SQL:
2768  if (oldintval) {
2770  } else {
2772  }
2773  break;
2774  case SCSC_PAUSE_ALL:
2775  if (oldintval) {
2776  switch_set_flag((&runtime), SCF_NO_NEW_SESSIONS);
2777  } else {
2779  }
2780  break;
2781  case SCSC_PAUSE_INBOUND:
2782  if (oldintval) {
2784  } else {
2786  }
2787  break;
2788  case SCSC_PAUSE_OUTBOUND:
2789  if (oldintval) {
2791  } else {
2793  }
2794  break;
2795  case SCSC_HUPALL:
2797  break;
2798  case SCSC_CANCEL_SHUTDOWN:
2800  break;
2801  case SCSC_SAVE_HISTORY:
2803  break;
2804  case SCSC_CRASH:
2805  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Declinatio Mortuus Obfirmo!\n");
2807  abort();
2808  break;
2809  case SCSC_SHUTDOWN_NOW:
2811  exit(0);
2812  break;
2813  case SCSC_REINCARNATE_NOW:
2815  exit(SWITCH_STATUS_RESTART);
2816  break;
2817  case SCSC_SHUTDOWN_ELEGANT:
2818  case SCSC_SHUTDOWN_ASAP:
2819  {
2820  int x = 19;
2821  uint32_t count;
2822  switch_event_t *shutdown_requested_event = NULL;
2823  if (switch_event_create(&shutdown_requested_event, SWITCH_EVENT_SHUTDOWN_REQUESTED) == SWITCH_STATUS_SUCCESS) {
2824  switch_event_add_header(shutdown_requested_event, SWITCH_STACK_BOTTOM, "Event-Info", "%s", cmd == SCSC_SHUTDOWN_ASAP ? "ASAP" : "elegant");
2825  switch_event_fire(&shutdown_requested_event);
2826  }
2828  if (cmd == SCSC_SHUTDOWN_ASAP) {
2829  switch_set_flag((&runtime), SCF_NO_NEW_SESSIONS);
2830  }
2831 
2832  while (runtime.running && switch_test_flag((&runtime), SCF_SHUTDOWN_REQUESTED) && (count = switch_core_session_count())) {
2833  switch_yield(500000);
2834  if (++x == 20) {
2836  "Shutdown in progress, %u session(s) remain.\nShutting down %s\n",
2837  count, cmd == SCSC_SHUTDOWN_ASAP ? "ASAP" : "once there are no active calls.");
2838  x = 0;
2839  }
2840  }
2841 
2842  if (switch_test_flag((&runtime), SCF_SHUTDOWN_REQUESTED)) {
2843  switch_set_flag((&runtime), SCF_NO_NEW_SESSIONS);
2844 #ifdef _MSC_VER
2845  win_shutdown();
2846 #endif
2847 
2848  if (oldintval) {
2849  switch_set_flag((&runtime), SCF_RESTART);
2851  } else {
2853 #ifdef HAVE_SYSTEMD
2854  sd_notifyf(0, "STOPPING=1\n");
2855 #endif
2856 #ifdef _MSC_VER
2857  fclose(stdin);
2858 #endif
2859  }
2860  runtime.running = 0;
2861  } else {
2862  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Shutdown Cancelled\n");
2864  }
2865  }
2866  break;
2867  case SCSC_PAUSE_CHECK:
2868  newintval = !!(switch_test_flag((&runtime), SCF_NO_NEW_SESSIONS) == SCF_NO_NEW_SESSIONS);
2869  break;
2871  newintval = !!switch_test_flag((&runtime), SCF_NO_NEW_INBOUND_SESSIONS);
2872  break;
2874  newintval = !!switch_test_flag((&runtime), SCF_NO_NEW_OUTBOUND_SESSIONS);
2875  break;
2876  case SCSC_READY_CHECK:
2877  newintval = switch_core_ready();
2878  break;
2879  case SCSC_SHUTDOWN_CHECK:
2880  newintval = !!switch_test_flag((&runtime), SCF_SHUTDOWN_REQUESTED);
2881  break;
2882  case SCSC_SHUTDOWN:
2883 
2884 #ifdef _MSC_VER
2885  win_shutdown();
2886 #endif
2887 
2888  if (oldintval) {
2889  switch_set_flag((&runtime), SCF_RESTART);
2891  } else {
2893 #ifdef _MSC_VER
2894  fclose(stdin);
2895 #endif
2896  }
2897  runtime.running = 0;
2898  break;
2899  case SCSC_CHECK_RUNNING:
2900  newintval = runtime.running;
2901  break;
2902  case SCSC_LOGLEVEL:
2903  if (oldintval >= SWITCH_LOG_DISABLE) {
2904  runtime.hard_log_level = oldintval;
2905  }
2906 
2907  if (runtime.hard_log_level > SWITCH_LOG_DEBUG) {
2908  runtime.hard_log_level = SWITCH_LOG_DEBUG;
2909  }
2910  newintval = runtime.hard_log_level;
2911  break;
2912  case SCSC_DEBUG_LEVEL:
2913  if (oldintval > -1) {
2914  if (oldintval > 10)
2915  oldintval = 10;
2916  runtime.debug_level = oldintval;
2917  }
2918  newintval = runtime.debug_level;
2919  break;
2920  case SCSC_MIN_IDLE_CPU:
2921  {
2922  double *dval = (double *) val;
2923  if (dval) {
2924  *dval = switch_core_min_idle_cpu(*dval);
2925  }
2926  intval = NULL;
2927  }
2928  break;
2929  case SCSC_MAX_SESSIONS:
2930  newintval = switch_core_session_limit(oldintval);
2931  break;
2932  case SCSC_LAST_SPS:
2933  newintval = runtime.sps_last;
2934  break;
2935  case SCSC_SPS_PEAK:
2936  if (oldintval == -1) {
2937  runtime.sps_peak = 0;
2938  }
2939  newintval = runtime.sps_peak;
2940  break;
2941  case SCSC_SPS_PEAK_FIVEMIN:
2942  newintval = runtime.sps_peak_fivemin;
2943  break;
2944  case SCSC_SESSIONS_PEAK:
2945  newintval = runtime.sessions_peak;
2946  break;
2948  newintval = runtime.sessions_peak_fivemin;
2949  break;
2951  newintval = switch_core_max_dtmf_duration(oldintval);
2952  break;
2954  newintval = switch_core_min_dtmf_duration(oldintval);
2955  break;
2957  newintval = switch_core_default_dtmf_duration(oldintval);
2958  break;
2959  case SCSC_SPS:
2961  if (oldintval > 0) {
2962  runtime.sps_total = oldintval;
2963  }
2964  newintval = runtime.sps_total;
2966  break;
2967 
2968  case SCSC_RECLAIM:
2970  newintval = 0;
2971  break;
2972  case SCSC_MDNS_RESOLVE:
2973  switch_core_media_set_resolveice(!!oldintval);
2974  break;
2975  case SCSC_SHUTDOWN_CAUSE:
2976  runtime.shutdown_cause = oldintval;
2977  break;
2978  }
2979 
2980  if (intval) {
2981  *intval = newintval;
2982  }
2983 
2984 
2985  return 0;
2986 }
2987 
2989 {
2990  return runtime.flags;
2991 }
2992 
2994 {
2995  return runtime.running ? SWITCH_TRUE : SWITCH_FALSE;
2996 }
2997 
2999 {
3001 }
3002 
3004 {
3006 }
3007 
3009 {
3011 }
3012 
3014 {
3015  if (switch_test_flag((&runtime), SCF_USE_SQL)) {
3017  }
3018 }
3019 
3021 {
3022  switch_event_t *event;
3023 
3025  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Shutting Down");
3026  switch_event_fire(&event);
3027  }
3028 
3029  switch_set_flag((&runtime), SCF_NO_NEW_SESSIONS);
3030  switch_set_flag((&runtime), SCF_SHUTTING_DOWN);
3031 
3032  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "End existing sessions\n");
3034  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Clean up modules.\n");
3035 
3037 
3039 
3041  EVP_cleanup();
3042 
3044 
3047 
3048  if (switch_test_flag((&runtime), SCF_USE_AUTO_NAT)) {
3050  }
3054 
3055  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Closing Event Engine.\n");
3057 
3058  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Finalizing Shutdown.\n");
3060 
3064 
3065  if (runtime.console && runtime.console != stdout && runtime.console != stderr) {
3066  fclose(runtime.console);
3067  runtime.console = NULL;
3068  }
3069 
3090 
3092 
3094  switch_core_hash_destroy(&runtime.ptimes);
3097 
3098  if (IP_LIST.hash) {
3099  switch_core_hash_destroy(&IP_LIST.hash);
3100  }
3101 
3102  if (IP_LIST.pool) {
3104  }
3105 
3107 
3108  if (runtime.memory_pool) {
3109  fspr_pool_destroy(runtime.memory_pool);
3110  fspr_terminate();
3111  }
3112 
3113  sqlite3_shutdown();
3114 
3116 }
3117 
3119 {
3120  const switch_management_interface_t *ptr;
3122 
3123  if ((ptr = switch_loadable_module_get_management_interface(relative_oid))) {
3124  status = ptr->management_function(relative_oid, action, data, datalen);
3125  }
3126 
3127  return status;
3128 }
3129 
3131 {
3135 }
3136 
3137 
3139  const char *cmd;
3143  int ret;
3144  int *fds;
3145 };
3146 
3148 {
3149  struct system_thread_handle *sth = (struct system_thread_handle *) obj;
3150 
3151 #if defined(HAVE_SETRLIMIT) && !defined(__FreeBSD__)
3152  struct rlimit rlim;
3153  struct rlimit rlim_save;
3154 
3155  memset(&rlim, 0, sizeof(rlim));
3156  getrlimit(RLIMIT_STACK, &rlim);
3157 
3158  memset(&rlim_save, 0, sizeof(rlim_save));
3159  getrlimit(RLIMIT_STACK, &rlim_save);
3160 
3161  rlim.rlim_cur = rlim.rlim_max;
3162  if (setrlimit(RLIMIT_STACK, &rlim) < 0) {
3163  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Setting stack size failed! (%s)\n", strerror(errno));
3164  }
3165 #endif
3166 
3167  if (sth->fds) {
3168  dup2(sth->fds[1], STDOUT_FILENO);
3169  }
3170 
3171  sth->ret = system(sth->cmd);
3172 
3173 #if defined(HAVE_SETRLIMIT) && !defined(__FreeBSD__)
3174  if (setrlimit(RLIMIT_STACK, &rlim_save) < 0) {
3175  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Setting stack size failed! (%s)\n", strerror(errno));
3176  }
3177 #endif
3178 
3179  switch_mutex_lock(sth->mutex);
3181  switch_mutex_unlock(sth->mutex);
3182 
3184 
3185  return NULL;
3186 }
3187 
3188 
3189 static int switch_system_thread(const char *cmd, switch_bool_t wait)
3190 {
3192  switch_threadattr_t *thd_attr;
3193  int ret = 0;
3194  struct system_thread_handle *sth;
3196 
3199  return 1;
3200  }
3201 
3202  if (!(sth = switch_core_alloc(pool, sizeof(struct system_thread_handle)))) {
3204  return 1;
3205  }
3206 
3207  sth->pool = pool;
3208  sth->cmd = switch_core_strdup(pool, cmd);
3209 
3210  switch_thread_cond_create(&sth->cond, sth->pool);
3212  switch_mutex_lock(sth->mutex);
3213 
3214  switch_threadattr_create(&thd_attr, sth->pool);
3216  switch_threadattr_detach_set(thd_attr, 1);
3217  switch_thread_create(&thread, thd_attr, system_thread, sth, sth->pool);
3218 
3219  if (wait) {
3220  switch_thread_cond_wait(sth->cond, sth->mutex);
3221  ret = sth->ret;
3222  }
3223  switch_mutex_unlock(sth->mutex);
3224 
3225  return ret;
3226 }
3227 
3229 {
3230  int max = 0;
3231 
3232 #ifndef WIN32
3233 #if defined(HAVE_GETDTABLESIZE)
3234  max = getdtablesize();
3235 #else
3236  max = sysconf(_SC_OPEN_MAX);
3237 #endif
3238 #endif
3239 
3240  return max;
3241 
3242 }
3243 
3244 SWITCH_DECLARE(void) switch_close_extra_files(int *keep, int keep_ttl)
3245 {
3246  int open_max = switch_max_file_desc();
3247  int i, j;
3248 
3249  for (i = 3; i < open_max; i++) {
3250  if (keep) {
3251  for (j = 0; j < keep_ttl; j++) {
3252  if (i == keep[j]) {
3253  goto skip;
3254  }
3255  }
3256  }
3257 
3258  close(i);
3259 
3260  skip:
3261 
3262  continue;
3263 
3264  }
3265 }
3266 
3267 
3268 #ifdef WIN32
3269 static int switch_system_fork(const char *cmd, switch_bool_t wait)
3270 {
3271  return switch_system_thread(cmd, wait);
3272 }
3273 
3274 SWITCH_DECLARE(pid_t) switch_fork(void)
3275 {
3276  return -1;
3277 }
3278 
3279 
3280 #else
3281 
3283 {
3284  int i = fork();
3285 
3286  if (!i) {
3287  set_low_priority();
3288  }
3289 
3290  return i;
3291 }
3292 
3293 
3294 
3295 static int switch_system_fork(const char *cmd, switch_bool_t wait)
3296 {
3297  int pid;
3298  char *dcmd = strdup(cmd);
3299 #if defined(HAVE_SETRLIMIT) && !defined(__FreeBSD__)
3300  struct rlimit rlim;
3301  struct rlimit rlim_save;
3302 #endif
3303 
3305 
3306  pid = switch_fork();
3307 
3308  if (pid) {
3309  if (wait) {
3310  waitpid(pid, NULL, 0);
3311  }
3312  free(dcmd);
3313  } else {
3314  switch_close_extra_files(NULL, 0);
3315 
3316 #if defined(HAVE_SETRLIMIT) && !defined(__FreeBSD__)
3317  memset(&rlim, 0, sizeof(rlim));
3318  getrlimit(RLIMIT_STACK, &rlim);
3319 
3320  memset(&rlim_save, 0, sizeof(rlim_save));
3321  getrlimit(RLIMIT_STACK, &rlim_save);
3322 
3323  rlim.rlim_cur = rlim.rlim_max;
3324  if (setrlimit(RLIMIT_STACK, &rlim) < 0) {
3325  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Setting stack size failed! (%s)\n", strerror(errno));
3326  }
3327 #endif
3328 
3329  if (system(dcmd) == -1) {
3330  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to execute because of a command error : %s\n", dcmd);
3331  }
3332  free(dcmd);
3333  exit(0);
3334  }
3335 
3336  return 0;
3337 }
3338 #endif
3339 
3340 
3341 
3342 SWITCH_DECLARE(int) switch_system(const char *cmd, switch_bool_t wait)
3343 {
3344  int retval = 0;
3345 #ifdef __linux__
3346  switch_bool_t spawn_instead_of_system = switch_true(switch_core_get_variable("spawn_instead_of_system"));
3347 #else
3348  switch_bool_t spawn_instead_of_system = SWITCH_FALSE;
3349 #endif
3350 
3351  if (spawn_instead_of_system) {
3352  retval = switch_stream_spawn(cmd, SWITCH_TRUE, wait, NULL);
3353  } else if (switch_test_flag((&runtime), SCF_THREADED_SYSTEM_EXEC)) {
3354  retval = switch_system_thread(cmd, wait);
3355  } else {
3356  retval = switch_system_fork(cmd, wait);
3357  }
3358  return retval;
3359 }
3360 
3361 
3362 
3364 {
3365  return switch_stream_system(cmd, stream);
3366 }
3367 
3368 #ifdef __linux__
3369 extern char **environ;
3370 #endif
3371 
3373 {
3374 #ifndef __linux__
3375  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "posix_spawn is unsupported on current platform\n");
3376  return 1;
3377 #else
3378  int status = 0;
3379  char buffer[1024];
3380  pid_t pid;
3381  char *pdata = NULL, *argv[64];
3382  posix_spawn_file_actions_t action;
3383  posix_spawnattr_t *attr;
3384  int cout_pipe[2];
3385  int cerr_pipe[2];
3386  struct pollfd pfds[2] = { {0} };
3387 
3388  if (zstr(cmd)) {
3389  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to execute switch_spawn_stream because of empty command\n");
3390  return 1;
3391  }
3392 
3393  if (shell) {
3394  argv[0] = switch_core_get_variable("spawn_system_shell");
3395  argv[1] = "-c";
3396  argv[2] = (char *)cmd;
3397  argv[3] = NULL;
3398  if (zstr(argv[0])) {
3399  argv[0] = "/bin/sh";
3400  }
3401  } else {
3402  if (!(pdata = strdup(cmd))) {
3403  return 1;
3404  }
3405  if (!switch_separate_string(pdata, ' ', argv, (sizeof(argv) / sizeof(argv[0])))) {
3406  free(pdata);
3407  return 1;
3408  }
3409  }
3410 
3411  if (!(attr = malloc(sizeof(posix_spawnattr_t)))) {
3412  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to execute switch_spawn_stream because of a memory error: %s\n", cmd);
3413  switch_safe_free(pdata);
3414  return 1;
3415  }
3416 
3417  if (stream) {
3418  if (pipe(cout_pipe)) {
3419  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to execute switch_spawn_stream because of a pipe error: %s\n", cmd);
3420  free(attr);
3421  switch_safe_free(pdata);
3422  return 1;
3423  }
3424 
3425  if (pipe(cerr_pipe)) {
3426  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to execute switch_spawn_stream because of a pipe error: %s\n", cmd);
3427  close(cout_pipe[0]);
3428  close(cout_pipe[1]);
3429  free(attr);
3430  switch_safe_free(pdata);
3431  return 1;
3432  }
3433  }
3434 
3435  memset(attr, 0, sizeof(posix_spawnattr_t));
3436  posix_spawnattr_init(attr);
3437  posix_spawnattr_setflags(attr, POSIX_SPAWN_USEVFORK);
3438 
3439  posix_spawn_file_actions_init(&action);
3440 
3441  if (stream) {
3442  posix_spawn_file_actions_addclose(&action, cout_pipe[0]);
3443  posix_spawn_file_actions_addclose(&action, cerr_pipe[0]);
3444  posix_spawn_file_actions_adddup2(&action, cout_pipe[1], 1);
3445  posix_spawn_file_actions_adddup2(&action, cerr_pipe[1], 2);
3446 
3447  posix_spawn_file_actions_addclose(&action, cout_pipe[1]);
3448  posix_spawn_file_actions_addclose(&action, cerr_pipe[1]);
3449  }
3450 
3451  if (posix_spawnp(&pid, argv[0], &action, attr, argv, environ) != 0) {
3452  status = 1;
3453  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to execute posix_spawnp: %s\n", cmd);
3454  if (stream) {
3455  close(cout_pipe[0]), close(cerr_pipe[0]);
3456  close(cout_pipe[1]), close(cerr_pipe[1]);
3457  }
3458  } else {
3459  if (stream) {
3460  close(cout_pipe[1]), close(cerr_pipe[1]); /* close child-side of pipes */
3461 
3462  pfds[0] = (struct pollfd) {
3463  .fd = cout_pipe[0],
3464  .events = POLLIN,
3465  .revents = 0
3466  };
3467 
3468  pfds[1] = (struct pollfd) {
3469  .fd = cerr_pipe[0],
3470  .events = POLLIN,
3471  .revents = 0
3472  };
3473 
3474  while (poll(pfds, 2, /*timeout*/-1) > 0) {
3475  if (pfds[0].revents & POLLIN) {
3476  int bytes_read = read(cout_pipe[0], buffer, sizeof(buffer));
3477  stream->raw_write_function(stream, (unsigned char *)buffer, bytes_read);
3478  } else if (pfds[1].revents & POLLIN) {
3479  int bytes_read = read(cerr_pipe[0], buffer, sizeof(buffer));
3480  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "STDERR of cmd (%s): %.*s\n", cmd, bytes_read, buffer);
3481  } else {
3482  break; /* nothing left to read */
3483  }
3484  }
3485 
3486  close(cout_pipe[0]), close(cerr_pipe[0]);
3487  }
3488 
3489  if (wait) {
3490  if (waitpid(pid, &status, 0) != pid) {
3491  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "waitpid failed: %s\n", cmd);
3492  } else if (status != 0) {
3493  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Exit status (%d): %s\n", status, cmd);
3494  }
3495  }
3496  }
3497 
3498  posix_spawnattr_destroy(attr);
3499  free(attr);
3500  posix_spawn_file_actions_destroy(&action);
3501  switch_safe_free(pdata);
3502 
3503  return status;
3504 #endif
3505 }
3506 
3507 SWITCH_DECLARE(int) switch_spawn(const char *cmd, switch_bool_t wait)
3508 {
3509  return switch_stream_spawn(cmd, SWITCH_FALSE, wait, NULL);
3510 }
3511 
3513 {
3514 #ifdef HAVE_SETRLIMIT
3515  struct rlimit rlp;
3516 
3517  memset(&rlp, 0, sizeof(rlp));
3518  getrlimit(RLIMIT_STACK, &rlp);
3519 
3520  *cur = rlp.rlim_cur;
3521  *max = rlp.rlim_max;
3522 
3523  return SWITCH_STATUS_SUCCESS;
3524 
3525 #else
3526 
3527  return SWITCH_STATUS_FALSE;
3528 
3529 #endif
3530 
3531 
3532 
3533 }
3534 
3535 
3537 {
3538 #ifdef __linux__
3539  switch_bool_t spawn_instead_of_system = switch_true(switch_core_get_variable("spawn_instead_of_system"));
3540 #else
3541  switch_bool_t spawn_instead_of_system = SWITCH_FALSE;
3542 #endif
3543 
3544  if (spawn_instead_of_system){
3545  return switch_stream_spawn(cmd, SWITCH_TRUE, SWITCH_TRUE, stream);
3546  } else {
3547  char buffer[128];
3548  size_t bytes;
3549  FILE* pipe = popen(cmd, "r");
3550  if (!pipe) return 1;
3551 
3552  while (!feof(pipe)) {
3553  while ((bytes = fread(buffer, 1, 128, pipe)) > 0) {
3554  if (stream != NULL) {
3555  stream->raw_write_function(stream, (unsigned char *)buffer, bytes);
3556  }
3557  }
3558  }
3559 
3560  if (ferror(pipe)) {
3561  pclose(pipe);
3562  return 1;
3563  }
3564 
3565  pclose(pipe);
3566  return 0;
3567  }
3568 }
3569 
3571 {
3572  uint16_t start_port = 0;
3573 
3574  /* By default pass rtp port range start value as zero in order to get actual
3575  * RTP port range start value as configured */
3576  start_port = (uint16_t)switch_rtp_set_start_port((switch_port_t)start_port);
3577 
3578  return start_port;
3579 }
3580 
3582 {
3583  uint16_t end_port = 0;
3584 
3585  /* By default pass rtp port range end value as zero in order to get actual
3586  * RTP port range end value as configured */
3587  end_port = (uint16_t)switch_rtp_set_end_port((switch_port_t)end_port);
3588 
3589  return end_port;
3590 }
3591 
3593 {
3594  return runtime.event_channel_key_separator;
3595 }
3596 
3597 /* For Emacs:
3598  * Local Variables:
3599  * mode:c
3600  * indent-tabs-mode:t
3601  * tab-width:4
3602  * c-basic-offset:4
3603  * End:
3604  * For VIM:
3605  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
3606  */
void switch_channel_global_uninit(void)
void switch_core_set_signal_handlers(void)
Definition: switch_core.c:2579
switch_memory_pool_t * switch_core_memory_init(void)
#define switch_core_permanent_strdup(_todup)
Copy a string using permanent memory allocation.
Definition: switch_core.h:707
int32_t change_user_group(const char *user, const char *group)
Change user and/or group of the running process.
Definition: switch_core.c:1083
const char * switch_core_get_switchname(void)
Definition: switch_core.c:361
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:413
void switch_xml_free(_In_opt_ switch_xml_t xml)
frees the memory allocated for an switch_xml structure
void switch_time_sync(void)
Definition: switch_time.c:609
#define switch_core_new_memory_pool(p)
Create a new sub memory pool from the core&#39;s master pool.
Definition: switch_core.h:633
switch_status_t switch_msrp_destroy(void)
Definition: switch_msrp.c:349
uint32_t min_dtmf_duration
int32_t switch_core_sps(void)
Definition: switch_core.c:2612
char * core_db_inner_post_trans_execute
switch_status_t switch_thread_rwlock_unlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:286
switch_status_t switch_core_mime_add_type(const char *type, const char *ext)
Definition: switch_core.c:1232
const char * switch_xml_attr_soft(_In_ switch_xml_t xml, _In_z_ const char *attr)
returns the value of the requested tag attribute, or "" if not found
static void check_ip(void)
Definition: switch_core.c:132
switch_status_t switch_thread_cond_create(switch_thread_cond_t **cond, switch_memory_pool_t *pool)
Definition: switch_apr.c:373
switch_frame_t dummy_cng_frame
switch_memory_pool_t * pool
Definition: switch_core.c:1368
static switch_bool_t switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:519
switch_status_t switch_xml_locate_domain(_In_z_ const char *domain_name, _In_opt_ switch_event_t *params, _Out_ switch_xml_t *root, _Out_ switch_xml_t *domain)
void switch_core_memory_reclaim_logger(void)
Definition: switch_log.c:783
switch_status_t switch_core_sqldb_start(switch_memory_pool_t *pool, switch_bool_t manage)
#define switch_set_flag(obj, flag)
Set a flag on an arbitrary object.
Definition: switch_utils.h:700
#define switch_test_subnet(_ip, _net, _mask)
#define SWITCH_THREAD_FUNC
void switch_core_dump_variables(switch_stream_handle_t *stream)
Definition: switch_core.c:345
switch_status_t switch_event_add_header_string_nodup(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
switch_text_channel_t
A target to write log/debug info to.
void switch_nat_shutdown(void)
Shuts down the NAT Traversal System.
Definition: switch_nat.c:741
int32_t switch_core_sps_peak_fivemin(void)
Definition: switch_core.c:2627
#define SWITCH_CHANNEL_LOG
switch_time_t initiated
switch_time_t switch_core_uptime(void)
Number of microseconds the system has been up.
Definition: switch_core.c:2552
uint16_t switch_core_get_rtp_port_range_end_port(void)
Get RTP port range end value.
Definition: switch_core.c:3581
switch_status_t switch_core_hash_destroy(_Inout_ switch_hash_t **hash)
Destroy an existing hash table.
const char *const const double number
Definition: switch_cJSON.h:254
void * switch_core_hash_find(_In_ switch_hash_t *hash, _In_z_ const char *key)
Retrieve data from a given hash.
int32_t switch_core_sessions_peak(void)
Definition: switch_core.c:2632
const char * switch_xml_attr(_In_opt_ switch_xml_t xml, _In_opt_z_ const char *attr)
returns the value of the requested tag attribute, or NULL if not found
#define switch_event_del_header(_e, _h)
Definition: switch_event.h:212
const switch_state_handler_table_t * state_handlers[SWITCH_MAX_STATE_HANDLERS]
uint32_t switch_core_sessions_per_second(_In_ uint32_t new_limit)
Set/Get Session Rate Limit.
switch_stream_handle_raw_write_function_t raw_write_function
int switch_core_test_flag(int flag)
Definition: switch_core.c:1792
#define switch_core_hash_init(_hash)
Definition: switch_core.h:1431
int switch_core_add_state_handler(const switch_state_handler_table_t *state_handler)
Definition: switch_core.c:317
const char * switch_version_full(void)
void switch_ssl_init_ssl_locks(void)
void switch_time_set_nanosleep(switch_bool_t enable)
Definition: switch_time.c:366
int32_t timer_affinity
switch_status_t switch_network_list_create(switch_network_list_t **list, const char *name, switch_bool_t default_type, switch_memory_pool_t *pool)
Definition: switch_utils.c:459
switch_status_t switch_network_list_add_cidr_token(switch_network_list_t *list, const char *cidr_str, switch_bool_t ok, const char *token)
Definition: switch_utils.c:702
char * core_db_post_trans_execute
int32_t set_low_priority(void)
Definition: switch_core.c:945
switch_mutex_t * mutex
Definition: switch_core.c:3141
void switch_core_memory_stop(void)
switch_bool_t switch_core_running(void)
Definition: switch_core.c:2993
void switch_scheduler_task_thread_start(void)
Start the scheduler system.
switch_bool_t
Definition: switch_types.h:437
int switch_parse_cidr(const char *string, ip_t *ip, ip_t *mask, uint32_t *bitp)
Definition: switch_utils.c:743
int switch_max_file_desc(void)
Definition: switch_core.c:3228
#define switch_core_strdup(_pool, _todup)
Copy a string using memory allocation from a given pool.
Definition: switch_core.h:733
void switch_close_extra_files(int *keep, int keep_ttl)
Definition: switch_core.c:3244
uint32_t max_audio_channels
int switch_core_session_sync_clock(void)
switch_status_t switch_threadattr_stacksize_set(switch_threadattr_t *attr, switch_size_t stacksize)
Definition: switch_apr.c:683
#define switch_core_destroy_memory_pool(p)
Returns a subpool back to the main pool.
Definition: switch_core.h:642
switch_memory_pool_t * pool
switch_status_t switch_core_session_read_video_frame(_In_ switch_core_session_t *session, switch_frame_t **frame, switch_io_flag_t flags, int stream_id)
Read a video frame from a session.
switch_status_t switch_event_add_header(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *fmt,...) PRINTF_FUNCTION(4
Add a header to an event.
Representation of an event.
Definition: switch_event.h:80
const char * cc
Definition: cc.h:2
void switch_core_set_globals(void)
Initiate Globals.
Definition: switch_core.c:633
void switch_core_session_init(switch_memory_pool_t *pool)
switch_status_t switch_network_list_add_host_port_mask(switch_network_list_t *list, const char *host, const char *mask_str, switch_bool_t ok, switch_network_port_range_p port)
Definition: switch_utils.c:707
An event Header.
Definition: switch_event.h:65
const char *const const char *const const cJSON *const value
#define switch_network_list_add_cidr(_list, _cidr_str, _ok)
switch_thread_rwlock_t * global_var_rwlock
switch_status_t switch_core_session_read_lock(_In_ switch_core_session_t *session)
Acquire a read lock on the session.
switch_bool_t switch_network_list_validate_ip6_port_token(switch_network_list_t *list, ip_t ip, int port, const char **token)
Definition: switch_utils.c:497
void switch_event_launch_dispatch_threads(uint32_t max)
Definition: switch_event.c:653
switch_management_interface_t * switch_loadable_module_get_management_interface(const char *relative_oid)
Retrieve the management interface by it&#39;s registered name.
switch_status_t switch_core_destroy(void)
Destroy the core.
Definition: switch_core.c:3020
const char * cc_s
Definition: cc.h:3
switch_event_t * global_vars
void switch_scheduler_task_thread_stop(void)
Stop the scheduler system.
char hostname[256]
#define SWITCH_DECLARE_DATA
uint32_t switch_default_ptime(const char *name, uint32_t number)
Definition: switch_core.c:2022
A representation of an XML tree.
Definition: switch_xml.h:79
switch_mutex_t * throttle_mutex
#define SWITCH_DEFAULT_DTMF_DURATION
Definition: switch_types.h:116
switch_status_t switch_thread_cond_wait(switch_thread_cond_t *cond, switch_mutex_t *mutex)
Definition: switch_apr.c:378
uint32_t switch_core_cpu_count(void)
Definition: switch_core.c:1055
const char * switch_core_mime_type2ext(const char *mime)
Definition: switch_core.c:1219
switch_call_cause_t shutdown_cause
#define BUFSIZE
switch_status_t switch_console_shutdown(void)
switch_status_t switch_core_init_and_modload(switch_core_flag_t flags, switch_bool_t console, const char **err)
Definition: switch_core.c:2443
static void switch_load_core_config(const char *file)
Definition: switch_core.c:2049
switch_status_t switch_event_dup(switch_event_t **event, switch_event_t *todup)
Duplicate an event.
void switch_time_set_timerfd(int enable)
Definition: switch_time.c:348
pack cur
#define SWITCH_MAX_STATE_HANDLERS
Definition: switch_types.h:592
static switch_thread_t * thread
Definition: switch_log.c:486
switch_status_t switch_core_thread_set_cpu_affinity(int cpu)
Definition: switch_core.c:1763
switch_thread_cond_t * cond
Definition: switch_core.c:3140
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
switch_status_t switch_core_get_stacksizes(switch_size_t *cur, switch_size_t *max)
Definition: switch_core.c:3512
switch_port_t switch_rtp_set_start_port(switch_port_t port)
Set/Get RTP start port.
Definition: switch_rtp.c:2582
uint32_t switch_channel_test_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Test for presence of given flag on a given channel.
uint32_t switch_core_flag_t
Definition: switch_types.h:395
uint32_t microseconds_per_tick
struct switch_runtime runtime
Definition: switch_core.c:86
uint32_t switch_scheduler_add_task(time_t task_runtime, switch_scheduler_func_t func, const char *desc, const char *group, uint32_t cmd_id, void *cmd_arg, switch_scheduler_flag_t flags)
Schedule a task in the future.
void switch_core_sqldb_destroy(void)
Definition: switch_core.c:3013
switch_status_t switch_core_session_read_frame(_In_ switch_core_session_t *session, switch_frame_t **frame, switch_io_flag_t flags, int stream_id)
Read a frame from a session.
void switch_loadable_module_shutdown(void)
Shutdown the module backend and call the shutdown routine in all loaded modules.
int switch_stream_system(const char *cmd, switch_stream_handle_t *stream)
Definition: switch_core.c:3536
#define SWITCH_PREFIX_DIR
switch_hash_index_t * switch_core_mime_index(void)
Definition: switch_core.c:1227
switch_memory_pool_t * pool
Definition: switch_core.h:225
uint16_t switch_core_get_rtp_port_range_start_port(void)
Get RTP port range start value.
Definition: switch_core.c:3570
#define zstr(x)
Definition: switch_utils.h:314
switch_bool_t switch_check_network_list_ip_port_token(const char *ip_str, int port, const char *list_name, const char **token)
Definition: switch_core.c:1374
switch_size_t switch_core_session_id(void)
Provide the current session_id.
const switch_state_handler_table_t * switch_core_get_state_handler(int index)
Definition: switch_core.c:335
uint32_t switch_core_max_dtmf_duration(uint32_t duration)
Definition: switch_core.c:1704
void switch_curl_destroy(void)
Definition: switch_curl.c:56
char * switch_core_get_domain(switch_bool_t dup)
Definition: switch_core.c:367
#define SWITCH_SEQ_FYELLOW
Definition: switch_types.h:95
switch_status_t switch_mutex_unlock(switch_mutex_t *lock)
Definition: switch_apr.c:313
void switch_core_media_deinit(void)
switch_status_t switch_msrp_init(void)
Definition: switch_msrp.c:294
switch_status_t switch_event_shutdown(void)
Stop the eventing system.
Definition: switch_event.c:549
void switch_rtp_init(switch_memory_pool_t *pool)
Initilize the RTP System.
Definition: switch_rtp.c:1522
int switch_system(const char *cmd, switch_bool_t wait)
Definition: switch_core.c:3342
switch_status_t switch_thread_rwlock_rdlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:250
_Ret_ switch_channel_t * switch_core_session_get_channel(_In_ switch_core_session_t *session)
Retrieve a pointer to the channel object associated with a given session.
switch_status_t switch_event_init(switch_memory_pool_t *pool)
Start the eventing system.
Definition: switch_event.c:692
#define switch_clear_flag(obj, flag)
Clear a flag on an arbitrary object while locked.
Definition: switch_utils.h:724
#define SWITCH_MUTEX_NESTED
Definition: switch_apr.h:318
uint32_t switch_core_min_dtmf_duration(uint32_t duration)
Definition: switch_core.c:1744
switch_hash_t * hash
Definition: switch_core.c:1369
switch_hash_t * ptimes
void switch_channel_global_init(switch_memory_pool_t *pool)
int32_t set_auto_priority(void)
Definition: switch_core.c:1065
switch_status_t switch_thread_rwlock_wrlock(switch_thread_rwlock_t *rwlock)
Definition: switch_apr.c:260
static int switch_system_thread(const char *cmd, switch_bool_t wait)
Definition: switch_core.c:3189
switch_dbtype_t odbc_dbtype
void switch_nat_init(switch_memory_pool_t *pool, switch_bool_t mapping)
Initilize the NAT Traversal System.
Definition: switch_nat.c:399
#define SWITCH_PATH_SEPARATOR
Definition: switch_types.h:124
switch_status_t switch_threadattr_detach_set(switch_threadattr_t *attr, int32_t on)
Definition: switch_apr.c:678
int64_t switch_time_t
Definition: switch_apr.h:188
double switch_core_idle_cpu(void)
uint32_t buflen
Definition: switch_frame.h:70
int32_t sessions_peak_fivemin
switch_xml_t next
Definition: switch_xml.h:91
switch_status_t switch_network_list_add_host_mask(switch_network_list_t *list, const char *host, const char *mask_str, switch_bool_t ok)
Definition: switch_utils.c:737
static void *SWITCH_THREAD_FUNC switch_core_service_thread(switch_thread_t *thread, void *obj)
Definition: switch_core.c:501
switch_status_t switch_core_get_variables(switch_event_t **event)
Definition: switch_core.c:386
#define switch_yield(ms)
Wait a desired number of microseconds and yield the CPU.
Definition: switch_utils.h:998
char * event_channel_key_separator
switch_bool_t colorize_console
unsigned int switch_separate_string(_In_ char *buf, char delim, _Post_count_(return) char **array, unsigned int arraylen)
Separate a string into an array based on a character delimiter.
uint32_t datalen
Definition: switch_frame.h:68
static char main_ip6[256]
Definition: switch_core.c:130
static switch_bool_t switch_string_var_check(char *s, switch_bool_t disable)
Definition: switch_utils.h:763
char * switch_core_get_uuid(void)
Retrieve the unique identifier from the core.
Definition: switch_core.c:495
switch_bool_t switch_testv6_subnet(ip_t _ip, ip_t _net, ip_t _mask)
Definition: switch_utils.c:483
int ports[MAX_NETWORK_PORTS]
Definition: switch_utils.h:53
switch_mutex_t * frame_read_mutex
#define SWITCH_MAX_DTMF_DURATION
Definition: switch_types.h:119
void switch_time_set_matrix(switch_bool_t enable)
Definition: switch_time.c:360
switch_status_t switch_mutex_lock(switch_mutex_t *lock)
Definition: switch_apr.c:308
switch_core_flag_t switch_core_flags(void)
return core flags
Definition: switch_core.c:2988
void switch_core_thread_session_end(switch_core_session_t *session)
Definition: switch_core.c:557
void switch_core_session_uninit(void)
switch_log_level_t switch_log_str2level(_In_z_ const char *str)
Return the level number of the specified log level name.
uint32_t v4
Definition: switch_utils.h:278
void switch_core_setrlimits(void)
Definition: switch_core.c:1322
void switch_core_session_hupall(_In_ switch_call_cause_t cause)
Hangup all sessions.
#define switch_core_alloc(_pool, _mem)
Allocate memory directly from a memory pool.
Definition: switch_core.h:684
Abstract interface to a management module.
int index
Definition: switch_cJSON.h:160
#define SWITCH_THREAD_STACKSIZE
Definition: switch_types.h:584
#define SWITCH_SYSTEM_THREAD_STACKSIZE
Definition: switch_types.h:585
switch_session_ctl_t
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
void switch_core_memory_reclaim_all(void)
Definition: switch_core.c:3130
static uint32_t d_30
Definition: switch_core.c:2047
switch_port_t switch_rtp_set_end_port(switch_port_t port)
Set/Get RTP end port.
Definition: switch_rtp.c:2596
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:885
uint32_t switch_core_max_audio_channels(uint32_t limit)
Definition: switch_core.c:2642
int32_t set_normal_priority(void)
Set the maximum priority the process can obtain.
Definition: switch_core.c:1060
unsigned long switch_atoul(const char *nptr)
static void load_mime_types(void)
Definition: switch_core.c:1272
switch_status_t switch_mutex_init(switch_mutex_t **lock, unsigned int flags, switch_memory_pool_t *pool)
Definition: switch_apr.c:293
void switch_core_sqldb_resume(void)
switch_status_t switch_console_init(switch_memory_pool_t *pool)
char * switch_network_ipv4_mapped_ipv6_addr(const char *ip_str)
Definition: switch_utils.c:582
int switch_spawn(const char *cmd, switch_bool_t wait)
Definition: switch_core.c:3507
#define SWITCH_TIME_T_FMT
static switch_ip_list_t IP_LIST
Definition: switch_core.c:1372
switch_hash_t * mime_type_exts
uint32_t event_heartbeat_interval
static void send_heartbeat(void)
Definition: switch_core.c:89
An abstraction of a data frame.
Definition: switch_frame.h:54
uintptr_t switch_size_t
void switch_core_set_variable(const char *varname, const char *value)
Definition: switch_core.c:442
uint32_t db_handle_timeout
FILE * switch_core_data_channel(switch_text_channel_t channel)
Retrieve a FILE stream of a given text channel name.
Definition: switch_core.c:286
uint16_t switch_port_t
switch_size_t switch_fp_read_dline(FILE *fd, char **buf, switch_size_t *len)
Definition: switch_utils.c:892
int switch_inet_pton(int af, const char *src, void *dst)
Definition: inet_pton.c:74
void switch_core_runtime_loop(int bg)
Run endlessly until the system is shutdown.
Definition: switch_core.c:1187
switch_mutex_t * session_hash_mutex
#define SWITCH_STANDARD_STREAM(s)
uint32_t switch_core_session_count(void)
Provide the total number of sessions.
double switch_core_min_idle_cpu(double new_limit)
#define MAX_NETWORK_PORTS
Definition: switch_utils.h:49
void switch_nat_late_init(void)
Initilize the rest of the NAT Traversal System.
Definition: switch_nat.c:708
static void handle_SIGHUP(int sig)
Definition: switch_core.c:2008
static char main_ip4[256]
Definition: switch_core.c:129
pid_t switch_fork(void)
Definition: switch_core.c:3282
static switch_status_t switch_event_create_plain(switch_event_t **event, switch_event_types_t event_id)
Definition: switch_event.h:386
static int switch_system_fork(const char *cmd, switch_bool_t wait)
Definition: switch_core.c:3295
void * objs[SWITCH_MAX_CORE_THREAD_SESSION_OBJS]
Definition: switch_core.h:222
void switch_core_memory_reclaim_events(void)
Definition: switch_event.c:524
switch_status_t switch_xml_destroy(void)
Definition: switch_xml.c:2473
void switch_core_session_rwunlock(_In_ switch_core_session_t *session)
Unlock a read or write lock on as given session.
int32_t set_realtime_priority(void)
Definition: switch_core.c:981
switch_status_t switch_log_shutdown(void)
Shut down the logging engine.
Definition: switch_log.c:799
switch_mutex_t * global_mutex
void switch_rtp_shutdown(void)
Definition: switch_rtp.c:2552
struct fspr_thread_mutex_t switch_mutex_t
Definition: switch_apr.h:314
void switch_core_sqldb_pause(void)
void switch_cache_db_flush_handles(void)
void switch_core_media_set_resolveice(switch_bool_t resolve_ice)
switch_status_t switch_thread_cond_signal(switch_thread_cond_t *cond)
Definition: switch_apr.c:394
switch_hash_t * mime_types
void *(SWITCH_THREAD_FUNC * switch_thread_start_t)(switch_thread_t *, void *)
Definition: switch_apr.h:950
#define SWITCH_DEFAULT_DIR_PERMS
Definition: switch_types.h:120
char * ip
Definition: switch_msrp.c:60
uint32_t max_dtmf_duration
switch_status_t switch_core_sqldb_init(const char **err)
Definition: switch_core.c:2428
switch_bool_t switch_core_set_var_conditional(const char *varname, const char *value, const char *val2)
Definition: switch_core.c:464
uint32_t switch_core_default_dtmf_duration(uint32_t duration)
Definition: switch_core.c:1721
void switch_uuid_format(char *buffer, const switch_uuid_t *uuid)
Definition: switch_apr.c:1140
char * switch_core_get_variable_pdup(const char *varname, switch_memory_pool_t *pool)
Definition: switch_core.c:419
uint32_t switch_core_session_limit(_In_ uint32_t new_limit)
Set/Get Session Limit.
switch_status_t switch_network_list_add_cidr_port_token(switch_network_list_t *list, const char *cidr_str, switch_bool_t ok, const char *token, switch_network_port_range_p port)
Definition: switch_utils.c:675
int32_t switch_core_sessions_peak_fivemin(void)
Definition: switch_core.c:2637
switch_stream_handle_write_function_t write_function
#define SWITCH_MIN_DTMF_DURATION
Definition: switch_types.h:118
void switch_time_set_monotonic(switch_bool_t enable)
Definition: switch_time.c:331
static void *SWITCH_THREAD_FUNC system_thread(switch_thread_t *thread, void *obj)
Definition: switch_core.c:3147
#define SWITCH_SIZE_T_FMT
switch_status_t
Common return values.
const char *const const char *const path
char * switch_string_replace(const char *string, const char *search, const char *replace)
struct switch_event_header * next
Definition: switch_event.h:76
switch_xml_t switch_xml_open_cfg(_In_z_ const char *file_path, _Out_ switch_xml_t *node, _In_opt_ switch_event_t *params)
open a config in the core registry
const char * switch_core_get_hostname(void)
Definition: switch_core.c:356
switch_status_t switch_dir_make_recursive(const char *path, switch_fileperms_t perm, switch_memory_pool_t *pool)
Definition: switch_apr.c:551
void switch_console_save_history(void)
static void switch_core_unset_variables(void)
Definition: switch_core.c:434
void switch_odbc_skip_autocommit_flip(void)
Definition: switch_odbc.c:67
void switch_core_sqldb_stop(void)
switch_status_t switch_core_hash_init_case(_Out_ switch_hash_t **hash, switch_bool_t case_sensitive)
Initialize a hash table.
#define switch_core_hash_insert(_h, _k, _d)
Definition: switch_core.h:1479
switch_log_level_t hard_log_level
#define FALSE
void switch_time_calibrate_clock(void)
Definition: switch_time.c:208
int32_t switch_core_sps_peak(void)
Definition: switch_core.c:2622
Main Library Header.
uint32_t default_dtmf_duration
#define SWITCH_SEQ_DEFAULT_COLOR
Definition: switch_types.h:70
uint32_t tipping_point
#define switch_event_create(event, id)
Create a new event assuming it will not be custom event and therefore hiding the unused parameters...
Definition: switch_event.h:384
switch_bool_t switch_core_ready_outbound(void)
Determines if the core is ready to place outbound calls.
Definition: switch_core.c:3008
const char * switch_core_banner(void)
Definition: switch_core.c:2406
int32_t sps_peak_fivemin
void switch_core_recovery_flush(const char *technology, const char *profile_name)
#define SWITCH_DECLARE(type)
struct fspr_thread_cond_t switch_thread_cond_t
Definition: switch_apr.h:463
switch_filenames SWITCH_GLOBAL_filenames
Definition: switch_core.c:83
void switch_uuid_get(switch_uuid_t *uuid)
Definition: switch_apr.c:1152
uint32_t max_db_handles
switch_bool_t switch_network_list_validate_ip_port_token(switch_network_list_t *list, uint32_t ip, int port, const char **token)
Definition: switch_utils.c:546
#define switch_event_get_header(_e, _h)
Definition: switch_event.h:172
switch_time_t switch_mono_micro_time_now(void)
Definition: switch_time.c:316
#define switch_channel_set_flag(_c, _f)
switch_xml_t switch_xml_child(_In_ switch_xml_t xml, _In_z_ const char *name)
returns the first child tag (one level deeper) with the given name or NULL \ if not found ...
switch_status_t switch_core_init(switch_core_flag_t flags, switch_bool_t console, const char **err)
Definition: switch_core.c:1798
SWITCH_STANDARD_SCHED_FUNC(heartbeat_callback)
Definition: switch_core.c:225
switch_directories SWITCH_GLOBAL_dirs
Definition: switch_core.c:82
#define switch_set_string(_dst, _src)
Definition: switch_utils.h:734
void switch_time_set_use_system_time(switch_bool_t enable)
Definition: switch_time.c:342
int switch_stream_system_fork(const char *cmd, switch_stream_handle_t *stream)
Definition: switch_core.c:3363
char * core_db_pre_trans_execute
uint32_t port_alloc_flags
int switch_stream_spawn(const char *cmd, switch_bool_t shell, switch_bool_t wait, switch_stream_handle_t *stream)
Definition: switch_core.c:3372
void switch_time_set_cond_yield(switch_bool_t enable)
Definition: switch_time.c:373
char * buffer
Definition: switch_cJSON.h:153
const char * switch_core_mime_ext2type(const char *ext)
Definition: switch_core.c:1211
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:322
#define SWITCH_SEQ_BBLUE
Definition: switch_types.h:104
switch_bool_t switch_check_network_list_ip_token(const char *ip_str, const char *list_name, const char **token)
Definition: switch_core.c:1447
switch_status_t switch_loadable_module_init(switch_bool_t autoload)
Initilize the module backend and load all the modules.
A generic object to pass as a thread&#39;s session object to allow mutiple arguements and a pool...
Definition: switch_core.h:216
switch_mutex_t * uuid_mutex
char * switch_core_get_variable(const char *varname)
Definition: switch_core.c:395
uint32_t switch_core_debug_level(void)
Definition: switch_core.c:2607
switch_status_t switch_thread_rwlock_create(switch_thread_rwlock_t **rwlock, switch_memory_pool_t *pool)
Definition: switch_apr.c:235
switch_memory_pool_t * memory_pool
#define switch_test_flag(obj, flag)
Test for the existance of a flag on an arbitary object.
Definition: switch_utils.h:693
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_log_init(_In_ switch_memory_pool_t *pool, _In_ switch_bool_t colorize)
Initilize the logging engine.
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.
Definition: switch_core.c:3118
switch_status_t switch_threadattr_create(switch_threadattr_t **new_attr, switch_memory_pool_t *pool)
Definition: switch_apr.c:665
switch_status_t switch_thread_create(switch_thread_t **new_thread, switch_threadattr_t *attr, switch_thread_start_t func, void *data, switch_memory_pool_t *cont)
Definition: switch_apr.c:698
switch_status_t switch_console_execute(char *xcmd, int rec, switch_stream_handle_t *istream)
switch_status_t(* management_function)(char *relative_oid, switch_management_action_t action, char *data, switch_size_t datalen)
switch_status_t switch_find_local_ip(_Out_opt_bytecapcount_(len) char *buf, _In_ int len, _In_opt_ int *mask, _In_ int family)
find local ip of the box
uint32_t debug_level
int count
Definition: switch_cJSON.h:204
FILE * switch_core_get_console(void)
Get the output console.
Definition: switch_core.c:253
struct fspr_pool_t switch_memory_pool_t
void switch_load_network_lists(switch_bool_t reload)
Definition: switch_core.c:1452
const char *const name
Definition: switch_cJSON.h:250
char * switch_core_get_variable_dup(const char *varname)
Definition: switch_core.c:404
void switch_event_destroy(switch_event_t **event)
Destroy an event.
int32_t switch_core_set_process_privileges(void)
Switch on the privilege awareness for the process and request required privileges.
Definition: switch_core.c:910
void switch_core_service_session_av(switch_core_session_t *session, switch_bool_t audio, switch_bool_t video)
Definition: switch_core.c:573
uint32_t cpu_idle_smoothing_depth
void switch_core_memory_reclaim(void)
const char * switch_version_revision_human(void)
int32_t switch_core_session_ctl(switch_session_ctl_t cmd, void *val)
send a control message to the core
Definition: switch_core.c:2651
void switch_core_media_init(void)
char * core_db_inner_pre_trans_execute
void switch_channel_clear_flag(switch_channel_t *channel, switch_channel_flag_t flag)
Clear given flag(s) from a channel.
void switch_curl_init(void)
Definition: switch_curl.c:51
#define switch_assert(expr)
struct fspr_thread_t switch_thread_t
Definition: switch_apr.h:941
int switch_core_recovery_recover(const char *technology, const char *profile_name)
void switch_core_remove_state_handler(const switch_state_handler_table_t *state_handler)
Definition: switch_core.c:292
void switch_core_state_machine_init(switch_memory_pool_t *pool)
#define switch_core_session_kill_channel(session, sig)
Send a signal to a channel.
Definition: switch_core.h:1393
switch_bool_t switch_core_ready_inbound(void)
Determines if the core is ready to take inbound calls.
Definition: switch_core.c:3003
#define switch_core_hash_first(_h)
Definition: switch_core.h:1592
switch_bool_t switch_core_ready(void)
Determines if the core is ready to take calls.
Definition: switch_core.c:2998
SWITCH_BEGIN_EXTERN_C char * switch_mprintf(const char *zFormat,...)
void switch_console_loop(void)
A simple comand loop that reads input from the terminal.
int32_t switch_core_sps_last(void)
Definition: switch_core.c:2617
char uuid_str[SWITCH_UUID_FORMATTED_LENGTH+1]
memset(buf, 0, buflen)
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.
Definition: switch_core.c:2535
switch_thread_t * switch_core_launch_thread(switch_thread_start_t func, void *obj, switch_memory_pool_t *pool)
Definition: switch_core.c:601
void switch_ssl_destroy_ssl_locks(void)
switch_status_t switch_core_check_core_db_dsn(void)
Returns error if no suitable database interface found to serve core db dsn.
void switch_core_screen_size(int *x, int *y)
Definition: switch_core.c:261
switch_management_action_t
Definition: switch_types.h:481
switch_event_header_t * headers
Definition: switch_event.h:90
uint32_t switch_default_rate(const char *name, uint32_t number)
Definition: switch_core.c:2033
switch_status_t switch_xml_init(_In_ switch_memory_pool_t *pool, _Out_ const char **err)
initilize the core XML backend
void switch_core_session_launch_thread(_In_ switch_core_session_t *session, _In_ void *(*func)(switch_thread_t *, void *), _In_opt_ void *obj)
Launch a thread designed to exist within the scope of a given session.
switch_memory_pool_t * pool
Definition: switch_core.c:3142
switch_status_t switch_core_set_console(const char *console)
Set the output console to the desired file.
Definition: switch_core.c:243
switch_status_t switch_threadattr_priority_set(switch_threadattr_t *attr, switch_thread_priority_t priority)
Definition: switch_apr.c:688
const char * switch_core_get_event_channel_key_separator(void)
Definition: switch_core.c:3592