01489 {
01490
int c;
01491
char filename[80] =
"";
01492
char hostname[256];
01493
char tmp[80];
01494
char * xarg = NULL;
01495
int x;
01496 FILE *f;
01497 sigset_t sigs;
01498
int num;
01499
char *buf;
01500
01501
01502
if (argc >
sizeof(_argv) /
sizeof(_argv[0]) - 1) {
01503 fprintf(stderr,
"Truncating argument size to %d\n", (
int)(
sizeof(_argv) /
sizeof(_argv[0])) - 1);
01504 argc =
sizeof(_argv) /
sizeof(_argv[0]) - 1;
01505 }
01506
for (x=0;x<argc;x++)
01507 _argv[x] = argv[x];
01508 _argv[x] = NULL;
01509
01510
01511
if ( argv[0] && (strstr(argv[0],
"rasterisk")) != NULL) {
01512
option_remote++;
01513
option_nofork++;
01514 }
01515
if (gethostname(hostname,
sizeof(hostname)))
01516 strncpy(hostname,
"<Unknown>",
sizeof(hostname)-1);
01517
ast_mainpid = getpid();
01518
ast_ulaw_init();
01519
ast_alaw_init();
01520
callerid_init();
01521
ast_utils_init();
01522
tdd_init();
01523
if (getenv(
"HOME"))
01524 snprintf(filename,
sizeof(filename),
"%s/.asterisk_history", getenv(
"HOME"));
01525
01526
while((c=getopt(argc, argv,
"hfdvqprRgcinx:C:")) != -1) {
01527
switch(c) {
01528
case 'd':
01529
option_debug++;
01530
option_nofork++;
01531
break;
01532
case 'c':
01533
option_console++;
01534
option_nofork++;
01535
break;
01536
case 'f':
01537
option_nofork++;
01538
break;
01539
case 'n':
01540
option_nocolor++;
01541
break;
01542
case 'r':
01543
option_remote++;
01544
option_nofork++;
01545
break;
01546
case 'R':
01547
option_remote++;
01548
option_nofork++;
01549
option_reconnect++;
01550
break;
01551
case 'p':
01552
option_highpriority++;
01553
break;
01554
case 'v':
01555
option_verbose++;
01556
option_nofork++;
01557
break;
01558
case 'q':
01559
option_quiet++;
01560
break;
01561
case 'x':
01562
option_exec++;
01563 xarg = optarg;
01564
break;
01565
case 'C':
01566 strncpy((
char *)ast_config_AST_CONFIG_FILE,optarg,
sizeof(ast_config_AST_CONFIG_FILE) - 1);
01567
option_overrideconfig++;
01568
break;
01569
case 'i':
01570
option_initcrypto++;
01571
break;
01572
case'g':
01573
option_dumpcore++;
01574
break;
01575
case 'h':
01576 show_cli_help();
01577 exit(0);
01578
case '?':
01579 exit(1);
01580 }
01581 }
01582
01583
if (
option_dumpcore) {
01584
struct rlimit l;
01585 memset(&l, 0,
sizeof(l));
01586 l.rlim_cur = RLIM_INFINITY;
01587 l.rlim_max = RLIM_INFINITY;
01588
if (setrlimit(RLIMIT_CORE, &l)) {
01589
ast_log(LOG_WARNING,
"Unable to disable core size resource limit: %s\n", strerror(errno));
01590 }
01591 }
01592
01593
term_init();
01594 printf(
term_end());
01595 fflush(stdout);
01596
01597
01598
if (
test_for_thread_safety())
01599
ast_verbose(
"Warning! Asterisk is not thread safe.\n");
01600
01601
if (
option_console && !
option_verbose)
01602
ast_verbose(
"[ Reading Master Configuration ]");
01603 ast_readconfig();
01604
01605
if (
option_console && !
option_verbose)
01606
ast_verbose(
"[ Initializing Custom Configuration Options]");
01607
01608
register_config_cli();
01609
read_ast_cust_config();
01610
01611
01612
if (
option_console) {
01613
if (el_hist == NULL || el == NULL)
01614 ast_el_initialize();
01615
01616
if (!ast_strlen_zero(filename))
01617 ast_el_read_history(filename);
01618 }
01619
01620
if (ast_tryconnect()) {
01621
01622
if (
option_remote) {
01623
if (
option_exec) {
01624 ast_remotecontrol(xarg);
01625 quit_handler(0, 0, 0, 0);
01626 exit(0);
01627 }
01628 printf(
term_quit());
01629
ast_register_verbose(console_verboser);
01630
WELCOME_MESSAGE;
01631 ast_remotecontrol(NULL);
01632 quit_handler(0, 0, 0, 0);
01633 exit(0);
01634 }
else {
01635
ast_log(LOG_ERROR,
"Asterisk already running on %s. Use 'asterisk -r' to connect.\n", (
char *)ast_config_AST_SOCKET);
01636 printf(
term_quit());
01637 exit(1);
01638 }
01639 }
else if (
option_remote ||
option_exec) {
01640
ast_log(LOG_ERROR,
"Unable to connect to remote asterisk\n");
01641 printf(
term_quit());
01642 exit(1);
01643 }
01644
01645 unlink((
char *)ast_config_AST_PID);
01646 f = fopen((
char *)ast_config_AST_PID,
"w");
01647
if (f) {
01648 fprintf(f,
"%d\n", getpid());
01649 fclose(f);
01650 }
else
01651
ast_log(LOG_WARNING,
"Unable to open pid file '%s': %s\n", (
char *)ast_config_AST_PID, strerror(errno));
01652
01653
if (!
option_verbose && !
option_debug && !
option_nofork && !
option_console) {
01654 daemon(0,0);
01655
01656 unlink((
char *)ast_config_AST_PID);
01657 f = fopen((
char *)ast_config_AST_PID,
"w");
01658
if (f) {
01659 fprintf(f,
"%d\n", getpid());
01660 fclose(f);
01661 }
else
01662
ast_log(LOG_WARNING,
"Unable to open pid file '%s': %s\n", (
char *)ast_config_AST_PID, strerror(errno));
01663 }
01664
01665 ast_makesocket();
01666 sigemptyset(&sigs);
01667 sigaddset(&sigs, SIGHUP);
01668 sigaddset(&sigs, SIGTERM);
01669 sigaddset(&sigs, SIGINT);
01670 sigaddset(&sigs, SIGPIPE);
01671 sigaddset(&sigs, SIGWINCH);
01672 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
01673
if (
option_console ||
option_verbose ||
option_remote)
01674
ast_register_verbose(console_verboser);
01675
01676
if (
option_verbose ||
option_console) {
01677
WELCOME_MESSAGE;
01678 }
01679
if (
option_console && !
option_verbose)
01680
ast_verbose(
"[ Booting...");
01681
01682 signal(SIGURG, urg_handler);
01683 signal(SIGINT, __quit_handler);
01684 signal(SIGTERM, __quit_handler);
01685 signal(SIGHUP, hup_handler);
01686 signal(SIGCHLD, child_handler);
01687 signal(SIGPIPE, SIG_IGN);
01688
01689
if (set_priority(option_highpriority)) {
01690 printf(
term_quit());
01691 exit(1);
01692 }
01693
if (
init_logger()) {
01694 printf(
term_quit());
01695 exit(1);
01696 }
01697
if (
init_manager()) {
01698 printf(
term_quit());
01699 exit(1);
01700 }
01701
ast_rtp_init();
01702
if (
ast_image_init()) {
01703 printf(
term_quit());
01704 exit(1);
01705 }
01706
if (
ast_file_init()) {
01707 printf(
term_quit());
01708 exit(1);
01709 }
01710
if (
load_pbx()) {
01711 printf(
term_quit());
01712 exit(1);
01713 }
01714
if (
load_modules()) {
01715 printf(
term_quit());
01716 exit(1);
01717 }
01718
if (
init_framer()) {
01719 printf(
term_quit());
01720 exit(1);
01721 }
01722
if (
astdb_init()) {
01723 printf(
term_quit());
01724 exit(1);
01725 }
01726
if (
ast_enum_init()) {
01727 printf(
term_quit());
01728 exit(1);
01729 }
01730
01731
reload_logger(0);
01732
01733
01734
01735
if (
option_console && !
option_verbose)
01736
ast_verbose(
" ]\n");
01737
if (
option_verbose ||
option_console)
01738
ast_verbose(
term_color(tmp,
"Asterisk Ready.\n", COLOR_BRWHITE, COLOR_BLACK,
sizeof(tmp)));
01739
if (
option_nofork)
01740 consolethread = pthread_self();
01741
fully_booted = 1;
01742 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
01743
#ifdef __AST_DEBUG_MALLOC
01744
__ast_mm_init();
01745
#endif
01746
time(&ast_startuptime);
01747
ast_cli_register(&astshutdownnow);
01748
ast_cli_register(&astshutdowngracefully);
01749
ast_cli_register(&astrestartnow);
01750
ast_cli_register(&astrestartgracefully);
01751
ast_cli_register(&astrestartwhenconvenient);
01752
ast_cli_register(&astshutdownwhenconvenient);
01753
ast_cli_register(&aborthalt);
01754
ast_cli_register(&astbang);
01755
if (
option_console) {
01756
01757
01758
char title[256];
01759 set_icon(
"Asterisk");
01760 snprintf(title,
sizeof(title),
"Asterisk Console on '%s' (pid %d)", hostname, ast_mainpid);
01761 set_title(title);
01762
ast_cli_register(&quit);
01763
ast_cli_register(&astexit);
01764
01765
for (;;) {
01766 buf = (
char *)el_gets(el, &num);
01767
if (buf) {
01768
if (buf[strlen(buf)-1] ==
'\n')
01769 buf[strlen(buf)-1] =
'\0';
01770
01771 consolehandler((
char *)buf);
01772 }
else {
01773
if (
option_remote)
01774
ast_cli(STDOUT_FILENO,
"\nUse EXIT or QUIT to exit the asterisk console\n");
01775 }
01776 }
01777
01778 }
else {
01779
01780
for(;;)
01781
poll(NULL,0, -1);
01782 }
01783
return 0;
01784 }