ctdb-common: Avoid unused value warning
[samba.git] / ctdb / common / run_event.c
1 /*
2    Run scripts in a directory with specific event arguments
3
4    Copyright (C) Amitay Isaacs  2017
5
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.
10
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.
15
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/>.
18 */
19
20 #include "replace.h"
21 #include "system/filesys.h"
22 #include "system/dir.h"
23 #include "system/glob.h"
24 #include "system/wait.h"
25
26 #include <talloc.h>
27 #include <tevent.h>
28
29 #include "lib/util/tevent_unix.h"
30 #include "lib/util/debug.h"
31
32 #include "common/logging.h"
33 #include "common/run_proc.h"
34 #include "common/event_script.h"
35
36 #include "common/run_event.h"
37
38 /*
39  * Utility functions
40  */
41
42 static int get_script_list(TALLOC_CTX *mem_ctx,
43                            const char *script_dir,
44                            struct run_event_script_list **out)
45 {
46         struct event_script_list *s_list;
47         struct run_event_script_list *script_list;
48         unsigned int i;
49         int ret;
50
51         ret = event_script_get_list(mem_ctx, script_dir, &s_list);
52         if (ret != 0) {
53                 if (ret == ENOENT) {
54                         D_WARNING("event script dir %s removed\n", script_dir);
55                 } else {
56                         D_WARNING("failed to get script list for %s, ret=%d\n",
57                                   script_dir, ret);
58                 }
59                 return ret;
60         }
61
62         if (s_list->num_scripts == 0) {
63                 *out = NULL;
64                 talloc_free(s_list);
65                 return 0;
66         }
67
68         script_list = talloc_zero(mem_ctx, struct run_event_script_list);
69         if (script_list == NULL) {
70                 talloc_free(s_list);
71                 return ENOMEM;
72         }
73
74         script_list->num_scripts = s_list->num_scripts;
75         script_list->script = talloc_zero_array(script_list,
76                                                 struct run_event_script,
77                                                 script_list->num_scripts);
78         if (script_list->script == NULL) {
79                 talloc_free(s_list);
80                 talloc_free(script_list);
81                 return ENOMEM;
82         }
83
84         for (i = 0; i < s_list->num_scripts; i++) {
85                 struct event_script *s = s_list->script[i];
86                 struct run_event_script *script = &script_list->script[i];
87
88                 script->name = talloc_steal(script_list->script, s->name);
89
90                 if (! s->enabled) {
91                         script->summary = -ENOEXEC;
92                 }
93         }
94
95         talloc_free(s_list);
96         *out = script_list;
97         return 0;
98 }
99
100 static int script_args(TALLOC_CTX *mem_ctx, const char *event_str,
101                        const char *arg_str, const char ***out)
102 {
103         const char **argv;
104         size_t argc;
105         size_t len;
106
107         /* Preallocate argv array to avoid reallocation. */
108         len = 8;
109         argv = talloc_array(mem_ctx, const char *, len);
110         if (argv == NULL) {
111                 return ENOMEM;
112         }
113
114         argv[0] = NULL; /* script name */
115         argv[1] = event_str;
116         argc = 2;
117
118         if (arg_str != NULL) {
119                 char *str, *t, *tok;
120
121                 str = talloc_strdup(argv, arg_str);
122                 if (str == NULL) {
123                         return ENOMEM;
124                 }
125
126                 t = str;
127                 while ((tok = strtok(t, " ")) != NULL) {
128                         argv[argc] = talloc_strdup(argv, tok);
129                         if (argv[argc] == NULL) {
130                                 talloc_free(argv);
131                                 return ENOMEM;
132                         }
133                         argc += 1;
134                         if (argc >= len) {
135                                 argv = talloc_realloc(mem_ctx, argv,
136                                                       const char *, len + 8);
137                                 if (argv == NULL) {
138                                         return ENOMEM;
139                                 }
140                                 len += 8;
141                         }
142                         t = NULL;
143                 }
144
145                 talloc_free(str);
146         }
147
148         argv[argc] = NULL;
149         /* argc += 1 */
150
151         *out = argv;
152         return 0;
153 }
154
155 struct run_event_context {
156         struct run_proc_context *run_proc_ctx;
157         const char *script_dir;
158         const char *debug_prog;
159         bool debug_running;
160
161         struct tevent_queue *queue;
162         struct tevent_req *current_req;
163         bool monitor_running;
164 };
165
166
167 int run_event_init(TALLOC_CTX *mem_ctx, struct run_proc_context *run_proc_ctx,
168                    const char *script_dir, const char *debug_prog,
169                    struct run_event_context **out)
170 {
171         struct run_event_context *run_ctx;
172         struct stat st;
173         int ret;
174
175         run_ctx = talloc_zero(mem_ctx, struct run_event_context);
176         if (run_ctx == NULL) {
177                 return ENOMEM;
178         }
179
180         run_ctx->run_proc_ctx = run_proc_ctx;
181
182         ret = stat(script_dir, &st);
183         if (ret != 0) {
184                 ret = errno;
185                 talloc_free(run_ctx);
186                 return ret;
187         }
188
189         if (! S_ISDIR(st.st_mode)) {
190                 talloc_free(run_ctx);
191                 return ENOTDIR;
192         }
193
194         run_ctx->script_dir = talloc_strdup(run_ctx, script_dir);
195         if (run_ctx->script_dir == NULL) {
196                 talloc_free(run_ctx);
197                 return ENOMEM;
198         }
199
200         if (debug_prog != NULL) {
201                 run_ctx->debug_prog = talloc_strdup(run_ctx, debug_prog);
202                 if (run_ctx->debug_prog == NULL) {
203                         talloc_free(run_ctx);
204                         return ENOMEM;
205                 }
206         }
207
208         run_ctx->debug_running = false;
209
210         run_ctx->queue = tevent_queue_create(run_ctx, "run event queue");
211         if (run_ctx->queue == NULL) {
212                 talloc_free(run_ctx);
213                 return ENOMEM;
214         }
215
216         run_ctx->monitor_running = false;
217
218         *out = run_ctx;
219         return 0;
220 }
221
222 static struct run_proc_context *
223 run_event_run_proc_context(struct run_event_context *run_ctx)
224 {
225         return run_ctx->run_proc_ctx;
226 }
227
228 static const char *run_event_script_dir(struct run_event_context *run_ctx)
229 {
230         return run_ctx->script_dir;
231 }
232
233 static const char *run_event_debug_prog(struct run_event_context *run_ctx)
234 {
235         return run_ctx->debug_prog;
236 }
237
238 static struct tevent_queue *run_event_queue(struct run_event_context *run_ctx)
239 {
240         return run_ctx->queue;
241 }
242
243 static void run_event_start_running(struct run_event_context *run_ctx,
244                                     struct tevent_req *req, bool is_monitor)
245 {
246         run_ctx->current_req = req;
247         run_ctx->monitor_running = is_monitor;
248 }
249
250 static void run_event_stop_running(struct run_event_context *run_ctx)
251 {
252         run_ctx->current_req = NULL;
253         run_ctx->monitor_running = false;
254 }
255
256 static struct tevent_req *run_event_get_running(
257                                 struct run_event_context *run_ctx,
258                                 bool *is_monitor)
259 {
260         *is_monitor = run_ctx->monitor_running;
261         return run_ctx->current_req;
262 }
263
264 static int run_event_script_status(struct run_event_script *script)
265 {
266         int ret;
267
268         if (script->result.sig > 0) {
269                 ret = -EINTR;
270         } else if (script->result.err > 0) {
271                 if (script->result.err == EACCES) {
272                         /* Map EACCESS to ENOEXEC */
273                         ret = -ENOEXEC;
274                 } else {
275                         ret = -script->result.err;
276                 }
277         } else {
278                 ret = script->result.status;
279         }
280
281         return ret;
282 }
283
284 int run_event_list(struct run_event_context *run_ctx,
285                    TALLOC_CTX *mem_ctx,
286                    struct run_event_script_list **output)
287 {
288         struct event_script_list *s_list = NULL;
289         struct run_event_script_list *script_list = NULL;
290         unsigned int i;
291         int ret;
292
293         ret = event_script_get_list(mem_ctx,
294                                     run_event_script_dir(run_ctx),
295                                     &s_list);
296         if (ret != 0) {
297                 return ret;
298         }
299
300         if (s_list->num_scripts == 0) {
301                 *output = NULL;
302                 talloc_free(s_list);
303                 return 0;
304         }
305
306         script_list = talloc_zero(mem_ctx, struct run_event_script_list);
307         if (script_list == NULL) {
308                 return ENOMEM;
309         }
310
311         script_list->num_scripts = s_list->num_scripts;
312         script_list->script = talloc_zero_array(script_list,
313                                                 struct run_event_script,
314                                                 script_list->num_scripts);
315         if (script_list->script == NULL) {
316                 talloc_free(s_list);
317                 talloc_free(script_list);
318                 return ENOMEM;
319         }
320
321         for (i=0; i < s_list->num_scripts; i++) {
322                 struct event_script *s = s_list->script[i];
323                 struct run_event_script *script = &script_list->script[i];
324
325                 script->name = talloc_steal(script_list->script, s->name);
326
327                 if (! s->enabled) {
328                         script->summary = -ENOEXEC;
329                 }
330         }
331
332
333         talloc_free(s_list);
334         *output = script_list;
335         return 0;
336 }
337
338 int run_event_script_enable(struct run_event_context *run_ctx,
339                             const char *script_name)
340 {
341         return event_script_chmod(run_event_script_dir(run_ctx),
342                                   script_name,
343                                   true);
344 }
345
346 int run_event_script_disable(struct run_event_context *run_ctx,
347                              const char *script_name)
348 {
349         return event_script_chmod(run_event_script_dir(run_ctx),
350                                   script_name,
351                                   false);
352 }
353
354 /*
355  * Run debug program to diagnose hung scripts
356  */
357
358 static int debug_args(TALLOC_CTX *mem_ctx, const char *path,
359                       const char *event_str, pid_t pid, const char ***out)
360 {
361         const char **argv;
362
363         argv = talloc_array(mem_ctx, const char *, 4);
364         if (argv == NULL) {
365                 return ENOMEM;
366         }
367
368         argv[0] = path;
369         argv[1] = talloc_asprintf(argv, "%d", pid);
370         argv[2] = event_str;
371         if (argv[1] == NULL) {
372                 talloc_free(argv);
373                 return ENOMEM;
374         }
375         argv[3] = NULL;
376
377         *out = argv;
378         return 0;
379 }
380
381 static void debug_log(int loglevel, const char *output, const char *log_prefix)
382 {
383         char *line, *s;
384
385         s = strdup(output);
386         if (s == NULL) {
387                 DEBUG(loglevel, ("%s: %s\n", log_prefix, output));
388                 return;
389         }
390
391         line = strtok(s, "\n");
392         while (line != NULL) {
393                 DEBUG(loglevel, ("%s: %s\n", log_prefix, line));
394                 line = strtok(NULL, "\n");
395         }
396         free(s);
397 }
398
399 struct run_debug_state {
400         struct run_event_context *run_ctx;
401         pid_t pid;
402 };
403
404 static void run_debug_done(struct tevent_req *subreq);
405
406 static struct tevent_req *run_debug_send(TALLOC_CTX *mem_ctx,
407                                          struct tevent_context *ev,
408                                          struct run_event_context *run_ctx,
409                                          const char *event_str, pid_t pid)
410 {
411         struct tevent_req *req, *subreq;
412         struct run_debug_state *state;
413         const char **argv;
414         const char *debug_prog;
415         int ret;
416
417         req = tevent_req_create(mem_ctx, &state, struct run_debug_state);
418         if (req == NULL) {
419                 return NULL;
420         }
421
422         state->run_ctx = run_ctx;
423         state->pid = pid;
424
425         debug_prog = run_event_debug_prog(run_ctx);
426         if (debug_prog == NULL) {
427                 tevent_req_done(req);
428                 return tevent_req_post(req, ev);
429         }
430
431         if (run_ctx->debug_running) {
432                 tevent_req_done(req);
433                 return tevent_req_post(req, ev);
434         }
435
436         if (pid == -1) {
437                 D_DEBUG("Event script terminated, nothing to debug\n");
438                 tevent_req_done(req);
439                 return tevent_req_post(req, ev);
440         }
441
442         ret = debug_args(state, debug_prog, event_str, pid, &argv);
443         if (ret != 0) {
444                 D_ERR("debug_args() failed\n");
445                 tevent_req_error(req, ret);
446                 return tevent_req_post(req, ev);
447         }
448
449         D_DEBUG("Running debug %s with args \"%s %s\"\n",
450                 debug_prog, argv[1], argv[2]);
451
452         subreq = run_proc_send(state, ev, run_event_run_proc_context(run_ctx),
453                                debug_prog, argv, -1, tevent_timeval_zero());
454         if (tevent_req_nomem(subreq, req)) {
455                 return tevent_req_post(req, ev);
456         }
457         tevent_req_set_callback(subreq, run_debug_done, req);
458
459         run_ctx->debug_running = true;
460
461         talloc_free(argv);
462         return req;
463 }
464
465 static void run_debug_done(struct tevent_req *subreq)
466 {
467         struct tevent_req *req = tevent_req_callback_data(
468                 subreq, struct tevent_req);
469         struct run_debug_state *state = tevent_req_data(
470                 req, struct run_debug_state);
471         char *output;
472         int ret;
473         bool status;
474
475         state->run_ctx->debug_running = false;
476
477         status = run_proc_recv(subreq, &ret, NULL, NULL, state, &output);
478         TALLOC_FREE(subreq);
479         if (! status) {
480                 D_ERR("Running debug failed, ret=%d\n", ret);
481         }
482
483         /* Log output */
484         if (output != NULL) {
485                 debug_log(DEBUG_ERR, output, "event_debug");
486                 talloc_free(output);
487         }
488
489         kill(-state->pid, SIGTERM);
490         tevent_req_done(req);
491 }
492
493 static bool run_debug_recv(struct tevent_req *req, int *perr)
494 {
495         int ret;
496
497         if (tevent_req_is_unix_error(req, &ret)) {
498                 if (perr != NULL) {
499                         *perr = ret;
500                 }
501                 return false;
502         }
503
504         return true;
505 }
506
507 /*
508  * Run a single event
509  */
510
511 struct run_event_state {
512         struct tevent_context *ev;
513         struct run_event_context *run_ctx;
514         const char *event_str;
515         const char *arg_str;
516         struct timeval timeout;
517         bool continue_on_failure;
518
519         struct run_event_script_list *script_list;
520         const char **argv;
521         struct tevent_req *script_subreq;
522         int index;
523         bool cancelled;
524 };
525
526 static void run_event_cancel(struct tevent_req *req);
527 static void run_event_trigger(struct tevent_req *req, void *private_data);
528 static struct tevent_req *run_event_run_script(struct tevent_req *req);
529 static void run_event_next_script(struct tevent_req *subreq);
530 static void run_event_debug(struct tevent_req *req, pid_t pid);
531 static void run_event_debug_done(struct tevent_req *subreq);
532
533 struct tevent_req *run_event_send(TALLOC_CTX *mem_ctx,
534                                   struct tevent_context *ev,
535                                   struct run_event_context *run_ctx,
536                                   const char *event_str,
537                                   const char *arg_str,
538                                   struct timeval timeout,
539                                   bool continue_on_failure)
540 {
541         struct tevent_req *req, *current_req;
542         struct run_event_state *state;
543         bool monitor_running, status;
544
545         req = tevent_req_create(mem_ctx, &state, struct run_event_state);
546         if (req == NULL) {
547                 return NULL;
548         }
549
550         state->ev = ev;
551         state->run_ctx = run_ctx;
552         state->event_str = talloc_strdup(state, event_str);
553         if (tevent_req_nomem(state->event_str, req)) {
554                 return tevent_req_post(req, ev);
555         }
556         if (arg_str != NULL) {
557                 state->arg_str = talloc_strdup(state, arg_str);
558                 if (tevent_req_nomem(state->arg_str, req)) {
559                         return tevent_req_post(req, ev);
560                 }
561         }
562         state->timeout = timeout;
563         state->continue_on_failure = continue_on_failure;
564         state->cancelled = false;
565
566         state->script_list = talloc_zero(state, struct run_event_script_list);
567         if (tevent_req_nomem(state->script_list, req)) {
568                 return tevent_req_post(req, ev);
569         }
570
571         /*
572          * If monitor event is running,
573          *   cancel the running monitor event and run new event
574          *
575          * If any other event is running,
576          *   if new event is monitor, cancel that event
577          *   else add new event to the queue
578          */
579
580         current_req = run_event_get_running(run_ctx, &monitor_running);
581         if (current_req != NULL) {
582                 if (monitor_running) {
583                         run_event_cancel(current_req);
584                 } else if (strcmp(event_str, "monitor") == 0) {
585                         state->script_list->summary = -ECANCELED;
586                         tevent_req_done(req);
587                         return tevent_req_post(req, ev);
588                 }
589         }
590
591         status = tevent_queue_add(run_event_queue(run_ctx), ev, req,
592                                   run_event_trigger, NULL);
593         if (! status) {
594                 tevent_req_error(req, ENOMEM);
595                 return tevent_req_post(req, ev);
596         }
597
598         return req;
599 }
600
601 static void run_event_cancel(struct tevent_req *req)
602 {
603         struct run_event_state *state = tevent_req_data(
604                 req, struct run_event_state);
605
606         run_event_stop_running(state->run_ctx);
607
608         state->script_list->summary = -ECANCELED;
609         state->cancelled = true;
610
611         TALLOC_FREE(state->script_subreq);
612
613         tevent_req_done(req);
614 }
615
616 static void run_event_trigger(struct tevent_req *req, void *private_data)
617 {
618         struct tevent_req *subreq;
619         struct run_event_state *state = tevent_req_data(
620                 req, struct run_event_state);
621         struct run_event_script_list *script_list;
622         int ret;
623         bool is_monitor = false;
624
625         D_DEBUG("Running event %s with args \"%s\"\n", state->event_str,
626                 state->arg_str == NULL ? "(null)" : state->arg_str);
627
628         ret = get_script_list(state,
629                               run_event_script_dir(state->run_ctx),
630                               &script_list);
631         if (ret != 0) {
632                 D_ERR("get_script_list() failed, ret=%d\n", ret);
633                 tevent_req_error(req, ret);
634                 return;
635         }
636
637         /* No scripts */
638         if (script_list == NULL || script_list->num_scripts == 0) {
639                 tevent_req_done(req);
640                 return;
641         }
642
643         talloc_free(state->script_list);
644         state->script_list = script_list;
645
646         ret = script_args(state, state->event_str, state->arg_str,
647                           &state->argv);
648         if (ret != 0) {
649                 D_ERR("script_args() failed, ret=%d\n", ret);
650                 tevent_req_error(req, ret);
651                 return;
652         }
653
654         state->index = 0;
655
656         subreq = run_event_run_script(req);
657         if (tevent_req_nomem(subreq, req)) {
658                 return;
659         }
660         tevent_req_set_callback(subreq, run_event_next_script, req);
661
662         state->script_subreq = subreq;
663
664         if (strcmp(state->event_str, "monitor") == 0) {
665                 is_monitor = true;
666         }
667         run_event_start_running(state->run_ctx, req, is_monitor);
668 }
669
670 static struct tevent_req *run_event_run_script(struct tevent_req *req)
671 {
672         struct run_event_state *state = tevent_req_data(
673                 req, struct run_event_state);
674         struct run_event_script *script;
675         struct tevent_req *subreq;
676         char *path;
677
678         script = &state->script_list->script[state->index];
679
680         path = talloc_asprintf(state, "%s/%s.script",
681                                run_event_script_dir(state->run_ctx),
682                                script->name);
683         if (path == NULL) {
684                 return NULL;
685         }
686
687         state->argv[0] = script->name;
688         script->begin = tevent_timeval_current();
689
690         D_DEBUG("Running %s with args \"%s %s\"\n",
691                 path, state->argv[0], state->argv[1]);
692
693         subreq = run_proc_send(state, state->ev,
694                                run_event_run_proc_context(state->run_ctx),
695                                path, state->argv, -1, state->timeout);
696
697         talloc_free(path);
698
699         return subreq;
700 }
701
702 static void run_event_next_script(struct tevent_req *subreq)
703 {
704         struct tevent_req *req = tevent_req_callback_data(
705                 subreq, struct tevent_req);
706         struct run_event_state *state = tevent_req_data(
707                 req, struct run_event_state);
708         struct run_event_script *script;
709         pid_t pid;
710         int ret;
711         bool status;
712
713         script = &state->script_list->script[state->index];
714         script->end = tevent_timeval_current();
715
716         status = run_proc_recv(subreq, &ret, &script->result, &pid,
717                                state->script_list, &script->output);
718         TALLOC_FREE(subreq);
719         state->script_subreq = NULL;
720         if (! status) {
721                 D_ERR("run_proc failed for %s, ret=%d\n", script->name, ret);
722                 run_event_stop_running(state->run_ctx);
723                 tevent_req_error(req, ret);
724                 return;
725         }
726
727         if (state->cancelled) {
728                 return;
729         }
730
731         /* Log output */
732         if (script->output != NULL) {
733                 debug_log(DEBUG_ERR, script->output, script->name);
734         }
735
736         D_DEBUG("Script %s finished sig=%d, err=%d, status=%d\n",
737                 script->name, script->result.sig, script->result.err,
738                 script->result.status);
739
740
741         /* If a script fails, stop running */
742         script->summary = run_event_script_status(script);
743         if (script->summary != 0 && script->summary != -ENOEXEC) {
744                 state->script_list->summary = script->summary;
745
746                 if (! state->continue_on_failure) {
747                         state->script_list->num_scripts = state->index + 1;
748
749                         if (script->summary == -ETIMEDOUT && pid != -1) {
750                                 run_event_debug(req, pid);
751                         }
752                         D_NOTICE("%s event %s\n", state->event_str,
753                                  (script->summary == -ETIMEDOUT) ?
754                                   "timed out" :
755                                   "failed");
756                         run_event_stop_running(state->run_ctx);
757                         tevent_req_done(req);
758                         return;
759                 }
760         }
761
762         state->index += 1;
763
764         /* All scripts executed */
765         if (state->index >= state->script_list->num_scripts) {
766                 run_event_stop_running(state->run_ctx);
767                 tevent_req_done(req);
768                 return;
769         }
770
771         subreq = run_event_run_script(req);
772         if (tevent_req_nomem(subreq, req)) {
773                 return;
774         }
775         tevent_req_set_callback(subreq, run_event_next_script, req);
776
777         state->script_subreq = subreq;
778 }
779
780 static void run_event_debug(struct tevent_req *req, pid_t pid)
781 {
782         struct run_event_state *state = tevent_req_data(
783                 req, struct run_event_state);
784         struct tevent_req *subreq;
785
786         /* Debug script is run with ectx as the memory context */
787         subreq = run_debug_send(state->run_ctx, state->ev, state->run_ctx,
788                                 state->event_str, pid);
789         if (subreq == NULL) {
790                 /* If run debug fails, it's not an error */
791                 D_NOTICE("Failed to run event debug\n");
792                 return;
793         }
794         tevent_req_set_callback(subreq, run_event_debug_done, NULL);
795 }
796
797 static void run_event_debug_done(struct tevent_req *subreq)
798 {
799         int ret = 0;
800         bool status;
801
802         status = run_debug_recv(subreq, &ret);
803         TALLOC_FREE(subreq);
804         if (! status) {
805                 D_NOTICE("run_debug() failed, ret=%d\n", ret);
806         }
807 }
808
809 bool run_event_recv(struct tevent_req *req, int *perr,
810                     TALLOC_CTX *mem_ctx,
811                     struct run_event_script_list **script_list)
812 {
813         struct run_event_state *state = tevent_req_data(
814                 req, struct run_event_state);
815         int ret;
816
817         if (tevent_req_is_unix_error(req, &ret)) {
818                 if (perr != NULL) {
819                         *perr = ret;
820                 }
821                 return false;
822         }
823
824         if (script_list != NULL) {
825                 *script_list = talloc_steal(mem_ctx, state->script_list);
826         }
827         return true;
828 }
829