[asterisk-commits] mjordan: branch mjordan/trunk-deadlock r376300 - /team/mjordan/trunk-deadlock...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Nov 15 09:24:50 CST 2012


Author: mjordan
Date: Thu Nov 15 09:24:47 2012
New Revision: 376300

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=376300
Log:
Move another check around

This moves the check for an already running process before
the daemon call.

Modified:
    team/mjordan/trunk-deadlock/main/asterisk.c

Modified: team/mjordan/trunk-deadlock/main/asterisk.c
URL: http://svnview.digium.com/svn/asterisk/team/mjordan/trunk-deadlock/main/asterisk.c?view=diff&rev=376300&r1=376299&r2=376300
==============================================================================
--- team/mjordan/trunk-deadlock/main/asterisk.c (original)
+++ team/mjordan/trunk-deadlock/main/asterisk.c Thu Nov 15 09:24:47 2012
@@ -1605,7 +1605,7 @@
 	int res;
 	ast_consock = socket(PF_LOCAL, SOCK_STREAM, 0);
 	if (ast_consock < 0) {
-		ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
+		fprintf(stderr, "Unable to create socket: %s\n", strerror(errno));
 		return 0;
 	}
 	memset(&sunaddr, 0, sizeof(sunaddr));
@@ -2439,7 +2439,7 @@
 				break;
 			if (errno == EINTR)
 				continue;
-			ast_log(LOG_ERROR, "poll failed: %s\n", strerror(errno));
+			fprintf(stderr, "poll failed: %s\n", strerror(errno));
 			break;
 		}
 
@@ -3703,8 +3703,9 @@
 	if (ast_opt_remote && remotesock != NULL)
 		ast_copy_string((char *) cfg_paths.socket_path, remotesock, sizeof(cfg_paths.socket_path));
 
-	if (!ast_language_is_prefix && !ast_opt_remote)
+	if (!ast_language_is_prefix && !ast_opt_remote) {
 		fprintf(stderr, "The 'languageprefix' option in asterisk.conf is deprecated; in a future release it will be removed, and your sound files will need to be organized in the 'new style' language layout.\n");
+	}
 
 	if (ast_opt_always_fork && (ast_opt_remote || ast_opt_console)) {
 		fprintf(stderr, "'alwaysfork' is not compatible with console or remote console mode; ignored\n");
@@ -3879,96 +3880,21 @@
 #endif
 		char dir[PATH_MAX];
 		if (!getcwd(dir, sizeof(dir)) || eaccess(dir, R_OK | X_OK | F_OK)) {
-			ast_log(LOG_ERROR, "Unable to access the running directory (%s).  Changing to '/' for compatibility.\n", strerror(errno));
+			fprintf(stderr, "Unable to access the running directory (%s).  Changing to '/' for compatibility.\n", strerror(errno));
 			/* If we cannot access the CWD, then we couldn't dump core anyway,
 			 * so chdir("/") won't break anything. */
 			if (chdir("/")) {
 				/* chdir(/) should never fail, so this ends up being a no-op */
-				ast_log(LOG_ERROR, "chdir(\"/\") failed?!! %s\n", strerror(errno));
+				fprintf(stderr, "chdir(\"/\") failed?!! %s\n", strerror(errno));
 			}
 		} else
 #endif /* defined(HAVE_EACCESS) || defined(HAVE_EUIDACCESS) */
 		if (!ast_opt_no_fork && !ast_opt_dump_core) {
 			/* Backgrounding, but no cores, so chdir won't break anything. */
 			if (chdir("/")) {
-				ast_log(LOG_ERROR, "Unable to chdir(\"/\") ?!! %s\n", strerror(errno));
-			}
-		}
-	}
-
-#if HAVE_WORKING_FORK
-	if (ast_opt_always_fork || !ast_opt_no_fork) {
-#ifndef HAVE_SBIN_LAUNCHD
-		if (daemon(1, 0) < 0) {
-			fprintf(stderr, "daemon() failed: %s\n", strerror(errno));
-		}
-#else
-		fprintf(stderr, "Mac OS X detected.  Use 'launchctl load /Library/LaunchDaemon/org.asterisk.asterisk.plist'.\n");
-#endif
-	}
-#endif
-
-	/* Spawning of astcanary must happen AFTER the call to daemon(3) */
-	if (isroot && ast_opt_high_priority) {
-		snprintf(canary_filename, sizeof(canary_filename), "%s/alt.asterisk.canary.tweet.tweet.tweet", ast_config_AST_RUN_DIR);
-
-		/* Don't let the canary child kill Asterisk, if it dies immediately */
-		sigaction(SIGPIPE, &ignore_sig_handler, NULL);
-
-		canary_pid = fork();
-		if (canary_pid == 0) {
-			char canary_binary[PATH_MAX], ppid[12];
-
-			/* Reset signal handler */
-			signal(SIGCHLD, SIG_DFL);
-			signal(SIGPIPE, SIG_DFL);
-
-			ast_close_fds_above_n(0);
-			ast_set_priority(0);
-			snprintf(ppid, sizeof(ppid), "%d", (int) ast_mainpid);
-
-			/* Use the astcanary binary that we installed */
-			snprintf(canary_binary, sizeof(canary_binary), "%s/astcanary", ast_config_AST_SBIN_DIR);
-			execl(canary_binary, "astcanary", canary_filename, ppid, (char *)NULL);
-
-			/* Should never happen */
-			_exit(1);
-		} else if (canary_pid > 0) {
-			pthread_t dont_care;
-			ast_pthread_create_detached(&dont_care, NULL, canary_thread, NULL);
-		}
-
-		/* Kill the canary when we exit */
-		ast_register_atexit(canary_exit);
-	}
-
-	ast_term_init();
-	printf("%s", term_end());
-	fflush(stdout);
-
-	if (ast_opt_console || option_verbose || (ast_opt_remote && !ast_opt_exec)) {
-		if (ast_register_verbose(console_verboser)) {
-			fprintf(stderr, "Unable to register console verboser?\n");
-		}
-		WELCOME_MESSAGE;
-	}
-	if (runuser) {
-		ast_verbose("Running as user %s", runuser);
-	}
-
-	if (ast_opt_console && !option_verbose) {
-		ast_verbose("[ Initializing Custom Configuration Options ]\n");
-	}
-	/* custom config setup */
-	register_config_cli();
-	read_config_maps();
-
-	if (ast_opt_console) {
-		if (el_hist == NULL || el == NULL)
-			ast_el_initialize();
-
-		if (!ast_strlen_zero(filename))
-			ast_el_read_history(filename);
+				fprintf(stderr, "Unable to chdir(\"/\") ?!! %s\n", strerror(errno));
+			}
+		}
 	}
 
 	if (ast_tryconnect()) {
@@ -3984,25 +3910,112 @@
 			quit_handler(0, SHUTDOWN_FAST, 0);
 			exit(0);
 		} else {
-			ast_log(LOG_ERROR, "Asterisk already running on %s.  Use 'asterisk -r' to connect.\n", ast_config_AST_SOCKET);
+			fprintf(stderr, "Asterisk already running on %s.  Use 'asterisk -r' to connect.\n", ast_config_AST_SOCKET);
 			printf("%s", term_quit());
 			exit(1);
 		}
 	} else if (ast_opt_remote || ast_opt_exec) {
-		ast_log(LOG_ERROR, "Unable to connect to remote asterisk (does %s exist?)\n", ast_config_AST_SOCKET);
+		fprintf(stderr, "Unable to connect to remote asterisk (does %s exist?)\n", ast_config_AST_SOCKET);
 		printf("%s", term_quit());
 		exit(1);
 	}
 
-	/* Blindly write pid file since we couldn't connect */
+	/* This needs to remain as high up in the initial start up as possible.
+	 * daemon causes a fork to occur, which has all sorts of unintended
+	 * consequences for things that interact with threads.  This call *must*
+	 * occur before anything in Asterisk spawns or manipulates thread related
+	 * primitives.
+	 */
+#if HAVE_WORKING_FORK
+	if (ast_opt_always_fork || !ast_opt_no_fork) {
+#ifndef HAVE_SBIN_LAUNCHD
+		if (daemon(1, 0) < 0) {
+			fprintf(stderr, "daemon() failed: %s\n", strerror(errno));
+		}
+#else
+		fprintf(stderr, "Mac OS X detected.  Use 'launchctl load /Library/LaunchDaemon/org.asterisk.asterisk.plist'.\n");
+#endif
+	}
+#endif
+
+	/* Spawning of astcanary must happen AFTER the call to daemon(3) */
+	if (isroot && ast_opt_high_priority) {
+		snprintf(canary_filename, sizeof(canary_filename), "%s/alt.asterisk.canary.tweet.tweet.tweet", ast_config_AST_RUN_DIR);
+
+		/* Don't let the canary child kill Asterisk, if it dies immediately */
+		sigaction(SIGPIPE, &ignore_sig_handler, NULL);
+
+		canary_pid = fork();
+		if (canary_pid == 0) {
+			char canary_binary[PATH_MAX], ppid[12];
+
+			/* Reset signal handler */
+			signal(SIGCHLD, SIG_DFL);
+			signal(SIGPIPE, SIG_DFL);
+
+			ast_close_fds_above_n(0);
+			ast_set_priority(0);
+			snprintf(ppid, sizeof(ppid), "%d", (int) ast_mainpid);
+
+			/* Use the astcanary binary that we installed */
+			snprintf(canary_binary, sizeof(canary_binary), "%s/astcanary", ast_config_AST_SBIN_DIR);
+			execl(canary_binary, "astcanary", canary_filename, ppid, (char *)NULL);
+
+			/* Should never happen */
+			_exit(1);
+		} else if (canary_pid > 0) {
+			pthread_t dont_care;
+			ast_pthread_create_detached(&dont_care, NULL, canary_thread, NULL);
+		}
+
+		/* Kill the canary when we exit */
+		ast_register_atexit(canary_exit);
+	}
+
+	/* At this point everything has been forked successfully and
+	 * we have determined that we aren't attempting to connect to
+	 * an Asterisk instance, and that there isn't one already running.
+	 * Blindly write the PID file. */
 	unlink(ast_config_AST_PID);
 	f = fopen(ast_config_AST_PID, "w");
 	if (f) {
 		fprintf(f, "%ld\n", (long)getpid());
 		fclose(f);
-	} else
-		ast_log(LOG_WARNING, "Unable to open pid file '%s': %s\n", ast_config_AST_PID, strerror(errno));
-
+	} else {
+		fprintf(stderr, "Unable to open pid file '%s': %s\n", ast_config_AST_PID, strerror(errno));
+	}
+
+	/* Initialize the terminal.  Since all processes have been forked,
+	 * we can now start using the standard log messages.
+	 */
+	ast_term_init();
+	printf("%s", term_end());
+	fflush(stdout);
+
+	if (ast_opt_console || option_verbose || (ast_opt_remote && !ast_opt_exec)) {
+		if (ast_register_verbose(console_verboser)) {
+			fprintf(stderr, "Unable to register console verboser?\n");
+		}
+		WELCOME_MESSAGE;
+	}
+	if (runuser) {
+		ast_verbose("Running as user '%s'", runuser);
+	}
+
+	if (ast_opt_console && !option_verbose) {
+		ast_verbose("[ Initializing Custom Configuration Options ]\n");
+	}
+	/* custom config setup */
+	register_config_cli();
+	read_config_maps();
+
+	if (ast_opt_console) {
+		if (el_hist == NULL || el == NULL)
+			ast_el_initialize();
+
+		if (!ast_strlen_zero(filename))
+			ast_el_read_history(filename);
+	}
 
 	ast_ulaw_init();
 	ast_alaw_init();




More information about the asterisk-commits mailing list