Debian Codegraph @

src/exec.c

raw
1 /**
2 * collectd - src/exec.c
3 * Copyright (C) 2007-2010 Florian octo Forster
4 * Copyright (C) 2007-2009 Sebastian Harl
5 * Copyright (C) 2008 Peter Holik
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; only version 2 of the License is applicable.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <octo at collectd.org>
22 * Sebastian Harl <sh at tokkee.org>
23 * Peter Holik <peter at holik.at>
24 **/
25
26 #define _DEFAULT_SOURCE
27 #define _BSD_SOURCE /* For setgroups */
28
29 /* _GNU_SOURCE is needed in Linux to use execvpe */
30 #define _GNU_SOURCE
31
32 #include "collectd.h"
33
34 #include "plugin.h"
35 #include "utils/common/common.h"
36
37 #include "utils/cmds/putnotif.h"
38 #include "utils/cmds/putval.h"
39
40 #include <grp.h>
41 #include <poll.h>
42 #include <pwd.h>
43 #include <signal.h>
44 #include <sys/types.h>
45
46 #ifdef HAVE_SYS_CAPABILITY_H
47 #include <sys/capability.h>
48 #endif
49
50 extern char **environ;
51
52 #define PL_NORMAL 0x01
53 #define PL_NOTIF_ACTION 0x02
54
55 #define PL_RUNNING 0x10
56
57 /*
58 * Private data types
59 */
60 /*
61 * Access to this structure is serialized using the `pl_lock' lock and the
62 * `PL_RUNNING' flag. The execution of notifications is *not* serialized, so
63 * all functions used to handle notifications MUST NOT write to this structure.
64 * The `pid' and `status' fields are thus unused if the `PL_NOTIF_ACTION' flag
65 * is set.
66 * The `PL_RUNNING' flag is set in `exec_read' and unset in `exec_read_one'.
67 */
68 struct program_list_s;
69 typedef struct program_list_s program_list_t;
70 struct program_list_s {
71 char *user;
72 char *group;
73 char *exec;
74 char **argv;
75 int pid;
76 int status;
77 int flags;
78 program_list_t *next;
79 };
80
81 typedef struct program_list_and_notification_s {
82 program_list_t *pl;
83 notification_t n;
84 } program_list_and_notification_t;
85
86 /*
87 * constants
88 */
89 const long int MAX_GRBUF_SIZE = 65536;
90
91 /*
92 * Private variables
93 */
94 static program_list_t *pl_head;
95 static pthread_mutex_t pl_lock = PTHREAD_MUTEX_INITIALIZER;
96
97 /*
98 * Functions
99 */
100 static void sigchld_handler(int __attribute__((unused)) signal) /* {{{ */
101 {
102 pid_t pid;
103 int status;
104 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
105 program_list_t *pl;
106 for (pl = pl_head; pl != NULL; pl = pl->next)
107 if (pl->pid == pid)
108 break;
109 if (pl != NULL)
110 pl->status = status;
111 } /* while (waitpid) */
112 } /* void sigchld_handler }}} */
113
114 static int exec_config_exec(oconfig_item_t *ci) /* {{{ */
115 {
116 program_list_t *pl;
117 char buffer[128];
118 int i;
119
120 if (ci->children_num != 0) {
121 WARNING("exec plugin: The config option `%s' may not be a block.", ci->key);
122 return -1;
123 }
124 if (ci->values_num < 2) {
125 WARNING("exec plugin: The config option `%s' needs at least two "
126 "arguments.",
127 ci->key);
128 return -1;
129 }
130 if ((ci->values[0].type != OCONFIG_TYPE_STRING) ||
131 (ci->values[1].type != OCONFIG_TYPE_STRING)) {
132 WARNING("exec plugin: The first two arguments to the `%s' option must "
133 "be string arguments.",
134 ci->key);
135 return -1;
136 }
137
138 pl = calloc(1, sizeof(*pl));
139 if (pl == NULL) {
140 ERROR("exec plugin: calloc failed.");
141 return -1;
142 }
143
144 if (strcasecmp("NotificationExec", ci->key) == 0)
145 pl->flags |= PL_NOTIF_ACTION;
146 else
147 pl->flags |= PL_NORMAL;
148
149 pl->user = strdup(ci->values[0].value.string);
150 if (pl->user == NULL) {
151 ERROR("exec plugin: strdup failed.");
152 sfree(pl);
153 return -1;
154 }
155
156 pl->group = strchr(pl->user, ':');
157 if (pl->group != NULL) {
158 *pl->group = '\0';
159 pl->group++;
160 }
161
162 pl->exec = strdup(ci->values[1].value.string);
163 if (pl->exec == NULL) {
164 ERROR("exec plugin: strdup failed.");
165 sfree(pl->user);
166 sfree(pl);
167 return -1;
168 }
169
170 pl->argv = calloc(ci->values_num, sizeof(*pl->argv));
171 if (pl->argv == NULL) {
172 ERROR("exec plugin: calloc failed.");
173 sfree(pl->exec);
174 sfree(pl->user);
175 sfree(pl);
176 return -1;
177 }
178
179 {
180 char *tmp = strrchr(ci->values[1].value.string, '/');
181 if (tmp == NULL)
182 sstrncpy(buffer, ci->values[1].value.string, sizeof(buffer));
183 else
184 sstrncpy(buffer, tmp + 1, sizeof(buffer));
185 }
186 pl->argv[0] = strdup(buffer);
187 if (pl->argv[0] == NULL) {
188 ERROR("exec plugin: strdup failed.");
189 sfree(pl->argv);
190 sfree(pl->exec);
191 sfree(pl->user);
192 sfree(pl);
193 return -1;
194 }
195
196 for (i = 1; i < (ci->values_num - 1); i++) {
197 if (ci->values[i + 1].type == OCONFIG_TYPE_STRING) {
198 pl->argv[i] = strdup(ci->values[i + 1].value.string);
199 } else {
200 if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER) {
201 snprintf(buffer, sizeof(buffer), "%lf", ci->values[i + 1].value.number);
202 } else {
203 if (ci->values[i + 1].value.boolean)
204 sstrncpy(buffer, "true", sizeof(buffer));
205 else
206 sstrncpy(buffer, "false", sizeof(buffer));
207 }
208
209 pl->argv[i] = strdup(buffer);
210 }
211
212 if (pl->argv[i] == NULL) {
213 ERROR("exec plugin: strdup failed.");
214 break;
215 }
216 } /* for (i) */
217
218 if (i < (ci->values_num - 1)) {
219 while ((--i) >= 0) {
220 sfree(pl->argv[i]);
221 }
222 sfree(pl->argv);
223 sfree(pl->exec);
224 sfree(pl->user);
225 sfree(pl);
226 return -1;
227 }
228
229 for (i = 0; pl->argv[i] != NULL; i++) {
230 DEBUG("exec plugin: argv[%i] = %s", i, pl->argv[i]);
231 }
232
233 pl->next = pl_head;
234 pl_head = pl;
235
236 return 0;
237 } /* int exec_config_exec }}} */
238
239 static int exec_config(oconfig_item_t *ci) /* {{{ */
240 {
241 for (int i = 0; i < ci->children_num; i++) {
242 oconfig_item_t *child = ci->children + i;
243 if ((strcasecmp("Exec", child->key) == 0) ||
244 (strcasecmp("NotificationExec", child->key) == 0))
245 exec_config_exec(child);
246 else {
247 WARNING("exec plugin: Unknown config option `%s'.", child->key);
248 }
249 } /* for (i) */
250
251 return 0;
252 } /* int exec_config }}} */
253
254 __attribute__((noreturn)) static void exec_child(program_list_t *pl,
255 char **envp, int uid, int gid,
256 int egid) /* {{{ */
257 {
258 int status;
259
260 #if HAVE_SETGROUPS
261 if (getuid() == 0) {
262 gid_t glist[2];
263 size_t glist_len;
264
265 glist[0] = gid;
266 glist_len = 1;
267
268 if ((gid != egid) && (egid != -1)) {
269 glist[1] = egid;
270 glist_len = 2;
271 }
272
273 setgroups(glist_len, glist);
274 }
275 #endif /* HAVE_SETGROUPS */
276
277 status = setgid(gid);
278 if (status != 0) {
279 ERROR("exec plugin: setgid (%i) failed: %s", gid, STRERRNO);
280 exit(-1);
281 }
282
283 if (egid != -1) {
284 status = setegid(egid);
285 if (status != 0) {
286 ERROR("exec plugin: setegid (%i) failed: %s", egid, STRERRNO);
287 exit(-1);
288 }
289 }
290
291 status = setuid(uid);
292 if (status != 0) {
293 ERROR("exec plugin: setuid (%i) failed: %s", uid, STRERRNO);
294 exit(-1);
295 }
296
297 #ifdef HAVE_EXECVPE
298 execvpe(pl->exec, pl->argv, envp);
299 #else
300 environ = envp;
301 execvp(pl->exec, pl->argv);
302 #endif
303
304 ERROR("exec plugin: Failed to execute ``%s'': %s", pl->exec, STRERRNO);
305 exit(-1);
306 } /* void exec_child }}} */
307
308 static void reset_signal_mask(void) /* {{{ */
309 {
310 sigset_t ss;
311
312 sigemptyset(&ss);
313 sigprocmask(SIG_SETMASK, &ss, /* old mask = */ NULL);
314 } /* }}} void reset_signal_mask */
315
316 static int create_pipe(int fd_pipe[2]) /* {{{ */
317 {
318 int status;
319
320 status = pipe(fd_pipe);
321 if (status != 0) {
322 ERROR("exec plugin: pipe failed: %s", STRERRNO);
323 return -1;
324 }
325
326 return 0;
327 } /* }}} int create_pipe */
328
329 static void close_pipe(int fd_pipe[2]) /* {{{ */
330 {
331 if (fd_pipe[0] != -1)
332 close(fd_pipe[0]);
333
334 if (fd_pipe[1] != -1)
335 close(fd_pipe[1]);
336 } /* }}} void close_pipe */
337
338 /*
339 * Get effective group ID from group name.
340 * Input arguments:
341 * pl :program list struct with group name
342 * gid :group id to fallback in case egid cannot be determined.
343 * Returns:
344 * egid effective group id if successfull,
345 * -1 if group is not defined/not found.
346 * -2 for any buffer allocation error.
347 */
348 static int getegr_id(program_list_t *pl, int gid) /* {{{ */
349 {
350 if (pl->group == NULL) {
351 return -1;
352 }
353 if (strcmp(pl->group, "") == 0) {
354 return gid;
355 }
356 struct group *gr_ptr = NULL;
357 struct group gr;
358
359 long int grbuf_size = sysconf(_SC_GETGR_R_SIZE_MAX);
360 if (grbuf_size <= 0)
361 grbuf_size = sysconf(_SC_PAGESIZE);
362 if (grbuf_size <= 0)
363 grbuf_size = 4096;
364
365 char *temp = NULL;
366 char *grbuf = NULL;
367
368 do {
369 temp = realloc(grbuf, grbuf_size);
370 if (temp == NULL) {
371 ERROR("exec plugin: getegr_id for %s: realloc buffer[%ld] failed ",
372 pl->group, grbuf_size);
373 sfree(grbuf);
374 return -2;
375 }
376 grbuf = temp;
377 if (getgrnam_r(pl->group, &gr, grbuf, grbuf_size, &gr_ptr) == 0) {
378 sfree(grbuf);
379 if (gr_ptr == NULL) {
380 ERROR("exec plugin: No such group: `%s'", pl->group);
381 return -1;
382 }
383 return gr.gr_gid;
384 } else if (errno == ERANGE) {
385 grbuf_size += grbuf_size; // increment buffer size and try again
386 } else {
387 ERROR("exec plugin: getegr_id failed %s", STRERRNO);
388 sfree(grbuf);
389 return -2;
390 }
391 } while (grbuf_size <= MAX_GRBUF_SIZE);
392 ERROR("exec plugin: getegr_id Max grbuf size reached for %s", pl->group);
393 sfree(grbuf);
394 return -2;
395 }
396
397 /*
398 * Creates three pipes (one for reading, one for writing and one for errors),
399 * forks a child, sets up the pipes so that fd_in is connected to STDIN of
400 * the child and fd_out is connected to STDOUT and fd_err is connected to STDERR
401 * of the child. Then is calls `exec_child'.
402 */
403 static int fork_child(program_list_t *pl, int *fd_in, int *fd_out,
404 int *fd_err) /* {{{ */
405 {
406 int fd_pipe_in[2] = {-1, -1};
407 int fd_pipe_out[2] = {-1, -1};
408 int fd_pipe_err[2] = {-1, -1};
409 int status;
410 int pid;
411
412 int uid;
413 int gid;
414 int egid;
415
416 struct passwd *sp_ptr;
417 struct passwd sp;
418
419 if (pl->pid != 0)
420 return -1;
421
422 long int nambuf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
423 if (nambuf_size <= 0)
424 nambuf_size = sysconf(_SC_PAGESIZE);
425 if (nambuf_size <= 0)
426 nambuf_size = 4096;
427 char nambuf[nambuf_size];
428
429 if ((create_pipe(fd_pipe_in) == -1) || (create_pipe(fd_pipe_out) == -1) ||
430 (create_pipe(fd_pipe_err) == -1))
431 goto failed;
432
433 sp_ptr = NULL;
434 status = getpwnam_r(pl->user, &sp, nambuf, sizeof(nambuf), &sp_ptr);
435 if (status != 0) {
436 ERROR("exec plugin: Failed to get user information for user ``%s'': %s",
437 pl->user, STRERROR(status));
438 goto failed;
439 }
440
441 if (sp_ptr == NULL) {
442 ERROR("exec plugin: No such user: `%s'", pl->user);
443 goto failed;
444 }
445
446 uid = sp.pw_uid;
447 gid = sp.pw_gid;
448 if (uid == 0) {
449 ERROR("exec plugin: Cowardly refusing to exec program as root.");
450 goto failed;
451 }
452
453 /* The group configured in the configfile is set as effective group, because
454 * this way the forked process can (re-)gain the user's primary group. */
455 egid = getegr_id(pl, gid);
456 if (egid == -2) {
457 goto failed;
458 }
459
460 double interval = CDTIME_T_TO_DOUBLE(plugin_get_interval());
461
462 pid = fork();
463 if (pid < 0) {
464 ERROR("exec plugin: fork failed: %s", STRERRNO);
465 goto failed;
466 } else if (pid == 0) {
467 char interval_buf[128];
468 snprintf(interval_buf, sizeof(interval_buf), "COLLECTD_INTERVAL=%.3f",
469 interval);
470
471 /* max hostname len is 255, so this should be enough */
472 char hostname_buf[300];
473 snprintf(hostname_buf, sizeof(hostname_buf), "COLLECTD_HOSTNAME=%s",
474 hostname_g);
475
476 size_t env_size = 0;
477 while (environ[env_size] != NULL) {
478 ++env_size;
479 }
480
481 /* Copy the environment variables */
482 char *envp[env_size + 3];
483 size_t envp_idx;
484 for (envp_idx = 0; environ[envp_idx] != NULL && envp_idx < env_size;
485 ++envp_idx) {
486 envp[envp_idx] = environ[envp_idx];
487 }
488
489 /* Add the collectd environment variables */
490 envp[envp_idx++] = interval_buf;
491 envp[envp_idx++] = hostname_buf;
492 envp[envp_idx++] = NULL;
493
494 /* Close all file descriptors but the pipe end we need. */
495 int fd_num = getdtablesize();
496 for (int fd = 0; fd < fd_num; fd++) {
497 if ((fd == fd_pipe_in[0]) || (fd == fd_pipe_out[1]) ||
498 (fd == fd_pipe_err[1]))
499 continue;
500 close(fd);
501 }
502
503 /* Connect the `in' pipe to STDIN */
504 if (fd_pipe_in[0] != STDIN_FILENO) {
505 dup2(fd_pipe_in[0], STDIN_FILENO);
506 close(fd_pipe_in[0]);
507 }
508
509 /* Now connect the `out' pipe to STDOUT */
510 if (fd_pipe_out[1] != STDOUT_FILENO) {
511 dup2(fd_pipe_out[1], STDOUT_FILENO);
512 close(fd_pipe_out[1]);
513 }
514
515 /* Now connect the `err' pipe to STDERR */
516 if (fd_pipe_err[1] != STDERR_FILENO) {
517 dup2(fd_pipe_err[1], STDERR_FILENO);
518 close(fd_pipe_err[1]);
519 }
520
521 /* Unblock all signals */
522 reset_signal_mask();
523
524 exec_child(pl, envp, uid, gid, egid);
525 /* does not return */
526 }
527
528 close(fd_pipe_in[0]);
529 close(fd_pipe_out[1]);
530 close(fd_pipe_err[1]);
531
532 if (fd_in != NULL)
533 *fd_in = fd_pipe_in[1];
534 else
535 close(fd_pipe_in[1]);
536
537 if (fd_out != NULL)
538 *fd_out = fd_pipe_out[0];
539 else
540 close(fd_pipe_out[0]);
541
542 if (fd_err != NULL)
543 *fd_err = fd_pipe_err[0];
544 else
545 close(fd_pipe_err[0]);
546
547 return pid;
548
549 failed:
550 close_pipe(fd_pipe_in);
551 close_pipe(fd_pipe_out);
552 close_pipe(fd_pipe_err);
553
554 return -1;
555 } /* int fork_child }}} */
556
557 static int parse_line(char *buffer) /* {{{ */
558 {
559 if (strncasecmp("PUTVAL", buffer, strlen("PUTVAL")) == 0)
560 return cmd_handle_putval(stdout, buffer);
561 else if (strncasecmp("PUTNOTIF", buffer, strlen("PUTNOTIF")) == 0)
562 return handle_putnotif(stdout, buffer);
563 else {
564 ERROR("exec plugin: Unable to parse command, ignoring line: \"%s\"",
565 buffer);
566 return -1;
567 }
568 } /* int parse_line }}} */
569
570 static void *exec_read_one(void *arg) /* {{{ */
571 {
572 program_list_t *pl = (program_list_t *)arg;
573 int fd, fd_err;
574 struct pollfd fds[2] = {{0}};
575 int status;
576 char buffer[1200]; /* if not completely read */
577 char buffer_err[1024];
578 char *pbuffer = buffer;
579 char *pbuffer_err = buffer_err;
580
581 status = fork_child(pl, NULL, &fd, &fd_err);
582 if (status < 0) {
583 /* Reset the "running" flag */
584 pthread_mutex_lock(&pl_lock);
585 pl->flags &= ~PL_RUNNING;
586 pthread_mutex_unlock(&pl_lock);
587 pthread_exit((void *)1);
588 }
589 pl->pid = status;
590
591 assert(pl->pid != 0);
592
593 fds[0].fd = fd;
594 fds[0].events = POLLIN;
595 fds[1].fd = fd_err;
596 fds[1].events = POLLIN;
597
598 while (1) {
599 int len;
600
601 status = poll(fds, STATIC_ARRAY_SIZE(fds), -1);
602 if (status < 0) {
603 if (errno == EINTR)
604 continue;
605 break;
606 }
607
608 if (fds[0].revents & (POLLIN | POLLHUP)) {
609 char *pnl;
610
611 len = read(fd, pbuffer, sizeof(buffer) - 1 - (pbuffer - buffer));
612
613 if (len < 0) {
614 if (errno == EAGAIN || errno == EINTR)
615 continue;
616 break;
617 } else if (len == 0)
618 break; /* We've reached EOF */
619
620 pbuffer[len] = '\0';
621
622 len += pbuffer - buffer;
623 pbuffer = buffer;
624
625 while ((pnl = strchr(pbuffer, '\n'))) {
626 *pnl = '\0';
627 if (*(pnl - 1) == '\r')
628 *(pnl - 1) = '\0';
629
630 parse_line(pbuffer);
631
632 pbuffer = ++pnl;
633 }
634 /* not completely read ? */
635 if (pbuffer - buffer < len) {
636 len -= pbuffer - buffer;
637 memmove(buffer, pbuffer, len);
638 pbuffer = buffer + len;
639 } else
640 pbuffer = buffer;
641 } else if (fds[0].revents & (POLLERR | POLLNVAL)) {
642 ERROR("exec plugin: Failed to read pipe from `%s'.", pl->exec);
643 break;
644 } else if (fds[1].revents & (POLLIN | POLLHUP)) {
645 char *pnl;
646
647 len = read(fd_err, pbuffer_err,
648 sizeof(buffer_err) - 1 - (pbuffer_err - buffer_err));
649
650 if (len < 0) {
651 if (errno == EAGAIN || errno == EINTR)
652 continue;
653 break;
654 } else if (len == 0) {
655 /* We've reached EOF */
656 NOTICE("exec plugin: Program `%s' has closed STDERR.", pl->exec);
657
658 /* Clean up file descriptor */
659 close(fd_err);
660 fd_err = -1;
661 fds[1].fd = -1;
662 fds[1].events = 0;
663 continue;
664 }
665
666 pbuffer_err[len] = '\0';
667
668 len += pbuffer_err - buffer_err;
669 pbuffer_err = buffer_err;
670
671 while ((pnl = strchr(pbuffer_err, '\n'))) {
672 *pnl = '\0';
673 if (*(pnl - 1) == '\r')
674 *(pnl - 1) = '\0';
675
676 ERROR("exec plugin: exec_read_one: error = %s", pbuffer_err);
677
678 pbuffer_err = ++pnl;
679 }
680 /* not completely read ? */
681 if (pbuffer_err - buffer_err < len) {
682 len -= pbuffer_err - buffer_err;
683 memmove(buffer_err, pbuffer_err, len);
684 pbuffer_err = buffer_err + len;
685 } else
686 pbuffer_err = buffer_err;
687 } else if (fds[1].revents & (POLLERR | POLLNVAL)) {
688 WARNING("exec plugin: Ignoring STDERR for program `%s'.", pl->exec);
689 /* Clean up file descriptor */
690 if ((fds[1].revents & POLLNVAL) == 0) {
691 close(fd_err);
692 fd_err = -1;
693 }
694 fds[1].fd = -1;
695 fds[1].events = 0;
696 }
697 }
698
699 DEBUG("exec plugin: exec_read_one: Waiting for `%s' to exit.", pl->exec);
700 if (waitpid(pl->pid, &status, 0) > 0)
701 pl->status = status;
702
703 DEBUG("exec plugin: Child %i exited with status %i.", (int)pl->pid,
704 pl->status);
705
706 pl->pid = 0;
707
708 pthread_mutex_lock(&pl_lock);
709 pl->flags &= ~PL_RUNNING;
710 pthread_mutex_unlock(&pl_lock);
711
712 close(fd);
713 if (fd_err >= 0)
714 close(fd_err);
715
716 pthread_exit((void *)0);
717 return NULL;
718 } /* void *exec_read_one }}} */
719
720 static void *exec_notification_one(void *arg) /* {{{ */
721 {
722 program_list_t *pl = ((program_list_and_notification_t *)arg)->pl;
723 notification_t *n = &((program_list_and_notification_t *)arg)->n;
724 int fd;
725 FILE *fh;
726 int pid;
727 int status;
728 const char *severity;
729
730 pid = fork_child(pl, &fd, NULL, NULL);
731 if (pid < 0) {
732 sfree(arg);
733 pthread_exit((void *)1);
734 }
735
736 fh = fdopen(fd, "w");
737 if (fh == NULL) {
738 ERROR("exec plugin: fdopen (%i) failed: %s", fd, STRERRNO);
739 kill(pid, SIGTERM);
740 close(fd);
741 sfree(arg);
742 pthread_exit((void *)1);
743 }
744
745 severity = "FAILURE";
746 if (n->severity == NOTIF_WARNING)
747 severity = "WARNING";
748 else if (n->severity == NOTIF_OKAY)
749 severity = "OKAY";
750
751 fprintf(fh,
752 "Severity: %s\n"
753 "Time: %.3f\n",
754 severity, CDTIME_T_TO_DOUBLE(n->time));
755
756 /* Print the optional fields */
757 if (strlen(n->host) > 0)
758 fprintf(fh, "Host: %s\n", n->host);
759 if (strlen(n->plugin) > 0)
760 fprintf(fh, "Plugin: %s\n", n->plugin);
761 if (strlen(n->plugin_instance) > 0)
762 fprintf(fh, "PluginInstance: %s\n", n->plugin_instance);
763 if (strlen(n->type) > 0)
764 fprintf(fh, "Type: %s\n", n->type);
765 if (strlen(n->type_instance) > 0)
766 fprintf(fh, "TypeInstance: %s\n", n->type_instance);
767
768 for (notification_meta_t *meta = n->meta; meta != NULL; meta = meta->next) {
769 if (meta->type == NM_TYPE_STRING)
770 fprintf(fh, "%s: %s\n", meta->name, meta->nm_value.nm_string);
771 else if (meta->type == NM_TYPE_SIGNED_INT)
772 fprintf(fh, "%s: %" PRIi64 "\n", meta->name,
773 meta->nm_value.nm_signed_int);
774 else if (meta->type == NM_TYPE_UNSIGNED_INT)
775 fprintf(fh, "%s: %" PRIu64 "\n", meta->name,
776 meta->nm_value.nm_unsigned_int);
777 else if (meta->type == NM_TYPE_DOUBLE)
778 fprintf(fh, "%s: %e\n", meta->name, meta->nm_value.nm_double);
779 else if (meta->type == NM_TYPE_BOOLEAN)
780 fprintf(fh, "%s: %s\n", meta->name,
781 meta->nm_value.nm_boolean ? "true" : "false");
782 }
783
784 fprintf(fh, "\n%s\n", n->message);
785
786 fflush(fh);
787 fclose(fh);
788
789 waitpid(pid, &status, 0);
790
791 DEBUG("exec plugin: Child %i exited with status %i.", pid, status);
792
793 if (n->meta != NULL)
794 plugin_notification_meta_free(n->meta);
795 n->meta = NULL;
796 sfree(arg);
797 pthread_exit((void *)0);
798 return NULL;
799 } /* void *exec_notification_one }}} */
800
801 static int exec_init(void) /* {{{ */
802 {
803 struct sigaction sa = {.sa_handler = sigchld_handler};
804
805 sigaction(SIGCHLD, &sa, NULL);
806
807 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_SETUID) && defined(CAP_SETGID)
808 if ((check_capability(CAP_SETUID) != 0) ||
809 (check_capability(CAP_SETGID) != 0)) {
810 if (getuid() == 0)
811 WARNING(
812 "exec plugin: Running collectd as root, but the CAP_SETUID "
813 "or CAP_SETGID capabilities are missing. The plugin's read function "
814 "will probably fail. Is your init system dropping capabilities?");
815 else
816 WARNING(
817 "exec plugin: collectd doesn't have the CAP_SETUID or "
818 "CAP_SETGID capabilities. If you don't want to run collectd as root, "
819 "try running \"setcap 'cap_setuid=ep cap_setgid=ep'\" on the "
820 "collectd binary.");
821 }
822 #endif
823
824 return 0;
825 } /* int exec_init }}} */
826
827 static int exec_read(void) /* {{{ */
828 {
829 for (program_list_t *pl = pl_head; pl != NULL; pl = pl->next) {
830 pthread_t t;
831
832 /* Only execute `normal' style executables here. */
833 if ((pl->flags & PL_NORMAL) == 0)
834 continue;
835
836 pthread_mutex_lock(&pl_lock);
837 /* Skip if a child is already running. */
838 if ((pl->flags & PL_RUNNING) != 0) {
839 pthread_mutex_unlock(&pl_lock);
840 continue;
841 }
842 pl->flags |= PL_RUNNING;
843 pthread_mutex_unlock(&pl_lock);
844
845 int status =
846 plugin_thread_create(&t, exec_read_one, (void *)pl, "exec read");
847 if (status == 0) {
848 pthread_detach(t);
849 } else {
850 ERROR("exec plugin: plugin_thread_create failed.");
851 }
852 } /* for (pl) */
853
854 return 0;
855 } /* int exec_read }}} */
856
857 static int exec_notification(const notification_t *n, /* {{{ */
858 user_data_t __attribute__((unused)) * user_data) {
859 program_list_and_notification_t *pln;
860
861 for (program_list_t *pl = pl_head; pl != NULL; pl = pl->next) {
862 pthread_t t;
863
864 /* Only execute `notification' style executables here. */
865 if ((pl->flags & PL_NOTIF_ACTION) == 0)
866 continue;
867
868 /* Skip if a child is already running. */
869 if (pl->pid != 0)
870 continue;
871
872 pln = malloc(sizeof(*pln));
873 if (pln == NULL) {
874 ERROR("exec plugin: malloc failed.");
875 continue;
876 }
877
878 pln->pl = pl;
879 memcpy(&pln->n, n, sizeof(notification_t));
880
881 /* Set the `meta' member to NULL, otherwise `plugin_notification_meta_copy'
882 * will run into an endless loop. */
883 pln->n.meta = NULL;
884 plugin_notification_meta_copy(&pln->n, n);
885
886 int status = plugin_thread_create(&t, exec_notification_one, (void *)pln,
887 "exec notify");
888 if (status == 0) {
889 pthread_detach(t);
890 } else {
891 ERROR("exec plugin: plugin_thread_create failed.");
892 }
893 } /* for (pl) */
894
895 return 0;
896 } /* }}} int exec_notification */
897
898 static int exec_shutdown(void) /* {{{ */
899 {
900 program_list_t *pl;
901 program_list_t *next;
902
903 pl = pl_head;
904 while (pl != NULL) {
905 next = pl->next;
906
907 if (pl->pid > 0) {
908 kill(pl->pid, SIGTERM);
909 INFO("exec plugin: Sent SIGTERM to %hu", (unsigned short int)pl->pid);
910 }
911
912 for (int i = 0; pl->argv[i] != NULL; i++) {
913 sfree(pl->argv[i]);
914 }
915 sfree(pl->argv);
916 sfree(pl->exec);
917 sfree(pl->user);
918 sfree(pl);
919
920 pl = next;
921 } /* while (pl) */
922 pl_head = NULL;
923
924 return 0;
925 } /* int exec_shutdown }}} */
926
927 void module_register(void) {
928 plugin_register_complex_config("exec", exec_config);
929 plugin_register_init("exec", exec_init);
930 plugin_register_read("exec", exec_read);
931 plugin_register_notification("exec", exec_notification,
932 /* user_data = */ NULL);
933 plugin_register_shutdown("exec", exec_shutdown);
934 } /* void module_register */