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 <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 dependency graph for asterisk.c:

Include dependency graph

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 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 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]


Define Documentation

#define AST_MAX_CONNECTS   128
 

Definition at line 56 of file asterisk.c.

#define ASTERISK_PROMPT   "*CLI> "
 

Definition at line 809 of file asterisk.c.

#define ASTERISK_PROMPT2   "%s*CLI> "
 

Definition at line 811 of file asterisk.c.

#define NUM_MSGS   64
 

Definition at line 57 of file asterisk.c.


Function Documentation

void ast_console_puts const char *  string  ) 
 

Definition at line 216 of file asterisk.c.

References string.

Referenced by ast_log().

00217 { 00218 fputs(string, stdout); 00219 fflush(stdout); 00220 ast_network_puts(string); 00221 }

AST_MUTEX_DEFINE_STATIC atexitslock   ) 
 

int ast_register_atexit void(*  func)(void)  ) 
 

Definition at line 121 of file asterisk.c.

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

00122 { 00123 int res = -1; 00124 struct ast_atexit *ae; 00125 ast_unregister_atexit(func); 00126 ae = malloc(sizeof(struct ast_atexit)); 00127 ast_mutex_lock(&atexitslock); 00128 if (ae) { 00129 memset(ae, 0, sizeof(struct ast_atexit)); 00130 ae->next = atexits; 00131 ae->func = func; 00132 atexits = ae; 00133 res = 0; 00134 } 00135 ast_mutex_unlock(&atexitslock); 00136 return res; 00137 }

int ast_safe_system const char *  s  ) 
 

Safely spawn an external program while closingn file descriptors.

Definition at line 163 of file asterisk.c.

References ast_log(), LOG_WARNING, and s.

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

void ast_unregister_atexit void(*  func)(void)  ) 
 

Definition at line 139 of file asterisk.c.

References ast_mutex_lock, and ast_mutex_unlock.

Referenced by ast_register_atexit().

00140 { 00141 struct ast_atexit *ae, *prev = NULL; 00142 ast_mutex_lock(&atexitslock); 00143 ae = atexits; 00144 while(ae) { 00145 if (ae->func == func) { 00146 if (prev) 00147 prev->next = ae->next; 00148 else 00149 atexits = ae->next; 00150 break; 00151 } 00152 prev = ae; 00153 ae = ae->next; 00154 } 00155 ast_mutex_unlock(&atexitslock); 00156 }

int main int  argc,
char *  argv[]
 

Definition at line 1446 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_image_init(), ast_log(), ast_mainpid, ast_register_verbose(), ast_rtp_init(), ast_startuptime, ast_ulaw_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_remote, option_verbose, poll(), tdd_init(), term_color(), term_end(), term_init(), and term_quit().

01447 { 01448 int c; 01449 char filename[80] = ""; 01450 char hostname[256]; 01451 char tmp[80]; 01452 char * xarg = NULL; 01453 int x; 01454 FILE *f; 01455 sigset_t sigs; 01456 int num; 01457 char *buf; 01458 01459 /* Remember original args for restart */ 01460 if (argc > sizeof(_argv) / sizeof(_argv[0]) - 1) { 01461 fprintf(stderr, "Truncating argument size to %d\n", sizeof(_argv) / sizeof(_argv[0]) - 1); 01462 argc = sizeof(_argv) / sizeof(_argv[0]) - 1; 01463 } 01464 for (x=0;x<argc;x++) 01465 _argv[x] = argv[x]; 01466 _argv[x] = NULL; 01467 01468 /* if the progname is rasterisk consider it a remote console */ 01469 if ( argv[0] && (strstr(argv[0], "rasterisk")) != NULL) { 01470 option_remote++; 01471 option_nofork++; 01472 } 01473 if (gethostname(hostname, sizeof(hostname))) 01474 strncpy(hostname, "<Unknown>", sizeof(hostname)-1); 01475 ast_mainpid = getpid(); 01476 ast_ulaw_init(); 01477 ast_alaw_init(); 01478 callerid_init(); 01479 tdd_init(); 01480 if (getenv("HOME")) 01481 snprintf(filename, sizeof(filename), "%s/.asterisk_history", getenv("HOME")); 01482 /* Check if we're root */ 01483 /* 01484 if (geteuid()) { 01485 ast_log(LOG_ERROR, "Must be run as root\n"); 01486 exit(1); 01487 } 01488 */ 01489 /* Check for options */ 01490 while((c=getopt(argc, argv, "hfdvqprgcinx:C:")) != -1) { 01491 switch(c) { 01492 case 'd': 01493 option_debug++; 01494 option_nofork++; 01495 break; 01496 case 'c': 01497 option_console++; 01498 option_nofork++; 01499 break; 01500 case 'f': 01501 option_nofork++; 01502 break; 01503 case 'n': 01504 option_nocolor++; 01505 break; 01506 case 'r': 01507 option_remote++; 01508 option_nofork++; 01509 break; 01510 case 'p': 01511 option_highpriority++; 01512 break; 01513 case 'v': 01514 option_verbose++; 01515 option_nofork++; 01516 break; 01517 case 'q': 01518 option_quiet++; 01519 break; 01520 case 'x': 01521 option_exec++; 01522 xarg = optarg; 01523 break; 01524 case 'C': 01525 strncpy((char *)ast_config_AST_CONFIG_FILE,optarg,sizeof(ast_config_AST_CONFIG_FILE)); 01526 option_overrideconfig++; 01527 break; 01528 case 'i': 01529 option_initcrypto++; 01530 break; 01531 case'g': 01532 option_dumpcore++; 01533 break; 01534 case 'h': 01535 show_cli_help(); 01536 exit(0); 01537 case '?': 01538 exit(1); 01539 } 01540 } 01541 01542 if (option_dumpcore) { 01543 struct rlimit l; 01544 memset(&l, 0, sizeof(l)); 01545 l.rlim_cur = RLIM_INFINITY; 01546 l.rlim_max = RLIM_INFINITY; 01547 if (setrlimit(RLIMIT_CORE, &l)) { 01548 ast_log(LOG_WARNING, "Unable to disable core size resource limit: %s\n", strerror(errno)); 01549 } 01550 } 01551 01552 term_init(); 01553 printf(term_end()); 01554 fflush(stdout); 01555 if (option_console && !option_verbose) 01556 ast_verbose("[ Reading Master Configuration ]"); 01557 ast_readconfig(); 01558 01559 if (option_console) { 01560 if (el_hist == NULL || el == NULL) 01561 ast_el_initialize(); 01562 01563 if (!ast_strlen_zero(filename)) 01564 ast_el_read_history(filename); 01565 } 01566 01567 if (ast_tryconnect()) { 01568 /* One is already running */ 01569 if (option_remote) { 01570 if (option_exec) { 01571 ast_remotecontrol(xarg); 01572 quit_handler(0, 0, 0, 0); 01573 exit(0); 01574 } 01575 printf(term_quit()); 01576 ast_register_verbose(console_verboser); 01577 ast_verbose( "Asterisk " ASTERISK_VERSION ", Copyright (C) 1999-2004 Digium.\n"); 01578 ast_verbose( "Written by Mark Spencer <markster@digium.com>\n"); 01579 ast_verbose( "=========================================================================\n"); 01580 ast_remotecontrol(NULL); 01581 quit_handler(0, 0, 0, 0); 01582 exit(0); 01583 } else { 01584 ast_log(LOG_ERROR, "Asterisk already running on %s. Use 'asterisk -r' to connect.\n", (char *)ast_config_AST_SOCKET); 01585 printf(term_quit()); 01586 exit(1); 01587 } 01588 } else if (option_remote || option_exec) { 01589 ast_log(LOG_ERROR, "Unable to connect to remote asterisk\n"); 01590 printf(term_quit()); 01591 exit(1); 01592 } 01593 /* Blindly write pid file since we couldn't connect */ 01594 unlink((char *)ast_config_AST_PID); 01595 f = fopen((char *)ast_config_AST_PID, "w"); 01596 if (f) { 01597 fprintf(f, "%d\n", getpid()); 01598 fclose(f); 01599 } else 01600 ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", (char *)ast_config_AST_PID, strerror(errno)); 01601 01602 if (!option_verbose && !option_debug && !option_nofork && !option_console) { 01603 daemon(0,0); 01604 /* Blindly re-write pid file since we are forking */ 01605 unlink((char *)ast_config_AST_PID); 01606 f = fopen((char *)ast_config_AST_PID, "w"); 01607 if (f) { 01608 fprintf(f, "%d\n", getpid()); 01609 fclose(f); 01610 } else 01611 ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", (char *)ast_config_AST_PID, strerror(errno)); 01612 } 01613 01614 ast_makesocket(); 01615 sigemptyset(&sigs); 01616 sigaddset(&sigs, SIGHUP); 01617 sigaddset(&sigs, SIGTERM); 01618 sigaddset(&sigs, SIGINT); 01619 sigaddset(&sigs, SIGPIPE); 01620 sigaddset(&sigs, SIGWINCH); 01621 pthread_sigmask(SIG_BLOCK, &sigs, NULL); 01622 if (option_console || option_verbose || option_remote) 01623 ast_register_verbose(console_verboser); 01624 /* Print a welcome message if desired */ 01625 if (option_verbose || option_console) { 01626 ast_verbose( "Asterisk " ASTERISK_VERSION ", Copyright (C) 1999-2004 Digium.\n"); 01627 ast_verbose( "Written by Mark Spencer <markster@digium.com>\n"); 01628 ast_verbose( "=========================================================================\n"); 01629 } 01630 if (option_console && !option_verbose) 01631 ast_verbose("[ Booting..."); 01632 01633 signal(SIGURG, urg_handler); 01634 signal(SIGINT, __quit_handler); 01635 signal(SIGTERM, __quit_handler); 01636 signal(SIGHUP, hup_handler); 01637 signal(SIGCHLD, child_handler); 01638 signal(SIGPIPE, SIG_IGN); 01639 01640 if (set_priority(option_highpriority)) { 01641 printf(term_quit()); 01642 exit(1); 01643 } 01644 if (init_logger()) { 01645 printf(term_quit()); 01646 exit(1); 01647 } 01648 if (init_manager()) { 01649 printf(term_quit()); 01650 exit(1); 01651 } 01652 ast_rtp_init(); 01653 if (ast_image_init()) { 01654 printf(term_quit()); 01655 exit(1); 01656 } 01657 if (load_pbx()) { 01658 printf(term_quit()); 01659 exit(1); 01660 } 01661 if (load_modules()) { 01662 printf(term_quit()); 01663 exit(1); 01664 } 01665 if (init_framer()) { 01666 printf(term_quit()); 01667 exit(1); 01668 } 01669 if (astdb_init()) { 01670 printf(term_quit()); 01671 exit(1); 01672 } 01673 if (ast_enum_init()) { 01674 printf(term_quit()); 01675 exit(1); 01676 } 01677 /* We might have the option of showing a console, but for now just 01678 do nothing... */ 01679 if (option_console && !option_verbose) 01680 ast_verbose(" ]\n"); 01681 if (option_verbose || option_console) 01682 ast_verbose(term_color(tmp, "Asterisk Ready.\n", COLOR_BRWHITE, COLOR_BLACK, sizeof(tmp))); 01683 if (option_nofork) 01684 consolethread = pthread_self(); 01685 fully_booted = 1; 01686 pthread_sigmask(SIG_UNBLOCK, &sigs, NULL); 01687 #ifdef __AST_DEBUG_MALLOC 01688 __ast_mm_init(); 01689 #endif 01690 time(&ast_startuptime); 01691 ast_cli_register(&astshutdownnow); 01692 ast_cli_register(&astshutdowngracefully); 01693 ast_cli_register(&astrestartnow); 01694 ast_cli_register(&astrestartgracefully); 01695 ast_cli_register(&astrestartwhenconvenient); 01696 ast_cli_register(&astshutdownwhenconvenient); 01697 ast_cli_register(&aborthalt); 01698 ast_cli_register(&astbang); 01699 if (option_console) { 01700 /* Console stuff now... */ 01701 /* Register our quit function */ 01702 char title[256]; 01703 set_icon("Asterisk"); 01704 snprintf(title, sizeof(title), "Asterisk Console on '%s' (pid %d)", hostname, ast_mainpid); 01705 set_title(title); 01706 ast_cli_register(&quit); 01707 ast_cli_register(&astexit); 01708 01709 for (;;) { 01710 buf = (char *)el_gets(el, &num); 01711 if (buf) { 01712 if (buf[strlen(buf)-1] == '\n') 01713 buf[strlen(buf)-1] = '\0'; 01714 01715 consolehandler((char *)buf); 01716 } else { 01717 if (option_remote) 01718 ast_cli(STDOUT_FILENO, "\nUse EXIT or QUIT to exit the asterisk console\n"); 01719 } 01720 } 01721 01722 } else { 01723 /* Do nothing */ 01724 for(;;) 01725 poll(NULL,0, -1); 01726 } 01727 return 0; 01728 }


Variable Documentation

char ast_config_AST_AGI_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 109 of file asterisk.c.

char ast_config_AST_CONFIG_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 103 of file asterisk.c.

char ast_config_AST_CONFIG_FILE[AST_CONFIG_MAX_PATH]
 

Definition at line 104 of file asterisk.c.

Referenced by main().

char ast_config_AST_DB[AST_CONFIG_MAX_PATH]
 

Definition at line 110 of file asterisk.c.

char ast_config_AST_KEY_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 111 of file asterisk.c.

char ast_config_AST_LOG_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 108 of file asterisk.c.

Referenced by init_logger(), and reload_logger().

char ast_config_AST_MODULE_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 105 of file asterisk.c.

Referenced by ast_load_resource(), and load_modules().

char ast_config_AST_PID[AST_CONFIG_MAX_PATH]
 

Definition at line 112 of file asterisk.c.

Referenced by main().

char ast_config_AST_RUN_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 114 of file asterisk.c.

char ast_config_AST_SOCKET[AST_CONFIG_MAX_PATH]
 

Definition at line 113 of file asterisk.c.

Referenced by main().

char ast_config_AST_SPOOL_DIR[AST_CONFIG_MAX_PATH]
 

Definition at line 106 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 107 of file asterisk.c.

Referenced by ast_linear_stream().

time_t ast_lastreloadtime
 

Definition at line 89 of file asterisk.c.

Referenced by ast_module_reload().

int ast_mainpid
 

Definition at line 75 of file asterisk.c.

Referenced by main().

time_t ast_startuptime
 

Definition at line 88 of file asterisk.c.

Referenced by main().

struct console consoles[AST_MAX_CONNECTS]
 

Definition at line 95 of file asterisk.c.

char defaultlanguage[MAX_LANGUAGE] = DEFAULT_LANGUAGE
 

Definition at line 97 of file asterisk.c.

Referenced by ast_channel_alloc().

int fully_booted = 0
 

Definition at line 71 of file asterisk.c.

Referenced by ast_load_resource(), and main().

int option_console = 0
 

Definition at line 63 of file asterisk.c.

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

int option_debug = 0
 

Definition at line 60 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 69 of file asterisk.c.

Referenced by main().

int option_exec = 0
 

Definition at line 66 of file asterisk.c.

Referenced by main().

int option_highpriority = 0
 

Definition at line 64 of file asterisk.c.

Referenced by main().

int option_initcrypto = 0
 

Definition at line 67 of file asterisk.c.

Referenced by main().

int option_nocolor
 

Definition at line 68 of file asterisk.c.

Referenced by main(), and term_init().

int option_nofork = 0
 

Definition at line 61 of file asterisk.c.

Referenced by main(), and term_init().

int option_overrideconfig = 0
 

Definition at line 70 of file asterisk.c.

Referenced by main().

int option_quiet = 0
 

Definition at line 62 of file asterisk.c.

Referenced by load_modules(), and main().

int option_remote = 0
 

Definition at line 65 of file asterisk.c.

Referenced by main().

int option_verbose = 0
 

Definition at line 59 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 Sat Jun 12 16:41:03 2004 for Asterisk by doxygen 1.3.7