[svn-commits] russell: trunk r56506 - in /trunk: ./ main/asterisk.c

svn-commits at lists.digium.com svn-commits at lists.digium.com
Fri Feb 23 16:25:22 MST 2007


Author: russell
Date: Fri Feb 23 17:25:22 2007
New Revision: 56506

URL: http://svn.digium.com/view/asterisk?view=rev&rev=56506
Log:
Merged revisions 56505 via svnmerge from 
https://origsvn.digium.com/svn/asterisk/branches/1.4

................
r56505 | russell | 2007-02-23 17:24:18 -0600 (Fri, 23 Feb 2007) | 16 lines

Merged revisions 56504 via svnmerge from 
https://origsvn.digium.com/svn/asterisk/branches/1.2

........
r56504 | russell | 2007-02-23 17:20:55 -0600 (Fri, 23 Feb 2007) | 8 lines

Fix up a couple more signal handlers to not do bad things that could cause
various undesirable results.  The other day, I made Asterisk deadlock by
hitting Control-C because of a bad signal handler.  Now, signal handlers
just set a flag and write to an alert pipe for the flag to be handled.  Then,
there is another thread that is monitoring for these flags.  If being run in
console mode, it is just the main thread.  If Asterisk is in the background,
a thread is created to do it.

........

................

Modified:
    trunk/   (props changed)
    trunk/main/asterisk.c

Propchange: trunk/
------------------------------------------------------------------------------
Binary property 'branch-1.4-merged' - no diff available.

Modified: trunk/main/asterisk.c
URL: http://svn.digium.com/view/asterisk/trunk/main/asterisk.c?view=diff&rev=56506&r1=56505&r2=56506
==============================================================================
--- trunk/main/asterisk.c (original)
+++ trunk/main/asterisk.c Fri Feb 23 17:25:22 2007
@@ -235,7 +235,11 @@
 
 static char randompool[256];
 
-static unsigned int need_reload;
+static int sig_alert_pipe[2] = { -1, -1 };
+static struct {
+	 unsigned int need_reload:1;
+	 unsigned int need_quit:1;
+} sig_flags;
 
 #if !defined(LOW_MEMORY)
 struct file_version {
@@ -1052,11 +1056,14 @@
 
 static void hup_handler(int num)
 {
+	int a = 0;
 	if (option_verbose > 1) 
 		printf("Received HUP signal -- Reloading configs\n");
 	if (restartnow)
 		execvp(_argv[0], _argv);
-	need_reload = 1;
+	sig_flags.need_reload = 1;
+	if (sig_alert_pipe[1] != -1)
+		write(sig_alert_pipe[1], &a, sizeof(a));
 	signal(num, hup_handler);
 }
 
@@ -1271,7 +1278,12 @@
 
 static void __quit_handler(int num)
 {
-	quit_handler(num, 0, 1, 0);
+	int a = 0;
+	sig_flags.need_quit = 1;
+	if (sig_alert_pipe[1] != -1)
+		write(sig_alert_pipe[1], &a, sizeof(a));
+	/* There is no need to restore the signal handler here, since the app
+	 * is going to exit */
 }
 
 static const char *fix_header(char *outbuf, int maxout, const char *s, char *cmp)
@@ -2192,11 +2204,6 @@
 				}
 			}
 		}
-
-		if (need_reload) {
-			need_reload = 0;
-			ast_module_reload(NULL);
-		}
 	}
 	printf("\nDisconnected from Asterisk server\n");
 }
@@ -2400,6 +2407,26 @@
 		}
 	}
 	ast_config_destroy(cfg);
+}
+
+static void *monitor_sig_flags(void *unused)
+{
+	for (;;) {
+		struct pollfd p = { sig_alert_pipe[0], POLLIN, 0 };
+		int a;
+		poll(&p, 1, -1);
+		if (sig_flags.need_reload) {
+			sig_flags.need_reload = 0;
+			ast_module_reload(NULL);
+		}
+		if (sig_flags.need_quit) {
+			sig_flags.need_quit = 0;
+			quit_handler(0, 0, 1, 0);
+		}
+		read(sig_alert_pipe[0], &a, sizeof(a));
+	}
+
+	return NULL;
 }
 
 int main(int argc, char *argv[])
@@ -2836,6 +2863,9 @@
 	if (ast_opt_no_fork)
 		consolethread = pthread_self();
 
+	if (pipe(sig_alert_pipe))
+		sig_alert_pipe[0] = sig_alert_pipe[1] = -1;
+
 	ast_set_flag(&ast_options, AST_OPT_FLAG_FULLY_BOOTED);
 	pthread_sigmask(SIG_UNBLOCK, &sigs, NULL);
 
@@ -2850,6 +2880,14 @@
 		/* Console stuff now... */
 		/* Register our quit function */
 		char title[256];
+		pthread_attr_t attr;
+		pthread_t dont_care;
+
+		pthread_attr_init(&attr);
+		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+		ast_pthread_create(&dont_care, &attr, monitor_sig_flags, NULL);
+		pthread_attr_destroy(&attr);
+
 		set_icon("Asterisk");
 		snprintf(title, sizeof(title), "Asterisk Console on '%s' (pid %ld)", hostname, (long)ast_mainpid);
 		set_title(title);
@@ -2873,22 +2911,10 @@
 					ast_log(LOG_WARNING, "Failed to open /dev/null to recover from dead console. Bad things will happen!\n");
 				break;
 			}
-			if (need_reload) {
-				need_reload = 0;
-				ast_module_reload(NULL);
-			}
-		}
-	}
-	/* Do nothing */
-	for (;;) {	/* apparently needed for Mac OS X */
-		struct pollfd p = { -1 /* no descriptor */, 0, 0 };
-		poll(&p, 0, -1);
- 		/* SIGHUP will cause this to break out of poll() */
- 		if (need_reload) {
- 			need_reload = 0;
- 			ast_module_reload(NULL);
- 		}
-	}
+		}
+	}
+
+	monitor_sig_flags(NULL);
 
 	return 0;
 }



More information about the svn-commits mailing list