2 * collectd - src/collectdmon.c
3 * Copyright (C) 2007 Sebastian Harl
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Sebastian Harl <sh at tokkee.org>
27 #if !defined(__GNUC__) || !__GNUC__
28 # define __attribute__(x) /**/
48 #include <sys/resource.h>
50 #include <sys/types.h>
58 #ifndef COLLECTDMON_PIDFILE
59 # define COLLECTDMON_PIDFILE LOCALSTATEDIR"/run/collectdmon.pid"
60 #endif /* ! COLLECTDMON_PIDFILE */
63 # define WCOREDUMP(s) 0
64 #endif /* ! WCOREDUMP */
67 static int restart = 0;
69 static char *pidfile = NULL;
70 static pid_t collectd_pid = 0;
72 static void exit_usage (char *name)
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_NAME" "PACKAGE_VERSION", http://collectd.org/\n"
84 "by Florian octo Forster <octo@collectd.org>\n"
85 "for contributions see `AUTHORS'\n", name);
89 static int pidfile_create (void)
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));
102 fprintf (file, "%i\n", (int)getpid ());
105 } /* pidfile_create */
107 static int pidfile_delete (void)
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));
117 } /* pidfile_remove */
119 static int daemonize (void)
126 if (0 != chdir ("/")) {
127 fprintf (stderr, "Error: chdir() failed: %s\n", strerror (errno));
131 if (0 != getrlimit (RLIMIT_NOFILE, &rl)) {
132 fprintf (stderr, "Error: getrlimit() failed: %s\n", strerror (errno));
136 if (0 > (pid = fork ())) {
137 fprintf (stderr, "Error: fork() failed: %s\n", strerror (errno));
144 if (0 != pidfile_create ())
149 if (RLIM_INFINITY == rl.rlim_max)
152 for (i = 0; i < (int)rl.rlim_max; ++i)
156 if (open ("/dev/null", O_RDWR) != 0) {
157 syslog (LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s",
164 syslog (LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s",
171 syslog (LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s",
178 static int collectd_start (char **argv)
182 if (0 > (pid = fork ())) {
183 syslog (LOG_ERR, "Error: fork() failed: %s", strerror (errno));
191 execvp (argv[0], argv);
192 syslog (LOG_ERR, "Error: execvp(%s) failed: %s",
193 argv[0], strerror (errno));
195 } /* collectd_start */
197 static int collectd_stop (void)
199 if (0 == collectd_pid)
202 if (0 != kill (collectd_pid, SIGTERM)) {
203 syslog (LOG_ERR, "Error: kill() failed: %s", strerror (errno));
207 } /* collectd_stop */
209 static void sig_int_term_handler (int __attribute__((unused)) signo)
213 } /* sig_int_term_handler */
215 static void sig_hup_handler (int __attribute__((unused)) signo)
219 } /* sig_hup_handler */
221 static void log_status (int status)
223 if (WIFEXITED (status)) {
224 if (0 == WEXITSTATUS (status))
225 syslog (LOG_INFO, "Info: collectd terminated with exit status %i",
226 WEXITSTATUS (status));
229 "Warning: collectd terminated with exit status %i",
230 WEXITSTATUS (status));
232 else if (WIFSIGNALED (status)) {
233 syslog (LOG_WARNING, "Warning: collectd was terminated by signal %i%s",
234 WTERMSIG (status), WCOREDUMP (status) ? " (core dumped)" : "");
239 static void check_respawn (void)
241 time_t t = time (NULL);
243 static time_t timestamp = 0;
244 static int counter = 0;
246 if ((t - 120) < timestamp)
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));
262 } /* check_respawn */
264 int main (int argc, char **argv)
266 int collectd_argc = 0;
267 char *collectd = NULL;
268 char **collectd_argv = NULL;
274 /* parse command line options */
276 int c = getopt (argc, argv, "hc:P:");
290 exit_usage (argv[0]);
294 for (i = optind; i < argc; ++i)
295 if (0 == strcmp (argv[i], "-f"))
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.");
307 collectd_argv[0] = (NULL == collectd) ? "collectd" : collectd;
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 ())
321 free (collectd_argv);
325 sa.sa_handler = sig_int_term_handler;
327 sigemptyset (&sa.sa_mask);
329 if (0 != sigaction (SIGINT, &sa, NULL)) {
330 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
331 free (collectd_argv);
335 if (0 != sigaction (SIGTERM, &sa, NULL)) {
336 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
337 free (collectd_argv);
341 sa.sa_handler = sig_hup_handler;
343 if (0 != sigaction (SIGHUP, &sa, NULL)) {
344 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
345 free (collectd_argv);
352 if (0 != collectd_start (collectd_argv)) {
353 syslog (LOG_ERR, "Error: failed to start collectd.");
357 assert (0 < collectd_pid);
358 while ((collectd_pid != waitpid (collectd_pid, &status, 0))
360 if ((0 != loop) || (0 != restart))
369 syslog (LOG_INFO, "Info: restarting collectd");
373 syslog (LOG_WARNING, "Warning: restarting collectd");
376 syslog (LOG_INFO, "Info: shutting down collectdmon");
381 free (collectd_argv);
385 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */