2 Run scripts in a directory with specific event arguments
4 Copyright (C) Amitay Isaacs 2017
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "system/dir.h"
23 #include "system/glob.h"
24 #include "system/wait.h"
29 #include "lib/util/tevent_unix.h"
30 #include "lib/util/debug.h"
32 #include "common/logging.h"
33 #include "common/run_proc.h"
34 #include "common/run_event.h"
40 static int script_filter(const struct dirent *de)
44 /* Match a script pattern */
45 ret = fnmatch("[0-9][0-9].*.script", de->d_name, 0);
53 static int get_script_list(TALLOC_CTX *mem_ctx,
54 const char *script_dir,
55 struct run_event_script_list **out)
57 struct dirent **namelist = NULL;
58 struct run_event_script_list *script_list;
63 count = scandir(script_dir, &namelist, script_filter, alphasort);
67 D_WARNING("event script dir %s removed\n", script_dir);
69 D_WARNING("scandir() failed on %s, ret=%d\n",
83 script_list = talloc_zero(mem_ctx, struct run_event_script_list);
84 if (script_list == NULL) {
88 script_list->num_scripts = count;
89 script_list->script = talloc_zero_array(script_list,
90 struct run_event_script,
92 if (script_list->script == NULL) {
94 talloc_free(script_list);
98 ls = strlen(".script");
99 for (i=0; i<count; i++) {
100 struct run_event_script *s = &script_list->script[i];
102 s->name = talloc_strndup(script_list,
104 strlen(namelist[i]->d_name) - ls);
105 if (s->name == NULL) {
107 talloc_free(script_list);
116 if (namelist != NULL && count != -1) {
117 for (i=0; i<count; i++) {
125 static int script_chmod(TALLOC_CTX *mem_ctx, const char *script_dir,
126 const char *script_name, bool enable)
130 char script_file[PATH_MAX];
137 ret = snprintf(script_file,
141 if (ret >= sizeof(script_file)) {
145 dirp = opendir(script_dir);
151 while ((de = readdir(dirp)) != NULL) {
152 if (strcmp(de->d_name, script_file) == 0) {
154 /* check for valid script names */
155 ret = script_filter(de);
171 filename = talloc_asprintf(mem_ctx, "%s/%s", script_dir, script_file);
172 if (filename == NULL) {
176 fd = open(filename, O_RDWR);
182 ret = fstat(fd, &st);
189 new_mode = st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH);
191 new_mode = st.st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH);
194 ret = fchmod(fd, new_mode);
204 talloc_free(filename);
208 static int script_args(TALLOC_CTX *mem_ctx, const char *event_str,
209 const char *arg_str, const char ***out)
215 /* Preallocate argv array to avoid reallocation. */
217 argv = talloc_array(mem_ctx, const char *, len);
222 argv[0] = NULL; /* script name */
226 if (arg_str != NULL) {
229 str = talloc_strdup(argv, arg_str);
235 while ((tok = strtok(t, " ")) != NULL) {
236 argv[argc] = talloc_strdup(argv, tok);
237 if (argv[argc] == NULL) {
243 argv = talloc_realloc(mem_ctx, argv,
244 const char *, len + 8);
263 struct run_event_context {
264 struct run_proc_context *run_proc_ctx;
265 const char *script_dir;
266 const char *debug_prog;
269 struct tevent_queue *queue;
270 struct tevent_req *current_req;
271 bool monitor_running;
275 int run_event_init(TALLOC_CTX *mem_ctx, struct run_proc_context *run_proc_ctx,
276 const char *script_dir, const char *debug_prog,
277 struct run_event_context **out)
279 struct run_event_context *run_ctx;
283 run_ctx = talloc_zero(mem_ctx, struct run_event_context);
284 if (run_ctx == NULL) {
288 run_ctx->run_proc_ctx = run_proc_ctx;
290 ret = stat(script_dir, &st);
293 talloc_free(run_ctx);
297 if (! S_ISDIR(st.st_mode)) {
298 talloc_free(run_ctx);
302 run_ctx->script_dir = talloc_strdup(run_ctx, script_dir);
303 if (run_ctx->script_dir == NULL) {
304 talloc_free(run_ctx);
308 if (debug_prog != NULL) {
309 run_ctx->debug_prog = talloc_strdup(run_ctx, debug_prog);
310 if (run_ctx->debug_prog == NULL) {
311 talloc_free(run_ctx);
316 run_ctx->debug_running = false;
318 run_ctx->queue = tevent_queue_create(run_ctx, "run event queue");
319 if (run_ctx->queue == NULL) {
320 talloc_free(run_ctx);
324 run_ctx->monitor_running = false;
330 static struct run_proc_context *
331 run_event_run_proc_context(struct run_event_context *run_ctx)
333 return run_ctx->run_proc_ctx;
336 static const char *run_event_script_dir(struct run_event_context *run_ctx)
338 return run_ctx->script_dir;
341 static const char *run_event_debug_prog(struct run_event_context *run_ctx)
343 return run_ctx->debug_prog;
346 static struct tevent_queue *run_event_queue(struct run_event_context *run_ctx)
348 return run_ctx->queue;
351 static void run_event_start_running(struct run_event_context *run_ctx,
352 struct tevent_req *req, bool is_monitor)
354 run_ctx->current_req = req;
355 run_ctx->monitor_running = is_monitor;
358 static void run_event_stop_running(struct run_event_context *run_ctx)
360 run_ctx->current_req = NULL;
361 run_ctx->monitor_running = false;
364 static struct tevent_req *run_event_get_running(
365 struct run_event_context *run_ctx,
368 *is_monitor = run_ctx->monitor_running;
369 return run_ctx->current_req;
372 static int run_event_script_status(struct run_event_script *script)
376 if (script->result.sig > 0) {
378 } else if (script->result.err > 0) {
379 if (script->result.err == EACCES) {
380 /* Map EACCESS to ENOEXEC */
383 ret = -script->result.err;
386 ret = script->result.status;
392 int run_event_script_list(struct run_event_context *run_ctx,
394 struct run_event_script_list **output)
396 struct run_event_script_list *script_list;
399 ret = get_script_list(mem_ctx, run_event_script_dir(run_ctx),
405 if (script_list == NULL) {
410 for (i=0; i<script_list->num_scripts; i++) {
411 struct run_event_script *script = &script_list->script[i];
415 path = talloc_asprintf(mem_ctx, "%s/%s.script",
416 run_event_script_dir(run_ctx),
422 ret = stat(path, &st);
428 if (! (st.st_mode & S_IXUSR)) {
429 script->summary = -ENOEXEC;
435 *output = script_list;
439 int run_event_script_enable(struct run_event_context *run_ctx,
440 const char *script_name)
442 return script_chmod(run_ctx, run_event_script_dir(run_ctx),
446 int run_event_script_disable(struct run_event_context *run_ctx,
447 const char *script_name)
449 return script_chmod(run_ctx, run_event_script_dir(run_ctx),
454 * Run debug program to diagnose hung scripts
457 static int debug_args(TALLOC_CTX *mem_ctx, const char *path,
458 const char *event_str, pid_t pid, const char ***out)
462 argv = talloc_array(mem_ctx, const char *, 4);
468 argv[1] = talloc_asprintf(argv, "%d", pid);
470 if (argv[1] == NULL) {
480 static void debug_log(int loglevel, const char *output, const char *log_prefix)
486 DEBUG(loglevel, ("%s: %s\n", log_prefix, output));
490 line = strtok(s, "\n");
491 while (line != NULL) {
492 DEBUG(loglevel, ("%s: %s\n", log_prefix, line));
493 line = strtok(NULL, "\n");
498 struct run_debug_state {
499 struct run_event_context *run_ctx;
503 static void run_debug_done(struct tevent_req *subreq);
505 static struct tevent_req *run_debug_send(TALLOC_CTX *mem_ctx,
506 struct tevent_context *ev,
507 struct run_event_context *run_ctx,
508 const char *event_str, pid_t pid)
510 struct tevent_req *req, *subreq;
511 struct run_debug_state *state;
513 const char *debug_prog;
516 req = tevent_req_create(mem_ctx, &state, struct run_debug_state);
521 state->run_ctx = run_ctx;
524 debug_prog = run_event_debug_prog(run_ctx);
525 if (debug_prog == NULL) {
526 tevent_req_done(req);
527 return tevent_req_post(req, ev);
530 if (run_ctx->debug_running) {
531 tevent_req_done(req);
532 return tevent_req_post(req, ev);
536 D_DEBUG("Event script terminated, nothing to debug\n");
537 tevent_req_done(req);
538 return tevent_req_post(req, ev);
541 ret = debug_args(state, debug_prog, event_str, pid, &argv);
543 D_ERR("debug_args() failed\n");
544 tevent_req_error(req, ret);
545 return tevent_req_post(req, ev);
548 D_DEBUG("Running debug %s with args \"%s %s\"\n",
549 debug_prog, argv[1], argv[2]);
551 subreq = run_proc_send(state, ev, run_event_run_proc_context(run_ctx),
552 debug_prog, argv, -1, tevent_timeval_zero());
553 if (tevent_req_nomem(subreq, req)) {
554 return tevent_req_post(req, ev);
556 tevent_req_set_callback(subreq, run_debug_done, req);
558 run_ctx->debug_running = true;
564 static void run_debug_done(struct tevent_req *subreq)
566 struct tevent_req *req = tevent_req_callback_data(
567 subreq, struct tevent_req);
568 struct run_debug_state *state = tevent_req_data(
569 req, struct run_debug_state);
574 state->run_ctx->debug_running = false;
576 status = run_proc_recv(subreq, &ret, NULL, NULL, state, &output);
579 D_ERR("Running debug failed, ret=%d\n", ret);
583 if (output != NULL) {
584 debug_log(DEBUG_ERR, output, "event_debug");
588 kill(-state->pid, SIGTERM);
589 tevent_req_done(req);
592 static bool run_debug_recv(struct tevent_req *req, int *perr)
596 if (tevent_req_is_unix_error(req, &ret)) {
610 struct run_event_state {
611 struct tevent_context *ev;
612 struct run_event_context *run_ctx;
613 const char *event_str;
615 struct timeval timeout;
617 struct run_event_script_list *script_list;
619 struct tevent_req *script_subreq;
624 static void run_event_cancel(struct tevent_req *req);
625 static void run_event_trigger(struct tevent_req *req, void *private_data);
626 static struct tevent_req *run_event_run_script(struct tevent_req *req);
627 static void run_event_next_script(struct tevent_req *subreq);
628 static void run_event_debug(struct tevent_req *req, pid_t pid);
629 static void run_event_debug_done(struct tevent_req *subreq);
631 struct tevent_req *run_event_send(TALLOC_CTX *mem_ctx,
632 struct tevent_context *ev,
633 struct run_event_context *run_ctx,
634 const char *event_str,
636 struct timeval timeout)
638 struct tevent_req *req, *current_req;
639 struct run_event_state *state;
640 bool monitor_running, status;
642 req = tevent_req_create(mem_ctx, &state, struct run_event_state);
648 state->run_ctx = run_ctx;
649 state->event_str = talloc_strdup(state, event_str);
650 if (tevent_req_nomem(state->event_str, req)) {
651 return tevent_req_post(req, ev);
653 if (arg_str != NULL) {
654 state->arg_str = talloc_strdup(state, arg_str);
655 if (tevent_req_nomem(state->arg_str, req)) {
656 return tevent_req_post(req, ev);
659 state->timeout = timeout;
660 state->cancelled = false;
663 * If monitor event is running,
664 * cancel the running monitor event and run new event
666 * If any other event is running,
667 * if new event is monitor, cancel that event
668 * else add new event to the queue
671 current_req = run_event_get_running(run_ctx, &monitor_running);
672 if (current_req != NULL) {
673 if (monitor_running) {
674 run_event_cancel(current_req);
675 } else if (strcmp(event_str, "monitor") == 0) {
676 state->script_list = talloc_zero(
677 state, struct run_event_script_list);
678 if (tevent_req_nomem(state->script_list, req)) {
679 return tevent_req_post(req, ev);
681 state->script_list->summary = -ECANCELED;
682 tevent_req_done(req);
683 return tevent_req_post(req, ev);
687 status = tevent_queue_add(run_event_queue(run_ctx), ev, req,
688 run_event_trigger, NULL);
690 tevent_req_error(req, ENOMEM);
691 return tevent_req_post(req, ev);
697 static void run_event_cancel(struct tevent_req *req)
699 struct run_event_state *state = tevent_req_data(
700 req, struct run_event_state);
702 run_event_stop_running(state->run_ctx);
704 state->script_list->summary = -ECANCELED;
705 state->cancelled = true;
707 TALLOC_FREE(state->script_subreq);
709 tevent_req_done(req);
712 static void run_event_trigger(struct tevent_req *req, void *private_data)
714 struct tevent_req *subreq;
715 struct run_event_state *state = tevent_req_data(
716 req, struct run_event_state);
718 bool is_monitor = false;
720 D_DEBUG("Running event %s with args \"%s\"\n", state->event_str,
721 state->arg_str == NULL ? "(null)" : state->arg_str);
723 ret = get_script_list(state, run_event_script_dir(state->run_ctx),
724 &state->script_list);
726 D_ERR("get_script_list() failed, ret=%d\n", ret);
727 tevent_req_error(req, ret);
732 if (state->script_list == NULL ||
733 state->script_list->num_scripts == 0) {
734 tevent_req_done(req);
738 ret = script_args(state, state->event_str, state->arg_str,
741 D_ERR("script_args() failed, ret=%d\n", ret);
742 tevent_req_error(req, ret);
748 subreq = run_event_run_script(req);
749 if (tevent_req_nomem(subreq, req)) {
752 tevent_req_set_callback(subreq, run_event_next_script, req);
754 state->script_subreq = subreq;
756 if (strcmp(state->event_str, "monitor") == 0) {
759 run_event_start_running(state->run_ctx, req, is_monitor);
762 static struct tevent_req *run_event_run_script(struct tevent_req *req)
764 struct run_event_state *state = tevent_req_data(
765 req, struct run_event_state);
766 struct run_event_script *script;
767 struct tevent_req *subreq;
770 script = &state->script_list->script[state->index];
772 path = talloc_asprintf(state, "%s/%s.script",
773 run_event_script_dir(state->run_ctx),
779 state->argv[0] = script->name;
780 script->begin = tevent_timeval_current();
782 D_DEBUG("Running %s with args \"%s %s\"\n",
783 path, state->argv[0], state->argv[1]);
785 subreq = run_proc_send(state, state->ev,
786 run_event_run_proc_context(state->run_ctx),
787 path, state->argv, -1, state->timeout);
794 static void run_event_next_script(struct tevent_req *subreq)
796 struct tevent_req *req = tevent_req_callback_data(
797 subreq, struct tevent_req);
798 struct run_event_state *state = tevent_req_data(
799 req, struct run_event_state);
800 struct run_event_script *script;
805 script = &state->script_list->script[state->index];
806 script->end = tevent_timeval_current();
808 status = run_proc_recv(subreq, &ret, &script->result, &pid,
809 state->script_list, &script->output);
811 state->script_subreq = NULL;
813 D_ERR("run_proc failed for %s, ret=%d\n", script->name, ret);
814 tevent_req_error(req, ret);
818 if (state->cancelled) {
823 if (script->output != NULL) {
824 debug_log(DEBUG_ERR, script->output, script->name);
827 D_DEBUG("Script %s finished sig=%d, err=%d, status=%d\n",
828 script->name, script->result.sig, script->result.err,
829 script->result.status);
832 /* If a script fails, stop running */
833 script->summary = run_event_script_status(script);
834 if (script->summary != 0 && script->summary != -ENOEXEC) {
835 state->script_list->num_scripts = state->index + 1;
837 if (script->summary == -ETIME && pid != -1) {
838 run_event_debug(req, pid);
841 state->script_list->summary = script->summary;
842 D_NOTICE("%s event %s\n", state->event_str,
843 (script->summary == -ETIME) ? "timed out" : "failed");
845 run_event_stop_running(state->run_ctx);
846 tevent_req_done(req);
852 /* All scripts executed */
853 if (state->index >= state->script_list->num_scripts) {
854 run_event_stop_running(state->run_ctx);
855 tevent_req_done(req);
859 subreq = run_event_run_script(req);
860 if (tevent_req_nomem(subreq, req)) {
863 tevent_req_set_callback(subreq, run_event_next_script, req);
865 state->script_subreq = subreq;
868 static void run_event_debug(struct tevent_req *req, pid_t pid)
870 struct run_event_state *state = tevent_req_data(
871 req, struct run_event_state);
872 struct tevent_req *subreq;
874 /* Debug script is run with ectx as the memory context */
875 subreq = run_debug_send(state->run_ctx, state->ev, state->run_ctx,
876 state->event_str, pid);
877 if (subreq == NULL) {
878 /* If run debug fails, it's not an error */
879 D_NOTICE("Failed to run event debug\n");
882 tevent_req_set_callback(subreq, run_event_debug_done, NULL);
885 static void run_event_debug_done(struct tevent_req *subreq)
890 status = run_debug_recv(subreq, &ret);
893 D_NOTICE("run_debug() failed, ret=%d\n", ret);
897 bool run_event_recv(struct tevent_req *req, int *perr,
899 struct run_event_script_list **script_list)
901 struct run_event_state *state = tevent_req_data(
902 req, struct run_event_state);
905 if (tevent_req_is_unix_error(req, &ret)) {
912 if (script_list != NULL) {
913 *script_list = talloc_steal(mem_ctx, state->script_list);