Merged revisions 115884 via svnmerge from
[asterisk-bristuff.git] / utils / astcanary.c
blob785ec14c4a91f51f30c1c98ba49a210874b5076d
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Digium, Inc.
6 * Tilghman Lesher <tlesher AT digium DOT com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
22 #include <sys/resource.h>
23 #include <utime.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <stdlib.h>
28 /*!\brief
29 * At one time, canaries were carried along with coal miners down
30 * into a mine. Their purpose was to alert the miners when they
31 * had drilled into a pocket of methane gas or another noxious
32 * substance. The canary, being the most sensitive animal would
33 * immediately fall over. Seeing this, the miners could take
34 * action to escape the mine, seeing an imminent danger.
36 * This process serves a similar purpose, though with the realtime
37 * priority being the reason. When a thread starts running away
38 * with the processor, it is typically difficult to tell what
39 * thread caused the problem, as the machine acts as if it is
40 * locked up (in fact, what has happened is that Asterisk runs at
41 * a higher priority than even the login shell, so the runaway
42 * thread hogs all available CPU time.
44 * If that happens, this canary process will cease to get any
45 * process time, which we can monitor with a realtime thread in
46 * Asterisk. Should that happen, that monitoring thread may take
47 * immediate action to slow down Asterisk to regular priority,
48 * thus allowing an administrator to login to the system and
49 * restart Asterisk or perhaps take another course of action
50 * (such as retrieving a backtrace to let the developers know
51 * what precisely went wrong).
53 * Note that according to POSIX.1, all threads inside a single
54 * process must share the same priority, so when the monitoring
55 * thread deprioritizes itself, it deprioritizes all threads at
56 * the same time. This is also why this canary must exist as a
57 * completely separate process and not simply as a thread within
58 * Asterisk itself.
61 int main(int argc, char *argv[])
63 int fd;
64 /* Run at normal priority */
65 setpriority(PRIO_PROCESS, 0, 0);
66 for (;;) {
67 /* Update the modification times (checked from Asterisk) */
68 if (utime(argv[1], NULL)) {
69 /* Recreate the file if it doesn't exist */
70 if ((fd = open(argv[1], O_RDWR | O_TRUNC | O_CREAT, 0777)) > -1)
71 close(fd);
72 else
73 exit(1);
74 continue;
77 /* Run occasionally */
78 sleep(5);
81 /* Never reached */
82 return 0;