Skip to content
Snippets Groups Projects
asterisk.c 57 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    	if (ast_tryconnect()) {
    		/* One is already running */
    		if (option_remote) {
    			if (option_exec) {
    				ast_remotecontrol(xarg);
    
    Mark Spencer's avatar
    Mark Spencer committed
    				quit_handler(0, 0, 0, 0);
    
    Mark Spencer's avatar
    Mark Spencer committed
    				exit(0);
    			}
    
    Mark Spencer's avatar
    Mark Spencer committed
    			printf(term_quit());
    
    Mark Spencer's avatar
    Mark Spencer committed
    			ast_register_verbose(console_verboser);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			WELCOME_MESSAGE;
    
    Mark Spencer's avatar
    Mark Spencer committed
    			ast_remotecontrol(NULL);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			quit_handler(0, 0, 0, 0);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			exit(0);
    		} else {
    
    Mark Spencer's avatar
    Mark Spencer committed
    			ast_log(LOG_ERROR, "Asterisk already running on %s.  Use 'asterisk -r' to connect.\n", (char *)ast_config_AST_SOCKET);
    
    Mark Spencer's avatar
    Mark Spencer committed
    			printf(term_quit());
    
    Mark Spencer's avatar
    Mark Spencer committed
    			exit(1);
    		}
    	} else if (option_remote || option_exec) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_log(LOG_ERROR, "Unable to connect to remote asterisk (does %s exist?)\n",ast_config_AST_SOCKET);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		printf(term_quit());
    
    Mark Spencer's avatar
    Mark Spencer committed
    		exit(1);
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Blindly write pid file since we couldn't connect */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	unlink((char *)ast_config_AST_PID);
    	f = fopen((char *)ast_config_AST_PID, "w");
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (f) {
    		fprintf(f, "%d\n", getpid());
    		fclose(f);
    	} else
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", (char *)ast_config_AST_PID, strerror(errno));
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    	if (!option_verbose && !option_debug && !option_nofork && !option_console) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		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", getpid());
    			fclose(f);
    		} else
    			ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", (char *)ast_config_AST_PID, strerror(errno));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	/* Test recursive mutex locking. */
    	if (test_for_thread_safety())
    		ast_verbose("Warning! Asterisk is not thread safe.\n");
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    	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);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* Print a welcome message if desired */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (option_verbose || option_console) {
    
    Mark Spencer's avatar
    Mark Spencer committed
    		WELCOME_MESSAGE;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (option_console && !option_verbose) 
    		ast_verbose("[ Booting...");
    
    Mark Spencer's avatar
    Mark Spencer committed
    	signal(SIGURG, urg_handler);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	signal(SIGINT, __quit_handler);
    	signal(SIGTERM, __quit_handler);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	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));
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (init_logger()) {
    		printf(term_quit());
    
    Mark Spencer's avatar
    Mark Spencer committed
    		exit(1);
    
    	/* load 'preload' modules, required for access to Realtime-mapped configuration files */
    	if (load_modules(1)) {
    		printf(term_quit());
    		exit(1);
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (init_manager()) {
    		printf(term_quit());
    		exit(1);
    	}
    
    	if (ast_cdr_engine_init()) {
    		printf(term_quit());
    		exit(1);
    	}
    
    	if (ast_device_state_engine_init()) {
    		printf(term_quit());
    		exit(1);
    	}
    
    	ast_rtp_init();
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (ast_image_init()) {
    		printf(term_quit());
    
    Mark Spencer's avatar
    Mark Spencer committed
    		exit(1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (load_pbx()) {
    		printf(term_quit());
    
    Mark Spencer's avatar
    Mark Spencer committed
    		exit(1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		printf(term_quit());
    
    Mark Spencer's avatar
    Mark Spencer committed
    		exit(1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    	if (init_framer()) {
    		printf(term_quit());
    
    Mark Spencer's avatar
    Mark Spencer committed
    		exit(1);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	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_logger(0);
    
    James Golovich's avatar
    James Golovich committed
    	reload_manager();
    	ast_enum_reload();
    	ast_rtp_reload();
    
    Mark Spencer's avatar
    Mark Spencer committed
    	/* We might have the option of showing a console, but for now just
    	   do nothing... */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (option_console && !option_verbose)
    		ast_verbose(" ]\n");
    	if (option_verbose || option_console)
    
    Mark Spencer's avatar
    Mark Spencer committed
    		ast_verbose(term_color(tmp, "Asterisk Ready.\n", COLOR_BRWHITE, COLOR_BLACK, sizeof(tmp)));
    
    James Golovich's avatar
    James Golovich committed
    	if (option_nofork)
    		consolethread = pthread_self();
    
    Mark Spencer's avatar
    Mark Spencer committed
    	fully_booted = 1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    	pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
    
    Mark Spencer's avatar
    Mark Spencer committed
    	time(&ast_startuptime);
    
    	ast_cli_register_multiple(core_cli, sizeof(core_cli) / sizeof(core_cli[0]));
    
    Mark Spencer's avatar
    Mark Spencer committed
    	if (option_console) {
    		/* Console stuff now... */
    		/* Register our quit function */
    
    Mark Spencer's avatar
    Mark Spencer committed
    		char title[256];
    		set_icon("Asterisk");
    
    		snprintf(title, sizeof(title), "Asterisk Console on '%s' (pid %d)", hostname, ast_mainpid);
    
    Mark Spencer's avatar
    Mark Spencer committed
    		set_title(title);
    
    		for (;;) {
    			buf = (char *)el_gets(el, &num);
    			if (buf) {
    				if (buf[strlen(buf)-1] == '\n')
    					buf[strlen(buf)-1] = '\0';
    
    				consolehandler((char *)buf);
    
    				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;
    				}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	}
    
    	for(;;)  {	/* apparently needed for the MACos */
    		struct pollfd p = { -1 /* no descriptor */, 0, 0 };
    		poll(&p, 0, -1);
    	}
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return 0;
    }