00199 {
00200 char pid_path[256] = "";
00201 char pid_buffer[32] = "";
00202 switch_size_t pid_len;
00203 const char *err = NULL;
00204 #ifndef WIN32
00205 int nf = 0;
00206 char *runas_user = NULL;
00207 char *runas_group = NULL;
00208 #endif
00209 int nc = 0;
00210 pid_t pid = 0;
00211 int x;
00212 int die = 0;
00213 char *usageDesc;
00214 int alt_dirs = 0;
00215 int known_opt;
00216 int high_prio = 0;
00217 switch_core_flag_t flags = SCF_USE_SQL;
00218 int ret;
00219 switch_file_t *fd;
00220 switch_memory_pool_t *pool = NULL;
00221
00222 usageDesc = "these are the optional arguments you can pass to freeswitch\n"
00223 #ifdef WIN32
00224 "\t-service [name] -- start freeswitch as a service, cannot be used if loaded as a console app\n"
00225 "\t-install [name] -- install freeswitch as a service, with optional service name\n"
00226 "\t-uninstall -- remove freeswitch as a service\n"
00227 #else
00228 "\t-nf -- no forking\n"
00229 "\t-u [user] -- specify user to switch to\n"
00230 "\t-g [group] -- specify group to switch to\n"
00231 #endif
00232 "\t-help -- this message\n"
00233 #ifdef HAVE_SETRLIMIT
00234 "\t-core -- dump cores\n"
00235 #endif
00236 "\t-hp -- enable high priority settings\n"
00237 "\t-nosql -- disable internal sql scoreboard\n"
00238 "\t-stop -- stop freeswitch\n"
00239 "\t-nc -- do not output to a console and background\n"
00240 "\t-c -- output to a console and stay in the foreground\n"
00241 "\t-conf [confdir] -- specify an alternate config dir\n"
00242 "\t-log [logdir] -- specify an alternate log dir\n"
00243 "\t-db [dbdir] -- specify an alternate db dir\n"
00244 "\t-scripts [scriptsdir] -- specify an alternate scripts dir\n";
00245
00246 for (x = 1; x < argc; x++) {
00247 known_opt = 0;
00248 #ifdef WIN32
00249 if (x == 1) {
00250 if (argv[x] && !strcmp(argv[x], "-service")) {
00251
00252 x++;
00253 if (argv[x] && strlen(argv[x])) {
00254 switch_copy_string(service_name, argv[x], SERVICENAME_MAXLEN);
00255 } else {
00256 switch_copy_string(service_name, SERVICENAME_DEFAULT, SERVICENAME_MAXLEN);
00257 }
00258 {
00259 SERVICE_TABLE_ENTRY dispatchTable[] = {
00260 {service_name, &service_main},
00261 {NULL, NULL}
00262 };
00263 known_opt++;
00264 if (StartServiceCtrlDispatcher(dispatchTable) == 0) {
00265
00266 fprintf(stderr, "Error Freeswitch loaded as a console app with -service option\n");
00267 fprintf(stderr, "To install the service load freeswitch with -install\n");
00268 }
00269 exit(0);
00270 }
00271 }
00272 if (argv[x] && !strcmp(argv[x], "-install")) {
00273 char exePath[1024];
00274 char servicePath[1024];
00275 x++;
00276 if (argv[x] && strlen(argv[x])) {
00277 switch_copy_string(service_name, argv[x], SERVICENAME_MAXLEN);
00278 } else {
00279 switch_copy_string(service_name, SERVICENAME_DEFAULT, SERVICENAME_MAXLEN);
00280 }
00281 known_opt++;
00282 GetModuleFileName(NULL, exePath, 1024);
00283 snprintf(servicePath, sizeof(servicePath), "%s -service %s", exePath, service_name);
00284 {
00285 SC_HANDLE hService;
00286 SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
00287 if (!hSCManager) {
00288 fprintf(stderr, "Could not open service manager (%d).\n", GetLastError());
00289 exit(1);
00290 }
00291 hService = CreateService(hSCManager,
00292 service_name,
00293 service_name,
00294 GENERIC_READ | GENERIC_EXECUTE | SERVICE_CHANGE_CONFIG,
00295 SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
00296 servicePath, NULL, NULL, NULL,
00297 "NT AUTHORITY\\NetworkService",
00298 NULL);
00299 if (!hService) {
00300 fprintf(stderr, "Error creating freeswitch service (%d).\n", GetLastError());
00301 } else {
00302
00303 SERVICE_DESCRIPTION desc;
00304 desc.lpDescription = "The FreeSWITCH service.";
00305 if (!ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &desc)) {
00306 fprintf(stderr, "FreeSWITCH installed, but could not set the service description (%d).\n", GetLastError());
00307 }
00308 CloseServiceHandle(hService);
00309 }
00310 CloseServiceHandle(hSCManager);
00311 exit(0);
00312 }
00313 }
00314 if (argv[x] && !strcmp(argv[x], "-uninstall")) {
00315 x++;
00316 if (argv[x] && strlen(argv[x])) {
00317 switch_copy_string(service_name, argv[x], SERVICENAME_MAXLEN);
00318 } else {
00319 switch_copy_string(service_name, SERVICENAME_DEFAULT, SERVICENAME_MAXLEN);
00320 }
00321 {
00322 SC_HANDLE hService;
00323 SC_HANDLE hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
00324 if (!hSCManager) {
00325 fprintf(stderr, "Could not open service manager (%d).\n", GetLastError());
00326 exit(1);
00327 }
00328 hService = OpenService(hSCManager, service_name, DELETE);
00329 known_opt++;
00330 if (hService != NULL) {
00331
00332 if (!DeleteService(hService)) {
00333 fprintf(stderr, "Error deleting service (%d).\n", GetLastError());
00334 }
00335 CloseServiceHandle(hService);
00336 } else {
00337 fprintf(stderr, "Error opening service (%d).\n", GetLastError());
00338 }
00339 CloseServiceHandle(hSCManager);
00340 exit(0);
00341 }
00342 }
00343 }
00344
00345 #else
00346 if (argv[x] && !strcmp(argv[x], "-u")) {
00347 x++;
00348 if (argv[x] && strlen(argv[x])) {
00349 runas_user = argv[x];
00350 }
00351 known_opt++;
00352 }
00353
00354 if (argv[x] && !strcmp(argv[x], "-g")) {
00355 x++;
00356 if (argv[x] && strlen(argv[x])) {
00357 runas_group = argv[x];
00358 }
00359 known_opt++;
00360 }
00361
00362 if (argv[x] && !strcmp(argv[x], "-nf")) {
00363 nf++;
00364 known_opt++;
00365 }
00366 #endif
00367 #ifdef HAVE_SETRLIMIT
00368 if (argv[x] && !strcmp(argv[x], "-core")) {
00369 struct rlimit rlp;
00370 memset(&rlp, 0, sizeof(rlp));
00371 rlp.rlim_cur = RLIM_INFINITY;
00372 rlp.rlim_max = RLIM_INFINITY;
00373 setrlimit(RLIMIT_CORE, &rlp);
00374 known_opt++;
00375 }
00376 #endif
00377
00378 if (argv[x] && !strcmp(argv[x], "-hp")) {
00379 high_prio++;
00380 known_opt++;
00381 }
00382
00383 if (argv[x] && !strcmp(argv[x], "-nosql")) {
00384 flags &= ~SCF_USE_SQL;
00385 known_opt++;
00386 }
00387
00388 if (argv[x] && !strcmp(argv[x], "-stop")) {
00389 die++;
00390 known_opt++;
00391 }
00392
00393 if (argv[x] && !strcmp(argv[x], "-nc")) {
00394 nc++;
00395 known_opt++;
00396 }
00397
00398 if (argv[x] && !strcmp(argv[x], "-c")) {
00399 nc = 0;
00400 known_opt++;
00401 }
00402
00403 if (argv[x] && !strcmp(argv[x], "-conf")) {
00404 x++;
00405 if (argv[x] && strlen(argv[x])) {
00406 SWITCH_GLOBAL_dirs.conf_dir = (char *) malloc(strlen(argv[x]) + 1);
00407 if (!SWITCH_GLOBAL_dirs.conf_dir) {
00408 fprintf(stderr, "Allocation error\n");
00409 return 255;
00410 }
00411 strcpy(SWITCH_GLOBAL_dirs.conf_dir, argv[x]);
00412 alt_dirs++;
00413 } else {
00414 fprintf(stderr, "When using -conf you must specify a config directory\n");
00415 return 255;
00416 }
00417 known_opt++;
00418 }
00419
00420 if (argv[x] && !strcmp(argv[x], "-log")) {
00421 x++;
00422 if (argv[x] && strlen(argv[x])) {
00423 SWITCH_GLOBAL_dirs.log_dir = (char *) malloc(strlen(argv[x]) + 1);
00424 if (!SWITCH_GLOBAL_dirs.log_dir) {
00425 fprintf(stderr, "Allocation error\n");
00426 return 255;
00427 }
00428 strcpy(SWITCH_GLOBAL_dirs.log_dir, argv[x]);
00429 alt_dirs++;
00430 } else {
00431 fprintf(stderr, "When using -log you must specify a log directory\n");
00432 return 255;
00433 }
00434 known_opt++;
00435 }
00436
00437 if (argv[x] && !strcmp(argv[x], "-db")) {
00438 x++;
00439 if (argv[x] && strlen(argv[x])) {
00440 SWITCH_GLOBAL_dirs.db_dir = (char *) malloc(strlen(argv[x]) + 1);
00441 if (!SWITCH_GLOBAL_dirs.db_dir) {
00442 fprintf(stderr, "Allocation error\n");
00443 return 255;
00444 }
00445 strcpy(SWITCH_GLOBAL_dirs.db_dir, argv[x]);
00446 alt_dirs++;
00447 } else {
00448 fprintf(stderr, "When using -db you must specify a db directory\n");
00449 return 255;
00450 }
00451 known_opt++;
00452 }
00453
00454 if (argv[x] && !strcmp(argv[x], "-scripts")) {
00455 x++;
00456 if (argv[x] && strlen(argv[x])) {
00457 SWITCH_GLOBAL_dirs.script_dir = (char *) malloc(strlen(argv[x]) + 1);
00458 if (!SWITCH_GLOBAL_dirs.script_dir) {
00459 fprintf(stderr, "Allocation error\n");
00460 return 255;
00461 }
00462 strcpy(SWITCH_GLOBAL_dirs.script_dir, argv[x]);
00463 } else {
00464 fprintf(stderr, "When using -scripts you must specify a scripts directory\n");
00465 return 255;
00466 }
00467 known_opt++;
00468 }
00469
00470 if (!known_opt || (argv[x] && (!strcmp(argv[x], "-help") || !strcmp(argv[x], "-h") || !strcmp(argv[x], "-?")))) {
00471 printf("%s\n", usageDesc);
00472 exit(0);
00473 }
00474 }
00475
00476 if (apr_initialize() != SWITCH_STATUS_SUCCESS) {
00477 fprintf(stderr, "FATAL ERROR! Could not initialize APR\n");
00478 return 255;
00479 }
00480
00481 if (die) {
00482 return freeswitch_kill_background();
00483 }
00484
00485 if (alt_dirs && alt_dirs != 3) {
00486 fprintf(stderr, "You must specify all or none of -conf, -log, and -db\n");
00487 return 255;
00488 }
00489
00490 signal(SIGTERM, handle_SIGTERM);
00491
00492 if (nc) {
00493 #ifdef WIN32
00494 FreeConsole();
00495 #else
00496 if (!nf && (pid = fork())) {
00497 fprintf(stderr, "%d Backgrounding.\n", (int) pid);
00498 exit(0);
00499 }
00500 #endif
00501 }
00502
00503 if (high_prio) {
00504 set_high_priority();
00505 }
00506
00507 switch_core_setrlimits();
00508
00509 #ifndef WIN32
00510 if (runas_user || runas_group) {
00511 if (change_user_group(runas_user, runas_group) < 0) {
00512 fprintf(stderr, "Failed to switch user / group\n" );
00513 return 255;
00514 }
00515 }
00516 #endif
00517
00518 switch_core_set_globals();
00519
00520 pid = getpid();
00521
00522 memset(pid_buffer, 0, sizeof(pid_buffer));
00523 switch_snprintf(pid_path, sizeof(pid_path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile);
00524 switch_snprintf(pid_buffer, sizeof(pid_buffer), "%d", pid);
00525 pid_len = sizeof(pid_buffer);
00526
00527 apr_pool_create(&pool, NULL);
00528 if (switch_file_open(&fd,
00529 pid_path,
00530 SWITCH_FOPEN_WRITE | SWITCH_FOPEN_CREATE,
00531 SWITCH_FPROT_UREAD | SWITCH_FPROT_UWRITE,
00532 pool) != SWITCH_STATUS_SUCCESS) {
00533 fprintf(stderr, "Cannot open pid file %s.\n", pid_path);
00534 return 255;
00535 }
00536
00537 if (switch_file_lock(fd, SWITCH_FLOCK_EXCLUSIVE | SWITCH_FLOCK_NONBLOCK) != SWITCH_STATUS_SUCCESS) {
00538 fprintf(stderr, "Cannot lock pid file %s.\n", pid_path);
00539 return 255;
00540 }
00541
00542 switch_file_write(fd, pid_buffer, &pid_len);
00543
00544 if (switch_core_init_and_modload(flags, nc ? SWITCH_FALSE : SWITCH_TRUE, &err) != SWITCH_STATUS_SUCCESS) {
00545 fprintf(stderr, "Cannot Initialize [%s]\n", err);
00546 return 255;
00547 }
00548
00549 switch_core_runtime_loop(nc);
00550
00551 ret = switch_core_destroy();
00552
00553 switch_file_close(fd);
00554
00555 if (unlink(pid_path) != 0) {
00556 fprintf(stderr, "Failed to delete pid file [%s]\n", pid_path);
00557 }
00558
00559 return ret;
00560 }