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>
51 #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 */
77 static const char *pidfile;
78 static pid_t collectd_pid;
80 __attribute__((noreturn)) static void exit_usage(const char *name) {
81 printf("Usage: %s <options> [-- <collectd options>]\n"
83 "\nAvailable options:\n"
84 " -h Display this help and exit.\n"
85 " -c <path> Path to the collectd binary.\n"
86 " -P <file> PID-file.\n"
88 "\nFor <collectd options> see collectd.conf(5).\n"
90 "\n" PACKAGE_NAME " " PACKAGE_VERSION ", http://collectd.org/\n"
91 "by Florian octo Forster <octo@collectd.org>\n"
92 "for contributions see `AUTHORS'\n",
97 static int pidfile_create(void) {
101 pidfile = COLLECTDMON_PIDFILE;
103 if ((file = fopen(pidfile, "w")) == NULL) {
104 syslog(LOG_ERR, "Error: couldn't open PID-file (%s) for writing: %s",
105 pidfile, strerror(errno));
109 fprintf(file, "%d\n", (int)getpid());
112 } /* pidfile_create */
114 static int pidfile_delete(void) {
117 if (unlink(pidfile) != 0) {
118 syslog(LOG_ERR, "Error: couldn't delete PID-file (%s): %s", pidfile,
123 } /* pidfile_remove */
125 static int daemonize(void) {
126 if (chdir("/") != 0) {
127 fprintf(stderr, "Error: chdir() failed: %s\n", strerror(errno));
132 if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
133 fprintf(stderr, "Error: getrlimit() failed: %s\n", strerror(errno));
139 fprintf(stderr, "Error: fork() failed: %s\n", strerror(errno));
141 } else if (pid != 0) {
145 if (pidfile_create() != 0)
150 if (rl.rlim_max == RLIM_INFINITY)
153 for (int i = 0; i < (int)rl.rlim_max; ++i)
156 int dev_null = open("/dev/null", O_RDWR);
157 if (dev_null == -1) {
158 syslog(LOG_ERR, "Error: couldn't open /dev/null: %s", strerror(errno));
162 if (dup2(dev_null, STDIN_FILENO) == -1) {
164 syslog(LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s",
169 if (dup2(dev_null, STDOUT_FILENO) == -1) {
171 syslog(LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s",
176 if (dup2(dev_null, STDERR_FILENO) == -1) {
178 syslog(LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s",
183 if ((dev_null != STDIN_FILENO) && (dev_null != STDOUT_FILENO) &&
184 (dev_null != STDERR_FILENO))
190 static int collectd_start(char **argv) {
194 syslog(LOG_ERR, "Error: fork() failed: %s", strerror(errno));
196 } else if (pid != 0) {
201 execvp(argv[0], argv);
202 syslog(LOG_ERR, "Error: execvp(%s) failed: %s", argv[0], strerror(errno));
204 } /* collectd_start */
206 static int collectd_stop(void) {
207 if (collectd_pid == 0)
210 if (kill(collectd_pid, SIGTERM) != 0) {
211 syslog(LOG_ERR, "Error: kill() failed: %s", strerror(errno));
215 } /* collectd_stop */
217 static void sig_int_term_handler(int __attribute__((unused)) signo) {
220 } /* sig_int_term_handler */
222 static void sig_hup_handler(int __attribute__((unused)) signo) {
225 } /* sig_hup_handler */
227 static void log_status(int status) {
228 if (WIFEXITED(status)) {
229 if (WEXITSTATUS(status) == 0)
230 syslog(LOG_INFO, "Info: collectd terminated with exit status %i",
231 WEXITSTATUS(status));
233 syslog(LOG_WARNING, "Warning: collectd terminated with exit status %i",
234 WEXITSTATUS(status));
235 } else if (WIFSIGNALED(status)) {
236 syslog(LOG_WARNING, "Warning: collectd was terminated by signal %i%s",
237 WTERMSIG(status), WCOREDUMP(status) ? " (core dumped)" : "");
242 static void check_respawn(void) {
243 time_t t = time(NULL);
245 static time_t timestamp;
248 if (timestamp >= t - 120)
256 unsigned int time_left = 300;
258 syslog(LOG_ERR, "Error: collectd is respawning too fast - "
259 "disabled for %i seconds",
262 while (((time_left = sleep(time_left)) > 0) && loop == 0)
266 } /* check_respawn */
268 int main(int argc, char **argv) {
269 int collectd_argc = 0;
270 char *collectd = NULL;
271 char **collectd_argv = NULL;
275 /* parse command line options */
277 int c = getopt(argc, argv, "hc:P:");
295 for (i = optind; i < argc; ++i)
296 if (strcmp(argv[i], "-f") == 0)
299 /* i < argc => -f already present */
300 collectd_argc = 1 + argc - optind + ((i < argc) ? 0 : 1);
301 collectd_argv = calloc(collectd_argc + 1, sizeof(*collectd_argv));
303 if (collectd_argv == NULL) {
304 fprintf(stderr, "Out of memory.");
308 collectd_argv[0] = (collectd == NULL) ? "collectd" : collectd;
311 collectd_argv[collectd_argc - 1] = "-f";
313 for (i = optind; i < argc; ++i)
314 collectd_argv[i - optind + 1] = argv[i];
316 collectd_argv[collectd_argc] = NULL;
318 openlog("collectdmon", LOG_CONS | LOG_PID, LOG_DAEMON);
320 if (daemonize() == -1) {
325 struct sigaction sa = {
326 .sa_handler = sig_int_term_handler, .sa_flags = 0,
328 sigemptyset(&sa.sa_mask);
330 if (sigaction(SIGINT, &sa, NULL) != 0) {
331 syslog(LOG_ERR, "Error: sigaction() failed: %s", strerror(errno));
336 if (sigaction(SIGTERM, &sa, NULL) != 0) {
337 syslog(LOG_ERR, "Error: sigaction() failed: %s", strerror(errno));
342 sa.sa_handler = sig_hup_handler;
344 if (sigaction(SIGHUP, &sa, NULL) != 0) {
345 syslog(LOG_ERR, "Error: sigaction() failed: %s", strerror(errno));
353 if (collectd_start(collectd_argv) != 0) {
354 syslog(LOG_ERR, "Error: failed to start collectd.");
358 assert(collectd_pid >= 0);
359 while ((collectd_pid != waitpid(collectd_pid, &status, 0)) &&
361 if (loop != 0 || restart != 0)
370 syslog(LOG_INFO, "Info: restarting collectd");
372 } else if (loop == 0)
373 syslog(LOG_WARNING, "Warning: restarting collectd");
376 syslog(LOG_INFO, "Info: shutting down collectdmon");