提交 9ac29908 authored 作者: Michael Jerris's avatar Michael Jerris

factor out some functions, some of this will probably move to the freeswitch lib still.

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@2334 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 d7f33e6a
...@@ -36,7 +36,21 @@ ...@@ -36,7 +36,21 @@
#include <sys/mman.h> #include <sys/mman.h>
#endif #endif
#ifdef CRASH_PROT
#define __CP "ENABLED"
#else
#define __CP "DISABLED"
#endif
#define PIDFILE "freeswitch.pid"
#define LOGFILE "freeswitch.log"
static int RUNNING = 0; static int RUNNING = 0;
static char *lfile = LOGFILE;
static char *pfile = PIDFILE;
#ifdef __ICC
#pragma warning (disable:167)
#endif
static int handle_SIGPIPE(int sig) static int handle_SIGPIPE(int sig)
{ {
...@@ -67,75 +81,87 @@ static int handle_SIGHUP(int sig) ...@@ -67,75 +81,87 @@ static int handle_SIGHUP(int sig)
return 0; return 0;
} }
int main(int argc, char *argv[]) static void set_high_priority()
{ {
char *lfile = "freeswitch.log";
char *pfile = "freeswitch.pid";
char path[256] = "";
char *ppath = NULL;
const char *err = NULL;
switch_event_t *event;
int bg = 0;
FILE *f;
#ifdef WIN32 #ifdef WIN32
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
#else #else
int pid;
nice(-20); nice(-20);
#endif #endif
}
#ifndef WIN32 static int freeswitch_shutdown()
if (argv[1] && !strcmp(argv[1], "-stop")) { {
pid_t pid = 0; switch_event_t *event;
switch_core_set_globals(); if (switch_event_create(&event, SWITCH_EVENT_SHUTDOWN) == SWITCH_STATUS_SUCCESS) {
snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Shutting Down");
if ((f = fopen(path, "r")) == 0) { switch_event_fire(&event);
fprintf(stderr, "Cannot open pid file %s.\n", path);
return 255;
}
fscanf(f, "%d", &pid);
if (pid > 0) {
fprintf(stderr, "Killing %d\n", (int) pid);
kill(pid, SIGTERM);
}
fclose(f);
return 0;
}
#endif
if (argv[1] && !strcmp(argv[1], "-nc")) {
bg++;
} }
if (bg) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "End existing sessions\n");
//snprintf(path, sizeof(path), "%s%c%s", SWITCH_GLOBAL_dirs.log_dir, sep, lfile); switch_core_session_hupall();
ppath = lfile; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Clean up modules.\n");
switch_loadable_module_shutdown();
switch_core_destroy();
return 0;
}
signal(SIGHUP, (void *) handle_SIGHUP); static void freeswitch_runtime_loop(int bg)
signal(SIGTERM, (void *) handle_SIGHUP); {
FILE *f;
char path[256] = "";
snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile);
if (bg) {
bg = 0;
RUNNING = 1;
while(RUNNING) {
#ifdef WIN32 #ifdef WIN32
FreeConsole(); bg++;
#else if(bg == 100) {
if ((pid = fork())) { if ((f = fopen(path, "r")) == 0) {
fprintf(stderr, "%d Backgrounding.\n", (int)pid); break;
exit(0); }
fclose(f);
bg = 0;
} }
#endif #endif
switch_yield(10000);
}
} else {
/* wait for console input */
switch_console_loop();
} }
}
static int freeswitch_kill_background()
if (switch_core_init(ppath, &err) != SWITCH_STATUS_SUCCESS) { {
fprintf(stderr, "Cannot Initilize [%s]\n", err); #ifdef WIN32
#else
char path[256] = "";
pid_t pid = 0;
snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile);
if ((f = fopen(path, "r")) == 0) {
fprintf(stderr, "Cannot open pid file %s.\n", path);
return 255; return 255;
} }
fscanf(f, "%d", &pid);
if (pid > 0) {
fprintf(stderr, "Killing %d\n", (int) pid);
kill(pid, SIGTERM);
}
fclose(f);
#ifdef __ICC
#pragma warning (disable:167)
#endif #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 */ /* set signal handlers */
signal(SIGINT, (void *) handle_SIGINT); signal(SIGINT, (void *) handle_SIGINT);
...@@ -145,32 +171,13 @@ int main(int argc, char *argv[]) ...@@ -145,32 +171,13 @@ int main(int argc, char *argv[])
#ifdef TRAP_BUS #ifdef TRAP_BUS
signal(SIGBUS, (void *) handle_SIGBUS); signal(SIGBUS, (void *) handle_SIGBUS);
#endif #endif
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", getpid());
fclose(f);
if (!err) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Bringing up environment.\n");
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");
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Loading Modules.\n"); if (switch_loadable_module_init() != SWITCH_STATUS_SUCCESS) {
if (switch_loadable_module_init() != SWITCH_STATUS_SUCCESS) { *err = "Cannot load modules";
err = "Cannot load modules";
}
}
if (err) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Error: %s", err); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Error: %s", err);
exit(-1); return 255;
} }
if (switch_event_create(&event, SWITCH_EVENT_STARTUP) == SWITCH_STATUS_SUCCESS) { if (switch_event_create(&event, SWITCH_EVENT_STARTUP) == SWITCH_STATUS_SUCCESS) {
...@@ -182,46 +189,60 @@ int main(int argc, char *argv[]) ...@@ -182,46 +189,60 @@ int main(int argc, char *argv[])
mlockall(MCL_CURRENT|MCL_FUTURE); mlockall(MCL_CURRENT|MCL_FUTURE);
#endif #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;
}
#ifdef CRASH_PROT int main(int argc, char *argv[])
#define __CP "ENABLED" {
#else char path[256] = "";
#define __CP "DISABLED" char *ppath = NULL;
#endif const char *err = NULL;
int bg = 0;
FILE *f;
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)); set_high_priority();
snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); switch_core_set_globals();
if (argv[1] && !strcmp(argv[1], "-stop")) {
return freeswitch_kill_background();
}
if (argv[1] && !strcmp(argv[1], "-nc")) {
bg++;
}
if (bg) { if (bg) {
bg = 0; ppath = lfile;
RUNNING = 1;
while(RUNNING) { signal(SIGHUP, (void *) handle_SIGHUP);
signal(SIGTERM, (void *) handle_SIGHUP);
#ifdef WIN32 #ifdef WIN32
bg++; FreeConsole();
if(bg == 100) { #else
if ((f = fopen(path, "r")) == 0) { if ((pid = fork())) {
break; fprintf(stderr, "%d Backgrounding.\n", (int)pid);
} exit(0);
fclose(f);
bg = 0;
} }
#endif #endif
switch_yield(10000);
}
} else {
/* wait for console input */
switch_console_loop();
} }
if (switch_event_create(&event, SWITCH_EVENT_SHUTDOWN) == SWITCH_STATUS_SUCCESS) { snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Shutting Down"); if ((f = fopen(path, "w")) == 0) {
switch_event_fire(&event); fprintf(stderr, "Cannot open pid file %s.\n", path);
return 255;
} }
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "End existing sessions\n"); fprintf(f, "%d", getpid());
switch_core_session_hupall(); fclose(f);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Clean up modules.\n");
switch_loadable_module_shutdown(); if (freeswitch_init(ppath, &err) == 255) {
switch_core_destroy(); fprintf(stderr, "Cannot Initilize [%s]\n", err);
return 0; return 255;
}
freeswitch_runtime_loop(bg);
return freeswitch_shutdown();
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论