ctdb-common: Add run_event abstraction
[Samba.git] / ctdb / tests / src / run_event_test.c
blob5539ceb57c747c6c9f56a77d56efd39da895d63e
1 /*
2 run_event test wrapper
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/>.
20 #include "replace.h"
22 #include <talloc.h>
23 #include <tevent.h>
25 #include "common/db_hash.c"
26 #include "common/run_proc.c"
27 #include "common/run_event.c"
29 static void usage(const char *prog)
31 fprintf(stderr, "Usage: %s <scriptdir> run|list|enable|disable <options>\n", prog);
32 fprintf(stderr, " %s <scriptdir> run <timeout> <event> [<args>]\n", prog);
33 fprintf(stderr, " %s <scriptdir> list\n", prog);
34 fprintf(stderr, " %s <scriptdir> enable <scriptname>\n", prog);
35 fprintf(stderr, " %s <scriptdir> disable <scriptname>\n", prog);
38 static char *compact_args(const char **argv, int argc, int from)
40 char *arg_str = NULL;
41 int i;
43 for (i = from; i < argc; i++) {
44 arg_str = talloc_asprintf_append(arg_str, "%s ", argv[i]);
45 if (arg_str == NULL) {
46 fprintf(stderr, "talloc_asprintf_append() failed\n");
47 exit(1);
51 return arg_str;
54 static void do_run(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
55 struct run_event_context *run_ctx,
56 int argc, const char **argv)
58 struct tevent_req *req;
59 struct timeval timeout;
60 struct run_event_script_list *script_list = NULL;
61 char *arg_str;
62 int ret, t, i;
63 bool status;
65 if (argc < 5) {
66 usage(argv[0]);
67 exit(1);
70 t = atoi(argv[3]);
71 if (t > 0) {
72 timeout = tevent_timeval_current_ofs(t, 0);
73 } else {
74 timeout = tevent_timeval_zero();
77 arg_str = compact_args(argv, argc, 5);
79 req = run_event_send(mem_ctx, ev, run_ctx, argv[4], arg_str, timeout);
80 if (req == NULL) {
81 fprintf(stderr, "run_proc_send() failed\n");
82 return;
85 tevent_req_poll(req, ev);
87 status = run_event_recv(req, &ret, mem_ctx, &script_list);
88 if (! status) {
89 fprintf(stderr, "run_proc_recv() failed, ret=%d\n", ret);
90 return;
93 if (script_list == NULL || script_list->num_scripts == 0) {
94 printf("No event scripts found\n");
95 return;
98 printf("Event %s completed with result=%d\n",
99 argv[4], script_list->summary);
100 for (i=0; i<script_list->num_scripts; i++) {
101 printf("%s result=%d\n", script_list->script[i].name,
102 script_list->script[i].summary);
106 static void do_list(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
107 struct run_event_context *run_ctx,
108 int argc, const char **argv)
110 struct run_event_script_list *script_list = NULL;
111 int ret, i;
113 ret = run_event_script_list(run_ctx, mem_ctx, &script_list);
114 if (ret != 0) {
115 printf("Script list failed with result=%d\n", ret);
116 return;
119 if (script_list == NULL || script_list->num_scripts == 0) {
120 printf("No event scripts found\n");
121 return;
124 for (i=0; i<script_list->num_scripts; i++) {
125 printf("%s\n", script_list->script[i].name);
129 static void do_enable(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
130 struct run_event_context *run_ctx,
131 int argc, const char **argv)
133 int ret;
135 if (argc != 4) {
136 usage(argv[0]);
137 exit(1);
140 ret = run_event_script_enable(run_ctx, argv[3]);
141 printf("Script enable %s completed with result=%d\n", argv[3], ret);
144 static void do_disable(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
145 struct run_event_context *run_ctx,
146 int argc, const char **argv)
148 int ret;
150 if (argc != 4) {
151 usage(argv[0]);
152 exit(1);
155 ret = run_event_script_disable(run_ctx, argv[3]);
156 printf("Script disable %s completed with result=%d\n", argv[3], ret);
159 int main(int argc, const char **argv)
161 TALLOC_CTX *mem_ctx;
162 struct tevent_context *ev;
163 struct run_event_context *run_ctx;
164 int ret;
166 if (argc < 3) {
167 usage(argv[0]);
168 exit(1);
171 mem_ctx = talloc_new(NULL);
172 if (mem_ctx == NULL) {
173 fprintf(stderr, "talloc_new() failed\n");
174 exit(1);
177 ev = tevent_context_init(mem_ctx);
178 if (ev == NULL) {
179 fprintf(stderr, "tevent_context_init() failed\n");
180 exit(1);
183 ret = run_event_init(mem_ctx, ev, argv[1], NULL, &run_ctx);
184 if (ret != 0) {
185 fprintf(stderr, "run_event_init() failed, ret=%d\n", ret);
186 exit(1);
189 if (strcmp(argv[2], "run") == 0) {
190 do_run(mem_ctx, ev, run_ctx, argc, argv);
191 } else if (strcmp(argv[2], "list") == 0) {
192 do_list(mem_ctx, ev, run_ctx, argc, argv);
193 } else if (strcmp(argv[2], "enable") == 0) {
194 do_enable(mem_ctx, ev, run_ctx, argc, argv);
195 } else if (strcmp(argv[2], "disable") == 0) {
196 do_disable(mem_ctx, ev, run_ctx, argc, argv);
197 } else {
198 fprintf(stderr, "Invalid command %s\n", argv[2]);
199 usage(argv[0]);
202 talloc_free(mem_ctx);
203 exit(0);