1 /**
2 * collectd - src/collectdmon.c
3 * Copyright (C) 2007 Sebastian Harl
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Sebastian Harl <sh at tokkee.org>
25 **/
27 #if !defined(__GNUC__) || !__GNUC__
28 # define __attribute__(x) /**/
29 #endif
31 #include "config.h"
33 #include <assert.h>
35 #include <errno.h>
37 #include <fcntl.h>
39 #include <signal.h>
41 #include <stdio.h>
42 #include <stdlib.h>
44 #include <string.h>
46 #include <syslog.h>
48 #include <sys/resource.h>
49 #include <sys/time.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <sys/wait.h>
54 #include <time.h>
56 #include <unistd.h>
58 #ifndef COLLECTDMON_PIDFILE
59 # define COLLECTDMON_PIDFILE LOCALSTATEDIR"/run/collectdmon.pid"
60 #endif /* ! COLLECTDMON_PIDFILE */
62 #ifndef WCOREDUMP
63 # define WCOREDUMP(s) 0
64 #endif /* ! WCOREDUMP */
66 static int loop = 0;
67 static int restart = 0;
69 static char *pidfile = NULL;
70 static pid_t collectd_pid = 0;
72 static void exit_usage (char *name)
73 {
74 printf ("Usage: %s <options> [-- <collectd options>]\n"
76 "\nAvailable options:\n"
77 " -h Display this help and exit.\n"
78 " -c <path> Path to the collectd binary.\n"
79 " -P <file> PID-file.\n"
81 "\nFor <collectd options> see collectd.conf(5).\n"
83 "\n"PACKAGE" "VERSION", http://collectd.org/\n"
84 "by Florian octo Forster <octo@collectd.org>\n"
85 "for contributions see `AUTHORS'\n", name);
86 exit (0);
87 } /* exit_usage */
89 static int pidfile_create (void)
90 {
91 FILE *file = NULL;
93 if (NULL == pidfile)
94 pidfile = COLLECTDMON_PIDFILE;
96 if (NULL == (file = fopen (pidfile, "w"))) {
97 syslog (LOG_ERR, "Error: couldn't open PID-file (%s) for writing: %s",
98 pidfile, strerror (errno));
99 return -1;
100 }
102 fprintf (file, "%i\n", (int)getpid ());
103 fclose (file);
104 return 0;
105 } /* pidfile_create */
107 static int pidfile_delete (void)
108 {
109 assert (NULL != pidfile);
111 if (0 != unlink (pidfile)) {
112 syslog (LOG_ERR, "Error: couldn't delete PID-file (%s): %s",
113 pidfile, strerror (errno));
114 return -1;
115 }
116 return 0;
117 } /* pidfile_remove */
119 static int daemonize (void)
120 {
121 struct rlimit rl;
123 pid_t pid = 0;
124 int i = 0;
126 if (0 != chdir ("/")) {
127 fprintf (stderr, "Error: chdir() failed: %s\n", strerror (errno));
128 return -1;
129 }
131 if (0 != getrlimit (RLIMIT_NOFILE, &rl)) {
132 fprintf (stderr, "Error: getrlimit() failed: %s\n", strerror (errno));
133 return -1;
134 }
136 if (0 > (pid = fork ())) {
137 fprintf (stderr, "Error: fork() failed: %s\n", strerror (errno));
138 return -1;
139 }
140 else if (pid != 0) {
141 exit (0);
142 }
144 if (0 != pidfile_create ())
145 return -1;
147 setsid ();
149 if (RLIM_INFINITY == rl.rlim_max)
150 rl.rlim_max = 1024;
152 for (i = 0; i < (int)rl.rlim_max; ++i)
153 close (i);
155 errno = 0;
156 if (open ("/dev/null", O_RDWR) != 0) {
157 syslog (LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s",
158 strerror (errno));
159 return -1;
160 }
162 errno = 0;
163 if (dup (0) != 1) {
164 syslog (LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s",
165 strerror (errno));
166 return -1;
167 }
169 errno = 0;
170 if (dup (0) != 2) {
171 syslog (LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s",
172 strerror (errno));
173 return -1;
174 }
175 return 0;
176 } /* daemonize */
178 static int collectd_start (char **argv)
179 {
180 pid_t pid = 0;
182 if (0 > (pid = fork ())) {
183 syslog (LOG_ERR, "Error: fork() failed: %s", strerror (errno));
184 return -1;
185 }
186 else if (pid != 0) {
187 collectd_pid = pid;
188 return 0;
189 }
191 execvp (argv[0], argv);
192 syslog (LOG_ERR, "Error: execvp(%s) failed: %s",
193 argv[0], strerror (errno));
194 exit (-1);
195 } /* collectd_start */
197 static int collectd_stop (void)
198 {
199 if (0 == collectd_pid)
200 return 0;
202 if (0 != kill (collectd_pid, SIGTERM)) {
203 syslog (LOG_ERR, "Error: kill() failed: %s", strerror (errno));
204 return -1;
205 }
206 return 0;
207 } /* collectd_stop */
209 static void sig_int_term_handler (int __attribute__((unused)) signo)
210 {
211 ++loop;
212 return;
213 } /* sig_int_term_handler */
215 static void sig_hup_handler (int __attribute__((unused)) signo)
216 {
217 ++restart;
218 return;
219 } /* sig_hup_handler */
221 static void log_status (int status)
222 {
223 if (WIFEXITED (status)) {
224 if (0 == WEXITSTATUS (status))
225 syslog (LOG_INFO, "Info: collectd terminated with exit status %i",
226 WEXITSTATUS (status));
227 else
228 syslog (LOG_WARNING,
229 "Warning: collectd terminated with exit status %i",
230 WEXITSTATUS (status));
231 }
232 else if (WIFSIGNALED (status)) {
233 syslog (LOG_WARNING, "Warning: collectd was terminated by signal %i%s",
234 WTERMSIG (status), WCOREDUMP (status) ? " (core dumped)" : "");
235 }
236 return;
237 } /* log_status */
239 static void check_respawn (void)
240 {
241 time_t t = time (NULL);
243 static time_t timestamp = 0;
244 static int counter = 0;
246 if ((t - 120) < timestamp)
247 ++counter;
248 else {
249 timestamp = t;
250 counter = 0;
251 }
253 if (10 < counter) {
254 unsigned int time_left = 300;
256 syslog (LOG_ERR, "Error: collectd is respawning too fast - "
257 "disabled for %i seconds", time_left);
259 while ((0 < (time_left = sleep (time_left))) && (0 == loop));
260 }
261 return;
262 } /* check_respawn */
264 int main (int argc, char **argv)
265 {
266 int collectd_argc = 0;
267 char *collectd = NULL;
268 char **collectd_argv = NULL;
270 struct sigaction sa;
272 int i = 0;
274 /* parse command line options */
275 while (42) {
276 int c = getopt (argc, argv, "hc:P:");
278 if (-1 == c)
279 break;
281 switch (c) {
282 case 'c':
283 collectd = optarg;
284 break;
285 case 'P':
286 pidfile = optarg;
287 break;
288 case 'h':
289 default:
290 exit_usage (argv[0]);
291 }
292 }
294 for (i = optind; i < argc; ++i)
295 if (0 == strcmp (argv[i], "-f"))
296 break;
298 /* i < argc => -f already present */
299 collectd_argc = 1 + argc - optind + ((i < argc) ? 0 : 1);
300 collectd_argv = (char **)calloc (collectd_argc + 1, sizeof (char *));
302 if (NULL == collectd_argv) {
303 fprintf (stderr, "Out of memory.");
304 return 3;
305 }
307 collectd_argv[0] = (NULL == collectd) ? "collectd" : collectd;
309 if (i == argc)
310 collectd_argv[collectd_argc - 1] = "-f";
312 for (i = optind; i < argc; ++i)
313 collectd_argv[i - optind + 1] = argv[i];
315 collectd_argv[collectd_argc] = NULL;
317 openlog ("collectdmon", LOG_CONS | LOG_PID, LOG_DAEMON);
319 if (-1 == daemonize ())
320 return 1;
322 sa.sa_handler = sig_int_term_handler;
323 sa.sa_flags = 0;
324 sigemptyset (&sa.sa_mask);
326 if (0 != sigaction (SIGINT, &sa, NULL)) {
327 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
328 return 1;
329 }
331 if (0 != sigaction (SIGTERM, &sa, NULL)) {
332 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
333 return 1;
334 }
336 sa.sa_handler = sig_hup_handler;
338 if (0 != sigaction (SIGHUP, &sa, NULL)) {
339 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
340 return 1;
341 }
343 while (0 == loop) {
344 int status = 0;
346 if (0 != collectd_start (collectd_argv)) {
347 syslog (LOG_ERR, "Error: failed to start collectd.");
348 break;
349 }
351 assert (0 < collectd_pid);
352 while ((collectd_pid != waitpid (collectd_pid, &status, 0))
353 && (EINTR == errno))
354 if ((0 != loop) || (0 != restart))
355 collectd_stop ();
357 collectd_pid = 0;
359 log_status (status);
360 check_respawn ();
362 if (0 != restart) {
363 syslog (LOG_INFO, "Info: restarting collectd");
364 restart = 0;
365 }
366 else if (0 == loop)
367 syslog (LOG_WARNING, "Warning: restarting collectd");
368 }
370 syslog (LOG_INFO, "Info: shutting down collectdmon");
372 pidfile_delete ();
373 closelog ();
375 free (collectd_argv);
376 return 0;
377 } /* main */
379 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */