Add switch_core_init_and_modload to core, from switch.c. Add additional shutdown functionality to switch_core_destroy. move around more code in switch.c and fix freeswitch -stop on windows.
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@2336 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
parent
9ac299089d
commit
c15da98579
|
@ -116,6 +116,14 @@ struct switch_core_runtime;
|
||||||
*/
|
*/
|
||||||
SWITCH_DECLARE(switch_status_t) switch_core_init(char *console, const char **err);
|
SWITCH_DECLARE(switch_status_t) switch_core_init(char *console, const char **err);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\brief Initilize the core and load modules
|
||||||
|
\param console optional FILE stream for output
|
||||||
|
\param err a pointer to set any errors to
|
||||||
|
\note to be called at application startup instead of switch_core_init. Includes module loading.
|
||||||
|
*/
|
||||||
|
SWITCH_DECLARE(switch_status_t) switch_core_init_and_modload(char *console, const char **err);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief Set/Get Session Limit
|
\brief Set/Get Session Limit
|
||||||
\param new_limit new value (if > 0)
|
\param new_limit new value (if > 0)
|
||||||
|
|
149
src/switch.c
149
src/switch.c
|
@ -31,16 +31,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
#include <switch_version.h>
|
|
||||||
#ifdef HAVE_MLOCKALL
|
|
||||||
#include <sys/mman.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef CRASH_PROT
|
|
||||||
#define __CP "ENABLED"
|
|
||||||
#else
|
|
||||||
#define __CP "DISABLED"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define PIDFILE "freeswitch.pid"
|
#define PIDFILE "freeswitch.pid"
|
||||||
#define LOGFILE "freeswitch.log"
|
#define LOGFILE "freeswitch.log"
|
||||||
|
@ -52,29 +42,12 @@ static char *pfile = PIDFILE;
|
||||||
#pragma warning (disable:167)
|
#pragma warning (disable:167)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int handle_SIGPIPE(int sig)
|
|
||||||
{
|
#ifdef WIN32
|
||||||
if(sig);
|
static HANDLE shutdown_event;
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Sig Pipe!\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#ifdef TRAP_BUS
|
|
||||||
static int handle_SIGBUS(int sig)
|
|
||||||
{
|
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Sig BUS!\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* no ctl-c mofo */
|
static int handle_SIGHUP(int sig)
|
||||||
static int handle_SIGINT(int sig)
|
|
||||||
{
|
|
||||||
if (sig);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static int handle_SIGHUP(int sig)
|
|
||||||
{
|
{
|
||||||
if(sig);
|
if(sig);
|
||||||
RUNNING = 0;
|
RUNNING = 0;
|
||||||
|
@ -90,44 +63,18 @@ static void set_high_priority()
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static int freeswitch_shutdown()
|
|
||||||
{
|
|
||||||
switch_event_t *event;
|
|
||||||
if (switch_event_create(&event, SWITCH_EVENT_SHUTDOWN) == SWITCH_STATUS_SUCCESS) {
|
|
||||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Shutting Down");
|
|
||||||
switch_event_fire(&event);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "End existing sessions\n");
|
|
||||||
switch_core_session_hupall();
|
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Clean up modules.\n");
|
|
||||||
switch_loadable_module_shutdown();
|
|
||||||
switch_core_destroy();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void freeswitch_runtime_loop(int bg)
|
static void freeswitch_runtime_loop(int bg)
|
||||||
{
|
{
|
||||||
FILE *f;
|
|
||||||
char path[256] = "";
|
|
||||||
snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile);
|
|
||||||
|
|
||||||
if (bg) {
|
if (bg) {
|
||||||
bg = 0;
|
bg = 0;
|
||||||
|
#ifdef WIN32
|
||||||
|
WaitForSingleObject(shutdown_event, INFINITE);
|
||||||
|
#else
|
||||||
RUNNING = 1;
|
RUNNING = 1;
|
||||||
while(RUNNING) {
|
while(RUNNING) {
|
||||||
#ifdef WIN32
|
|
||||||
bg++;
|
|
||||||
if(bg == 100) {
|
|
||||||
if ((f = fopen(path, "r")) == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
fclose(f);
|
|
||||||
bg = 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
switch_yield(10000);
|
switch_yield(10000);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
/* wait for console input */
|
/* wait for console input */
|
||||||
switch_console_loop();
|
switch_console_loop();
|
||||||
|
@ -136,8 +83,7 @@ static void freeswitch_runtime_loop(int bg)
|
||||||
|
|
||||||
static int freeswitch_kill_background()
|
static int freeswitch_kill_background()
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
FILE *f;
|
||||||
#else
|
|
||||||
char path[256] = "";
|
char path[256] = "";
|
||||||
pid_t pid = 0;
|
pid_t pid = 0;
|
||||||
snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile);
|
snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile);
|
||||||
|
@ -147,49 +93,23 @@ static int freeswitch_kill_background()
|
||||||
}
|
}
|
||||||
fscanf(f, "%d", &pid);
|
fscanf(f, "%d", &pid);
|
||||||
if (pid > 0) {
|
if (pid > 0) {
|
||||||
fprintf(stderr, "Killing %d\n", (int) pid);
|
fprintf(stderr, "Killing: %d\n", (int) pid);
|
||||||
|
#ifdef WIN32
|
||||||
|
snprintf(path, sizeof(path), "Global\\Freeswitch.%d", pid);
|
||||||
|
shutdown_event = OpenEvent(EVENT_MODIFY_STATE, FALSE, path);
|
||||||
|
if (!shutdown_event) {
|
||||||
|
/* we can't get the event, so we can't signal the process to shutdown */
|
||||||
|
fprintf(stderr, "ERROR: Can't Shutdown: %d\n", (int) pid);
|
||||||
|
} else {
|
||||||
|
SetEvent(shutdown_event);
|
||||||
|
}
|
||||||
|
CloseHandle(shutdown_event);
|
||||||
|
#else
|
||||||
kill(pid, SIGTERM);
|
kill(pid, SIGTERM);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
#endif
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int freeswitch_init(char *path, const char **err)
|
|
||||||
{
|
|
||||||
switch_event_t *event;
|
|
||||||
if (switch_core_init(path, err) != SWITCH_STATUS_SUCCESS) {
|
|
||||||
return 255;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* set signal handlers */
|
|
||||||
signal(SIGINT, (void *) handle_SIGINT);
|
|
||||||
#ifdef SIGPIPE
|
|
||||||
signal(SIGPIPE, (void *) handle_SIGPIPE);
|
|
||||||
#endif
|
|
||||||
#ifdef TRAP_BUS
|
|
||||||
signal(SIGBUS, (void *) handle_SIGBUS);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Bringing up environment.\n");
|
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Loading Modules.\n");
|
|
||||||
if (switch_loadable_module_init() != SWITCH_STATUS_SUCCESS) {
|
|
||||||
*err = "Cannot load modules";
|
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Error: %s", err);
|
|
||||||
return 255;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (switch_event_create(&event, SWITCH_EVENT_STARTUP) == SWITCH_STATUS_SUCCESS) {
|
|
||||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Ready");
|
|
||||||
switch_event_fire(&event);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef HAVE_MLOCKALL
|
|
||||||
mlockall(MCL_CURRENT|MCL_FUTURE);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "freeswitch Version %s Started. Crash Protection [%s] Max Sessions[%u]\n\n", SWITCH_VERSION_FULL, __CP, switch_core_session_limit(0));
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,6 +120,7 @@ int main(int argc, char *argv[])
|
||||||
const char *err = NULL;
|
const char *err = NULL;
|
||||||
int bg = 0;
|
int bg = 0;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
|
pid_t pid = 0;
|
||||||
|
|
||||||
set_high_priority();
|
set_high_priority();
|
||||||
switch_core_set_globals();
|
switch_core_set_globals();
|
||||||
|
@ -212,6 +133,15 @@ int main(int argc, char *argv[])
|
||||||
bg++;
|
bg++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile);
|
||||||
|
if ((f = fopen(path, "w")) == 0) {
|
||||||
|
fprintf(stderr, "Cannot open pid file %s.\n", path);
|
||||||
|
return 255;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(f, "%d", pid = getpid());
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
if (bg) {
|
if (bg) {
|
||||||
ppath = lfile;
|
ppath = lfile;
|
||||||
|
|
||||||
|
@ -220,6 +150,8 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
FreeConsole();
|
FreeConsole();
|
||||||
|
snprintf(path, sizeof(path), "Global\\Freeswitch.%d", pid);
|
||||||
|
shutdown_event = CreateEvent(NULL, FALSE, FALSE, path);
|
||||||
#else
|
#else
|
||||||
if ((pid = fork())) {
|
if ((pid = fork())) {
|
||||||
fprintf(stderr, "%d Backgrounding.\n", (int)pid);
|
fprintf(stderr, "%d Backgrounding.\n", (int)pid);
|
||||||
|
@ -228,21 +160,12 @@ int main(int argc, char *argv[])
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile);
|
if (switch_core_init_and_modload(ppath, &err) != SWITCH_STATUS_SUCCESS) {
|
||||||
if ((f = fopen(path, "w")) == 0) {
|
|
||||||
fprintf(stderr, "Cannot open pid file %s.\n", path);
|
|
||||||
return 255;
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(f, "%d", getpid());
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
if (freeswitch_init(ppath, &err) == 255) {
|
|
||||||
fprintf(stderr, "Cannot Initilize [%s]\n", err);
|
fprintf(stderr, "Cannot Initilize [%s]\n", err);
|
||||||
return 255;
|
return 255;
|
||||||
}
|
}
|
||||||
|
|
||||||
freeswitch_runtime_loop(bg);
|
freeswitch_runtime_loop(bg);
|
||||||
|
|
||||||
return freeswitch_shutdown();
|
return switch_core_destroy();
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,8 +32,20 @@
|
||||||
|
|
||||||
#include <switch.h>
|
#include <switch.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <switch_version.h>
|
||||||
|
|
||||||
|
#ifdef HAVE_MLOCKALL
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
//#define DEBUG_ALLOC
|
//#define DEBUG_ALLOC
|
||||||
|
|
||||||
|
#ifdef CRASH_PROT
|
||||||
|
#define __CP "ENABLED"
|
||||||
|
#else
|
||||||
|
#define __CP "DISABLED"
|
||||||
|
#endif
|
||||||
|
|
||||||
#define SWITCH_EVENT_QUEUE_LEN 256
|
#define SWITCH_EVENT_QUEUE_LEN 256
|
||||||
#define SWITCH_SQL_QUEUE_LEN 2000
|
#define SWITCH_SQL_QUEUE_LEN 2000
|
||||||
|
|
||||||
|
@ -3109,7 +3121,65 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(char *console, const char **err
|
||||||
runtime.initiated = switch_time_now();
|
runtime.initiated = switch_time_now();
|
||||||
return SWITCH_STATUS_SUCCESS;
|
return SWITCH_STATUS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
#ifdef SIGPIPE
|
||||||
|
static int handle_SIGPIPE(int sig)
|
||||||
|
{
|
||||||
|
if(sig);
|
||||||
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Sig Pipe!\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#ifdef TRAP_BUS
|
||||||
|
static int handle_SIGBUS(int sig)
|
||||||
|
{
|
||||||
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Sig BUS!\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* no ctl-c mofo */
|
||||||
|
static int handle_SIGINT(int sig)
|
||||||
|
{
|
||||||
|
if (sig);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
SWITCH_DECLARE(switch_status_t) switch_core_init_and_modload(char *console, const char **err)
|
||||||
|
{
|
||||||
|
switch_event_t *event;
|
||||||
|
if (switch_core_init(console, err) != SWITCH_STATUS_SUCCESS) {
|
||||||
|
return SWITCH_STATUS_GENERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* set signal handlers */
|
||||||
|
signal(SIGINT, (void *) handle_SIGINT);
|
||||||
|
#ifdef SIGPIPE
|
||||||
|
signal(SIGPIPE, (void *) handle_SIGPIPE);
|
||||||
|
#endif
|
||||||
|
#ifdef TRAP_BUS
|
||||||
|
signal(SIGBUS, (void *) handle_SIGBUS);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Bringing up environment.\n");
|
||||||
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Loading Modules.\n");
|
||||||
|
if (switch_loadable_module_init() != SWITCH_STATUS_SUCCESS) {
|
||||||
|
*err = "Cannot load modules";
|
||||||
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Error: %s", err);
|
||||||
|
return SWITCH_STATUS_GENERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (switch_event_create(&event, SWITCH_EVENT_STARTUP) == SWITCH_STATUS_SUCCESS) {
|
||||||
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Ready");
|
||||||
|
switch_event_fire(&event);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_MLOCKALL
|
||||||
|
mlockall(MCL_CURRENT|MCL_FUTURE);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "freeswitch Version %s Started. Crash Protection [%s] Max Sessions[%u]\n\n", SWITCH_VERSION_FULL, __CP, switch_core_session_limit(0));
|
||||||
|
return SWITCH_STATUS_SUCCESS;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
SWITCH_DECLARE(void) switch_core_measure_time(switch_time_t total_ms, switch_core_time_duration_t *duration)
|
SWITCH_DECLARE(void) switch_core_measure_time(switch_time_t total_ms, switch_core_time_duration_t *duration)
|
||||||
{
|
{
|
||||||
|
@ -3135,6 +3205,16 @@ SWITCH_DECLARE(switch_time_t) switch_core_uptime(void)
|
||||||
|
|
||||||
SWITCH_DECLARE(switch_status_t) switch_core_destroy(void)
|
SWITCH_DECLARE(switch_status_t) switch_core_destroy(void)
|
||||||
{
|
{
|
||||||
|
switch_event_t *event;
|
||||||
|
if (switch_event_create(&event, SWITCH_EVENT_SHUTDOWN) == SWITCH_STATUS_SUCCESS) {
|
||||||
|
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Shutting Down");
|
||||||
|
switch_event_fire(&event);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "End existing sessions\n");
|
||||||
|
switch_core_session_hupall();
|
||||||
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Clean up modules.\n");
|
||||||
|
switch_loadable_module_shutdown();
|
||||||
|
|
||||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Closing Event Engine.\n");
|
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Closing Event Engine.\n");
|
||||||
switch_event_shutdown();
|
switch_event_shutdown();
|
||||||
|
|
Loading…
Reference in New Issue