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",
82 script_list = talloc_zero(mem_ctx, struct run_event_script_list);
83 if (script_list == NULL) {
87 script_list->num_scripts = count;
88 script_list->script = talloc_zero_array(script_list,
89 struct run_event_script,
91 if (script_list->script == NULL) {
93 talloc_free(script_list);
97 ls = strlen(".script");
98 for (i=0; i<count; i++) {
99 struct run_event_script *s = &script_list->script[i];
101 s->name = talloc_strndup(script_list,
103 strlen(namelist[i]->d_name) - ls);
104 if (s->name == NULL) {
106 talloc_free(script_list);
115 if (namelist != NULL && count != -1) {
116 for (i=0; i<count; i++) {
124 static int script_chmod(TALLOC_CTX *mem_ctx, const char *script_dir,
125 const char *script_name, bool enable)
129 char script_file[PATH_MAX];
136 ret = snprintf(script_file,
140 if (ret >= sizeof(script_file)) {
144 dirp = opendir(script_dir);
150 while ((de = readdir(dirp)) != NULL) {
151 if (strcmp(de->d_name, script_file) == 0) {
153 /* check for valid script names */
154 ret = script_filter(de);
170 filename = talloc_asprintf(mem_ctx, "%s/%s", script_dir, script_file);
171 if (filename == NULL) {
175 fd = open(filename, O_RDWR);
181 ret = fstat(fd, &st);
188 new_mode = st.st_mode | (S_IXUSR | S_IXGRP | S_IXOTH);
190 new_mode = st.st_mode & ~(S_IXUSR | S_IXGRP | S_IXOTH);
193 ret = fchmod(fd, new_mode);
203 talloc_free(filename);
207 static int script_args(TALLOC_CTX *mem_ctx, const char *event_str,
208 const char *arg_str, const char ***out)
214 /* Preallocate argv array to avoid reallocation. */
216 argv = talloc_array(mem_ctx, const char *, len);
221 argv[0] = NULL; /* script name */
225 if (arg_str != NULL) {
228 str = talloc_strdup(argv, arg_str);
234 while ((tok = strtok(t, " ")) != NULL) {
235 argv[argc] = talloc_strdup(argv, tok);
236 if (argv[argc] == NULL) {
242 argv = talloc_realloc(mem_ctx, argv,
243 const char *, len + 8);
262 struct run_event_context {
263 struct run_proc_context *run_proc_ctx;
264 const char *script_dir;
265 const char *debug_prog;
268 struct tevent_queue *queue;
269 struct tevent_req *current_req;
270 bool monitor_running;
274 int run_event_init(TALLOC_CTX *mem_ctx, struct run_proc_context *run_proc_ctx,
275 const char *script_dir, const char *debug_prog,
276 struct run_event_context **out)
278 struct run_event_context *run_ctx;
282 run_ctx = talloc_zero(mem_ctx, struct run_event_context);
283 if (run_ctx == NULL) {
287 run_ctx->run_proc_ctx = run_proc_ctx;
289 ret = stat(script_dir, &st);
292 talloc_free(run_ctx);
296 if (! S_ISDIR(st.st_mode)) {
297 talloc_free(run_ctx);
301 run_ctx->script_dir = talloc_strdup(run_ctx, script_dir);
302 if (run_ctx->script_dir == NULL) {
303 talloc_free(run_ctx);
307 if (debug_prog != NULL) {
308 run_ctx->debug_prog = talloc_strdup(run_ctx, debug_prog);
309 if (run_ctx->debug_prog == NULL) {
310 talloc_free(run_ctx);
315 run_ctx->debug_running = false;
317 run_ctx->queue = tevent_queue_create(run_ctx, "run event queue");
318 if (run_ctx->queue == NULL) {
319 talloc_free(run_ctx);
323 run_ctx->monitor_running = false;
329 static struct run_proc_context *
330 run_event_run_proc_context(struct run_event_context *run_ctx)
332 return run_ctx->run_proc_ctx;
335 static const char *run_event_script_dir(struct run_event_context *run_ctx)
337 return run_ctx->script_dir;
340 static const char *run_event_debug_prog(struct run_event_context *run_ctx)
342 return run_ctx->debug_prog;
345 static struct tevent_queue *run_event_queue(struct run_event_context *run_ctx)
347 return run_ctx->queue;
350 static void run_event_start_running(struct run_event_context *run_ctx,
351 struct tevent_req *req, bool is_monitor)
353 run_ctx->current_req = req;
354 run_ctx->monitor_running = is_monitor;
357 static void run_event_stop_running(struct run_event_context *run_ctx)
359 run_ctx->current_req = NULL;
360 run_ctx->monitor_running = false;
363 static struct tevent_req *run_event_get_running(
364 struct run_event_context *run_ctx,
367 *is_monitor = run_ctx->monitor_running;
368 return run_ctx->current_req;
371 static int run_event_script_status(struct run_event_script *script)
375 if (script->result.sig > 0) {
377 } else if (script->result.err > 0) {
378 if (script->result.err == EACCES) {
379 /* Map EACCESS to ENOEXEC */
382 ret = -script->result.err;
385 ret = script->result.status;
391 int run_event_list(struct run_event_context *run_ctx,
393 struct run_event_script_list **output)
395 struct run_event_script_list *script_list;
398 ret = get_script_list(mem_ctx, run_event_script_dir(run_ctx),
404 if (script_list == NULL) {
409 for (i=0; i<script_list->num_scripts; i++) {
410 struct run_event_script *script = &script_list->script[i];
414 path = talloc_asprintf(mem_ctx, "%s/%s.script",
415 run_event_script_dir(run_ctx),
421 ret = stat(path, &st);
427 if (! (st.st_mode & S_IXUSR)) {
428 script->summary = -ENOEXEC;
434 *output = script_list;
438 int run_event_script_enable(struct run_event_context *run_ctx,
439 const char *script_name)
441 return script_chmod(run_ctx, run_event_script_dir(run_ctx),
445 int run_event_script_disable(struct run_event_context *run_ctx,
446 const char *script_name)
448 return script_chmod(run_ctx, run_event_script_dir(run_ctx),
453 * Run debug program to diagnose hung scripts
456 static int debug_args(TALLOC_CTX *mem_ctx, const char *path,
457 const char *event_str, pid_t pid, const char ***out)
461 argv = talloc_array(mem_ctx, const char *, 4);
467 argv[1] = talloc_asprintf(argv, "%d", pid);
469 if (argv[1] == NULL) {
479 static void debug_log(int loglevel, const char *output, const char *log_prefix)
485 DEBUG(loglevel, ("%s: %s\n", log_prefix, output));
489 line = strtok(s, "\n");
490 while (line != NULL) {
491 DEBUG(loglevel, ("%s: %s\n", log_prefix, line));
492 line = strtok(NULL, "\n");
497 struct run_debug_state {
498 struct run_event_context *run_ctx;
502 static void run_debug_done(struct tevent_req *subreq);
504 static struct tevent_req *run_debug_send(TALLOC_CTX *mem_ctx,
505 struct tevent_context *ev,
506 struct run_event_context *run_ctx,
507 const char *event_str, pid_t pid)
509 struct tevent_req *req, *subreq;
510 struct run_debug_state *state;
512 const char *debug_prog;
515 req = tevent_req_create(mem_ctx, &state, struct run_debug_state);
520 state->run_ctx = run_ctx;
523 debug_prog = run_event_debug_prog(run_ctx);
524 if (debug_prog == NULL) {
525 tevent_req_done(req);
526 return tevent_req_post(req, ev);
529 if (run_ctx->debug_running) {
530 tevent_req_done(req);
531 return tevent_req_post(req, ev);
535 D_DEBUG("Event script terminated, nothing to debug\n");
536 tevent_req_done(req);
537 return tevent_req_post(req, ev);
540 ret = debug_args(state, debug_prog, event_str, pid, &argv);
542 D_ERR("debug_args() failed\n");
543 tevent_req_error(req, ret);
544 return tevent_req_post(req, ev);
547 D_DEBUG("Running debug %s with args \"%s %s\"\n",
548 debug_prog, argv[1], argv[2]);
550 subreq = run_proc_send(state, ev, run_event_run_proc_context(run_ctx),
551 debug_prog, argv, -1, tevent_timeval_zero());
552 if (tevent_req_nomem(subreq, req)) {
553 return tevent_req_post(req, ev);
555 tevent_req_set_callback(subreq, run_debug_done, req);
557 run_ctx->debug_running = true;
563 static void run_debug_done(struct tevent_req *subreq)
565 struct tevent_req *req = tevent_req_callback_data(
566 subreq, struct tevent_req);
567 struct run_debug_state *state = tevent_req_data(
568 req, struct run_debug_state);
573 state->run_ctx->debug_running = false;
575 status = run_proc_recv(subreq, &ret, NULL, NULL, state, &output);
578 D_ERR("Running debug failed, ret=%d\n", ret);
582 if (output != NULL) {
583 debug_log(DEBUG_ERR, output, "event_debug");
587 kill(-state->pid, SIGTERM);
588 tevent_req_done(req);
591 static bool run_debug_recv(struct tevent_req *req, int *perr)
595 if (tevent_req_is_unix_error(req, &ret)) {
609 struct run_event_state {
610 struct tevent_context *ev;
611 struct run_event_context *run_ctx;
612 const char *event_str;
614 struct timeval timeout;
616 struct run_event_script_list *script_list;
618 struct tevent_req *script_subreq;
623 static void run_event_cancel(struct tevent_req *req);
624 static void run_event_trigger(struct tevent_req *req, void *private_data);
625 static struct tevent_req *run_event_run_script(struct tevent_req *req);
626 static void run_event_next_script(struct tevent_req *subreq);
627 static void run_event_debug(struct tevent_req *req, pid_t pid);
628 static void run_event_debug_done(struct tevent_req *subreq);
630 struct tevent_req *run_event_send(TALLOC_CTX *mem_ctx,
631 struct tevent_context *ev,
632 struct run_event_context *run_ctx,
633 const char *event_str,
635 struct timeval timeout)
637 struct tevent_req *req, *current_req;
638 struct run_event_state *state;
639 bool monitor_running, status;
641 req = tevent_req_create(mem_ctx, &state, struct run_event_state);
647 state->run_ctx = run_ctx;
648 state->event_str = talloc_strdup(state, event_str);
649 if (tevent_req_nomem(state->event_str, req)) {
650 return tevent_req_post(req, ev);
652 if (arg_str != NULL) {
653 state->arg_str = talloc_strdup(state, arg_str);
654 if (tevent_req_nomem(state->arg_str, req)) {
655 return tevent_req_post(req, ev);
658 state->timeout = timeout;
659 state->cancelled = false;
661 state->script_list = talloc_zero(state, struct run_event_script_list);
662 if (tevent_req_nomem(state->script_list, req)) {
663 return tevent_req_post(req, ev);
667 * If monitor event is running,
668 * cancel the running monitor event and run new event
670 * If any other event is running,
671 * if new event is monitor, cancel that event
672 * else add new event to the queue
675 current_req = run_event_get_running(run_ctx, &monitor_running);
676 if (current_req != NULL) {
677 if (monitor_running) {
678 run_event_cancel(current_req);
679 } else if (strcmp(event_str, "monitor") == 0) {
680 state->script_list->summary = -ECANCELED;
681 tevent_req_done(req);
682 return tevent_req_post(req, ev);
686 status = tevent_queue_add(run_event_queue(run_ctx), ev, req,
687 run_event_trigger, NULL);
689 tevent_req_error(req, ENOMEM);
690 return tevent_req_post(req, ev);
696 static void run_event_cancel(struct tevent_req *req)
698 struct run_event_state *state = tevent_req_data(
699 req, struct run_event_state);
701 run_event_stop_running(state->run_ctx);
703 state->script_list->summary = -ECANCELED;
704 state->cancelled = true;
706 TALLOC_FREE(state->script_subreq);
708 tevent_req_done(req);
711 static void run_event_trigger(struct tevent_req *req, void *private_data)
713 struct tevent_req *subreq;
714 struct run_event_state *state = tevent_req_data(
715 req, struct run_event_state);
716 struct run_event_script_list *script_list;
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,
724 run_event_script_dir(state->run_ctx),
727 D_ERR("get_script_list() failed, ret=%d\n", ret);
728 tevent_req_error(req, ret);
733 if (script_list == NULL || script_list->num_scripts == 0) {
734 tevent_req_done(req);
738 talloc_free(state->script_list);
739 state->script_list = script_list;
741 ret = script_args(state, state->event_str, state->arg_str,
744 D_ERR("script_args() failed, ret=%d\n", ret);
745 tevent_req_error(req, ret);
751 subreq = run_event_run_script(req);
752 if (tevent_req_nomem(subreq, req)) {
755 tevent_req_set_callback(subreq, run_event_next_script, req);
757 state->script_subreq = subreq;
759 if (strcmp(state->event_str, "monitor") == 0) {
762 run_event_start_running(state->run_ctx, req, is_monitor);
765 static struct tevent_req *run_event_run_script(struct tevent_req *req)
767 struct run_event_state *state = tevent_req_data(
768 req, struct run_event_state);
769 struct run_event_script *script;
770 struct tevent_req *subreq;
773 script = &state->script_list->script[state->index];
775 path = talloc_asprintf(state, "%s/%s.script",
776 run_event_script_dir(state->run_ctx),
782 state->argv[0] = script->name;
783 script->begin = tevent_timeval_current();
785 D_DEBUG("Running %s with args \"%s %s\"\n",
786 path, state->argv[0], state->argv[1]);
788 subreq = run_proc_send(state, state->ev,
789 run_event_run_proc_context(state->run_ctx),
790 path, state->argv, -1, state->timeout);
797 static void run_event_next_script(struct tevent_req *subreq)
799 struct tevent_req *req = tevent_req_callback_data(
800 subreq, struct tevent_req);
801 struct run_event_state *state = tevent_req_data(
802 req, struct run_event_state);
803 struct run_event_script *script;
808 script = &state->script_list->script[state->index];
809 script->end = tevent_timeval_current();
811 status = run_proc_recv(subreq, &ret, &script->result, &pid,
812 state->script_list, &script->output);
814 state->script_subreq = NULL;
816 D_ERR("run_proc failed for %s, ret=%d\n", script->name, ret);
817 tevent_req_error(req, ret);
821 if (state->cancelled) {
826 if (script->output != NULL) {
827 debug_log(DEBUG_ERR, script->output, script->name);
830 D_DEBUG("Script %s finished sig=%d, err=%d, status=%d\n",
831 script->name, script->result.sig, script->result.err,
832 script->result.status);
835 /* If a script fails, stop running */
836 script->summary = run_event_script_status(script);
837 if (script->summary != 0 && script->summary != -ENOEXEC) {
838 state->script_list->num_scripts = state->index + 1;
840 if (script->summary == -ETIME && pid != -1) {
841 run_event_debug(req, pid);
844 state->script_list->summary = script->summary;
845 D_NOTICE("%s event %s\n", state->event_str,
846 (script->summary == -ETIME) ? "timed out" : "failed");
848 run_event_stop_running(state->run_ctx);
849 tevent_req_done(req);
855 /* All scripts executed */
856 if (state->index >= state->script_list->num_scripts) {
857 run_event_stop_running(state->run_ctx);
858 tevent_req_done(req);
862 subreq = run_event_run_script(req);
863 if (tevent_req_nomem(subreq, req)) {
866 tevent_req_set_callback(subreq, run_event_next_script, req);
868 state->script_subreq = subreq;
871 static void run_event_debug(struct tevent_req *req, pid_t pid)
873 struct run_event_state *state = tevent_req_data(
874 req, struct run_event_state);
875 struct tevent_req *subreq;
877 /* Debug script is run with ectx as the memory context */
878 subreq = run_debug_send(state->run_ctx, state->ev, state->run_ctx,
879 state->event_str, pid);
880 if (subreq == NULL) {
881 /* If run debug fails, it's not an error */
882 D_NOTICE("Failed to run event debug\n");
885 tevent_req_set_callback(subreq, run_event_debug_done, NULL);
888 static void run_event_debug_done(struct tevent_req *subreq)
893 status = run_debug_recv(subreq, &ret);
896 D_NOTICE("run_debug() failed, ret=%d\n", ret);
900 bool run_event_recv(struct tevent_req *req, int *perr,
902 struct run_event_script_list **script_list)
904 struct run_event_state *state = tevent_req_data(
905 req, struct run_event_state);
908 if (tevent_req_is_unix_error(req, &ret)) {
915 if (script_list != NULL) {
916 *script_list = talloc_steal(mem_ctx, state->script_list);