aboutsummaryrefslogtreecommitdiffstats
path: root/utils
diff options
context:
space:
mode:
authortilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2007-12-18 23:06:05 +0000
committertilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2007-12-18 23:06:05 +0000
commit11a5516081314ebee69ff72fa76d3fbf554c0488 (patch)
treedc6fe6d88013e7f3321d1468c36650a90fc0a2b8 /utils
parentf3230e3b5b2f9d81b59764085ca39988843cd6dd (diff)
Add a canary process, for high priority mode (asterisk -p) to ensure that if
Asterisk goes into a busy loop, the machine will be recoverable. We'd still need to do a restart to put Asterisk back into high priority mode, but at least a reboot won't be required. (Closes issue #11559) git-svn-id: http://svn.digium.com/svn/asterisk/trunk@93804 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'utils')
-rw-r--r--utils/Makefile2
-rw-r--r--utils/astcanary.c84
2 files changed, 85 insertions, 1 deletions
diff --git a/utils/Makefile b/utils/Makefile
index 6cbd39818..4c6bb1114 100644
--- a/utils/Makefile
+++ b/utils/Makefile
@@ -17,7 +17,7 @@ ASTTOPDIR?=..
.PHONY: clean all uninstall
# to get check_expr, add it to the ALL_UTILS list
-ALL_UTILS:=astman smsq stereorize streamplayer aelparse muted check_expr conf2ael hashtest2 hashtest
+ALL_UTILS:=astman smsq stereorize streamplayer aelparse muted check_expr conf2ael hashtest2 hashtest astcanary
UTILS:=$(ALL_UTILS)
LIBS += $(BKTR_LIB) # astobj2 with devmode uses backtrace
diff --git a/utils/astcanary.c b/utils/astcanary.c
new file mode 100644
index 000000000..eb9f17208
--- /dev/null
+++ b/utils/astcanary.c
@@ -0,0 +1,84 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2007, Digium, Inc.
+ *
+ * Tilghman Lesher <tlesher AT digium DOT com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <utime.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+/*!\brief
+ * 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
+ * immediately fall over. Seeing this, the miners could take
+ * action to escape the mine, seeing an imminent danger.
+ *
+ * This process serves a similar purpose, though with the realtime
+ * priority being the reason. When a thread starts running away
+ * with the processor, it is typically difficult to tell what
+ * thread caused the problem, as the machine acts as if it is
+ * locked up (in fact, what has happened is that Asterisk runs at
+ * a higher priority than even the login shell, so the runaway
+ * thread hogs all available CPU time.
+ *
+ * If that happens, this canary process will cease to get any
+ * process time, which we can monitor with a realtime thread in
+ * Asterisk. Should that happen, that monitoring thread may take
+ * immediate action to slow down Asterisk to regular priority,
+ * thus allowing an administrator to login to the system and
+ * restart Asterisk or perhaps take another course of action
+ * (such as retrieving a backtrace to let the developers know
+ * what precisely went wrong).
+ *
+ * Note that according to POSIX.1, all threads inside a single
+ * process must share the same priority, so when the monitoring
+ * thread deprioritizes itself, it deprioritizes all threads at
+ * 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.
+ */
+
+int main(int argc, char *argv[])
+{
+ int fd;
+ /* Run at normal priority */
+ setpriority(PRIO_PROCESS, 0, 0);
+ for (;;) {
+ /* Update the modification times (checked from Asterisk) */
+ if (utime(argv[1], NULL)) {
+ /* Recreate the file if it doesn't exist */
+ if ((fd = open(argv[1], O_RDWR | O_TRUNC | O_CREAT)) > -1)
+ close(fd);
+ else
+ exit(1);
+ continue;
+ }
+
+ /* Run occasionally */
+ sleep(5);
+ }
+
+ /* Never reached */
+ return 0;
+}
+