smbd: use metadata_fsp(fsp) in copy_access_posix_acl() for SMB_VFS_SYS_ACL_SET_FD
[Samba.git] / ctdb / event / event_tool.c
blobd6b7156c69bf96edf28fa50102156574e7c82182
1 /*
2 CTDB event daemon utility code
4 Copyright (C) Amitay Isaacs 2018
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/>.
20 #include "replace.h"
21 #include "system/filesys.h"
22 #include "system/time.h"
24 #include <popt.h>
25 #include <talloc.h>
26 #include <tevent.h>
28 #include "lib/util/debug.h"
30 #include "common/cmdline.h"
31 #include "common/logging.h"
32 #include "common/path.h"
33 #include "common/event_script.h"
35 #include "event/event_protocol_api.h"
36 #include "event/event.h"
37 #include "event/event_tool.h"
39 struct event_tool_context {
40 struct cmdline_context *cmdline;
41 struct tevent_context *ev;
42 struct ctdb_event_context *eclient;
45 static int compact_args(TALLOC_CTX *mem_ctx,
46 const char **argv,
47 int argc,
48 int from,
49 const char **result)
51 char *arg_str;
52 int i;
54 if (argc <= from) {
55 *result = NULL;
56 return 0;
59 arg_str = talloc_strdup(mem_ctx, argv[from]);
60 if (arg_str == NULL) {
61 return ENOMEM;
64 for (i = from+1; i < argc; i++) {
65 arg_str = talloc_asprintf_append(arg_str, " %s", argv[i]);
66 if (arg_str == NULL) {
67 return ENOMEM;
71 *result = arg_str;
72 return 0;
75 static int event_command_run(TALLOC_CTX *mem_ctx,
76 int argc,
77 const char **argv,
78 void *private_data)
80 struct event_tool_context *ctx = talloc_get_type_abort(
81 private_data, struct event_tool_context);
82 struct tevent_req *req;
83 struct ctdb_event_request_run request_run;
84 const char *arg_str = NULL;
85 const char *t;
86 int timeout, ret = 0, result = 0;
87 bool ok;
89 if (argc < 3) {
90 cmdline_usage(ctx->cmdline, "run");
91 return 1;
94 ret = ctdb_event_init(ctx, ctx->ev, &ctx->eclient);
95 if (ret != 0) {
96 D_ERR("Failed to initialize event client, ret=%d\n", ret);
97 return ret;
100 timeout = atoi(argv[0]);
101 if (timeout < 0) {
102 timeout = 0;
105 ret = compact_args(mem_ctx, argv, argc, 3, &arg_str);
106 if (ret != 0) {
107 D_ERR("Memory allocation error\n");
108 return 1;
111 request_run.component = argv[1];
112 request_run.event = argv[2];
113 request_run.args = arg_str;
114 request_run.timeout = timeout;
115 request_run.flags = 0;
117 t = getenv("CTDB_TEST_MODE");
118 if (t != NULL) {
119 t = getenv("CTDB_EVENT_RUN_ALL");
120 if (t != NULL) {
121 request_run.flags = CTDB_EVENT_RUN_ALL;
125 req = ctdb_event_run_send(mem_ctx,
126 ctx->ev,
127 ctx->eclient,
128 &request_run);
129 if (req == NULL) {
130 D_ERR("Memory allocation error\n");
131 return 1;
134 tevent_req_poll(req, ctx->ev);
136 ok = ctdb_event_run_recv(req, &ret, &result);
137 if (!ok) {
138 D_ERR("Failed to run event %s in %s, ret=%d\n",
139 argv[2],
140 argv[1],
141 ret);
142 return 1;
145 D_NOTICE("Command run finished with result=%d\n", result);
147 if (result == ENOENT) {
148 printf("Event dir for %s does not exist\n", argv[1]);
149 } else if (result == ETIMEDOUT) {
150 printf("Event %s in %s timed out\n", argv[2], argv[1]);
151 } else if (result == ECANCELED) {
152 printf("Event %s in %s got cancelled\n", argv[2], argv[1]);
153 } else if (result == ENOEXEC) {
154 printf("Event %s in %s failed\n", argv[2], argv[1]);
155 } else if (result != 0) {
156 printf("Failed to run event %s in %s, result=%d\n",
157 argv[2],
158 argv[1],
159 result);
162 ret = (result < 0) ? -result : result;
163 return ret;
166 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
168 return (tv2->tv_sec - tv->tv_sec) +
169 (tv2->tv_usec - tv->tv_usec) * 1.0e-6;
172 static void print_status_one(struct ctdb_event_script *script)
174 if (script->result == -ETIMEDOUT) {
175 printf("%-20s %-10s %s",
176 script->name,
177 "TIMEDOUT",
178 ctime(&script->begin.tv_sec));
179 } else if (script->result == -ENOEXEC) {
180 printf("%-20s %-10s\n", script->name, "DISABLED");
181 } else if (script->result < 0) {
182 printf("%-20s %-10s (%s)\n",
183 script->name,
184 "CANNOT RUN",
185 strerror(-script->result));
186 } else if (script->result == 0) {
187 printf("%-20s %-10s %.3lf %s",
188 script->name,
189 "OK",
190 timeval_delta(&script->end, &script->begin),
191 ctime(&script->begin.tv_sec));
192 } else {
193 printf("%-20s %-10s %.3lf %s",
194 script->name,
195 "ERROR",
196 timeval_delta(&script->end, &script->begin),
197 ctime(&script->begin.tv_sec));
200 if ((script->result != 0 && script->result != -ENOEXEC) ||
201 script->output != NULL) {
202 /* Empty output is informative so always print it on failure */
203 const char *t = script->output == NULL ? "" : script->output;
204 size_t len = strlen(t);
205 char output[len+1];
206 char *t1, *t2;
208 strlcpy(output, t, sizeof(output));
211 * Strip trailing newlines, they are clutter and
212 * interfere with multi-line detection
214 t1 = output + len - 1;
215 while (t1 >= output && *t1 == '\n') {
216 *t1 = '\0';
217 t1--;
220 /* If the output is a single line then print it inline */
221 t2 = strchr(output, '\n');
222 if (t2 == NULL) {
223 printf(" OUTPUT: %s\n", output);
224 return;
228 * More than 1 line. Print a header and then each
229 * line, with suitable indent. There are more general
230 * ways to do this, but let's maintain intermediate
231 * blank lines (e.g. strv_split() loses blank lines).
233 printf(" OUTPUT:\n");
234 t1 = output;
235 do {
237 * Points to newline character. t2 initially
238 * set non-NULL outside loop because this loop
239 * only covers multi-line output.
241 *t2 = '\0';
244 printf(" %s\n", t1);
245 t1 = t2 + 1;
247 if (t1 >= output + len) {
248 break;
251 /* strchrnul() would be awesome, but isn't portable */
252 t2 = strchr(t1, '\n');
253 if (t2 == NULL) {
254 t2 = output + len;
256 } while (true);
260 static void print_status(const char *component,
261 const char *event,
262 int result,
263 struct ctdb_event_reply_status *status)
265 int i;
267 if (result != 0) {
268 if (result == ENOENT) {
269 printf("Event dir for %s does not exist\n", component);
270 } else if (result == EINVAL) {
271 printf("Event %s has never run in %s\n",
272 event,
273 component);
274 } else {
275 printf("Unknown error (%d) for event %s in %s\n",
276 result,
277 event,
278 component);
280 return;
283 for (i=0; i<status->script_list->num_scripts; i++) {
284 print_status_one(&status->script_list->script[i]);
288 static int event_command_status(TALLOC_CTX *mem_ctx,
289 int argc,
290 const char **argv,
291 void *private_data)
293 struct event_tool_context *ctx = talloc_get_type_abort(
294 private_data, struct event_tool_context);
295 struct tevent_req *req;
296 struct ctdb_event_request_status request_status;
297 struct ctdb_event_reply_status *reply_status;
298 int ret = 0, result = 0;
299 bool ok;
301 if (argc != 2) {
302 cmdline_usage(ctx->cmdline, "status");
303 return 1;
306 ret = ctdb_event_init(ctx, ctx->ev, &ctx->eclient);
307 if (ret != 0) {
308 D_ERR("Failed to initialize event client, ret=%d\n", ret);
309 return ret;
312 request_status.component = argv[0];
313 request_status.event = argv[1];
315 req = ctdb_event_status_send(mem_ctx,
316 ctx->ev,
317 ctx->eclient,
318 &request_status);
319 if (req == NULL) {
320 D_ERR("Memory allocation error\n");
321 return 1;
324 tevent_req_poll(req, ctx->ev);
326 ok = ctdb_event_status_recv(req,
327 &ret,
328 &result,
329 mem_ctx,
330 &reply_status);
331 if (!ok) {
332 D_ERR("Failed to get status for event %s in %s, ret=%d\n",
333 argv[1],
334 argv[0],
335 ret);
336 return 1;
339 D_NOTICE("Command status finished with result=%d\n", result);
341 print_status(argv[0], argv[1], result, reply_status);
343 if (reply_status == NULL) {
344 ret = result;
345 } else {
346 ret = reply_status->summary;
347 ret = (ret < 0) ? -ret : ret;
349 return ret;
352 #define EVENT_SCRIPT_DISABLED ' '
353 #define EVENT_SCRIPT_ENABLED '*'
355 static int event_command_script_list(TALLOC_CTX *mem_ctx,
356 int argc,
357 const char **argv,
358 void *private_data)
360 struct event_tool_context *ctx = talloc_get_type_abort(
361 private_data, struct event_tool_context);
362 char *subdir = NULL;
363 char *data_dir = NULL;
364 char *etc_dir = NULL;
365 char *t = NULL;
366 struct event_script_list *data_list = NULL;
367 struct event_script_list *etc_list = NULL;
368 unsigned int i, j, matched;
369 int ret = 0;
371 if (argc != 1) {
372 cmdline_usage(ctx->cmdline, "script list");
373 return 1;
376 subdir = talloc_asprintf(mem_ctx, "events/%s", argv[0]);
377 if (subdir == NULL) {
378 return ENOMEM;
381 data_dir = path_datadir_append(mem_ctx, subdir);
382 if (data_dir == NULL) {
383 return ENOMEM;
386 t = talloc_size(mem_ctx, PATH_MAX);
387 if (t == NULL) {
388 return ENOMEM;
391 data_dir = realpath(data_dir, t);
392 if (data_dir == NULL) {
393 if (errno != ENOENT) {
394 return errno;
396 D_ERR("Command script list finished with result=%d\n", ENOENT);
397 return ENOENT;
400 etc_dir = path_etcdir_append(mem_ctx, subdir);
401 if (etc_dir == NULL) {
402 return ENOMEM;
406 * Ignore error on ENOENT for cut down (e.g. fixed/embedded)
407 * installs that don't use symlinks but just populate etc_dir
408 * directly
410 ret = event_script_get_list(mem_ctx, data_dir, &data_list);
411 if (ret != 0 && ret != ENOENT) {
412 D_ERR("Command script list finished with result=%d\n", ret);
413 goto done;
416 ret = event_script_get_list(mem_ctx, etc_dir, &etc_list);
417 if (ret != 0) {
418 D_ERR("Command script list finished with result=%d\n", ret);
419 goto done;
422 D_NOTICE("Command script list finished with result=%d\n", ret);
424 if (data_list == NULL) {
425 goto list_enabled_only;
429 * First list scripts provided by CTDB. Flag those that are
430 * enabled via a symlink and arrange for them to be excluded
431 * from the subsequent list of local scripts.
433 * Both lists are sorted, so walk the list of enabled scripts
434 * only once in this pass.
436 j = 0;
437 matched = 0;
438 for (i = 0; i < data_list->num_scripts; i++) {
439 struct event_script *d = data_list->script[i];
440 char flag = EVENT_SCRIPT_DISABLED;
441 char buf[PATH_MAX];
442 ssize_t len;
444 /* Check to see if this script is enabled */
445 while (j < etc_list->num_scripts) {
446 struct event_script *e = etc_list->script[j];
448 ret = strcmp(e->name, d->name);
450 if (ret > 0) {
452 * Enabled name is greater, so needs
453 * to be considered later: done
455 break;
458 if (ret < 0) {
459 /* Enabled name is less: next */
460 j++;
461 continue;
464 len = readlink(e->path, buf, sizeof(buf));
465 if (len == -1 || (size_t)len >= sizeof(buf)) {
467 * Not a link? Disappeared? Invalid
468 * link target? Something else?
470 * Doesn't match provided script: next, done
472 j++;
473 break;
476 /* readlink() does not NUL-terminate */
477 buf[len] = '\0';
479 ret = strcmp(buf, d->path);
480 if (ret != 0) {
481 /* Enabled link doesn't match: next, done */
482 j++;
483 break;
487 * Enabled script's symlink matches our
488 * script: flag our script as enabled
490 * Also clear the enabled script so it can be
491 * trivially skipped in the next pass
493 flag = EVENT_SCRIPT_ENABLED;
494 TALLOC_FREE(etc_list->script[j]);
495 j++;
496 matched++;
497 break;
500 printf("%c %s\n", flag, d->name);
503 /* Print blank line if both provided and local lists are being printed */
504 if (data_list->num_scripts > 0 && matched != etc_list->num_scripts) {
505 printf("\n");
508 list_enabled_only:
510 /* Now print details of local scripts, after a blank line */
511 for (j = 0; j < etc_list->num_scripts; j++) {
512 struct event_script *e = etc_list->script[j];
513 char flag = EVENT_SCRIPT_DISABLED;
515 if (e == NULL) {
516 /* Matched in previous pass: next */
517 continue;
520 /* Script is local: if executable then flag as enabled */
521 if (e->enabled) {
522 flag = EVENT_SCRIPT_ENABLED;
525 printf("%c %s\n", flag, e->name);
528 ret = 0;
530 done:
531 talloc_free(subdir);
532 talloc_free(data_dir);
533 talloc_free(etc_dir);
534 talloc_free(data_list);
535 talloc_free(etc_list);
537 return ret;
540 static int event_command_script(TALLOC_CTX *mem_ctx,
541 struct event_tool_context *ctx,
542 const char *component,
543 const char *script,
544 bool enable)
546 char *subdir, *etc_dir;
547 int result = 0;
549 subdir = talloc_asprintf(mem_ctx, "events/%s", component);
550 if (subdir == NULL) {
551 return ENOMEM;
554 etc_dir = path_etcdir_append(mem_ctx, subdir);
555 if (etc_dir == NULL) {
556 return ENOMEM;
559 if (enable) {
560 result = event_script_chmod(etc_dir, script, true);
561 } else {
562 result = event_script_chmod(etc_dir, script, false);
565 talloc_free(subdir);
566 talloc_free(etc_dir);
568 D_NOTICE("Command script finished with result=%d\n", result);
570 if (result == EINVAL) {
571 printf("Script %s is invalid in %s\n", script, component);
572 } else if (result == ENOENT) {
573 printf("Script %s does not exist in %s\n", script, component);
576 return result;
579 static int event_command_script_enable(TALLOC_CTX *mem_ctx,
580 int argc,
581 const char **argv,
582 void *private_data)
584 struct event_tool_context *ctx = talloc_get_type_abort(
585 private_data, struct event_tool_context);
586 struct stat statbuf;
587 char *script, *etc_script;
588 int ret;
590 if (argc != 2) {
591 cmdline_usage(ctx->cmdline, "script enable");
592 return 1;
595 script = talloc_asprintf(mem_ctx, "events/%s/%s.script", argv[0], argv[1]);
596 if (script == NULL) {
597 return ENOMEM;
600 etc_script = path_etcdir_append(mem_ctx, script);
601 if (etc_script == NULL) {
602 return ENOMEM;
605 ret = lstat(etc_script, &statbuf);
606 if (ret == 0) {
607 if (S_ISLNK(statbuf.st_mode)) {
608 /* Link already exists */
609 return 0;
610 } else if (S_ISREG(statbuf.st_mode)) {
611 return event_command_script(mem_ctx,
612 ctx,
613 argv[0],
614 argv[1],
615 true);
618 printf("Script %s is not a file or a link\n", etc_script);
619 return EINVAL;
620 } else {
621 if (errno == ENOENT) {
622 char *t;
623 char *data_script;
625 data_script = path_datadir_append(mem_ctx, script);
626 if (data_script == NULL) {
627 return ENOMEM;
630 t = talloc_size(mem_ctx, PATH_MAX);
631 if (t == NULL) {
632 return ENOMEM;
635 data_script = realpath(data_script, t);
636 if (data_script == NULL) {
637 if (errno != ENOENT) {
638 return errno;
640 printf("Script %s does not exist in %s\n",
641 argv[1],
642 argv[0]);
643 return ENOENT;
646 ret = stat(data_script, &statbuf);
647 if (ret != 0) {
648 printf("Script %s does not exist in %s\n",
649 argv[1], argv[0]);
650 return ENOENT;
653 ret = symlink(data_script, etc_script);
654 if (ret != 0) {
655 printf("Failed to create symlink %s\n",
656 etc_script);
657 return EIO;
660 return 0;
663 printf("Script %s does not exist\n", etc_script);
664 return EINVAL;
668 static int event_command_script_disable(TALLOC_CTX *mem_ctx,
669 int argc,
670 const char **argv,
671 void *private_data)
673 struct event_tool_context *ctx = talloc_get_type_abort(
674 private_data, struct event_tool_context);
675 struct stat statbuf;
676 char *script, *etc_script;
677 int ret;
680 if (argc != 2) {
681 cmdline_usage(ctx->cmdline, "script disable");
682 return 1;
685 script = talloc_asprintf(mem_ctx, "events/%s/%s.script", argv[0], argv[1]);
686 if (script == NULL) {
687 return ENOMEM;
690 etc_script = path_etcdir_append(mem_ctx, script);
691 if (etc_script == NULL) {
692 return ENOMEM;
695 ret = lstat(etc_script, &statbuf);
696 if (ret == 0) {
697 if (S_ISLNK(statbuf.st_mode)) {
698 /* Link exists */
699 ret = unlink(etc_script);
700 if (ret != 0) {
701 printf("Failed to remove symlink %s\n",
702 etc_script);
703 return EIO;
706 return 0;
707 } else if (S_ISREG(statbuf.st_mode)) {
708 return event_command_script(mem_ctx,
709 ctx,
710 argv[0],
711 argv[1],
712 false);
715 printf("Script %s is not a file or a link\n", etc_script);
716 return EINVAL;
719 return 0;
722 struct cmdline_command event_commands[] = {
723 { "run", event_command_run,
724 "Run an event", "<timeout> <component> <event> <args>" },
725 { "status", event_command_status,
726 "Get status of an event", "<component> <event>" },
727 { "script list", event_command_script_list,
728 "List event scripts", "<component>" },
729 { "script enable", event_command_script_enable,
730 "Enable an event script", "<component> <script>" },
731 { "script disable", event_command_script_disable,
732 "Disable an event script", "<component> <script>" },
733 CMDLINE_TABLEEND
736 int event_tool_init(TALLOC_CTX *mem_ctx,
737 const char *prog,
738 struct poptOption *options,
739 int argc,
740 const char **argv,
741 bool parse_options,
742 struct event_tool_context **result)
744 struct event_tool_context *ctx;
745 int ret;
747 ctx = talloc_zero(mem_ctx, struct event_tool_context);
748 if (ctx == NULL) {
749 D_ERR("Memory allocation error\n");
750 return ENOMEM;
753 ret = cmdline_init(mem_ctx,
754 prog,
755 options,
756 NULL,
757 event_commands,
758 &ctx->cmdline);
759 if (ret != 0) {
760 D_ERR("Failed to initialize cmdline, ret=%d\n", ret);
761 talloc_free(ctx);
762 return ret;
765 ret = cmdline_parse(ctx->cmdline, argc, argv, parse_options);
766 if (ret != 0) {
767 cmdline_usage(ctx->cmdline, NULL);
768 talloc_free(ctx);
769 return ret;
772 *result = ctx;
773 return 0;
776 int event_tool_run(struct event_tool_context *ctx, int *result)
778 int ret;
780 ctx->ev = tevent_context_init(ctx);
781 if (ctx->ev == NULL) {
782 D_ERR("Failed to initialize tevent\n");
783 return ENOMEM;
786 ret = cmdline_run(ctx->cmdline, ctx, result);
787 return ret;
790 #ifdef CTDB_EVENT_TOOL
792 static struct {
793 const char *debug;
794 } event_data = {
795 .debug = "ERROR",
798 struct poptOption event_options[] = {
799 { "debug", 'd', POPT_ARG_STRING, &event_data.debug, 0,
800 "debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
801 POPT_TABLEEND
804 int main(int argc, const char **argv)
806 TALLOC_CTX *mem_ctx;
807 struct event_tool_context *ctx;
808 int ret, result = 0;
809 int level;
810 bool ok;
812 mem_ctx = talloc_new(NULL);
813 if (mem_ctx == NULL) {
814 fprintf(stderr, "Memory allocation error\n");
815 exit(1);
818 ret = event_tool_init(mem_ctx,
819 "ctdb-event",
820 event_options,
821 argc,
822 argv,
823 true,
824 &ctx);
825 if (ret != 0) {
826 talloc_free(mem_ctx);
827 exit(1);
830 setup_logging("ctdb-event", DEBUG_STDERR);
831 ok = debug_level_parse(event_data.debug, &level);
832 if (!ok) {
833 level = DEBUG_ERR;
835 debuglevel_set(level);
837 ret = event_tool_run(ctx, &result);
838 if (ret != 0) {
839 exit(1);
842 talloc_free(mem_ctx);
843 exit(result);
846 #endif /* CTDB_EVENT_TOOL */