From bb2b9b1ff447120d539589b31275ad8b7ea6c4a9 Mon Sep 17 00:00:00 2001
From: Tilghman Lesher <tilghman@meg.abyt.es>
Date: Tue, 24 Feb 2009 17:51:36 +0000
Subject: [PATCH] Cause astcanary to exit if Asterisk exits abnormally and
 doesn't kill astcanary. Also, add some documentation supporting the use of
 astcanary. (closes issue #14538)  Reported by: KNK  Patches:       
 asterisk-1.6.x-astcanary.diff uploaded by KNK (license 545)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@178303 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 utils/astcanary.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/utils/astcanary.c b/utils/astcanary.c
index da91e6dd2e..eea228916d 100644
--- a/utils/astcanary.c
+++ b/utils/astcanary.c
@@ -30,7 +30,7 @@
  * At one time, canaries were carried along with coal miners down
  * into a mine.  Their purpose was to alert the miners when they
  * had drilled into a pocket of methane gas or another noxious
- * substance.  The canary, being the most sensitive animal would
+ * substance.  The canary, being the most sensitive animal, would
  * immediately fall over.  Seeing this, the miners could take
  * action to escape the mine, seeing an imminent danger.
  *
@@ -57,6 +57,18 @@
  * the same time.  This is also why this canary must exist as a
  * completely separate process and not simply as a thread within
  * Asterisk itself.
+ *
+ * Quote:
+ * "The nice value set with setpriority() shall be applied to the
+ * process. If the process is multi-threaded, the nice value shall
+ * affect all system scope threads in the process."
+ *
+ * Source:
+ * http://www.opengroup.org/onlinepubs/000095399/functions/setpriority.html
+ *
+ * In answer to the question, what aren't system scope threads, the
+ * answer is, in Asterisk, nothing.  Process scope threads are the
+ * alternative, but they aren't supported in Linux.
  */
 
 static const char explanation[] =
@@ -77,7 +89,7 @@ int main(int argc, char *argv[])
 	int fd;
 	/* Run at normal priority */
 	setpriority(PRIO_PROCESS, 0, 0);
-	for (;;) {
+	for (; getppid() != 1;) {
 		/* Update the modification times (checked from Asterisk) */
 		if (utime(argv[1], NULL)) {
 			/* Recreate the file if it doesn't exist */
@@ -96,7 +108,7 @@ int main(int argc, char *argv[])
 		sleep(5);
 	}
 
-	/* Never reached */
+	/* Reached if asterisk (our parent process) dies - its chldren are inherited by the init process (pid is 1). */
 	return 0;
 }
 
-- 
GitLab