]> git.proxmox.com Git - mirror_ovs.git/blob - tests/test-jsonrpc.c
daemon: Integrate checking for an existing pidfile into daemonize_start().
[mirror_ovs.git] / tests / test-jsonrpc.c
1 /*
2 * Copyright (c) 2009, 2010, 2011 Nicira Networks.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18
19 #include "jsonrpc.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <getopt.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "command-line.h"
28 #include "daemon.h"
29 #include "json.h"
30 #include "poll-loop.h"
31 #include "stream-ssl.h"
32 #include "stream.h"
33 #include "timeval.h"
34 #include "util.h"
35 #include "vlog.h"
36
37 static struct command all_commands[];
38
39 static void usage(void) NO_RETURN;
40 static void parse_options(int argc, char *argv[]);
41
42 int
43 main(int argc, char *argv[])
44 {
45 proctitle_init(argc, argv);
46 set_program_name(argv[0]);
47 parse_options(argc, argv);
48 run_command(argc - optind, argv + optind, all_commands);
49 return 0;
50 }
51
52 static void
53 parse_options(int argc, char *argv[])
54 {
55 enum {
56 OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
57 DAEMON_OPTION_ENUMS
58 };
59 static struct option long_options[] = {
60 {"verbose", optional_argument, 0, 'v'},
61 {"help", no_argument, 0, 'h'},
62 DAEMON_LONG_OPTIONS,
63 #ifdef HAVE_OPENSSL
64 {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
65 STREAM_SSL_LONG_OPTIONS
66 #endif
67 {0, 0, 0, 0},
68 };
69 char *short_options = long_options_to_short_options(long_options);
70
71 for (;;) {
72 int c = getopt_long(argc, argv, short_options, long_options, NULL);
73 if (c == -1) {
74 break;
75 }
76
77 switch (c) {
78 case 'h':
79 usage();
80
81 case 'v':
82 vlog_set_verbosity(optarg);
83 break;
84
85 DAEMON_OPTION_HANDLERS
86
87 #ifdef HAVE_OPENSSL
88 STREAM_SSL_OPTION_HANDLERS
89
90 case OPT_BOOTSTRAP_CA_CERT:
91 stream_ssl_set_ca_cert_file(optarg, true);
92 break;
93 #endif
94
95 case '?':
96 exit(EXIT_FAILURE);
97
98 default:
99 abort();
100 }
101 }
102 free(short_options);
103 }
104
105 static void
106 usage(void)
107 {
108 printf("%s: JSON-RPC test utility\n"
109 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
110 " listen LOCAL listen for connections on LOCAL\n"
111 " request REMOTE METHOD PARAMS send request, print reply\n"
112 " notify REMOTE METHOD PARAMS send notification and exit\n",
113 program_name, program_name);
114 stream_usage("JSON-RPC", true, true, true);
115 daemon_usage();
116 vlog_usage();
117 printf("\nOther options:\n"
118 " -h, --help display this help message\n");
119 exit(EXIT_SUCCESS);
120 }
121 \f
122 /* Command helper functions. */
123
124 static struct json *
125 parse_json(const char *s)
126 {
127 struct json *json = json_from_string(s);
128 if (json->type == JSON_STRING) {
129 ovs_fatal(0, "\"%s\": %s", s, json->u.string);
130 }
131 return json;
132 }
133
134 static void
135 print_and_free_json(struct json *json)
136 {
137 char *string = json_to_string(json, JSSF_SORT);
138 json_destroy(json);
139 puts(string);
140 free(string);
141 }
142 \f
143 /* Command implementations. */
144
145 static void
146 handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done)
147 {
148 struct jsonrpc_msg *reply = NULL;
149 if (msg->type == JSONRPC_REQUEST) {
150 if (!strcmp(msg->method, "echo")) {
151 reply = jsonrpc_create_reply(json_clone(msg->params), msg->id);
152 } else {
153 struct json *error = json_object_create();
154 json_object_put_string(error, "error", "unknown method");
155 reply = jsonrpc_create_error(error, msg->id);
156 ovs_error(0, "unknown request %s", msg->method);
157 }
158
159 } else if (msg->type == JSONRPC_NOTIFY) {
160 if (!strcmp(msg->method, "shutdown")) {
161 *done = true;
162 } else {
163 jsonrpc_error(rpc, ENOTTY);
164 ovs_error(0, "unknown notification %s", msg->method);
165 }
166 } else {
167 jsonrpc_error(rpc, EPROTO);
168 ovs_error(0, "unsolicited JSON-RPC reply or error");
169 }
170
171 if (reply) {
172 jsonrpc_send(rpc, reply);
173 }
174 }
175
176 static void
177 do_listen(int argc OVS_UNUSED, char *argv[])
178 {
179 struct pstream *pstream;
180 struct jsonrpc **rpcs;
181 size_t n_rpcs, allocated_rpcs;
182 bool done;
183 int error;
184
185 error = jsonrpc_pstream_open(argv[1], &pstream);
186 if (error) {
187 ovs_fatal(error, "could not listen on \"%s\"", argv[1]);
188 }
189
190 daemonize();
191
192 rpcs = NULL;
193 n_rpcs = allocated_rpcs = 0;
194 done = false;
195 for (;;) {
196 struct stream *stream;
197 size_t i;
198
199 /* Accept new connections. */
200 error = pstream_accept(pstream, &stream);
201 if (!error) {
202 if (n_rpcs >= allocated_rpcs) {
203 rpcs = x2nrealloc(rpcs, &allocated_rpcs, sizeof *rpcs);
204 }
205 rpcs[n_rpcs++] = jsonrpc_open(stream);
206 } else if (error != EAGAIN) {
207 ovs_fatal(error, "pstream_accept failed");
208 }
209
210 /* Service existing connections. */
211 for (i = 0; i < n_rpcs; ) {
212 struct jsonrpc *rpc = rpcs[i];
213 struct jsonrpc_msg *msg;
214
215 jsonrpc_run(rpc);
216 if (!jsonrpc_get_backlog(rpc)) {
217 error = jsonrpc_recv(rpc, &msg);
218 if (!error) {
219 handle_rpc(rpc, msg, &done);
220 jsonrpc_msg_destroy(msg);
221 }
222 }
223
224 error = jsonrpc_get_status(rpc);
225 if (error) {
226 jsonrpc_close(rpc);
227 ovs_error(error, "connection closed");
228 memmove(&rpcs[i], &rpcs[i + 1],
229 (n_rpcs - i - 1) * sizeof *rpcs);
230 n_rpcs--;
231 } else {
232 i++;
233 }
234 }
235
236 /* Wait for something to do. */
237 if (done && !n_rpcs) {
238 break;
239 }
240 pstream_wait(pstream);
241 for (i = 0; i < n_rpcs; i++) {
242 struct jsonrpc *rpc = rpcs[i];
243
244 jsonrpc_wait(rpc);
245 if (!jsonrpc_get_backlog(rpc)) {
246 jsonrpc_recv_wait(rpc);
247 }
248 }
249 poll_block();
250 }
251 free(rpcs);
252 pstream_close(pstream);
253 }
254
255 static void
256 do_request(int argc OVS_UNUSED, char *argv[])
257 {
258 struct jsonrpc_msg *msg;
259 struct jsonrpc *rpc;
260 struct json *params;
261 struct stream *stream;
262 const char *method;
263 char *string;
264 int error;
265
266 method = argv[2];
267 params = parse_json(argv[3]);
268 msg = jsonrpc_create_request(method, params, NULL);
269 string = jsonrpc_msg_is_valid(msg);
270 if (string) {
271 ovs_fatal(0, "not a valid JSON-RPC request: %s", string);
272 }
273
274 error = stream_open_block(jsonrpc_stream_open(argv[1], &stream), &stream);
275 if (error) {
276 ovs_fatal(error, "could not open \"%s\"", argv[1]);
277 }
278 rpc = jsonrpc_open(stream);
279
280 error = jsonrpc_send(rpc, msg);
281 if (error) {
282 ovs_fatal(error, "could not send request");
283 }
284
285 error = jsonrpc_recv_block(rpc, &msg);
286 if (error) {
287 ovs_fatal(error, "error waiting for reply");
288 }
289 print_and_free_json(jsonrpc_msg_to_json(msg));
290
291 jsonrpc_close(rpc);
292 }
293
294 static void
295 do_notify(int argc OVS_UNUSED, char *argv[])
296 {
297 struct jsonrpc_msg *msg;
298 struct jsonrpc *rpc;
299 struct json *params;
300 struct stream *stream;
301 const char *method;
302 char *string;
303 int error;
304
305 method = argv[2];
306 params = parse_json(argv[3]);
307 msg = jsonrpc_create_notify(method, params);
308 string = jsonrpc_msg_is_valid(msg);
309 if (string) {
310 ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
311 }
312
313 error = stream_open_block(jsonrpc_stream_open(argv[1], &stream), &stream);
314 if (error) {
315 ovs_fatal(error, "could not open \"%s\"", argv[1]);
316 }
317 rpc = jsonrpc_open(stream);
318
319 error = jsonrpc_send_block(rpc, msg);
320 if (error) {
321 ovs_fatal(error, "could not send notification");
322 }
323 jsonrpc_close(rpc);
324 }
325
326 static void
327 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
328 {
329 usage();
330 }
331
332 static struct command all_commands[] = {
333 { "listen", 1, 1, do_listen },
334 { "request", 3, 3, do_request },
335 { "notify", 3, 3, do_notify },
336 { "help", 0, INT_MAX, do_help },
337 { NULL, 0, 0, NULL },
338 };