Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

asterisk.c File Reference

#include <unistd.h>
#include <stdlib.h>
#include <sys/poll.h>
#include <asterisk/logger.h>
#include <asterisk/options.h>
#include <asterisk/cli.h>
#include <asterisk/channel.h>
#include <asterisk/ulaw.h>
#include <asterisk/alaw.h>
#include <asterisk/callerid.h>
#include <asterisk/module.h>
#include <asterisk/image.h>
#include <asterisk/tdd.h>
#include <asterisk/term.h>
#include <asterisk/manager.h>
#include <asterisk/pbx.h>
#include <asterisk/enum.h>
#include <asterisk/rtp.h>
#include <asterisk/app.h>
#include <asterisk/lock.h>
#include <asterisk/utils.h>
#include <asterisk/file.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
#include <sched.h>
#include <asterisk/io.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "editline/histedit.h"
#include "asterisk.h"
#include <asterisk/config.h>
#include <asterisk/config_pvt.h>

Go to the source code of this file.

Data Structures

struct  console
struct  ast_atexit

Defines

#define AST_MAX_CONNECTS   128
#define NUM_MSGS   64
#define WELCOME_MESSAGE
#define ASTERISK_PROMPT   "*CLI> "
#define ASTERISK_PROMPT2   "%s*CLI> "

Functions

 AST_MUTEX_DEFINE_STATIC (atexitslock)
int ast_register_atexit (void(*func)(void))
void ast_unregister_atexit (void(*func)(void))
int ast_safe_system (const char *s)
 Safely spawn an external program while closingn file descriptors.
void ast_console_puts (const char *string)
int main (int argc, char *argv[])

Variables

int option_verbose = 0
int option_debug = 0
int option_nofork = 0
int option_quiet = 0
int option_console = 0
int option_highpriority = 0
int option_remote = 0
int option_exec = 0
int option_initcrypto = 0
int option_nocolor
int option_dumpcore = 0
int option_overrideconfig = 0
int option_reconnect = 0
int fully_booted = 0
int ast_mainpid
time_t ast_startuptime
time_t ast_lastreloadtime
console consoles [AST_MAX_CONNECTS]
char defaultlanguage [MAX_LANGUAGE] = DEFAULT_LANGUAGE
char ast_config_AST_CONFIG_DIR [AST_CONFIG_MAX_PATH]
char ast_config_AST_CONFIG_FILE [AST_CONFIG_MAX_PATH]
char ast_config_AST_MODULE_DIR [AST_CONFIG_MAX_PATH]
char ast_config_AST_SPOOL_DIR [AST_CONFIG_MAX_PATH]
char ast_config_AST_VAR_DIR [AST_CONFIG_MAX_PATH]
char ast_config_AST_LOG_DIR [AST_CONFIG_MAX_PATH]
char ast_config_AST_AGI_DIR [AST_CONFIG_MAX_PATH]
char ast_config_AST_DB [AST_CONFIG_MAX_PATH]
char ast_config_AST_KEY_DIR [AST_CONFIG_MAX_PATH]
char ast_config_AST_PID [AST_CONFIG_MAX_PATH]
char ast_config_AST_SOCKET [AST_CONFIG_MAX_PATH]
char ast_config_AST_RUN_DIR [AST_CONFIG_MAX_PATH]
char ast_config_AST_DATA_DIR [AST_CONFIG_MAX_PATH]


Define Documentation

#define AST_MAX_CONNECTS   128
 

Definition at line 59 of file asterisk.c.

#define ASTERISK_PROMPT   "*CLI> "
 

Definition at line 825 of file asterisk.c.

#define ASTERISK_PROMPT2   "%s*CLI> "
 

Definition at line 827 of file asterisk.c.

#define NUM_MSGS   64
 

Definition at line 60 of file asterisk.c.

#define WELCOME_MESSAGE
 

Value:

ast_verbose( "Asterisk " ASTERISK_VERSION ", Copyright (C) 1999-2004 Digium.\n"); \ ast_verbose( "Written by Mark Spencer <markster@digium.com>\n"); \ ast_verbose( "=========================================================================\n")
Definition at line 62 of file asterisk.c.

Referenced by main().


Function Documentation

void ast_console_puts const char *  string  ) 
 

Definition at line 225 of file asterisk.c.

References string.

Referenced by ast_log().

00226 { 00227 fputs(string, stdout); 00228 fflush(stdout); 00229 ast_network_puts(string); 00230 }

AST_MUTEX_DEFINE_STATIC atexitslock   ) 
 

int ast_register_atexit void(*  func)(void)  ) 
 

Definition at line 130 of file asterisk.c.

References ast_mutex_lock, ast_mutex_unlock, ast_unregister_atexit(), and malloc.

00131 { 00132 int res = -1; 00133 struct ast_atexit *ae; 00134 ast_unregister_atexit(func); 00135 ae = malloc(sizeof(struct ast_atexit)); 00136 ast_mutex_lock(&atexitslock); 00137 if (ae) { 00138 memset(ae, 0, sizeof(struct ast_atexit)); 00139 ae->next = atexits; 00140 ae->func = func; 00141 atexits = ae; 00142 res = 0; 00143 } 00144 ast_mutex_unlock(&atexitslock); 00145 return res; 00146 }

int ast_safe_system const char *  s  ) 
 

Safely spawn an external program while closingn file descriptors.

Definition at line 172 of file asterisk.c.

References ast_log(), LOG_WARNING, and s.

00173 { 00174 /* XXX This function needs some optimization work XXX */ 00175 pid_t pid; 00176 int x; 00177 int res; 00178 struct rusage rusage; 00179 int status; 00180 pid = fork(); 00181 if (pid == 0) { 00182 /* Close file descriptors and launch system command */ 00183 for (x=STDERR_FILENO + 1; x<4096;x++) { 00184 close(x); 00185 } 00186 res = execl("/bin/sh", "/bin/sh", "-c", s, NULL); 00187 exit(1); 00188 } else if (pid > 0) { 00189 for(;;) { 00190 res = wait4(pid, &status, 0, &rusage); 00191 if (res > -1) { 00192 if (WIFEXITED(status)) 00193 res = WEXITSTATUS(status); 00194 else 00195 res = -1; 00196 break; 00197 } else { 00198 if (errno != EINTR) 00199 break; 00200 } 00201 } 00202 } else { 00203 ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno)); 00204 res = -1; 00205 } 00206 return res; 00207 }

void ast_unregister_atexit void(*  func)(void)  ) 
 

Definition at line 148 of file asterisk.c.

References ast_mutex_lock, and ast_mutex_unlock.

Referenced by ast_register_atexit().

00149 { 00150 struct ast_atexit *ae, *prev = NULL; 00151 ast_mutex_lock(&atexitslock); 00152 ae = atexits; 00153 while(ae) { 00154 if (ae->func == func) { 00155 if (prev) 00156 prev->next = ae->next; 00157 else 00158 atexits = ae->next; 00159 break; 00160 } 00161 prev = ae; 00162 ae = ae->next; 00163 } 00164 ast_mutex_unlock(&atexitslock); 00165 }

int main int  argc,
char *  argv[]
 

Definition at line 1488 of file asterisk.c.

References __ast_mm_init(), ast_alaw_init(), ast_cli(), ast_cli_register(), ast_config_AST_CONFIG_FILE, ast_config_AST_PID, ast_config_AST_SOCKET, ast_enum_init(), ast_file_init(), ast_image_init(), ast_log(), ast_mainpid, ast_register_verbose(), ast_rtp_init(), ast_startuptime, ast_ulaw_init(), ast_utils_init(), ast_verbose(), astdb_init(), callerid_init(), COLOR_BLACK, COLOR_BRWHITE, fully_booted, init_framer(), init_logger(), init_manager(), load_modules(), load_pbx(), LOG_ERROR, LOG_WARNING, option_console, option_debug, option_dumpcore, option_exec, option_highpriority, option_initcrypto, option_nocolor, option_nofork, option_overrideconfig, option_quiet, option_reconnect, option_remote, option_verbose, poll(), read_ast_cust_config(), register_config_cli(), reload_logger(), tdd_init(), term_color(), term_end(), term_init(), term_quit(), test_for_thread_safety(), and WELCOME_MESSAGE.

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 /* Remember original args for restart */ 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 /* if the progname is rasterisk consider it a remote console */ 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 /* Check for options */ 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 /* Test recursive mutex locking. */ 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 /* custom config setup */ 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 /* One is already running */ 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 /* Blindly write pid file since we couldn't connect */ 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 /* Blindly re-write pid file since we are forking */ 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 /* Print a welcome message if desired */ 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 /* reload logger in case a custom config handler binded to logger.conf*/ 01731 reload_logger(0); 01732 01733 /* We might have the option of showing a console, but for now just 01734 do nothing... */ 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 /* Console stuff now... */ 01757 /* Register our quit function */ 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 /* Do nothing */ 01780 for(;;) 01781 poll(NULL,0, -1); 01782 } 01783 return 0; 01784 }


Variable Documentation

char ast_config_AST_AGI_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 117 of file asterisk.c.

char ast_config_AST_CONFIG_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 111 of file asterisk.c.

char ast_config_AST_CONFIG_FILE[AST_CONFIG_MAX_PATH]
 

Definition at line 112 of file asterisk.c.

Referenced by main().

char ast_config_AST_DATA_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 123 of file asterisk.c.

char ast_config_AST_DB[AST_CONFIG_MAX_PATH]
 

Definition at line 118 of file asterisk.c.

char ast_config_AST_KEY_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 119 of file asterisk.c.

char ast_config_AST_LOG_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 116 of file asterisk.c.

Referenced by init_logger(), and reload_logger().

char ast_config_AST_MODULE_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 113 of file asterisk.c.

Referenced by ast_load_resource(), and load_modules().

char ast_config_AST_PID[AST_CONFIG_MAX_PATH]
 

Definition at line 120 of file asterisk.c.

Referenced by main().

char ast_config_AST_RUN_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 122 of file asterisk.c.

char ast_config_AST_SOCKET[AST_CONFIG_MAX_PATH]
 

Definition at line 121 of file asterisk.c.

Referenced by main().

char ast_config_AST_SPOOL_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 114 of file asterisk.c.

Referenced by ast_app_has_voicemail(), and ast_app_messagecount().

char ast_config_AST_VAR_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 115 of file asterisk.c.

Referenced by ast_linear_stream().

time_t ast_lastreloadtime
 

Definition at line 97 of file asterisk.c.

Referenced by ast_module_reload().

int ast_mainpid
 

Definition at line 83 of file asterisk.c.

Referenced by main().

time_t ast_startuptime
 

Definition at line 96 of file asterisk.c.

Referenced by main().

struct console consoles[AST_MAX_CONNECTS]
 

Definition at line 103 of file asterisk.c.

char defaultlanguage[MAX_LANGUAGE] = DEFAULT_LANGUAGE
 

Definition at line 105 of file asterisk.c.

Referenced by ast_channel_alloc().

int fully_booted = 0
 

Definition at line 79 of file asterisk.c.

Referenced by ast_load_resource(), and main().

int option_console = 0
 

Definition at line 70 of file asterisk.c.

Referenced by ast_load_resource(), main(), and term_init().

int option_debug = 0
 

Definition at line 67 of file asterisk.c.

Referenced by ast_channel_register_ex(), ast_channel_unregister(), ast_context_create(), ast_hangup(), ast_log(), ast_pbx_run(), ast_rtcp_read(), ast_save(), ast_set_read_format(), ast_set_write_format(), ast_softhangup_nolock(), load_modules(), and main().

int option_dumpcore = 0
 

Definition at line 76 of file asterisk.c.

Referenced by main().

int option_exec = 0
 

Definition at line 73 of file asterisk.c.

Referenced by main().

int option_highpriority = 0
 

Definition at line 71 of file asterisk.c.

Referenced by main().

int option_initcrypto = 0
 

Definition at line 74 of file asterisk.c.

Referenced by main().

int option_nocolor
 

Definition at line 75 of file asterisk.c.

Referenced by main(), and term_init().

int option_nofork = 0
 

Definition at line 68 of file asterisk.c.

Referenced by main(), and term_init().

int option_overrideconfig = 0
 

Definition at line 77 of file asterisk.c.

Referenced by main().

int option_quiet = 0
 

Definition at line 69 of file asterisk.c.

Referenced by load_modules(), and main().

int option_reconnect = 0
 

Definition at line 78 of file asterisk.c.

Referenced by main().

int option_remote = 0
 

Definition at line 72 of file asterisk.c.

Referenced by main().

int option_verbose = 0
 

Definition at line 66 of file asterisk.c.

Referenced by ast_cdr_unregister(), ast_channel_bridge(), ast_channel_register_ex(), ast_channel_unregister(), ast_context_add_include2(), ast_context_add_switch2(), ast_context_create(), ast_format_register(), ast_format_unregister(), ast_image_register(), ast_image_unregister(), ast_load_resource(), ast_log(), ast_manager_unregister(), ast_module_reload(), ast_pbx_outgoing_app(), ast_pbx_outgoing_exten(), ast_pbx_run(), ast_register_application(), ast_register_indication_country(), ast_register_translator(), ast_rtp_reload(), ast_save(), ast_set_indication_country(), ast_streamfile(), ast_unregister_application(), ast_unregister_indication_country(), ast_unregister_translator(), init_logger(), init_manager(), load_modules(), load_pbx(), main(), pbx_builtin_setvar_helper(), and reload_logger().


Generated on Tue Aug 17 16:13:54 2004 for Asterisk by doxygen 1.3.8