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>
22 #include <sys/resource.h>
30 * At one time, canaries were carried along with coal miners down
31 * into a mine. Their purpose was to alert the miners when they
32 * had drilled into a pocket of methane gas or another noxious
33 * substance. The canary, being the most sensitive animal would
34 * immediately fall over. Seeing this, the miners could take
35 * action to escape the mine, seeing an imminent danger.
37 * This process serves a similar purpose, though with the realtime
38 * priority being the reason. When a thread starts running away
39 * with the processor, it is typically difficult to tell what
40 * thread caused the problem, as the machine acts as if it is
41 * locked up (in fact, what has happened is that Asterisk runs at
42 * a higher priority than even the login shell, so the runaway
43 * thread hogs all available CPU time.
45 * If that happens, this canary process will cease to get any
46 * process time, which we can monitor with a realtime thread in
47 * Asterisk. Should that happen, that monitoring thread may take
48 * immediate action to slow down Asterisk to regular priority,
49 * thus allowing an administrator to login to the system and
50 * restart Asterisk or perhaps take another course of action
51 * (such as retrieving a backtrace to let the developers know
52 * what precisely went wrong).
54 * Note that according to POSIX.1, all threads inside a single
55 * process must share the same priority, so when the monitoring
56 * thread deprioritizes itself, it deprioritizes all threads at
57 * the same time. This is also why this canary must exist as a
58 * completely separate process and not simply as a thread within
62 const char explanation
[] =
63 "This file is created when Asterisk is run with a realtime priority (-p). It\n"
64 "must continue to exist, and the astcanary process must be allowed to continue\n"
65 "running, or else the Asterisk process will, within a short period of time,\n"
66 "slow itself down to regular priority.\n\n"
67 "The technical explanation for this file is to provide an assurance to Asterisk\n"
68 "that there are no threads that have gone into runaway mode, thus hogging the\n"
69 "CPU, and making the Asterisk machine seem to be unresponsive. When that\n"
70 "happens, the astcanary process will be unable to update the timestamp on this\n"
71 "file, and Asterisk will notice within 120 seconds and react. Slowing the\n"
72 "Asterisk process down to regular priority will permit an administrator to\n"
73 "intervene, thus avoiding a need to reboot the entire machine.\n";
75 int main(int argc
, char *argv
[])
78 /* Run at normal priority */
79 setpriority(PRIO_PROCESS
, 0, 0);
81 /* Update the modification times (checked from Asterisk) */
82 if (utime(argv
[1], NULL
)) {
83 /* Recreate the file if it doesn't exist */
84 if ((fd
= open(argv
[1], O_RDWR
| O_TRUNC
| O_CREAT
, 0777)) > -1) {
85 write(fd
, explanation
, strlen(explanation
));
93 /* Run occasionally */