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 const char *pidfile = NULL;
70 static pid_t collectd_pid = 0;
72 __attribute__((noreturn))
73 static void exit_usage (const char *name)
75 printf ("Usage: %s <options> [-- <collectd options>]\n"
77 "\nAvailable options:\n"
78 " -h Display this help and exit.\n"
79 " -c <path> Path to the collectd binary.\n"
80 " -P <file> PID-file.\n"
82 "\nFor <collectd options> see collectd.conf(5).\n"
84 "\n"PACKAGE_NAME" "PACKAGE_VERSION", http://collectd.org/\n"
85 "by Florian octo Forster <octo@collectd.org>\n"
86 "for contributions see `AUTHORS'\n", name);
90 static int pidfile_create (void)
95 pidfile = COLLECTDMON_PIDFILE;
97 if (NULL == (file = fopen (pidfile, "w"))) {
98 syslog (LOG_ERR, "Error: couldn't open PID-file (%s) for writing: %s",
99 pidfile, strerror (errno));
103 fprintf (file, "%i\n", (int)getpid ());
106 } /* pidfile_create */
108 static int pidfile_delete (void)
110 assert (NULL != pidfile);
112 if (0 != unlink (pidfile)) {
113 syslog (LOG_ERR, "Error: couldn't delete PID-file (%s): %s",
114 pidfile, strerror (errno));
118 } /* pidfile_remove */
120 static int daemonize (void)
128 if (0 != chdir ("/")) {
129 fprintf (stderr, "Error: chdir() failed: %s\n", strerror (errno));
133 if (0 != getrlimit (RLIMIT_NOFILE, &rl)) {
134 fprintf (stderr, "Error: getrlimit() failed: %s\n", strerror (errno));
138 if (0 > (pid = fork ())) {
139 fprintf (stderr, "Error: fork() failed: %s\n", strerror (errno));
146 if (0 != pidfile_create ())
151 if (RLIM_INFINITY == rl.rlim_max)
154 for (i = 0; i < (int)rl.rlim_max; ++i)
157 dev_null = open ("/dev/null", O_RDWR);
158 if (dev_null == -1) {
159 syslog (LOG_ERR, "Error: couldn't open /dev/null: %s", strerror (errno));
163 if (dup2 (dev_null, STDIN_FILENO) == -1) {
165 syslog (LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s", strerror (errno));
169 if (dup2 (dev_null, STDOUT_FILENO) == -1) {
171 syslog (LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s", strerror (errno));
175 if (dup2 (dev_null, STDERR_FILENO) == -1) {
177 syslog (LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s", strerror (errno));
181 if ((dev_null != STDIN_FILENO) && (dev_null != STDOUT_FILENO) && (dev_null != STDERR_FILENO))
187 static int collectd_start (char **argv)
191 if (0 > (pid = fork ())) {
192 syslog (LOG_ERR, "Error: fork() failed: %s", strerror (errno));
200 execvp (argv[0], argv);
201 syslog (LOG_ERR, "Error: execvp(%s) failed: %s",
202 argv[0], strerror (errno));
204 } /* collectd_start */
206 static int collectd_stop (void)
208 if (0 == collectd_pid)
211 if (0 != kill (collectd_pid, SIGTERM)) {
212 syslog (LOG_ERR, "Error: kill() failed: %s", strerror (errno));
216 } /* collectd_stop */
218 static void sig_int_term_handler (int __attribute__((unused)) signo)
222 } /* sig_int_term_handler */
224 static void sig_hup_handler (int __attribute__((unused)) signo)
228 } /* sig_hup_handler */
230 static void log_status (int status)
232 if (WIFEXITED (status)) {
233 if (0 == WEXITSTATUS (status))
234 syslog (LOG_INFO, "Info: collectd terminated with exit status %i",
235 WEXITSTATUS (status));
238 "Warning: collectd terminated with exit status %i",
239 WEXITSTATUS (status));
241 else if (WIFSIGNALED (status)) {
242 syslog (LOG_WARNING, "Warning: collectd was terminated by signal %i%s",
243 WTERMSIG (status), WCOREDUMP (status) ? " (core dumped)" : "");
248 static void check_respawn (void)
250 time_t t = time (NULL);
252 static time_t timestamp = 0;
253 static int counter = 0;
255 if ((t - 120) < timestamp)
263 unsigned int time_left = 300;
265 syslog (LOG_ERR, "Error: collectd is respawning too fast - "
266 "disabled for %i seconds", time_left);
268 while ((0 < (time_left = sleep (time_left))) && (0 == loop));
271 } /* check_respawn */
273 int main (int argc, char **argv)
275 int collectd_argc = 0;
276 char *collectd = NULL;
277 char **collectd_argv = NULL;
283 /* parse command line options */
285 int c = getopt (argc, argv, "hc:P:");
299 exit_usage (argv[0]);
303 for (i = optind; i < argc; ++i)
304 if (0 == strcmp (argv[i], "-f"))
307 /* i < argc => -f already present */
308 collectd_argc = 1 + argc - optind + ((i < argc) ? 0 : 1);
309 collectd_argv = (char **)calloc (collectd_argc + 1, sizeof (char *));
311 if (NULL == collectd_argv) {
312 fprintf (stderr, "Out of memory.");
316 collectd_argv[0] = (NULL == collectd) ? "collectd" : collectd;
319 collectd_argv[collectd_argc - 1] = "-f";
321 for (i = optind; i < argc; ++i)
322 collectd_argv[i - optind + 1] = argv[i];
324 collectd_argv[collectd_argc] = NULL;
326 openlog ("collectdmon", LOG_CONS | LOG_PID, LOG_DAEMON);
328 if (-1 == daemonize ())
330 free (collectd_argv);
334 sa.sa_handler = sig_int_term_handler;
336 sigemptyset (&sa.sa_mask);
338 if (0 != sigaction (SIGINT, &sa, NULL)) {
339 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
340 free (collectd_argv);
344 if (0 != sigaction (SIGTERM, &sa, NULL)) {
345 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
346 free (collectd_argv);
350 sa.sa_handler = sig_hup_handler;
352 if (0 != sigaction (SIGHUP, &sa, NULL)) {
353 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
354 free (collectd_argv);
361 if (0 != collectd_start (collectd_argv)) {
362 syslog (LOG_ERR, "Error: failed to start collectd.");
366 assert (0 < collectd_pid);
367 while ((collectd_pid != waitpid (collectd_pid, &status, 0))
369 if ((0 != loop) || (0 != restart))
378 syslog (LOG_INFO, "Info: restarting collectd");
382 syslog (LOG_WARNING, "Warning: restarting collectd");
385 syslog (LOG_INFO, "Info: shutting down collectdmon");
390 free (collectd_argv);
394 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */