Newer
Older
if (ast_tryconnect()) {
/* One is already running */
if (option_remote) {
if (option_exec) {
ast_remotecontrol(xarg);
ast_log(LOG_ERROR, "Asterisk already running on %s. Use 'asterisk -r' to connect.\n", (char *)ast_config_AST_SOCKET);
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);
/* 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", 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) {
/* 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", getpid());
fclose(f);
} else
ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", (char *)ast_config_AST_PID, strerror(errno));
/* 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);
if (option_console && !option_verbose)
ast_verbose("[ Booting...");
signal(SIGINT, __quit_handler);
signal(SIGTERM, __quit_handler);
signal(SIGCHLD, child_handler);
signal(SIGPIPE, SIG_IGN);
Kevin P. Fleming
committed
/* 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));
Kevin P. Fleming
committed
/* load 'preload' modules, required for access to Realtime-mapped configuration files */
if (load_modules(1)) {
printf(term_quit());
exit(1);
}
ast_channels_init();
if (init_manager()) {
printf(term_quit());
exit(1);
}
if (ast_cdr_engine_init()) {
printf(term_quit());
exit(1);
}
Kevin P. Fleming
committed
if (ast_device_state_engine_init()) {
printf(term_quit());
exit(1);
}
if (ast_image_init()) {
printf(term_quit());
Mark Spencer
committed
if (ast_file_init()) {
printf(term_quit());
exit(1);
}
Kevin P. Fleming
committed
if (load_modules(0)) {
}
if (init_framer()) {
printf(term_quit());
if (astdb_init()) {
printf(term_quit());
exit(1);
}
if (ast_enum_init()) {
printf(term_quit());
exit(1);
}
if (dnsmgr_init()) {
printf(term_quit());
exit(1);
}
#if 0
/* This should no longer be necessary */
/* sync cust config and reload some internals in case a custom config handler binded to them */
read_ast_cust_config();
reload_manager();
ast_enum_reload();
ast_rtp_reload();
#endif
Anthony Minessale II
committed
/* We might have the option of showing a console, but for now just
do nothing... */
if (option_console && !option_verbose)
ast_verbose(" ]\n");
if (option_verbose || option_console)
ast_verbose(term_color(tmp, "Asterisk Ready.\n", COLOR_BRWHITE, COLOR_BLACK, sizeof(tmp)));
if (option_nofork)
consolethread = pthread_self();
Mark Spencer
committed
#ifdef __AST_DEBUG_MALLOC
__ast_mm_init();
#endif
Kevin P. Fleming
committed
ast_cli_register_multiple(core_cli, sizeof(core_cli) / sizeof(core_cli[0]));
if (option_console) {
/* Console stuff now... */
/* Register our quit function */
Mark Spencer
committed
snprintf(title, sizeof(title), "Asterisk Console on '%s' (pid %d)", hostname, ast_mainpid);
for (;;) {
buf = (char *)el_gets(el, &num);
if (buf) {
if (buf[strlen(buf)-1] == '\n')
buf[strlen(buf)-1] = '\0';
consolehandler((char *)buf);
} else {
if (write(STDOUT_FILENO, "\nUse EXIT or QUIT to exit the asterisk console\n",
strlen("\nUse EXIT or QUIT to exit the asterisk console\n")) < 0) {
/* Whoa, stdout disappeared from under us... Make /dev/null's */
int fd;
fd = open("/dev/null", O_RDWR);
if (fd > -1) {
dup2(fd, STDOUT_FILENO);
dup2(fd, STDIN_FILENO);
} else
ast_log(LOG_WARNING, "Failed to open /dev/null to recover from dead console. Bad things will happen!\n");
break;
}
for(;;) { /* apparently needed for the MACos */
struct pollfd p = { -1 /* no descriptor */, 0, 0 };
poll(&p, 0, -1);
}