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 */
75 static int restart = 0;
77 static const char *pidfile = NULL;
78 static pid_t collectd_pid = 0;
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 (NULL == (file = fopen(pidfile, "w"))) {
104 syslog(LOG_ERR, "Error: couldn't open PID-file (%s) for writing: %s",
105 pidfile, strerror(errno));
109 fprintf(file, "%i\n", (int)getpid());
112 } /* pidfile_create */
114 static int pidfile_delete(void) {
115 assert(NULL != pidfile);
117 if (0 != unlink(pidfile)) {
118 syslog(LOG_ERR, "Error: couldn't delete PID-file (%s): %s", pidfile,
123 } /* pidfile_remove */
125 static int daemonize(void) {
132 if (0 != chdir("/")) {
133 fprintf(stderr, "Error: chdir() failed: %s\n", strerror(errno));
137 if (0 != getrlimit(RLIMIT_NOFILE, &rl)) {
138 fprintf(stderr, "Error: getrlimit() failed: %s\n", strerror(errno));
142 if (0 > (pid = fork())) {
143 fprintf(stderr, "Error: fork() failed: %s\n", strerror(errno));
145 } else if (pid != 0) {
149 if (0 != pidfile_create())
154 if (RLIM_INFINITY == rl.rlim_max)
157 for (i = 0; i < (int)rl.rlim_max; ++i)
160 dev_null = open("/dev/null", O_RDWR);
161 if (dev_null == -1) {
162 syslog(LOG_ERR, "Error: couldn't open /dev/null: %s", strerror(errno));
166 if (dup2(dev_null, STDIN_FILENO) == -1) {
168 syslog(LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s",
173 if (dup2(dev_null, STDOUT_FILENO) == -1) {
175 syslog(LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s",
180 if (dup2(dev_null, STDERR_FILENO) == -1) {
182 syslog(LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s",
187 if ((dev_null != STDIN_FILENO) && (dev_null != STDOUT_FILENO) &&
188 (dev_null != STDERR_FILENO))
194 static int collectd_start(char **argv) {
197 if (0 > (pid = fork())) {
198 syslog(LOG_ERR, "Error: fork() failed: %s", strerror(errno));
200 } else if (pid != 0) {
205 execvp(argv[0], argv);
206 syslog(LOG_ERR, "Error: execvp(%s) failed: %s", argv[0], strerror(errno));
208 } /* collectd_start */
210 static int collectd_stop(void) {
211 if (0 == collectd_pid)
214 if (0 != kill(collectd_pid, SIGTERM)) {
215 syslog(LOG_ERR, "Error: kill() failed: %s", strerror(errno));
219 } /* collectd_stop */
221 static void sig_int_term_handler(int __attribute__((unused)) signo) {
224 } /* sig_int_term_handler */
226 static void sig_hup_handler(int __attribute__((unused)) signo) {
229 } /* sig_hup_handler */
231 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));
237 syslog(LOG_WARNING, "Warning: collectd terminated with exit status %i",
238 WEXITSTATUS(status));
239 } else if (WIFSIGNALED(status)) {
240 syslog(LOG_WARNING, "Warning: collectd was terminated by signal %i%s",
241 WTERMSIG(status), WCOREDUMP(status) ? " (core dumped)" : "");
246 static void check_respawn(void) {
247 time_t t = time(NULL);
249 static time_t timestamp = 0;
250 static int counter = 0;
252 if ((t - 120) < timestamp)
260 unsigned int time_left = 300;
262 syslog(LOG_ERR, "Error: collectd is respawning too fast - "
263 "disabled for %i seconds",
266 while ((0 < (time_left = sleep(time_left))) && (0 == loop))
270 } /* check_respawn */
272 int main(int argc, char **argv) {
273 int collectd_argc = 0;
274 char *collectd = NULL;
275 char **collectd_argv = NULL;
281 /* parse command line options */
283 int c = getopt(argc, argv, "hc:P:");
301 for (i = optind; i < argc; ++i)
302 if (0 == strcmp(argv[i], "-f"))
305 /* i < argc => -f already present */
306 collectd_argc = 1 + argc - optind + ((i < argc) ? 0 : 1);
307 collectd_argv = (char **)calloc(collectd_argc + 1, sizeof(char *));
309 if (NULL == collectd_argv) {
310 fprintf(stderr, "Out of memory.");
314 collectd_argv[0] = (NULL == collectd) ? "collectd" : collectd;
317 collectd_argv[collectd_argc - 1] = "-f";
319 for (i = optind; i < argc; ++i)
320 collectd_argv[i - optind + 1] = argv[i];
322 collectd_argv[collectd_argc] = NULL;
324 openlog("collectdmon", LOG_CONS | LOG_PID, LOG_DAEMON);
326 if (-1 == daemonize()) {
331 sa.sa_handler = sig_int_term_handler;
333 sigemptyset(&sa.sa_mask);
335 if (0 != sigaction(SIGINT, &sa, NULL)) {
336 syslog(LOG_ERR, "Error: sigaction() failed: %s", strerror(errno));
341 if (0 != sigaction(SIGTERM, &sa, NULL)) {
342 syslog(LOG_ERR, "Error: sigaction() failed: %s", strerror(errno));
347 sa.sa_handler = sig_hup_handler;
349 if (0 != sigaction(SIGHUP, &sa, NULL)) {
350 syslog(LOG_ERR, "Error: sigaction() failed: %s", strerror(errno));
358 if (0 != collectd_start(collectd_argv)) {
359 syslog(LOG_ERR, "Error: failed to start collectd.");
363 assert(0 < collectd_pid);
364 while ((collectd_pid != waitpid(collectd_pid, &status, 0)) &&
366 if ((0 != loop) || (0 != restart))
375 syslog(LOG_INFO, "Info: restarting collectd");
377 } else if (0 == loop)
378 syslog(LOG_WARNING, "Warning: restarting collectd");
381 syslog(LOG_INFO, "Info: shutting down collectdmon");