Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / collectdmon.c
1 /**
2  * collectd - src/collectdmon.c
3  * Copyright (C) 2007       Sebastian Harl
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Sebastian Harl <sh at tokkee.org>
25  **/
26
27 #if !defined(__GNUC__) || !__GNUC__
28 # define __attribute__(x) /**/
29 #endif
30
31 #include "config.h"
32
33 #include <assert.h>
34
35 #include <errno.h>
36
37 #include <fcntl.h>
38
39 #include <signal.h>
40
41 #include <stdio.h>
42 #include <stdlib.h>
43
44 #include <string.h>
45
46 #include <syslog.h>
47
48 #include <sys/resource.h>
49 #include <sys/time.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <sys/wait.h>
53
54 #include <time.h>
55
56 #include <unistd.h>
57
58 #ifndef COLLECTDMON_PIDFILE
59 # define COLLECTDMON_PIDFILE LOCALSTATEDIR"/run/collectdmon.pid"
60 #endif /* ! COLLECTDMON_PIDFILE */
61
62 #ifndef WCOREDUMP
63 # define WCOREDUMP(s) 0
64 #endif /* ! WCOREDUMP */
65
66 static int loop    = 0;
67 static int restart = 0;
68
69 static char  *pidfile      = NULL;
70 static pid_t  collectd_pid = 0;
71
72 static void exit_usage (char *name)
73 {
74         printf ("Usage: %s <options> [-- <collectd options>]\n"
75
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"
80
81                         "\nFor <collectd options> see collectd.conf(5).\n"
82
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);
86         exit (0);
87 } /* exit_usage */
88
89 static int pidfile_create (void)
90 {
91         FILE *file = NULL;
92
93         if (NULL == pidfile)
94                 pidfile = COLLECTDMON_PIDFILE;
95
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));
99                 return -1;
100         }
101
102         fprintf (file, "%i\n", (int)getpid ());
103         fclose (file);
104         return 0;
105 } /* pidfile_create */
106
107 static int pidfile_delete (void)
108 {
109         assert (NULL != pidfile);
110
111         if (0 != unlink (pidfile)) {
112                 syslog (LOG_ERR, "Error: couldn't delete PID-file (%s): %s",
113                                 pidfile, strerror (errno));
114                 return -1;
115         }
116         return 0;
117 } /* pidfile_remove */
118
119 static int daemonize (void)
120 {
121         struct rlimit rl;
122
123         pid_t pid = 0;
124         int   i   = 0;
125
126         if (0 != chdir ("/")) {
127                 fprintf (stderr, "Error: chdir() failed: %s\n", strerror (errno));
128                 return -1;
129         }
130
131         if (0 != getrlimit (RLIMIT_NOFILE, &rl)) {
132                 fprintf (stderr, "Error: getrlimit() failed: %s\n", strerror (errno));
133                 return -1;
134         }
135
136         if (0 > (pid = fork ())) {
137                 fprintf (stderr, "Error: fork() failed: %s\n", strerror (errno));
138                 return -1;
139         }
140         else if (pid != 0) {
141                 exit (0);
142         }
143
144         if (0 != pidfile_create ())
145                 return -1;
146
147         setsid ();
148
149         if (RLIM_INFINITY == rl.rlim_max)
150                 rl.rlim_max = 1024;
151
152         for (i = 0; i < (int)rl.rlim_max; ++i)
153                 close (i);
154
155         errno = 0;
156         if (open ("/dev/null", O_RDWR) != 0) {
157                 syslog (LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s",
158                                 strerror (errno));
159                 return -1;
160         }
161
162         errno = 0;
163         if (dup (0) != 1) {
164                 syslog (LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s",
165                                 strerror (errno));
166                 return -1;
167         }
168
169         errno = 0;
170         if (dup (0) != 2) {
171                 syslog (LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s",
172                                 strerror (errno));
173                 return -1;
174         }
175         return 0;
176 } /* daemonize */
177
178 static int collectd_start (char **argv)
179 {
180         pid_t pid = 0;
181
182         if (0 > (pid = fork ())) {
183                 syslog (LOG_ERR, "Error: fork() failed: %s", strerror (errno));
184                 return -1;
185         }
186         else if (pid != 0) {
187                 collectd_pid = pid;
188                 return 0;
189         }
190
191         execvp (argv[0], argv);
192         syslog (LOG_ERR, "Error: execvp(%s) failed: %s",
193                         argv[0], strerror (errno));
194         exit (-1);
195 } /* collectd_start */
196
197 static int collectd_stop (void)
198 {
199         if (0 == collectd_pid)
200                 return 0;
201
202         if (0 != kill (collectd_pid, SIGTERM)) {
203                 syslog (LOG_ERR, "Error: kill() failed: %s", strerror (errno));
204                 return -1;
205         }
206         return 0;
207 } /* collectd_stop */
208
209 static void sig_int_term_handler (int __attribute__((unused)) signo)
210 {
211         ++loop;
212         return;
213 } /* sig_int_term_handler */
214
215 static void sig_hup_handler (int __attribute__((unused)) signo)
216 {
217         ++restart;
218         return;
219 } /* sig_hup_handler */
220
221 static void log_status (int status)
222 {
223         if (WIFEXITED (status)) {
224                 if (0 == WEXITSTATUS (status))
225                         syslog (LOG_INFO, "Info: collectd terminated with exit status %i",
226                                         WEXITSTATUS (status));
227                 else
228                         syslog (LOG_WARNING,
229                                         "Warning: collectd terminated with exit status %i",
230                                         WEXITSTATUS (status));
231         }
232         else if (WIFSIGNALED (status)) {
233                 syslog (LOG_WARNING, "Warning: collectd was terminated by signal %i%s",
234                                 WTERMSIG (status), WCOREDUMP (status) ? " (core dumped)" : "");
235         }
236         return;
237 } /* log_status */
238
239 static void check_respawn (void)
240 {
241         time_t t = time (NULL);
242
243         static time_t timestamp = 0;
244         static int    counter   = 0;
245
246         if ((t - 120) < timestamp)
247                 ++counter;
248         else {
249                 timestamp = t;
250                 counter   = 0;
251         }
252
253         if (10 < counter) {
254                 unsigned int time_left = 300;
255
256                 syslog (LOG_ERR, "Error: collectd is respawning too fast - "
257                                 "disabled for %i seconds", time_left);
258
259                 while ((0 < (time_left = sleep (time_left))) && (0 == loop));
260         }
261         return;
262 } /* check_respawn */
263
264 int main (int argc, char **argv)
265 {
266         int    collectd_argc = 0;
267         char  *collectd      = NULL;
268         char **collectd_argv = NULL;
269
270         struct sigaction sa;
271
272         int i = 0;
273
274         /* parse command line options */
275         while (42) {
276                 int c = getopt (argc, argv, "hc:P:");
277
278                 if (-1 == c)
279                         break;
280
281                 switch (c) {
282                         case 'c':
283                                 collectd = optarg;
284                                 break;
285                         case 'P':
286                                 pidfile = optarg;
287                                 break;
288                         case 'h':
289                         default:
290                                 exit_usage (argv[0]);
291                 }
292         }
293
294         for (i = optind; i < argc; ++i)
295                 if (0 == strcmp (argv[i], "-f"))
296                         break;
297
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 *));
301
302         if (NULL == collectd_argv) {
303                 fprintf (stderr, "Out of memory.");
304                 return 3;
305         }
306
307         collectd_argv[0] = (NULL == collectd) ? "collectd" : collectd;
308
309         if (i == argc)
310                 collectd_argv[collectd_argc - 1] = "-f";
311
312         for (i = optind; i < argc; ++i)
313                 collectd_argv[i - optind + 1] = argv[i];
314
315         collectd_argv[collectd_argc] = NULL;
316
317         openlog ("collectdmon", LOG_CONS | LOG_PID, LOG_DAEMON);
318
319         if (-1 == daemonize ())
320         {
321                 free (collectd_argv);
322                 return 1;
323         }
324
325         sa.sa_handler = sig_int_term_handler;
326         sa.sa_flags   = 0;
327         sigemptyset (&sa.sa_mask);
328
329         if (0 != sigaction (SIGINT, &sa, NULL)) {
330                 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
331                 free (collectd_argv);
332                 return 1;
333         }
334
335         if (0 != sigaction (SIGTERM, &sa, NULL)) {
336                 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
337                 free (collectd_argv);
338                 return 1;
339         }
340
341         sa.sa_handler = sig_hup_handler;
342
343         if (0 != sigaction (SIGHUP, &sa, NULL)) {
344                 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
345                 free (collectd_argv);
346                 return 1;
347         }
348
349         while (0 == loop) {
350                 int status = 0;
351
352                 if (0 != collectd_start (collectd_argv)) {
353                         syslog (LOG_ERR, "Error: failed to start collectd.");
354                         break;
355                 }
356
357                 assert (0 < collectd_pid);
358                 while ((collectd_pid != waitpid (collectd_pid, &status, 0))
359                                 && (EINTR == errno))
360                         if ((0 != loop) || (0 != restart))
361                                 collectd_stop ();
362
363                 collectd_pid = 0;
364
365                 log_status (status);
366                 check_respawn ();
367
368                 if (0 != restart) {
369                         syslog (LOG_INFO, "Info: restarting collectd");
370                         restart = 0;
371                 }
372                 else if (0 == loop)
373                         syslog (LOG_WARNING, "Warning: restarting collectd");
374         }
375
376         syslog (LOG_INFO, "Info: shutting down collectdmon");
377
378         pidfile_delete ();
379         closelog ();
380
381         free (collectd_argv);
382         return 0;
383 } /* main */
384
385 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
386