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/>.
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
)
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");
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
;
72 timeout
= tevent_timeval_current_ofs(t
, 0);
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
);
81 fprintf(stderr
, "run_proc_send() failed\n");
85 tevent_req_poll(req
, ev
);
87 status
= run_event_recv(req
, &ret
, mem_ctx
, &script_list
);
89 fprintf(stderr
, "run_proc_recv() failed, ret=%d\n", ret
);
93 if (script_list
== NULL
|| script_list
->num_scripts
== 0) {
94 printf("No event scripts found\n");
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
;
113 ret
= run_event_script_list(run_ctx
, mem_ctx
, &script_list
);
115 printf("Script list failed with result=%d\n", ret
);
119 if (script_list
== NULL
|| script_list
->num_scripts
== 0) {
120 printf("No event scripts found\n");
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
)
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
)
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
)
162 struct tevent_context
*ev
;
163 struct run_event_context
*run_ctx
;
171 mem_ctx
= talloc_new(NULL
);
172 if (mem_ctx
== NULL
) {
173 fprintf(stderr
, "talloc_new() failed\n");
177 ev
= tevent_context_init(mem_ctx
);
179 fprintf(stderr
, "tevent_context_init() failed\n");
183 ret
= run_event_init(mem_ctx
, ev
, argv
[1], NULL
, &run_ctx
);
185 fprintf(stderr
, "run_event_init() failed, ret=%d\n", ret
);
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
);
198 fprintf(stderr
, "Invalid command %s\n", argv
[2]);
202 talloc_free(mem_ctx
);