492 char pid_path[PATH_MAX] =
"";
493 char pid_buffer[32] =
"";
494 char old_pid_buffer[32] = { 0 };
496 const char *err = NULL;
500 char *runas_user = NULL;
501 char *runas_group = NULL;
503 int fds[2] = { 0, 0 };
513 char opts_str[1024] =
"";
514 char *local_argv[1024] = { 0 };
515 int local_argc = argc;
516 char *arg_argv[128] = { 0 };
517 int alt_dirs = 0, alt_base = 0, log_set = 0, run_set = 0, do_kill = 0;
519 #if (defined(__SVR4) && defined(__sun)) 528 #ifdef HAVE_SETRLIMIT 534 for (x = 0; x < argc; x++) {
535 local_argv[x] = argv[x];
538 if ((opts = getenv(
"FREESWITCH_OPTS"))) {
539 strncpy(opts_str, opts,
sizeof(opts_str) - 1);
541 for (x = 0; x < i; x++) {
542 local_argv[local_argc++] = arg_argv[x];
546 if (local_argv[0] && strstr(local_argv[0],
"freeswitchd")) {
553 for (x = 1; x < local_argc; x++) {
558 if (!strcmp(local_argv[x],
"-help") || !strcmp(local_argv[x],
"-h") || !strcmp(local_argv[x],
"-?")) {
559 printf(
"%s\n",
usage);
563 if (x == 1 && !strcmp(local_argv[x],
"-service")) {
576 else if (x == 1 && !strcmp(local_argv[x],
"-install")) {
577 char servicePath[PATH_MAX];
578 char exePath[PATH_MAX];
580 SC_HANDLE hSCManager;
581 SERVICE_DESCRIPTION desc;
582 desc.lpDescription =
"The FreeSWITCH service.";
591 GetModuleFileName(NULL, exePath,
sizeof(exePath));
592 snprintf(servicePath,
sizeof(servicePath),
"%s -service %s", exePath, service_name);
596 hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
598 fprintf(stderr,
"Could not open service manager (%u).\n", GetLastError());
602 hService = CreateService(hSCManager, service_name, service_name, GENERIC_READ | GENERIC_EXECUTE | SERVICE_CHANGE_CONFIG, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
603 servicePath, NULL, NULL, NULL, NULL, NULL);
605 fprintf(stderr,
"Error creating freeswitch service (%u).\n", GetLastError());
606 CloseServiceHandle(hSCManager);
611 if (!ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &desc)) {
612 fprintf(stderr,
"FreeSWITCH installed, but could not set the service description (%u).\n", GetLastError());
615 CloseServiceHandle(hService);
616 CloseServiceHandle(hSCManager);
620 else if (x == 1 && !strcmp(local_argv[x],
"-uninstall")) {
622 SC_HANDLE hSCManager;
633 hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
635 fprintf(stderr,
"Could not open service manager (%u).\n", GetLastError());
639 hService = OpenService(hSCManager, service_name, DELETE);
641 fprintf(stderr,
"Error opening service (%u).\n", GetLastError());
642 CloseServiceHandle(hSCManager);
647 deleted = DeleteService(hService);
649 fprintf(stderr,
"Error deleting service (%u).\n", GetLastError());
652 CloseServiceHandle(hService);
653 CloseServiceHandle(hSCManager);
654 exit(deleted ? EXIT_SUCCESS : EXIT_FAILURE);
657 else if (!strcmp(local_argv[x],
"-monotonic-clock")) {
661 else if (!strcmp(local_argv[x],
"-u")) {
664 fprintf(stderr,
"Option '%s' requires an argument!\n", local_argv[x - 1]);
667 runas_user = local_argv[x];
670 else if (!strcmp(local_argv[x],
"-g")) {
673 fprintf(stderr,
"Option '%s' requires an argument!\n", local_argv[x - 1]);
676 runas_group = local_argv[x];
679 else if (!strcmp(local_argv[x],
"-nf")) {
683 else if (!strcmp(local_argv[x],
"-elegant-term")) {
687 else if (!strcmp(local_argv[x],
"-reincarnate")) {
690 else if (!strcmp(local_argv[x],
"-reincarnate-reexec")) {
695 #ifdef HAVE_SETRLIMIT 696 else if (!strcmp(local_argv[x],
"-core")) {
698 memset(&rlp, 0,
sizeof(rlp));
699 rlp.rlim_cur = RLIM_INFINITY;
700 rlp.rlim_max = RLIM_INFINITY;
701 setrlimit(RLIMIT_CORE, &rlp);
704 else if (!strcmp(local_argv[x],
"-waste")) {
706 fprintf(stderr,
"WARNING: Wasting up to 8 megs of memory per thread.\n");
712 else if (!strcmp(local_argv[x],
"-no-auto-stack")) {
718 else if (!strcmp(local_argv[x],
"-version")) {
723 else if (!strcmp(local_argv[x],
"-hp") || !strcmp(local_argv[x],
"-rp")) {
727 else if (!strcmp(local_argv[x],
"-lp")) {
731 else if (!strcmp(local_argv[x],
"-np")) {
735 else if (!strcmp(local_argv[x],
"-nosql")) {
739 else if (!strcmp(local_argv[x],
"-nonat")) {
743 else if (!strcmp(local_argv[x],
"-nat")) {
747 else if (!strcmp(local_argv[x],
"-nonatmap")) {
751 else if (!strcmp(local_argv[x],
"-heavy-timer")) {
755 else if (!strcmp(local_argv[x],
"-nort")) {
759 else if (!strcmp(local_argv[x],
"-nocal")) {
763 else if (!strcmp(local_argv[x],
"-vg")) {
767 else if (!strcmp(local_argv[x],
"-stop")) {
771 else if (!strcmp(local_argv[x],
"-nc")) {
775 else if (!strcmp(local_argv[x],
"-ncwait")) {
780 else if (!strcmp(local_argv[x],
"-c")) {
784 else if (!strcmp(local_argv[x],
"-conf")) {
787 fprintf(stderr,
"When using -conf you must specify a config directory\n");
793 fprintf(stderr,
"Allocation error\n");
800 else if (!strcmp(local_argv[x],
"-mod")) {
803 fprintf(stderr,
"When using -mod you must specify a module directory\n");
809 fprintf(stderr,
"Allocation error\n");
815 else if (!strcmp(local_argv[x],
"-log")) {
818 fprintf(stderr,
"When using -log you must specify a log directory\n");
824 fprintf(stderr,
"Allocation error\n");
832 else if (!strcmp(local_argv[x],
"-run")) {
835 fprintf(stderr,
"When using -run you must specify a pid directory\n");
841 fprintf(stderr,
"Allocation error\n");
848 else if (!strcmp(local_argv[x],
"-db")) {
851 fprintf(stderr,
"When using -db you must specify a db directory\n");
857 fprintf(stderr,
"Allocation error\n");
864 else if (!strcmp(local_argv[x],
"-scripts")) {
867 fprintf(stderr,
"When using -scripts you must specify a scripts directory\n");
873 fprintf(stderr,
"Allocation error\n");
879 else if (!strcmp(local_argv[x],
"-htdocs")) {
882 fprintf(stderr,
"When using -htdocs you must specify a htdocs directory\n");
888 fprintf(stderr,
"Allocation error\n");
894 else if (!strcmp(local_argv[x],
"-base")) {
897 fprintf(stderr,
"When using -base you must specify a base directory\n");
903 fprintf(stderr,
"Allocation error\n");
910 else if (!strcmp(local_argv[x],
"-temp")) {
913 fprintf(stderr,
"When using -temp you must specify a temp directory\n");
919 fprintf(stderr,
"Allocation error\n");
925 else if (!strcmp(local_argv[x],
"-storage")) {
928 fprintf(stderr,
"When using -storage you must specify a storage directory\n");
934 fprintf(stderr,
"Allocation error\n");
940 else if (!strcmp(local_argv[x],
"-cache")) {
943 fprintf(stderr,
"When using -cache you must specify a cache directory\n");
949 fprintf(stderr,
"Allocation error\n");
955 else if (!strcmp(local_argv[x],
"-recordings")) {
958 fprintf(stderr,
"When using -recordings you must specify a recording directory\n");
964 fprintf(stderr,
"Allocation error\n");
970 else if (!strcmp(local_argv[x],
"-grammar")) {
973 fprintf(stderr,
"When using -grammar you must specify a grammar directory\n");
979 fprintf(stderr,
"Allocation error\n");
985 else if (!strcmp(local_argv[x],
"-certs")) {
988 fprintf(stderr,
"When using -certs you must specify a certificates directory\n");
994 fprintf(stderr,
"Allocation error\n");
1000 else if (!strcmp(local_argv[x],
"-sounds")) {
1003 fprintf(stderr,
"When using -sounds you must specify a sounds directory\n");
1009 fprintf(stderr,
"Allocation error\n");
1015 else if (!strcmp(local_argv[x],
"-cfgname")) {
1018 fprintf(stderr,
"When using -cfgname you must specify a filename\n");
1024 fprintf(stderr,
"Allocation error\n");
1032 fprintf(stderr,
"Unknown option '%s', see '%s -help' for a list of valid options\n",
1033 local_argv[x], local_argv[0]);
1038 if (log_set && !run_set) {
1041 fprintf(stderr,
"Allocation error\n");
1052 fprintf(stderr,
"FATAL ERROR! Could not initialize APR\n");
1056 if (alt_dirs && alt_dirs != 3 && !alt_base) {
1057 fprintf(stderr,
"You must specify all or none of -conf, -log, and -db\n");
1062 #if defined(HAVE_SETRLIMIT) && !defined(__sun) 1063 if (!waste && !(flags &
SCF_VG)) {
1066 memset(&rlp, 0,
sizeof(rlp));
1067 getrlimit(RLIMIT_STACK, &rlp);
1070 char buf[1024] =
"";
1073 memset(&rlp, 0,
sizeof(rlp));
1076 setrlimit(RLIMIT_STACK, &rlp);
1079 if (argv) ret = (int) execv(argv[0], argv);
1081 for (i = 0; i < argc; i++) {
1082 switch_snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%s ", argv[i]);
1100 fprintf(stderr,
"System Error!\n");
1143 if (runas_user || runas_group) {
1145 fprintf(stderr,
"Failed to switch user [%s] / group [%s]\n",
1152 if (win32_service) {
1154 SERVICE_TABLE_ENTRY dispatchTable[] = {
1155 {service_name, &service_main}
1159 service_flags = flags;
1161 if (StartServiceCtrlDispatcher(dispatchTable) == 0) {
1163 fprintf(stderr,
"Error Freeswitch loaded as a console app with -service option\n");
1164 fprintf(stderr,
"To install the service load freeswitch with -install\n");
1174 memset(pid_buffer, 0,
sizeof(pid_buffer));
1177 pid_len = strlen(pid_buffer);
1179 fspr_pool_create(&pool, NULL);
1185 old_pid_len =
sizeof(old_pid_buffer) -1;
1194 fprintf(stderr,
"Cannot open pid file %s.\n", pid_path);
1199 fprintf(stderr,
"Cannot lock pid file %s.\n", pid_path);
1200 old_pid_len = strlen(old_pid_buffer);
1201 if (strlen(old_pid_buffer)) {
1210 fprintf(stderr,
"Cannot Initialize [%s]\n", err);
1219 if (write(fds[1], &v,
sizeof(v)) < 0) {
1220 fprintf(stderr,
"System Error [%s]\n", strerror(errno));
1222 (void)read(fds[1], &v,
sizeof(v));
1225 shutdown(fds[1], 2);
1241 fspr_pool_destroy(pool);
1243 if (unlink(pid_path) != 0) {
1244 fprintf(stderr,
"Failed to delete pid file [%s]\n", pid_path);
1248 char buf[1024] = { 0 };
1252 if (!argv || !argv[0] || execv(argv[0], argv) == -1) {
1253 fprintf(stderr,
"Restart Failed [%s] resorting to plan b\n", strerror(errno));
1254 for (j = 0; j < argc; j++) {
1255 switch_snprintf(buf + strlen(buf),
sizeof(buf) - strlen(buf),
"%s ", argv[j]);
int32_t change_user_group(const char *user, const char *group)
Change user and/or group of the running process.
#define SWITCH_FOPEN_TRUNCATE
switch_status_t switch_file_write(switch_file_t *thefile, const void *buf, switch_size_t *nbytes)
static void handle_SIGILL(int sig)
const char * switch_version_full(void)
#define SWITCH_FOPEN_READ
void switch_sleep(switch_interval_time_t t)
int32_t set_low_priority(void)
#define SWITCH_FLOCK_EXCLUSIVE
static void handle_SIGTERM(int sig)
switch_memory_pool_t * pool
static int freeswitch_kill_background(void)
static void reincarnate_protect(char **argv)
void switch_core_set_globals(void)
Initiate Globals.
switch_status_t switch_core_destroy(void)
Destroy the core.
int switch_snprintf(_Out_z_cap_(len) char *buf, _In_ switch_size_t len, _In_z_ _Printf_format_string_ const char *format,...)
uint32_t switch_core_flag_t
static switch_bool_t is_option(const char *p)
static const char usage[]
int32_t set_auto_priority(void)
static void daemonize(int *fds)
#define SWITCH_PATH_SEPARATOR
switch_byte_t switch_byte_t * buf
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.
switch_status_t switch_core_init_and_modload(_In_ switch_core_flag_t flags, _In_ switch_bool_t console, _Out_ const char **err)
Initilize the core and load modules.
#define switch_strlen_zero(x)
void switch_core_setrlimits(void)
#define SWITCH_THREAD_STACKSIZE
#define SWITCH_SYSTEM_THREAD_STACKSIZE
int32_t set_normal_priority(void)
Set the maximum priority the process can obtain.
#define SWITCH_FPROT_UWRITE
switch_filenames SWITCH_GLOBAL_filenames
void switch_core_runtime_loop(int bg)
Run endlessly until the system is shutdown.
char * switch_copy_string(_Out_z_cap_(dst_size) char *dst, _In_z_ const char *src, _In_ switch_size_t dst_size)
#define SWITCH_FPROT_UREAD
switch_status_t switch_file_read(switch_file_t *thefile, void *buf, switch_size_t *nbytes)
switch_directories SWITCH_GLOBAL_dirs
int32_t set_realtime_priority(void)
#define SWITCH_DEFAULT_DIR_PERMS
switch_status_t
Common return values.
switch_status_t switch_dir_make_recursive(const char *path, switch_fileperms_t perm, switch_memory_pool_t *pool)
struct fspr_file_t switch_file_t
#define SWITCH_FLOCK_NONBLOCK
switch_status_t switch_file_lock(switch_file_t *thefile, int type)
struct fspr_pool_t switch_memory_pool_t
switch_status_t switch_file_open(switch_file_t **newf, const char *fname, int32_t flag, switch_fileperms_t perm, switch_memory_pool_t *pool)
int32_t switch_core_set_process_privileges(void)
Switch on the privilege awareness for the process and request required privileges.
#define SWITCH_FOPEN_CREATE
const char * switch_version_revision_human(void)
switch_status_t switch_file_close(switch_file_t *thefile)
#define SWITCH_FOPEN_WRITE