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/>.
21 #include "system/filesys.h"
22 #include "system/time.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,
59 arg_str = talloc_strdup(mem_ctx, argv[from]);
60 if (arg_str == NULL) {
64 for (i = from+1; i < argc; i++) {
65 arg_str = talloc_asprintf_append(arg_str, " %s", argv[i]);
66 if (arg_str == NULL) {
75 static int event_command_run(TALLOC_CTX *mem_ctx,
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;
86 int timeout, ret = 0, result = 0;
90 cmdline_usage(ctx->cmdline, "run");
94 ret = ctdb_event_init(ctx, ctx->ev, &ctx->eclient);
96 D_ERR("Failed to initialize event client, ret=%d\n", ret);
100 timeout = atoi(argv[0]);
105 ret = compact_args(mem_ctx, argv, argc, 3, &arg_str);
107 D_ERR("Memory allocation error\n");
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");
119 t = getenv("CTDB_EVENT_RUN_ALL");
121 request_run.flags = CTDB_EVENT_RUN_ALL;
125 req = ctdb_event_run_send(mem_ctx,
130 D_ERR("Memory allocation error\n");
134 tevent_req_poll(req, ctx->ev);
136 ok = ctdb_event_run_recv(req, &ret, &result);
138 D_ERR("Failed to run event %s in %s, ret=%d\n",
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",
162 ret = (result < 0) ? -result : result;
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",
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",
185 strerror(-script->result));
186 } else if (script->result == 0) {
187 printf("%-20s %-10s %.3lf %s",
190 timeval_delta(&script->end, &script->begin),
191 ctime(&script->begin.tv_sec));
193 printf("%-20s %-10s %.3lf %s",
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 printf(" OUTPUT: %s\n",
203 script->output == NULL ? "" : script->output);
207 static void print_status(const char *component,
210 struct ctdb_event_reply_status *status)
215 if (result == ENOENT) {
216 printf("Event dir for %s does not exist\n", component);
217 } else if (result == EINVAL) {
218 printf("Event %s has never run in %s\n",
222 printf("Unknown error (%d) for event %s in %s\n",
230 for (i=0; i<status->script_list->num_scripts; i++) {
231 print_status_one(&status->script_list->script[i]);
235 static int event_command_status(TALLOC_CTX *mem_ctx,
240 struct event_tool_context *ctx = talloc_get_type_abort(
241 private_data, struct event_tool_context);
242 struct tevent_req *req;
243 struct ctdb_event_request_status request_status;
244 struct ctdb_event_reply_status *reply_status;
245 int ret = 0, result = 0;
249 cmdline_usage(ctx->cmdline, "status");
253 ret = ctdb_event_init(ctx, ctx->ev, &ctx->eclient);
255 D_ERR("Failed to initialize event client, ret=%d\n", ret);
259 request_status.component = argv[0];
260 request_status.event = argv[1];
262 req = ctdb_event_status_send(mem_ctx,
267 D_ERR("Memory allocation error\n");
271 tevent_req_poll(req, ctx->ev);
273 ok = ctdb_event_status_recv(req,
279 D_ERR("Failed to get status for event %s in %s, ret=%d\n",
286 D_NOTICE("Command status finished with result=%d\n", result);
288 print_status(argv[0], argv[1], result, reply_status);
290 if (reply_status == NULL) {
293 ret = reply_status->summary;
294 ret = (ret < 0) ? -ret : ret;
299 #define EVENT_SCRIPT_DISABLED ' '
300 #define EVENT_SCRIPT_ENABLED '*'
302 static int event_command_script_list(TALLOC_CTX *mem_ctx,
307 struct event_tool_context *ctx = talloc_get_type_abort(
308 private_data, struct event_tool_context);
310 char *data_dir = NULL;
311 char *etc_dir = NULL;
313 struct event_script_list *data_list = NULL;
314 struct event_script_list *etc_list = NULL;
315 unsigned int i, j, matched;
319 cmdline_usage(ctx->cmdline, "script list");
323 subdir = talloc_asprintf(mem_ctx, "events/%s", argv[0]);
324 if (subdir == NULL) {
328 data_dir = path_datadir_append(mem_ctx, subdir);
329 if (data_dir == NULL) {
333 t = talloc_size(mem_ctx, PATH_MAX);
338 data_dir = realpath(data_dir, t);
339 if (data_dir == NULL) {
340 if (errno != ENOENT) {
343 D_ERR("Command script list finished with result=%d\n", ENOENT);
347 etc_dir = path_etcdir_append(mem_ctx, subdir);
348 if (etc_dir == NULL) {
353 * Ignore error on ENOENT for cut down (e.g. fixed/embedded)
354 * installs that don't use symlinks but just populate etc_dir
357 ret = event_script_get_list(mem_ctx, data_dir, &data_list);
358 if (ret != 0 && ret != ENOENT) {
359 D_ERR("Command script list finished with result=%d\n", ret);
363 ret = event_script_get_list(mem_ctx, etc_dir, &etc_list);
365 D_ERR("Command script list finished with result=%d\n", ret);
369 D_NOTICE("Command script list finished with result=%d\n", ret);
371 if (data_list == NULL) {
372 goto list_enabled_only;
376 * First list scripts provided by CTDB. Flag those that are
377 * enabled via a symlink and arrange for them to be excluded
378 * from the subsequent list of local scripts.
380 * Both lists are sorted, so walk the list of enabled scripts
381 * only once in this pass.
385 for (i = 0; i < data_list->num_scripts; i++) {
386 struct event_script *d = data_list->script[i];
387 char flag = EVENT_SCRIPT_DISABLED;
391 /* Check to see if this script is enabled */
392 while (j < etc_list->num_scripts) {
393 struct event_script *e = etc_list->script[j];
395 ret = strcmp(e->name, d->name);
399 * Enabled name is greater, so needs
400 * to be considered later: done
406 /* Enabled name is less: next */
411 len = readlink(e->path, buf, sizeof(buf));
412 if (len == -1 || (size_t)len >= sizeof(buf)) {
414 * Not a link? Disappeared? Invalid
415 * link target? Something else?
417 * Doesn't match provided script: next, done
423 /* readlink() does not NUL-terminate */
426 ret = strcmp(buf, d->path);
428 /* Enabled link doesn't match: next, done */
434 * Enabled script's symlink matches our
435 * script: flag our script as enabled
437 * Also clear the enabled script so it can be
438 * trivially skipped in the next pass
440 flag = EVENT_SCRIPT_ENABLED;
441 TALLOC_FREE(etc_list->script[j]);
447 printf("%c %s\n", flag, d->name);
450 /* Print blank line if both provided and local lists are being printed */
451 if (data_list->num_scripts > 0 && matched != etc_list->num_scripts) {
457 /* Now print details of local scripts, after a blank line */
458 for (j = 0; j < etc_list->num_scripts; j++) {
459 struct event_script *e = etc_list->script[j];
460 char flag = EVENT_SCRIPT_DISABLED;
463 /* Matched in previous pass: next */
467 /* Script is local: if executable then flag as enabled */
469 flag = EVENT_SCRIPT_ENABLED;
472 printf("%c %s\n", flag, e->name);
479 talloc_free(data_dir);
480 talloc_free(etc_dir);
481 talloc_free(data_list);
482 talloc_free(etc_list);
487 static int event_command_script(TALLOC_CTX *mem_ctx,
488 struct event_tool_context *ctx,
489 const char *component,
493 char *subdir, *etc_dir;
496 subdir = talloc_asprintf(mem_ctx, "events/%s", component);
497 if (subdir == NULL) {
501 etc_dir = path_etcdir_append(mem_ctx, subdir);
502 if (etc_dir == NULL) {
507 result = event_script_chmod(etc_dir, script, true);
509 result = event_script_chmod(etc_dir, script, false);
513 talloc_free(etc_dir);
515 D_NOTICE("Command script finished with result=%d\n", result);
517 if (result == EINVAL) {
518 printf("Script %s is invalid in %s\n", script, component);
519 } else if (result == ENOENT) {
520 printf("Script %s does not exist in %s\n", script, component);
526 static int event_command_script_enable(TALLOC_CTX *mem_ctx,
531 struct event_tool_context *ctx = talloc_get_type_abort(
532 private_data, struct event_tool_context);
534 char *script, *etc_script;
538 cmdline_usage(ctx->cmdline, "script enable");
542 script = talloc_asprintf(mem_ctx, "events/%s/%s.script", argv[0], argv[1]);
543 if (script == NULL) {
547 etc_script = path_etcdir_append(mem_ctx, script);
548 if (etc_script == NULL) {
552 ret = lstat(etc_script, &statbuf);
554 if (S_ISLNK(statbuf.st_mode)) {
555 /* Link already exists */
557 } else if (S_ISREG(statbuf.st_mode)) {
558 return event_command_script(mem_ctx,
565 printf("Script %s is not a file or a link\n", etc_script);
568 if (errno == ENOENT) {
572 data_script = path_datadir_append(mem_ctx, script);
573 if (data_script == NULL) {
577 t = talloc_size(mem_ctx, PATH_MAX);
582 data_script = realpath(data_script, t);
583 if (data_script == NULL) {
584 if (errno != ENOENT) {
587 printf("Script %s does not exist in %s\n",
593 ret = stat(data_script, &statbuf);
595 printf("Script %s does not exist in %s\n",
600 ret = symlink(data_script, etc_script);
602 printf("Failed to create symlink %s\n",
610 printf("Script %s does not exist\n", etc_script);
615 static int event_command_script_disable(TALLOC_CTX *mem_ctx,
620 struct event_tool_context *ctx = talloc_get_type_abort(
621 private_data, struct event_tool_context);
623 char *script, *etc_script;
628 cmdline_usage(ctx->cmdline, "script disable");
632 script = talloc_asprintf(mem_ctx, "events/%s/%s.script", argv[0], argv[1]);
633 if (script == NULL) {
637 etc_script = path_etcdir_append(mem_ctx, script);
638 if (etc_script == NULL) {
642 ret = lstat(etc_script, &statbuf);
644 if (S_ISLNK(statbuf.st_mode)) {
646 ret = unlink(etc_script);
648 printf("Failed to remove symlink %s\n",
654 } else if (S_ISREG(statbuf.st_mode)) {
655 return event_command_script(mem_ctx,
662 printf("Script %s is not a file or a link\n", etc_script);
669 struct cmdline_command event_commands[] = {
670 { "run", event_command_run,
671 "Run an event", "<timeout> <component> <event> <args>" },
672 { "status", event_command_status,
673 "Get status of an event", "<component> <event>" },
674 { "script list", event_command_script_list,
675 "List event scripts", "<component>" },
676 { "script enable", event_command_script_enable,
677 "Enable an event script", "<component> <script>" },
678 { "script disable", event_command_script_disable,
679 "Disable an event script", "<component> <script>" },
683 int event_tool_init(TALLOC_CTX *mem_ctx,
685 struct poptOption *options,
689 struct event_tool_context **result)
691 struct event_tool_context *ctx;
694 ctx = talloc_zero(mem_ctx, struct event_tool_context);
696 D_ERR("Memory allocation error\n");
700 ret = cmdline_init(mem_ctx,
707 D_ERR("Failed to initialize cmdline, ret=%d\n", ret);
712 ret = cmdline_parse(ctx->cmdline, argc, argv, parse_options);
714 cmdline_usage(ctx->cmdline, NULL);
723 int event_tool_run(struct event_tool_context *ctx, int *result)
727 ctx->ev = tevent_context_init(ctx);
728 if (ctx->ev == NULL) {
729 D_ERR("Failed to initialize tevent\n");
733 ret = cmdline_run(ctx->cmdline, ctx, result);
737 #ifdef CTDB_EVENT_TOOL
745 struct poptOption event_options[] = {
746 { "debug", 'd', POPT_ARG_STRING, &event_data.debug, 0,
747 "debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
751 int main(int argc, const char **argv)
754 struct event_tool_context *ctx;
759 mem_ctx = talloc_new(NULL);
760 if (mem_ctx == NULL) {
761 fprintf(stderr, "Memory allocation error\n");
765 ret = event_tool_init(mem_ctx,
773 talloc_free(mem_ctx);
777 setup_logging("ctdb-event", DEBUG_STDERR);
778 ok = debug_level_parse(event_data.debug, &level);
782 debuglevel_set(level);
784 ret = event_tool_run(ctx, &result);
789 talloc_free(mem_ctx);
793 #endif /* CTDB_EVENT_TOOL */