01572 {
01573
int c;
01574
char filename[80] =
"";
01575
char hostname[256];
01576
char tmp[80];
01577
char * xarg = NULL;
01578
int x;
01579 FILE *f;
01580 sigset_t sigs;
01581
int num;
01582
char *buf;
01583
char *runuser=NULL, *rungroup=NULL;
01584
01585
01586
if (argc >
sizeof(_argv) /
sizeof(_argv[0]) - 1) {
01587 fprintf(stderr,
"Truncating argument size to %d\n", (
int)(
sizeof(_argv) /
sizeof(_argv[0])) - 1);
01588 argc =
sizeof(_argv) /
sizeof(_argv[0]) - 1;
01589 }
01590
for (x=0;x<argc;x++)
01591 _argv[x] = argv[x];
01592 _argv[x] = NULL;
01593
01594
01595
if ( argv[0] && (strstr(argv[0],
"rasterisk")) != NULL) {
01596
option_remote++;
01597
option_nofork++;
01598 }
01599
if (gethostname(hostname,
sizeof(hostname)))
01600 strncpy(hostname,
"<Unknown>",
sizeof(hostname)-1);
01601
ast_mainpid = getpid();
01602
ast_ulaw_init();
01603
ast_alaw_init();
01604
callerid_init();
01605
ast_utils_init();
01606
tdd_init();
01607
if (getenv(
"HOME"))
01608 snprintf(filename,
sizeof(filename),
"%s/.asterisk_history", getenv(
"HOME"));
01609
01610
while((c=getopt(argc, argv,
"thfdvVqprRgcinx:U:G:C:")) != -1) {
01611
switch(c) {
01612
case 'd':
01613
option_debug++;
01614
option_nofork++;
01615
break;
01616
case 'c':
01617
option_console++;
01618
option_nofork++;
01619
break;
01620
case 'f':
01621
option_nofork++;
01622
break;
01623
case 'n':
01624
option_nocolor++;
01625
break;
01626
case 'r':
01627
option_remote++;
01628
option_nofork++;
01629
break;
01630
case 'R':
01631
option_remote++;
01632
option_nofork++;
01633
option_reconnect++;
01634
break;
01635
case 'p':
01636
option_highpriority++;
01637
break;
01638
case 'v':
01639
option_verbose++;
01640
option_nofork++;
01641
break;
01642
case 'q':
01643
option_quiet++;
01644
break;
01645
case 't':
01646
option_cache_record_files++;
01647
break;
01648
case 'x':
01649
option_exec++;
01650 xarg = optarg;
01651
break;
01652
case 'C':
01653 strncpy((
char *)ast_config_AST_CONFIG_FILE,optarg,
sizeof(ast_config_AST_CONFIG_FILE) - 1);
01654
option_overrideconfig++;
01655
break;
01656
case 'i':
01657
option_initcrypto++;
01658
break;
01659
case'g':
01660
option_dumpcore++;
01661
break;
01662
case 'h':
01663 show_cli_help();
01664 exit(0);
01665
case 'V':
01666 show_version();
01667 exit(0);
01668
case 'U':
01669 runuser = optarg;
01670
break;
01671
case 'G':
01672 rungroup = optarg;
01673
break;
01674
case '?':
01675 exit(1);
01676 }
01677 }
01678
01679
if (
option_dumpcore) {
01680
struct rlimit l;
01681 memset(&l, 0,
sizeof(l));
01682 l.rlim_cur = RLIM_INFINITY;
01683 l.rlim_max = RLIM_INFINITY;
01684
if (setrlimit(RLIMIT_CORE, &l)) {
01685
ast_log(LOG_WARNING,
"Unable to disable core size resource limit: %s\n", strerror(errno));
01686 }
01687 }
01688
01689
if (rungroup) {
01690
struct group *gr;
01691 gr = getgrnam(rungroup);
01692
if (!gr) {
01693
ast_log(LOG_WARNING,
"No such group '%s'!\n", rungroup);
01694 exit(1);
01695 }
01696
if (setgid(gr->gr_gid)) {
01697
ast_log(LOG_WARNING,
"Unable to setgid to %d (%s)\n", gr->gr_gid, rungroup);
01698 exit(1);
01699 }
01700
if (
option_verbose)
01701
ast_verbose(
"Running as group '%s'\n", rungroup);
01702 }
01703
01704
if (set_priority(option_highpriority)) {
01705 exit(1);
01706 }
01707
if (runuser) {
01708
struct passwd *pw;
01709 pw = getpwnam(runuser);
01710
if (!pw) {
01711
ast_log(LOG_WARNING,
"No such user '%s'!\n", runuser);
01712 exit(1);
01713 }
01714
if (setuid(pw->pw_uid)) {
01715
ast_log(LOG_WARNING,
"Unable to setuid to %d (%s)\n", pw->pw_uid, runuser);
01716 exit(1);
01717 }
01718
if (
option_verbose)
01719
ast_verbose(
"Running as user '%s'\n", runuser);
01720 }
01721
01722
term_init();
01723 printf(
term_end());
01724 fflush(stdout);
01725
01726
01727
if (
test_for_thread_safety())
01728
ast_verbose(
"Warning! Asterisk is not thread safe.\n");
01729
01730
if (
option_console && !
option_verbose)
01731
ast_verbose(
"[ Reading Master Configuration ]");
01732 ast_readconfig();
01733
01734
if (
option_console && !
option_verbose)
01735
ast_verbose(
"[ Initializing Custom Configuration Options]");
01736
01737
register_config_cli();
01738
read_ast_cust_config();
01739
01740
01741
if (
option_console) {
01742
if (el_hist == NULL || el == NULL)
01743 ast_el_initialize();
01744
01745
if (!ast_strlen_zero(filename))
01746 ast_el_read_history(filename);
01747 }
01748
01749
if (ast_tryconnect()) {
01750
01751
if (
option_remote) {
01752
if (
option_exec) {
01753 ast_remotecontrol(xarg);
01754 quit_handler(0, 0, 0, 0);
01755 exit(0);
01756 }
01757 printf(
term_quit());
01758
ast_register_verbose(console_verboser);
01759
WELCOME_MESSAGE;
01760 ast_remotecontrol(NULL);
01761 quit_handler(0, 0, 0, 0);
01762 exit(0);
01763 }
else {
01764
ast_log(LOG_ERROR,
"Asterisk already running on %s. Use 'asterisk -r' to connect.\n", (
char *)ast_config_AST_SOCKET);
01765 printf(
term_quit());
01766 exit(1);
01767 }
01768 }
else if (
option_remote ||
option_exec) {
01769
ast_log(LOG_ERROR,
"Unable to connect to remote asterisk\n");
01770 printf(
term_quit());
01771 exit(1);
01772 }
01773
01774 unlink((
char *)ast_config_AST_PID);
01775 f = fopen((
char *)ast_config_AST_PID,
"w");
01776
if (f) {
01777 fprintf(f,
"%d\n", getpid());
01778 fclose(f);
01779 }
else
01780
ast_log(LOG_WARNING,
"Unable to open pid file '%s': %s\n", (
char *)ast_config_AST_PID, strerror(errno));
01781
01782
if (!
option_verbose && !
option_debug && !
option_nofork && !
option_console) {
01783 daemon(0,0);
01784
01785 unlink((
char *)ast_config_AST_PID);
01786 f = fopen((
char *)ast_config_AST_PID,
"w");
01787
if (f) {
01788 fprintf(f,
"%d\n", getpid());
01789 fclose(f);
01790 }
else
01791
ast_log(LOG_WARNING,
"Unable to open pid file '%s': %s\n", (
char *)ast_config_AST_PID, strerror(errno));
01792 }
01793
01794 ast_makesocket();
01795 sigemptyset(&sigs);
01796 sigaddset(&sigs, SIGHUP);
01797 sigaddset(&sigs, SIGTERM);
01798 sigaddset(&sigs, SIGINT);
01799 sigaddset(&sigs, SIGPIPE);
01800 sigaddset(&sigs, SIGWINCH);
01801 pthread_sigmask(SIG_BLOCK, &sigs, NULL);
01802
if (
option_console ||
option_verbose ||
option_remote)
01803
ast_register_verbose(console_verboser);
01804
01805
if (
option_verbose ||
option_console) {
01806
WELCOME_MESSAGE;
01807 }
01808
if (
option_console && !
option_verbose)
01809
ast_verbose(
"[ Booting...");
01810
01811 signal(SIGURG, urg_handler);
01812 signal(SIGINT, __quit_handler);
01813 signal(SIGTERM, __quit_handler);
01814 signal(SIGHUP, hup_handler);
01815 signal(SIGCHLD, child_handler);
01816 signal(SIGPIPE, SIG_IGN);
01817
01818
if (
init_logger()) {
01819 printf(
term_quit());
01820 exit(1);
01821 }
01822
if (
init_manager()) {
01823 printf(
term_quit());
01824 exit(1);
01825 }
01826
ast_rtp_init();
01827
if (
ast_image_init()) {
01828 printf(
term_quit());
01829 exit(1);
01830 }
01831
if (
ast_file_init()) {
01832 printf(
term_quit());
01833 exit(1);
01834 }
01835
if (
load_pbx()) {
01836 printf(
term_quit());
01837 exit(1);
01838 }
01839
if (
load_modules()) {
01840 printf(
term_quit());
01841 exit(1);
01842 }
01843
if (
init_framer()) {
01844 printf(
term_quit());
01845 exit(1);
01846 }
01847
if (
astdb_init()) {
01848 printf(
term_quit());
01849 exit(1);
01850 }
01851
if (
ast_enum_init()) {
01852 printf(
term_quit());
01853 exit(1);
01854 }
01855
01856
read_ast_cust_config();
01857
reload_logger(0);
01858
reload_manager();
01859
ast_enum_reload();
01860
ast_rtp_reload();
01861
01862
01863
01864
01865
if (
option_console && !
option_verbose)
01866
ast_verbose(
" ]\n");
01867
if (
option_verbose ||
option_console)
01868
ast_verbose(
term_color(tmp,
"Asterisk Ready.\n", COLOR_BRWHITE, COLOR_BLACK,
sizeof(tmp)));
01869
if (
option_nofork)
01870 consolethread = pthread_self();
01871
fully_booted = 1;
01872 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
01873
#ifdef __AST_DEBUG_MALLOC
01874
__ast_mm_init();
01875
#endif
01876
time(&ast_startuptime);
01877
ast_cli_register(&astshutdownnow);
01878
ast_cli_register(&astshutdowngracefully);
01879
ast_cli_register(&astrestartnow);
01880
ast_cli_register(&astrestartgracefully);
01881
ast_cli_register(&astrestartwhenconvenient);
01882
ast_cli_register(&astshutdownwhenconvenient);
01883
ast_cli_register(&aborthalt);
01884
ast_cli_register(&astbang);
01885
if (
option_console) {
01886
01887
01888
char title[256];
01889 set_icon(
"Asterisk");
01890 snprintf(title,
sizeof(title),
"Asterisk Console on '%s' (pid %d)", hostname, ast_mainpid);
01891 set_title(title);
01892
ast_cli_register(&quit);
01893
ast_cli_register(&astexit);
01894
01895
for (;;) {
01896 buf = (
char *)el_gets(el, &num);
01897
if (buf) {
01898
if (buf[strlen(buf)-1] ==
'\n')
01899 buf[strlen(buf)-1] =
'\0';
01900
01901 consolehandler((
char *)buf);
01902 }
else {
01903
if (
option_remote)
01904
ast_cli(STDOUT_FILENO,
"\nUse EXIT or QUIT to exit the asterisk console\n");
01905 }
01906 }
01907
01908 }
else {
01909
01910
for(;;)
01911
poll(NULL,0, -1);
01912 }
01913
return 0;
01914 }