Asterisk - The Open Source Telephony Project GIT-master-f36a736
astcanary.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2007, Digium, Inc.
5 *
6 * Tilghman Lesher <tlesher AT digium DOT com>
7 *
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.
13 *
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.
17 */
18
19/*** MODULEINFO
20 <support_level>core</support_level>
21 ***/
22
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/resource.h>
27#include <utime.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <stdlib.h>
31#include <string.h>
32#include <stdio.h>
33
34/*!\brief
35 * At one time, canaries were carried along with coal miners down
36 * into a mine. Their purpose was to alert the miners when they
37 * had drilled into a pocket of methane gas or another noxious
38 * substance. The canary, being the most sensitive animal, would
39 * immediately fall over. Seeing this, the miners could take
40 * action to escape the mine, seeing an imminent danger.
41 *
42 * This process serves a similar purpose, though with the realtime
43 * priority being the reason. When a thread starts running away
44 * with the processor, it is typically difficult to tell what
45 * thread caused the problem, as the machine acts as if it is
46 * locked up (in fact, what has happened is that Asterisk runs at
47 * a higher priority than even the login shell, so the runaway
48 * thread hogs all available CPU time.
49 *
50 * If that happens, this canary process will cease to get any
51 * process time, which we can monitor with a realtime thread in
52 * Asterisk. Should that happen, that monitoring thread may take
53 * immediate action to slow down Asterisk to regular priority,
54 * thus allowing an administrator to login to the system and
55 * restart Asterisk or perhaps take another course of action
56 * (such as retrieving a backtrace to let the developers know
57 * what precisely went wrong).
58 *
59 * Note that according to POSIX.1, all threads inside a single
60 * process must share the same priority, so when the monitoring
61 * thread deprioritizes itself, it deprioritizes all threads at
62 * the same time. This is also why this canary must exist as a
63 * completely separate process and not simply as a thread within
64 * Asterisk itself.
65 *
66 * Quote:
67 * "The nice value set with setpriority() shall be applied to the
68 * process. If the process is multi-threaded, the nice value shall
69 * affect all system scope threads in the process."
70 *
71 * Source:
72 * http://www.opengroup.org/onlinepubs/000095399/functions/setpriority.html
73 *
74 * In answer to the question, what aren't system scope threads, the
75 * answer is, in Asterisk, nothing. Process scope threads are the
76 * alternative, but they aren't supported in Linux.
77 */
78
79static const char explanation[] =
80"This file is created when Asterisk is run with a realtime priority (-p). It\n"
81"must continue to exist, and the astcanary process must be allowed to continue\n"
82"running, or else the Asterisk process will, within a short period of time,\n"
83"slow itself down to regular priority.\n\n"
84"The technical explanation for this file is to provide an assurance to Asterisk\n"
85"that there are no threads that have gone into runaway mode, thus hogging the\n"
86"CPU, and making the Asterisk machine seem to be unresponsive. When that\n"
87"happens, the astcanary process will be unable to update the timestamp on this\n"
88"file, and Asterisk will notice within 120 seconds and react. Slowing the\n"
89"Asterisk process down to regular priority will permit an administrator to\n"
90"intervene, thus avoiding a need to reboot the entire machine.\n";
91
92int main(int argc, char *argv[])
93{
94 int fd;
95 pid_t parent;
96
97 if (argc < 3) {
98 fprintf(stderr, "Usage: %s <monitor-filename> <ppid>\n", argv[0]);
99 exit(1);
100 }
101
102 /* Run at normal priority */
103 setpriority(PRIO_PROCESS, 0, 0);
104
105 /*!\note
106 * See http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_265
107 * for a justification of this approach. The PPID after the creator dies in Linux and
108 * most other Unix-like systems will be 1, but this is not strictly the case. The POSIX
109 * specification allows it to be an implementation-defined system process. However, it
110 * most certainly will not be the original parent PID, which makes the following code
111 * POSIX-compliant.
112 */
113 for (parent = atoi(argv[2]); parent == getppid() ;) {
114 /* Update the modification times (checked from Asterisk) */
115 if (utime(argv[1], NULL)) {
116 /* Recreate the file if it doesn't exist */
117 if ((fd = open(argv[1], O_RDWR | O_TRUNC | O_CREAT, 0777)) > -1) {
118 if (write(fd, explanation, strlen(explanation)) < 0) {
119 exit(1);
120 }
121 close(fd);
122 } else {
123 exit(1);
124 }
125 continue;
126 }
127
128 /* Run occasionally */
129 sleep(5);
130 }
131
132 /* Exit when the parent dies */
133 return 0;
134}
static const char explanation[]
At one time, canaries were carried along with coal miners down into a mine. Their purpose was to aler...
Definition: astcanary.c:79
int main(int argc, char *argv[])
Definition: astcanary.c:92
#define setpriority
Definition: asterisk.h:50
#define NULL
Definition: resample.c:96