📄 asterisk.c
字号:
ast_copy_string(ast_config_AST_CONFIG_DIR, AST_CONFIG_DIR, sizeof(ast_config_AST_CONFIG_DIR)); ast_copy_string(ast_config_AST_SPOOL_DIR, AST_SPOOL_DIR, sizeof(ast_config_AST_SPOOL_DIR)); ast_copy_string(ast_config_AST_MODULE_DIR, AST_MODULE_DIR, sizeof(ast_config_AST_MODULE_DIR)); snprintf(ast_config_AST_MONITOR_DIR, sizeof(ast_config_AST_MONITOR_DIR) - 1, "%s/monitor", ast_config_AST_SPOOL_DIR); ast_copy_string(ast_config_AST_VAR_DIR, AST_VAR_DIR, sizeof(ast_config_AST_VAR_DIR)); ast_copy_string(ast_config_AST_LOG_DIR, AST_LOG_DIR, sizeof(ast_config_AST_LOG_DIR)); ast_copy_string(ast_config_AST_AGI_DIR, AST_AGI_DIR, sizeof(ast_config_AST_AGI_DIR)); ast_copy_string(ast_config_AST_DB, AST_DB, sizeof(ast_config_AST_DB)); ast_copy_string(ast_config_AST_KEY_DIR, AST_KEY_DIR, sizeof(ast_config_AST_KEY_DIR)); ast_copy_string(ast_config_AST_PID, AST_PID, sizeof(ast_config_AST_PID)); ast_copy_string(ast_config_AST_SOCKET, AST_SOCKET, sizeof(ast_config_AST_SOCKET)); ast_copy_string(ast_config_AST_RUN_DIR, AST_RUN_DIR, sizeof(ast_config_AST_RUN_DIR)); /* no asterisk.conf? no problem, use buildtime config! */ if (!cfg) { return; } v = ast_variable_browse(cfg, "files"); while (v) { if (!strcasecmp(v->name, "astctlpermissions")) { ast_copy_string(ast_config_AST_CTL_PERMISSIONS, v->value, sizeof(ast_config_AST_CTL_PERMISSIONS)); } else if (!strcasecmp(v->name, "astctlowner")) { ast_copy_string(ast_config_AST_CTL_OWNER, v->value, sizeof(ast_config_AST_CTL_OWNER)); } else if (!strcasecmp(v->name, "astctlgroup")) { ast_copy_string(ast_config_AST_CTL_GROUP, v->value, sizeof(ast_config_AST_CTL_GROUP)); } else if (!strcasecmp(v->name, "astctl")) { ast_copy_string(ast_config_AST_CTL, v->value, sizeof(ast_config_AST_CTL)); } v = v->next; } v = ast_variable_browse(cfg, "directories"); while(v) { if (!strcasecmp(v->name, "astetcdir")) { ast_copy_string(ast_config_AST_CONFIG_DIR, v->value, sizeof(ast_config_AST_CONFIG_DIR)); } else if (!strcasecmp(v->name, "astspooldir")) { ast_copy_string(ast_config_AST_SPOOL_DIR, v->value, sizeof(ast_config_AST_SPOOL_DIR)); snprintf(ast_config_AST_MONITOR_DIR, sizeof(ast_config_AST_MONITOR_DIR) - 1, "%s/monitor", v->value); } else if (!strcasecmp(v->name, "astvarlibdir")) { ast_copy_string(ast_config_AST_VAR_DIR, v->value, sizeof(ast_config_AST_VAR_DIR)); snprintf(ast_config_AST_DB, sizeof(ast_config_AST_DB), "%s/astdb", v->value); snprintf(ast_config_AST_KEY_DIR, sizeof(ast_config_AST_KEY_DIR), "%s/keys", v->value); } else if (!strcasecmp(v->name, "astlogdir")) { ast_copy_string(ast_config_AST_LOG_DIR, v->value, sizeof(ast_config_AST_LOG_DIR)); } else if (!strcasecmp(v->name, "astagidir")) { ast_copy_string(ast_config_AST_AGI_DIR, v->value, sizeof(ast_config_AST_AGI_DIR)); } else if (!strcasecmp(v->name, "astrundir")) { snprintf(ast_config_AST_PID, sizeof(ast_config_AST_PID), "%s/%s", v->value, "asterisk.pid"); snprintf(ast_config_AST_SOCKET, sizeof(ast_config_AST_SOCKET), "%s/%s", v->value, ast_config_AST_CTL); ast_copy_string(ast_config_AST_RUN_DIR, v->value, sizeof(ast_config_AST_RUN_DIR)); } else if (!strcasecmp(v->name, "astmoddir")) { ast_copy_string(ast_config_AST_MODULE_DIR, v->value, sizeof(ast_config_AST_MODULE_DIR)); } v = v->next; } v = ast_variable_browse(cfg, "options"); while(v) { /* verbose level (-v at startup) */ if (!strcasecmp(v->name, "verbose")) { option_verbose = atoi(v->value); /* whether or not to force timestamping. (-T at startup) */ } else if (!strcasecmp(v->name, "timestamp")) { option_timestamp = ast_true(v->value); /* whether or not to support #exec in config files */ } else if (!strcasecmp(v->name, "execincludes")) { option_exec_includes = ast_true(v->value); /* debug level (-d at startup) */ } else if (!strcasecmp(v->name, "debug")) { option_debug = 0; if (sscanf(v->value, "%d", &option_debug) != 1) { option_debug = ast_true(v->value); } /* Disable forking (-f at startup) */ } else if (!strcasecmp(v->name, "nofork")) { option_nofork = ast_true(v->value); /* Run quietly (-q at startup ) */ } else if (!strcasecmp(v->name, "quiet")) { option_quiet = ast_true(v->value); /* Run as console (-c at startup, implies nofork) */ } else if (!strcasecmp(v->name, "console")) { option_console = ast_true(v->value); /* Run with highg priority if the O/S permits (-p at startup) */ } else if (!strcasecmp(v->name, "highpriority")) { option_highpriority = ast_true(v->value); /* Initialize RSA auth keys (IAX2) (-i at startup) */ } else if (!strcasecmp(v->name, "initcrypto")) { option_initcrypto = ast_true(v->value); /* Disable ANSI colors for console (-c at startup) */ } else if (!strcasecmp(v->name, "nocolor")) { option_nocolor = ast_true(v->value); /* Disable some usage warnings for picky people :p */ } else if (!strcasecmp(v->name, "dontwarn")) { option_dontwarn = ast_true(v->value); /* Dump core in case of crash (-g) */ } else if (!strcasecmp(v->name, "dumpcore")) { option_dumpcore = ast_true(v->value); /* Cache recorded sound files to another directory during recording */ } else if (!strcasecmp(v->name, "cache_record_files")) { option_cache_record_files = ast_true(v->value); /* Specify cache directory */ } else if (!strcasecmp(v->name, "record_cache_dir")) { ast_copy_string(record_cache_dir, v->value, AST_CACHE_DIR_LEN); /* Build transcode paths via SLINEAR, instead of directly */ } else if (!strcasecmp(v->name, "transcode_via_sln")) { option_transcode_slin = ast_true(v->value); /* Transmit SLINEAR silence while a channel is being recorded */ } else if (!strcasecmp(v->name, "transmit_silence_during_record")) { option_transmit_silence_during_record = ast_true(v->value); } else if (!strcasecmp(v->name, "maxcalls")) { if ((sscanf(v->value, "%d", &option_maxcalls) != 1) || (option_maxcalls < 0)) { option_maxcalls = 0; } } else if (!strcasecmp(v->name, "maxload")) { double test[1]; if (getloadavg(test, 1) == -1) { ast_log(LOG_ERROR, "Cannot obtain load average on this system. 'maxload' option disabled.\n"); option_maxload = 0.0; } else if ((sscanf(v->value, "%lf", &option_maxload) != 1) || (option_maxload < 0.0)) { option_maxload = 0.0; } /* What user to run as */ } else if (!strcasecmp(v->name, "runuser")) { ast_copy_string(ast_config_AST_RUN_USER, v->value, sizeof(ast_config_AST_RUN_USER)); /* What group to run as */ } else if (!strcasecmp(v->name, "rungroup")) { ast_copy_string(ast_config_AST_RUN_GROUP, v->value, sizeof(ast_config_AST_RUN_GROUP)); } v = v->next; } ast_config_destroy(cfg);}int main(int argc, char *argv[]){ int c; char filename[80] = ""; char hostname[MAXHOSTNAMELEN]=""; char tmp[80]; char * xarg = NULL; int x; FILE *f; sigset_t sigs; int num; int is_child_of_nonroot=0; char *buf; char *runuser=NULL, *rungroup=NULL; /* Remember original args for restart */ if (argc > sizeof(_argv) / sizeof(_argv[0]) - 1) { fprintf(stderr, "Truncating argument size to %d\n", (int)(sizeof(_argv) / sizeof(_argv[0])) - 1); argc = sizeof(_argv) / sizeof(_argv[0]) - 1; } for (x=0;x<argc;x++) _argv[x] = argv[x]; _argv[x] = NULL; /* if the progname is rasterisk consider it a remote console */ if (argv[0] && (strstr(argv[0], "rasterisk")) != NULL) { option_remote++; option_nofork++; } if (gethostname(hostname, sizeof(hostname)-1)) ast_copy_string(hostname, "<Unknown>", sizeof(hostname)); ast_mainpid = getpid(); ast_ulaw_init(); ast_alaw_init(); callerid_init(); ast_utils_init(); tdd_init(); /* When Asterisk restarts after it has dropped the root privileges, * it can't issue setuid(), setgid(), setgroups() or set_priority() * */ if (getenv("ASTERISK_ALREADY_NONROOT")) is_child_of_nonroot=1; if (getenv("HOME")) snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME")); /* Check if we're root */ /* if (geteuid()) { ast_log(LOG_ERROR, "Must be run as root\n"); exit(1); } */ /* Check for options */ while((c=getopt(argc, argv, "tThfdvVqprRgcinx:U:G:C:L:M:")) != -1) { switch(c) { case 'd': option_debug++; option_nofork++; break; case 'c': option_console++; option_nofork++; break; case 'f': option_nofork++; break; case 'n': option_nocolor++; break; case 'r': option_remote++; option_nofork++; break; case 'R': option_remote++; option_nofork++; option_reconnect++; break; case 'p': option_highpriority++; break; case 'v': option_verbose++; option_nofork++; break; case 'M': if ((sscanf(optarg, "%d", &option_maxcalls) != 1) || (option_maxcalls < 0)) option_maxcalls = 0; break; case 'L': if ((sscanf(optarg, "%lf", &option_maxload) != 1) || (option_maxload < 0.0)) option_maxload = 0.0; break; case 'q': option_quiet++; break; case 't': option_cache_record_files++; break; case 'T': option_timestamp++; break; case 'x': option_exec++; xarg = optarg; break; case 'C': ast_copy_string((char *)ast_config_AST_CONFIG_FILE,optarg,sizeof(ast_config_AST_CONFIG_FILE)); option_overrideconfig++; break; case 'i': option_initcrypto++; break; case'g': option_dumpcore++; break; case 'h': show_cli_help(); exit(0); case 'V': show_version(); exit(0); case 'U': runuser = optarg; break; case 'G': rungroup = optarg; break; case '?': exit(1); } } /* For remote connections, change the name of the remote connection. * We do this for the benefit of init scripts (which need to know if/when * the main asterisk process has died yet). */ if (option_remote) { strcpy(argv[0], "rasterisk"); for (x = 1; x < argc; x++) { argv[x] = argv[0] + 10; } } if (option_console && !option_verbose) ast_verbose("[ Reading Master Configuration ]"); ast_readconfig(); if (option_dumpcore) { struct rlimit l; memset(&l, 0, sizeof(l)); l.rlim_cur = RLIM_INFINITY; l.rlim_max = RLIM_INFINITY; if (setrlimit(RLIMIT_CORE, &l)) { ast_log(LOG_WARNING, "Unable to disable core size resource limit: %s\n", strerror(errno)); } } if ((!rungroup) && !ast_strlen_zero(ast_config_AST_RUN_GROUP)) rungroup = ast_config_AST_RUN_GROUP; if ((!runuser) && !ast_strlen_zero(ast_config_AST_RUN_USER)) runuser = ast_config_AST_RUN_USER;#ifndef __CYGWIN__ if (!is_child_of_nonroot) ast_set_priority(option_highpriority); if (!is_child_of_nonroot && rungroup) { struct group *gr; gr = getgrnam(rungroup); if (!gr) { ast_log(LOG_WARNING, "No such group '%s'!\n", rungroup); exit(1); } if (setgid(gr->gr_gid)) { ast_log(LOG_WARNING, "Unable to setgid to %d (%s)\n", (int)gr->gr_gid, rungroup); exit(1); } if (setgroups(0, NULL)) { ast_log(LOG_WARNING, "Unable to drop unneeded groups\n"); exit(1); } if (option_verbose) ast_verbose("Running as group '%s'\n", rungroup); } if (!is_child_of_nonroot && runuser) { struct passwd *pw; pw = getpwnam(runuser); if (!pw) { ast_log(LOG_WARNING, "No such user '%s'!\n", runuser); exit(1); } if (!rungroup) { if (setgid(pw->pw_gid)) { ast_log(LOG_WARNING, "Unable to setgid to %d!\n", (int)pw->pw_gid); exit(1); } if (initgroups(pw->pw_name, pw->pw_gid)) { ast_log(LOG_WARNING, "Unable to init groups for '%s'\n", runuser); exit(1); } } if (setuid(pw->pw_uid)) { ast_log(LOG_WARNING, "Unable to setuid to %d (%s)\n", (int)pw->pw_uid, runuser); exit(1); } setenv("ASTERISK_ALREADY_NONROOT","yes",1); if (option_verbose) ast_verbose("Running as user '%s'\n", runuser); }#endif /* __CYGWIN__ */#ifdef linux if (geteuid() && option_dumpcore) { if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) < 0) { ast_log(LOG_WARNING, "Unable to set the process for core dumps after changing to a non-root user. %s\n", strerror(errno)); } }#endif term_init(); printf(term_end()); fflush(stdout); if (option_console && !option_verbose) ast_verbose("[ Initializing Custom Configuration Options ]"); /* custom config setup */ register_config_cli(); read_config_maps(); if (option_console) { if (el_hist == NULL || el == NULL) ast_el_initialize(); if (!ast_strlen_zero(filename)) ast_el_read_history(filename); } if (ast_tryconnect()) { /* One is already running */ if (option_remote) { if (option_exec) { ast_remotecontrol(xarg); quit_handler(0, 0, 0, 0); exit(0); } printf(term_quit()); ast_register_verbose(console_verboser); WELCOME_MESSAGE; ast_remotecontrol(NULL); quit_handler(0, 0, 0, 0); exit(0); } else { ast_log(LOG_ERROR, "Asterisk already running on %s. Use 'asterisk -r' to connect.\n", (char *)ast_config_AST_SOCKET); printf(term_quit()); exit(1); } } else if (option_remote || option_exec) { ast_log(LOG_ERROR, "Unable to connect to remote asterisk (does %s exist?)\n",ast_config_AST_SOCKET); printf(term_quit()); exit(1); } /* Blindly write pid file since we couldn't connect */ unlink((char *)ast_config_AST_PID); f = fopen((char *)ast_config_AST_PID, "w"); if (f) { fprintf(f, "%d\n", (int)getpid()); fclose(f); } else ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", (char *)ast_config_AST_PID, strerror(errno)); if (!option_verbose && !option_debug && !option_nofork && !option_console) { daemon(0,0); /* Blindly re-write pid file since we are forking */ unlink((char *)ast_config_AST_PID); f = fopen((char *)ast_config_AST_PID, "w"); if (f) { fprintf(f, "%d\n", (int)getpid()); fclose(f); } else ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", (char *)ast_config_AST_PID, strerror(errno)); ast_mainpid = getpid(); } /* Test recursive mutex locking. */ if (test_for_thread_safety()) ast_verbose("Warning! Asterisk is not thread safe.\n"); ast_makesocket(); sigemptyset(&sigs); sigaddset(&sigs, SIGHUP); sigaddset(&sigs, SIGTERM); sigaddset(&sigs, SIGINT); sigaddset(&sigs, SIGPIPE); sigaddset(&sigs, SIGWINCH); pthread_sigmask(SIG_BLOCK, &sigs, NULL); if (option_console || option_verbose || option_remote) ast_register_verbose(console_verboser); /* Print a welcome message if desired */ if (option_verbose || option_console) { WELCOME_MESSAGE; } if (option_console && !option_verbose) ast_verbose("[ Booting..."); signal(SIGURG, urg_handler); signal(SIGINT, __quit_handler); signal(SIGTERM, __quit_handler); signal(SIGHUP, hup_handler); signal(SIGCHLD, child_handler); signal(SIGPIPE, SIG_IGN); /* ensure that the random number generators are seeded with a different value every time Asterisk is started */ srand((unsigned int) getpid() + (unsigned int) time(NULL)); srandom((unsigned int) getpid() + (unsigned int) time(NULL)); if (init_logger()) { printf(term_quit()); exit(1); } if (dnsmgr_init()) { printf(term_quit()); exit(1); } if (load_modules(1)) { printf(term_qu
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -