1 /*
2    run_event test wrapper
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 
22 #include <talloc.h>
23 #include <tevent.h>
24 
25 #include "common/db_hash.c"
26 #include "common/run_proc.c"
27 #include "common/event_script.c"
28 #include "common/run_event.c"
29 
usage(const char * prog)30 static void usage(const char *prog)
31 {
32 	fprintf(stderr, "Usage: %s <scriptdir> run|list|enable|disable <options>\n", prog);
33 	fprintf(stderr, "       %s <scriptdir> run <timeout> <event> [<args>]\n", prog);
34 	fprintf(stderr, "       %s <scriptdir> list\n", prog);
35 	fprintf(stderr, "       %s <scriptdir> enable <scriptname>\n", prog);
36 	fprintf(stderr, "       %s <scriptdir> disable <scriptname>\n", prog);
37 }
38 
compact_args(const char ** argv,int argc,int from)39 static char *compact_args(const char **argv, int argc, int from)
40 {
41 	char *arg_str = NULL;
42 	int i;
43 
44 	for (i = from; i < argc; i++) {
45 		arg_str = talloc_asprintf_append(arg_str, "%s ", argv[i]);
46 		if (arg_str == NULL) {
47 			fprintf(stderr, "talloc_asprintf_append() failed\n");
48 			exit(1);
49 		}
50 	}
51 
52 	return arg_str;
53 }
54 
do_run(TALLOC_CTX * mem_ctx,struct tevent_context * ev,struct run_event_context * run_ctx,int argc,const char ** argv)55 static void do_run(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
56 		   struct run_event_context *run_ctx,
57 		   int argc, const char **argv)
58 {
59 	struct tevent_req *req;
60 	struct timeval timeout;
61 	struct run_event_script_list *script_list = NULL;
62 	char *arg_str;
63 	unsigned int i;
64 	int ret, t;
65 	bool status;
66 
67 	if (argc < 5) {
68 		usage(argv[0]);
69 		exit(1);
70 	}
71 
72 	t = atoi(argv[3]);
73 	if (t > 0) {
74 		timeout = tevent_timeval_current_ofs(t, 0);
75 	} else {
76 		timeout = tevent_timeval_zero();
77 	}
78 
79 	arg_str = compact_args(argv, argc, 5);
80 
81 	req = run_event_send(mem_ctx,
82 			     ev,
83 			     run_ctx,
84 			     argv[4],
85 			     arg_str,
86 			     timeout,
87 			     false);
88 	if (req == NULL) {
89 		fprintf(stderr, "run_proc_send() failed\n");
90 		return;
91 	}
92 
93 	tevent_req_poll(req, ev);
94 
95 	status = run_event_recv(req, &ret, mem_ctx, &script_list);
96 	if (! status) {
97 		fprintf(stderr, "run_proc_recv() failed, ret=%d\n", ret);
98 		return;
99 	}
100 
101 	if (script_list == NULL || script_list->num_scripts == 0) {
102 		printf("No event scripts found\n");
103 		return;
104 	}
105 
106 	printf("Event %s completed with result=%d\n",
107 	       argv[4], script_list->summary);
108 	for (i=0; i<script_list->num_scripts; i++) {
109 		printf("%s result=%d\n", script_list->script[i].name,
110 		       script_list->script[i].summary);
111 	}
112 }
113 
do_list(TALLOC_CTX * mem_ctx,struct tevent_context * ev,struct run_event_context * run_ctx,int argc,const char ** argv)114 static void do_list(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
115 		    struct run_event_context *run_ctx,
116 		    int argc, const char **argv)
117 {
118 	struct run_event_script_list *script_list = NULL;
119 	unsigned int i;
120 	int ret;
121 
122 	ret = run_event_list(run_ctx, mem_ctx, &script_list);
123 	if (ret != 0) {
124 		printf("Script list failed with result=%d\n", ret);
125 		return;
126 	}
127 
128 	if (script_list == NULL || script_list->num_scripts == 0) {
129 		printf("No event scripts found\n");
130 		return;
131 	}
132 
133 	for (i=0; i<script_list->num_scripts; i++) {
134 		printf("%s\n", script_list->script[i].name);
135 	}
136 }
137 
do_enable(TALLOC_CTX * mem_ctx,struct tevent_context * ev,struct run_event_context * run_ctx,int argc,const char ** argv)138 static void do_enable(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
139 		      struct run_event_context *run_ctx,
140 		      int argc, const char **argv)
141 {
142 	int ret;
143 
144 	if (argc != 4) {
145 		usage(argv[0]);
146 		exit(1);
147 	}
148 
149 	ret = run_event_script_enable(run_ctx, argv[3]);
150 	printf("Script enable %s completed with result=%d\n", argv[3], ret);
151 }
152 
do_disable(TALLOC_CTX * mem_ctx,struct tevent_context * ev,struct run_event_context * run_ctx,int argc,const char ** argv)153 static void do_disable(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
154 		       struct run_event_context *run_ctx,
155 		       int argc, const char **argv)
156 {
157 	int ret;
158 
159 	if (argc != 4) {
160 		usage(argv[0]);
161 		exit(1);
162 	}
163 
164 	ret = run_event_script_disable(run_ctx, argv[3]);
165 	printf("Script disable %s completed with result=%d\n", argv[3], ret);
166 }
167 
main(int argc,const char ** argv)168 int main(int argc, const char **argv)
169 {
170 	TALLOC_CTX *mem_ctx;
171 	struct tevent_context *ev;
172 	struct run_proc_context *run_proc_ctx = NULL;
173 	struct run_event_context *run_ctx = NULL;
174 	int ret;
175 
176 	if (argc < 3) {
177 		usage(argv[0]);
178 		exit(1);
179 	}
180 
181 	mem_ctx = talloc_new(NULL);
182 	if (mem_ctx == NULL) {
183 		fprintf(stderr, "talloc_new() failed\n");
184 		exit(1);
185 	}
186 
187 	ev = tevent_context_init(mem_ctx);
188 	if (ev == NULL) {
189 		fprintf(stderr, "tevent_context_init() failed\n");
190 		exit(1);
191 	}
192 
193 	ret = run_proc_init(mem_ctx, ev, &run_proc_ctx);
194 	if (ret != 0) {
195 		fprintf(stderr, "run_proc_init() failed, ret=%d\n", ret);
196 		exit(1);
197 	}
198 
199 	ret = run_event_init(mem_ctx, run_proc_ctx, argv[1], NULL, &run_ctx);
200 	if (ret != 0) {
201 		fprintf(stderr, "run_event_init() failed, ret=%d\n", ret);
202 		exit(1);
203 	}
204 
205 	if (strcmp(argv[2], "run") == 0) {
206 		do_run(mem_ctx, ev, run_ctx, argc, argv);
207 	} else if (strcmp(argv[2], "list") == 0) {
208 		do_list(mem_ctx, ev, run_ctx, argc, argv);
209 	} else if (strcmp(argv[2], "enable") == 0) {
210 		do_enable(mem_ctx, ev, run_ctx, argc, argv);
211 	} else if (strcmp(argv[2], "disable") == 0) {
212 		do_disable(mem_ctx, ev, run_ctx, argc, argv);
213 	} else {
214 		fprintf(stderr, "Invalid command %s\n", argv[2]);
215 		usage(argv[0]);
216 	}
217 
218 	talloc_free(mem_ctx);
219 	exit(0);
220 }
221 
222