Merge branch '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         int dev_null;
123
124         pid_t pid = 0;
125         int   i   = 0;
126
127         if (0 != chdir ("/")) {
128                 fprintf (stderr, "Error: chdir() failed: %s\n", strerror (errno));
129                 return -1;
130         }
131
132         if (0 != getrlimit (RLIMIT_NOFILE, &rl)) {
133                 fprintf (stderr, "Error: getrlimit() failed: %s\n", strerror (errno));
134                 return -1;
135         }
136
137         if (0 > (pid = fork ())) {
138                 fprintf (stderr, "Error: fork() failed: %s\n", strerror (errno));
139                 return -1;
140         }
141         else if (pid != 0) {
142                 exit (0);
143         }
144
145         if (0 != pidfile_create ())
146                 return -1;
147
148         setsid ();
149
150         if (RLIM_INFINITY == rl.rlim_max)
151                 rl.rlim_max = 1024;
152
153         for (i = 0; i < (int)rl.rlim_max; ++i)
154                 close (i);
155
156         dev_null = open ("/dev/null", O_RDWR);
157         if (dev_null == -1) {
158                 syslog (LOG_ERR, "Error: couldn't failed to open /dev/null: %s", strerror (errno));
159                 return -1;
160         }
161
162         if (dup2 (dev_null, STDIN_FILENO) == -1) {
163                 syslog (LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s", strerror (errno));
164                 return -1;
165         }
166
167         if (dup2 (dev_null, STDOUT_FILENO) == -1) {
168                 syslog (LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s", strerror (errno));
169                 return -1;
170         }
171
172         if (dup2 (dev_null, STDERR_FILENO) == -1) {
173                 syslog (LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s", strerror (errno));
174                 return -1;
175         }
176
177         if ((dev_null != STDIN_FILENO) && (dev_null != STDOUT_FILENO) && (dev_null != STDERR_FILENO))
178                 close (dev_null);
179
180         return 0;
181 } /* daemonize */
182
183 static int collectd_start (char **argv)
184 {
185         pid_t pid = 0;
186
187         if (0 > (pid = fork ())) {
188                 syslog (LOG_ERR, "Error: fork() failed: %s", strerror (errno));
189                 return -1;
190         }
191         else if (pid != 0) {
192                 collectd_pid = pid;
193                 return 0;
194         }
195
196         execvp (argv[0], argv);
197         syslog (LOG_ERR, "Error: execvp(%s) failed: %s",
198                         argv[0], strerror (errno));
199         exit (-1);
200 } /* collectd_start */
201
202 static int collectd_stop (void)
203 {
204         if (0 == collectd_pid)
205                 return 0;
206
207         if (0 != kill (collectd_pid, SIGTERM)) {
208                 syslog (LOG_ERR, "Error: kill() failed: %s", strerror (errno));
209                 return -1;
210         }
211         return 0;
212 } /* collectd_stop */
213
214 static void sig_int_term_handler (int __attribute__((unused)) signo)
215 {
216         ++loop;
217         return;
218 } /* sig_int_term_handler */
219
220 static void sig_hup_handler (int __attribute__((unused)) signo)
221 {
222         ++restart;
223         return;
224 } /* sig_hup_handler */
225
226 static void log_status (int status)
227 {
228         if (WIFEXITED (status)) {
229                 if (0 == WEXITSTATUS (status))
230                         syslog (LOG_INFO, "Info: collectd terminated with exit status %i",
231                                         WEXITSTATUS (status));
232                 else
233                         syslog (LOG_WARNING,
234                                         "Warning: collectd terminated with exit status %i",
235                                         WEXITSTATUS (status));
236         }
237         else if (WIFSIGNALED (status)) {
238                 syslog (LOG_WARNING, "Warning: collectd was terminated by signal %i%s",
239                                 WTERMSIG (status), WCOREDUMP (status) ? " (core dumped)" : "");
240         }
241         return;
242 } /* log_status */
243
244 static void check_respawn (void)
245 {
246         time_t t = time (NULL);
247
248         static time_t timestamp = 0;
249         static int    counter   = 0;
250
251         if ((t - 120) < timestamp)
252                 ++counter;
253         else {
254                 timestamp = t;
255                 counter   = 0;
256         }
257
258         if (10 < counter) {
259                 unsigned int time_left = 300;
260
261                 syslog (LOG_ERR, "Error: collectd is respawning too fast - "
262                                 "disabled for %i seconds", time_left);
263
264                 while ((0 < (time_left = sleep (time_left))) && (0 == loop));
265         }
266         return;
267 } /* check_respawn */
268
269 int main (int argc, char **argv)
270 {
271         int    collectd_argc = 0;
272         char  *collectd      = NULL;
273         char **collectd_argv = NULL;
274
275         struct sigaction sa;
276
277         int i = 0;
278
279         /* parse command line options */
280         while (42) {
281                 int c = getopt (argc, argv, "hc:P:");
282
283                 if (-1 == c)
284                         break;
285
286                 switch (c) {
287                         case 'c':
288                                 collectd = optarg;
289                                 break;
290                         case 'P':
291                                 pidfile = optarg;
292                                 break;
293                         case 'h':
294                         default:
295                                 exit_usage (argv[0]);
296                 }
297         }
298
299         for (i = optind; i < argc; ++i)
300                 if (0 == strcmp (argv[i], "-f"))
301                         break;
302
303         /* i < argc => -f already present */
304         collectd_argc = 1 + argc - optind + ((i < argc) ? 0 : 1);
305         collectd_argv = (char **)calloc (collectd_argc + 1, sizeof (char *));
306
307         if (NULL == collectd_argv) {
308                 fprintf (stderr, "Out of memory.");
309                 return 3;
310         }
311
312         collectd_argv[0] = (NULL == collectd) ? "collectd" : collectd;
313
314         if (i == argc)
315                 collectd_argv[collectd_argc - 1] = "-f";
316
317         for (i = optind; i < argc; ++i)
318                 collectd_argv[i - optind + 1] = argv[i];
319
320         collectd_argv[collectd_argc] = NULL;
321
322         openlog ("collectdmon", LOG_CONS | LOG_PID, LOG_DAEMON);
323
324         if (-1 == daemonize ())
325         {
326                 free (collectd_argv);
327                 return 1;
328         }
329
330         sa.sa_handler = sig_int_term_handler;
331         sa.sa_flags   = 0;
332         sigemptyset (&sa.sa_mask);
333
334         if (0 != sigaction (SIGINT, &sa, NULL)) {
335                 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
336                 free (collectd_argv);
337                 return 1;
338         }
339
340         if (0 != sigaction (SIGTERM, &sa, NULL)) {
341                 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
342                 free (collectd_argv);
343                 return 1;
344         }
345
346         sa.sa_handler = sig_hup_handler;
347
348         if (0 != sigaction (SIGHUP, &sa, NULL)) {
349                 syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno));
350                 free (collectd_argv);
351                 return 1;
352         }
353
354         while (0 == loop) {
355                 int status = 0;
356
357                 if (0 != collectd_start (collectd_argv)) {
358                         syslog (LOG_ERR, "Error: failed to start collectd.");
359                         break;
360                 }
361
362                 assert (0 < collectd_pid);
363                 while ((collectd_pid != waitpid (collectd_pid, &status, 0))
364                                 && (EINTR == errno))
365                         if ((0 != loop) || (0 != restart))
366                                 collectd_stop ();
367
368                 collectd_pid = 0;
369
370                 log_status (status);
371                 check_respawn ();
372
373                 if (0 != restart) {
374                         syslog (LOG_INFO, "Info: restarting collectd");
375                         restart = 0;
376                 }
377                 else if (0 == loop)
378                         syslog (LOG_WARNING, "Warning: restarting collectd");
379         }
380
381         syslog (LOG_INFO, "Info: shutting down collectdmon");
382
383         pidfile_delete ();
384         closelog ();
385
386         free (collectd_argv);
387         return 0;
388 } /* main */
389
390 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
391