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>
59 # define PREFIX "/opt/" PACKAGE_NAME
63 # define LOCALSTATEDIR PREFIX "/var"
66 #ifndef COLLECTDMON_PIDFILE
67 # define COLLECTDMON_PIDFILE LOCALSTATEDIR"/run/collectdmon.pid"
68 #endif /* ! COLLECTDMON_PIDFILE */
71 # define WCOREDUMP(s) 0
72 #endif /* ! WCOREDUMP */
75 static int restart = 0;
77 static const char *pidfile = NULL;
78 static pid_t collectd_pid = 0;
80 __attribute__((noreturn))
81 static void exit_usage (const char *name)
83 printf ("Usage: %s <options> [-- <collectd options>]\n"
85 "\nAvailable options:\n"
86 " -h Display this help and exit.\n"
87 " -c <path> Path to the collectd binary.\n"
88 " -P <file> PID-file.\n"
90 "\nFor <collectd options> see collectd.conf(5).\n"
92 "\n"PACKAGE_NAME" "PACKAGE_VERSION", http://collectd.org/\n"
93 "by Florian octo Forster <octo@collectd.org>\n"
94 "for contributions see `AUTHORS'\n", name);
98 static int pidfile_create (void)
103 pidfile = COLLECTDMON_PIDFILE;
105 if (NULL == (file = fopen (pidfile, "w"))) {
106 syslog (LOG_ERR, "Error: couldn't open PID-file (%s) for writing: %s",
107 pidfile, strerror (errno));
111 fprintf (file, "%i\n", (int)getpid ());
114 } /* pidfile_create */
116 static int pidfile_delete (void)
118 assert (NULL != pidfile);
120 if (0 != unlink (pidfile)) {
121 syslog (LOG_ERR, "Error: couldn't delete PID-file (%s): %s",
122 pidfile, strerror (errno));
126 } /* pidfile_remove */
128 static int daemonize (void)
136 if (0 != chdir ("/")) {
137 fprintf (stderr, "Error: chdir() failed: %s\n", strerror (errno));
141 if (0 != getrlimit (RLIMIT_NOFILE, &rl)) {
142 fprintf (stderr, "Error: getrlimit() failed: %s\n", strerror (errno));
146 if (0 > (pid = fork ())) {
147 fprintf (stderr, "Error: fork() failed: %s\n", strerror (errno));
154 if (0 != pidfile_create ())
159 if (RLIM_INFINITY == rl.rlim_max)
162 for (i = 0; i < (int)rl.rlim_max; ++i)
165 dev_null = open ("/dev/null", O_RDWR);
166 if (dev_null == -1) {
167 syslog (LOG_ERR, "Error: couldn't open /dev/null: %s", strerror (errno));
171 if (dup2 (dev_null, STDIN_FILENO) == -1) {
173 syslog (LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s", strerror (errno));
177 if (dup2 (dev_null, STDOUT_FILENO) == -1) {
179 syslog (LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s", strerror (errno));
183 if (dup2 (dev_null, STDERR_FILENO) == -1) {
185 syslog (LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s", strerror (errno));
189 if ((dev_null != STDIN_FILENO) && (dev_null != STDOUT_FILENO) && (dev_null != STDERR_FILENO))
195 static int collectd_start (char **argv)
199 if (0 > (pid = fork ())) {
200 syslog (LOG_ERR, "Error: fork() failed: %s", strerror (errno));
208 execvp (argv[0], argv);
209 syslog (LOG_ERR, "Error: execvp(%s) failed: %s",
210 argv[0], strerror (errno));
212 } /* collectd_start */
214 static int collectd_stop (void)
216 if (0 == collectd_pid)
219 if (0 != kill (collectd_pid, SIGTERM)) {
220 syslog (LOG_ERR, "Error: kill() failed: %s", strerror (errno));
224 } /* collectd_stop */
226 static void sig_int_term_handler (int __attribute__((unused)) signo)
230 } /* sig_int_term_handler */
232 static void sig_hup_handler (int __attribute__((unused)) signo)
236 } /* sig_hup_handler */
238 static void log_status (int status)
240 if (WIFEXITED (status)) {
241 if (0 == WEXITSTATUS (status))
242 syslog (LOG_INFO, "Info: collectd terminated with exit status %i",
243 WEXITSTATUS (status));
246 "Warning: collectd terminated with exit status %i",
247 WEXITSTATUS (status));
249 else if (WIFSIGNALED (status)) {
250 syslog (LOG_WARNING, "Warning: collectd was terminated by signal %i%s",
251 WTERMSIG (status), WCOREDUMP (status) ? " (core dumped)" : "");
256 static void check_respawn (void)
258 time_t t = time (NULL);
260 static time_t timestamp = 0;
261 static int counter = 0;
263 if ((t - 120) < timestamp)
271 unsigned int time_left = 300;
273 syslog (LOG_ERR, "Error: collectd is respawning too fast - "
274 "disabled for %i seconds", time_left);
276 while ((0 < (time_left = sleep (time_left))) && (0 == loop));
279 } /* check_respawn */
281 int main (int argc, char **argv)
283 int collectd_argc = 0;
284 char *collectd = NULL;
285 char **collectd_argv = NULL;
291 /* parse command line options */
293 int c = getopt (argc, argv, "hc:P:");
307 exit_usage (argv[0]);
311 for (i = optind; i < argc; ++i)
312 if (0 == strcmp (argv[i], "-f"))
315 /* i < argc => -f already present */
316 collectd_argc = 1 + argc - optind + ((i < argc) ? 0 : 1);
317 collectd_argv = (char **)calloc (collectd_argc + 1, sizeof (char *));
319 if (NULL == collectd_argv) {
320 fprintf (stderr, "Out of memory.");
324 collectd_argv[0] = (NULL == collectd) ? "collectd" : collectd;
327 collectd_argv[collectd_argc - 1] = "-f";
329 for (i = optind; i < argc; ++i)
330 collectd_argv[i - optind + 1] = argv[i];
332 collectd_argv[collectd_argc] = NULL;
334 openlog ("collectdmon", LOG_CONS | LOG_PID, LOG_DAEMON);
336 if (-1 == daemonize ())
338 free (collectd_argv);
342 sa.sa_handler = sig_int_term_handler;
344 sigemptyset (&sa.sa_mask);
346 if (0 != sigaction (SIGINT, &sa, NULL)) {
347 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
348 free (collectd_argv);
352 if (0 != sigaction (SIGTERM, &sa, NULL)) {
353 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
354 free (collectd_argv);
358 sa.sa_handler = sig_hup_handler;
360 if (0 != sigaction (SIGHUP, &sa, NULL)) {
361 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
362 free (collectd_argv);
369 if (0 != collectd_start (collectd_argv)) {
370 syslog (LOG_ERR, "Error: failed to start collectd.");
374 assert (0 < collectd_pid);
375 while ((collectd_pid != waitpid (collectd_pid, &status, 0))
377 if ((0 != loop) || (0 != restart))
386 syslog (LOG_INFO, "Info: restarting collectd");
390 syslog (LOG_WARNING, "Warning: restarting collectd");
393 syslog (LOG_INFO, "Info: shutting down collectdmon");
398 free (collectd_argv);
402 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */