]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
bb338451 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
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. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "unixctl.h" | |
064af421 | 19 | #include <errno.h> |
064af421 BP |
20 | #include <unistd.h> |
21 | #include "coverage.h" | |
22 | #include "dirs.h" | |
3e8a2ad1 | 23 | #include "openvswitch/dynamic-string.h" |
ee89ea7b | 24 | #include "openvswitch/json.h" |
bde9f75d | 25 | #include "jsonrpc.h" |
b19bab5b | 26 | #include "openvswitch/list.h" |
064af421 | 27 | #include "poll-loop.h" |
ee89ea7b | 28 | #include "openvswitch/shash.h" |
bde9f75d | 29 | #include "stream.h" |
cb54a8c5 | 30 | #include "stream-provider.h" |
3c442619 | 31 | #include "svec.h" |
e6211adc | 32 | #include "openvswitch/vlog.h" |
064af421 | 33 | |
d98e6007 | 34 | VLOG_DEFINE_THIS_MODULE(unixctl); |
d76f09ea BP |
35 | |
36 | COVERAGE_DEFINE(unixctl_received); | |
37 | COVERAGE_DEFINE(unixctl_replied); | |
064af421 BP |
38 | \f |
39 | struct unixctl_command { | |
0e15264f BP |
40 | const char *usage; |
41 | int min_args, max_args; | |
8ca79daa BP |
42 | unixctl_cb_func *cb; |
43 | void *aux; | |
064af421 BP |
44 | }; |
45 | ||
46 | struct unixctl_conn { | |
ca6ba700 | 47 | struct ovs_list node; |
bde9f75d | 48 | struct jsonrpc *rpc; |
064af421 | 49 | |
bde9f75d EJ |
50 | /* Only one request can be in progress at a time. While the request is |
51 | * being processed, 'request_id' is populated, otherwise it is null. */ | |
52 | struct json *request_id; /* ID of the currently active request. */ | |
064af421 BP |
53 | }; |
54 | ||
55 | /* Server for control connection. */ | |
56 | struct unixctl_server { | |
bde9f75d | 57 | struct pstream *listener; |
ca6ba700 | 58 | struct ovs_list conns; |
064af421 BP |
59 | }; |
60 | ||
064af421 BP |
61 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); |
62 | ||
63 | static struct shash commands = SHASH_INITIALIZER(&commands); | |
64 | ||
65 | static void | |
91a11f5b AW |
66 | unixctl_list_commands(struct unixctl_conn *conn, int argc OVS_UNUSED, |
67 | const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) | |
064af421 BP |
68 | { |
69 | struct ds ds = DS_EMPTY_INITIALIZER; | |
7ff2009a | 70 | const struct shash_node **nodes = shash_sort(&commands); |
3c442619 | 71 | size_t i; |
064af421 BP |
72 | |
73 | ds_put_cstr(&ds, "The available commands are:\n"); | |
3c442619 | 74 | |
7ff2009a JP |
75 | for (i = 0; i < shash_count(&commands); i++) { |
76 | const struct shash_node *node = nodes[i]; | |
77 | const struct unixctl_command *command = node->data; | |
bde9f75d | 78 | |
e7b5947a | 79 | ds_put_format(&ds, " %-23s %s\n", node->name, command->usage); |
3c442619 | 80 | } |
7ff2009a | 81 | free(nodes); |
3c442619 | 82 | |
bde9f75d | 83 | unixctl_command_reply(conn, ds_cstr(&ds)); |
064af421 BP |
84 | ds_destroy(&ds); |
85 | } | |
86 | ||
d5e1e5ed | 87 | static void |
0e15264f BP |
88 | unixctl_version(struct unixctl_conn *conn, int argc OVS_UNUSED, |
89 | const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) | |
d5e1e5ed | 90 | { |
b0248b2a | 91 | unixctl_command_reply(conn, ovs_get_program_version()); |
d5e1e5ed JP |
92 | } |
93 | ||
0e15264f BP |
94 | /* Registers a unixctl command with the given 'name'. 'usage' describes the |
95 | * arguments to the command; it is used only for presentation to the user in | |
91a11f5b | 96 | * "list-commands" output. |
0e15264f | 97 | * |
b558fd90 BP |
98 | * 'cb' is called when the command is received. It is passed an array |
99 | * containing the command name and arguments, plus a copy of 'aux'. Normally | |
100 | * 'cb' should reply by calling unixctl_command_reply() or | |
101 | * unixctl_command_reply_error() before it returns, but if the command cannot | |
102 | * be handled immediately then it can defer the reply until later. A given | |
103 | * connection can only process a single request at a time, so a reply must be | |
104 | * made eventually to avoid blocking that connection. */ | |
064af421 | 105 | void |
0e15264f BP |
106 | unixctl_command_register(const char *name, const char *usage, |
107 | int min_args, int max_args, | |
108 | unixctl_cb_func *cb, void *aux) | |
064af421 BP |
109 | { |
110 | struct unixctl_command *command; | |
4895c701 SH |
111 | struct unixctl_command *lookup = shash_find_data(&commands, name); |
112 | ||
cb22974d | 113 | ovs_assert(!lookup || lookup->cb == cb); |
4895c701 SH |
114 | |
115 | if (lookup) { | |
116 | return; | |
117 | } | |
064af421 | 118 | |
064af421 | 119 | command = xmalloc(sizeof *command); |
0e15264f BP |
120 | command->usage = usage; |
121 | command->min_args = min_args; | |
122 | command->max_args = max_args; | |
064af421 | 123 | command->cb = cb; |
8ca79daa | 124 | command->aux = aux; |
064af421 BP |
125 | shash_add(&commands, name, command); |
126 | } | |
127 | ||
bde9f75d EJ |
128 | static void |
129 | unixctl_command_reply__(struct unixctl_conn *conn, | |
130 | bool success, const char *body) | |
064af421 | 131 | { |
bde9f75d EJ |
132 | struct json *body_json; |
133 | struct jsonrpc_msg *reply; | |
134 | ||
135 | COVERAGE_INC(unixctl_replied); | |
cb22974d | 136 | ovs_assert(conn->request_id); |
bde9f75d EJ |
137 | |
138 | if (!body) { | |
139 | body = ""; | |
064af421 | 140 | } |
bde9f75d EJ |
141 | |
142 | if (body[0] && body[strlen(body) - 1] != '\n') { | |
143 | body_json = json_string_create_nocopy(xasprintf("%s\n", body)); | |
144 | } else { | |
145 | body_json = json_string_create(body); | |
146 | } | |
147 | ||
148 | if (success) { | |
149 | reply = jsonrpc_create_reply(body_json, conn->request_id); | |
150 | } else { | |
151 | reply = jsonrpc_create_error(body_json, conn->request_id); | |
152 | } | |
153 | ||
bb338451 BP |
154 | if (VLOG_IS_DBG_ENABLED()) { |
155 | char *id = json_to_string(conn->request_id, 0); | |
156 | VLOG_DBG("replying with %s, id=%s: \"%s\"", | |
157 | success ? "success" : "error", id, body); | |
158 | free(id); | |
159 | } | |
160 | ||
bde9f75d EJ |
161 | /* If jsonrpc_send() returns an error, the run loop will take care of the |
162 | * problem eventually. */ | |
163 | jsonrpc_send(conn->rpc, reply); | |
164 | json_destroy(conn->request_id); | |
165 | conn->request_id = NULL; | |
064af421 BP |
166 | } |
167 | ||
bde9f75d EJ |
168 | /* Replies to the active unixctl connection 'conn'. 'result' is sent to the |
169 | * client indicating the command was processed successfully. Only one call to | |
170 | * unixctl_command_reply() or unixctl_command_reply_error() may be made per | |
171 | * request. */ | |
064af421 | 172 | void |
bde9f75d | 173 | unixctl_command_reply(struct unixctl_conn *conn, const char *result) |
064af421 | 174 | { |
bde9f75d EJ |
175 | unixctl_command_reply__(conn, true, result); |
176 | } | |
064af421 | 177 | |
bde9f75d | 178 | /* Replies to the active unixctl connection 'conn'. 'error' is sent to the |
ec9f40dc | 179 | * client indicating an error occurred processing the command. Only one call to |
bde9f75d EJ |
180 | * unixctl_command_reply() or unixctl_command_reply_error() may be made per |
181 | * request. */ | |
182 | void | |
183 | unixctl_command_reply_error(struct unixctl_conn *conn, const char *error) | |
184 | { | |
185 | unixctl_command_reply__(conn, false, error); | |
064af421 BP |
186 | } |
187 | ||
cb54a8c5 | 188 | /* Creates a unixctl server listening on 'path', which for POSIX may be: |
064af421 BP |
189 | * |
190 | * - NULL, in which case <rundir>/<program>.<pid>.ctl is used. | |
191 | * | |
192 | * - A name that does not start with '/', in which case it is put in | |
193 | * <rundir>. | |
194 | * | |
195 | * - An absolute path (starting with '/') that gives the exact name of | |
196 | * the Unix domain socket to listen on. | |
197 | * | |
922247c6 | 198 | * For Windows, a local named pipe is used. A file is created in 'path' |
cb54a8c5 GS |
199 | * which may be: |
200 | * | |
201 | * - NULL, in which case <rundir>/<program>.ctl is used. | |
202 | * | |
203 | * - An absolute path that gives the name of the file. | |
204 | * | |
205 | * For both POSIX and Windows, if the path is "none", the function will | |
206 | * return successfully but no socket will actually be created. | |
207 | * | |
064af421 BP |
208 | * A program that (optionally) daemonizes itself should call this function |
209 | * *after* daemonization, so that the socket name contains the pid of the | |
210 | * daemon instead of the pid of the program that exited. (Otherwise, | |
3fbe1d30 | 211 | * "ovs-appctl --target=<program>" will fail.) |
064af421 BP |
212 | * |
213 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
614c4892 BP |
214 | * sets '*serverp' to the new unixctl_server (or to NULL if 'path' was "none"), |
215 | * otherwise to NULL. */ | |
064af421 BP |
216 | int |
217 | unixctl_server_create(const char *path, struct unixctl_server **serverp) | |
218 | { | |
219 | struct unixctl_server *server; | |
bde9f75d | 220 | struct pstream *listener; |
e3f512b0 | 221 | char *punix_path; |
064af421 BP |
222 | int error; |
223 | ||
bde9f75d | 224 | *serverp = NULL; |
614c4892 | 225 | if (path && !strcmp(path, "none")) { |
614c4892 BP |
226 | return 0; |
227 | } | |
228 | ||
064af421 | 229 | if (path) { |
e3f512b0 GS |
230 | char *abs_path; |
231 | #ifndef _WIN32 | |
cb54a8c5 | 232 | abs_path = abs_file_name(ovs_rundir(), path); |
e3f512b0 | 233 | #else |
13a233f7 | 234 | abs_path = xstrdup(path); |
e3f512b0 | 235 | #endif |
bde9f75d | 236 | punix_path = xasprintf("punix:%s", abs_path); |
e3f512b0 | 237 | free(abs_path); |
064af421 | 238 | } else { |
e3f512b0 | 239 | #ifndef _WIN32 |
bde9f75d EJ |
240 | punix_path = xasprintf("punix:%s/%s.%ld.ctl", ovs_rundir(), |
241 | program_name, (long int) getpid()); | |
cb54a8c5 | 242 | #else |
e3f512b0 | 243 | punix_path = xasprintf("punix:%s/%s.ctl", ovs_rundir(), program_name); |
cb54a8c5 | 244 | #endif |
e3f512b0 | 245 | } |
064af421 | 246 | |
ef8a3d14 | 247 | error = pstream_open(punix_path, &listener, 0); |
bde9f75d | 248 | if (error) { |
324f0c59 BP |
249 | ovs_error(error, "could not initialize control socket %s", punix_path); |
250 | goto exit; | |
064af421 BP |
251 | } |
252 | ||
91a11f5b AW |
253 | unixctl_command_register("list-commands", "", 0, 0, unixctl_list_commands, |
254 | NULL); | |
bde9f75d | 255 | unixctl_command_register("version", "", 0, 0, unixctl_version, NULL); |
064af421 | 256 | |
bde9f75d EJ |
257 | server = xmalloc(sizeof *server); |
258 | server->listener = listener; | |
417e7e66 | 259 | ovs_list_init(&server->conns); |
064af421 | 260 | *serverp = server; |
324f0c59 BP |
261 | |
262 | exit: | |
263 | free(punix_path); | |
264 | return error; | |
064af421 BP |
265 | } |
266 | ||
267 | static void | |
bde9f75d | 268 | process_command(struct unixctl_conn *conn, struct jsonrpc_msg *request) |
064af421 | 269 | { |
bde9f75d | 270 | char *error = NULL; |
064af421 | 271 | |
064af421 | 272 | struct unixctl_command *command; |
bde9f75d | 273 | struct json_array *params; |
064af421 BP |
274 | |
275 | COVERAGE_INC(unixctl_received); | |
bde9f75d EJ |
276 | conn->request_id = json_clone(request->id); |
277 | ||
bb338451 BP |
278 | if (VLOG_IS_DBG_ENABLED()) { |
279 | char *params_s = json_to_string(request->params, 0); | |
280 | char *id_s = json_to_string(request->id, 0); | |
281 | VLOG_DBG("received request %s%s, id=%s", | |
282 | request->method, params_s, id_s); | |
283 | free(params_s); | |
284 | free(id_s); | |
285 | } | |
286 | ||
bde9f75d EJ |
287 | params = json_array(request->params); |
288 | command = shash_find_data(&commands, request->method); | |
289 | if (!command) { | |
4410f206 BP |
290 | error = xasprintf("\"%s\" is not a valid command (use " |
291 | "\"list-commands\" to see a list of valid commands)", | |
292 | request->method); | |
bde9f75d EJ |
293 | } else if (params->n < command->min_args) { |
294 | error = xasprintf("\"%s\" command requires at least %d arguments", | |
295 | request->method, command->min_args); | |
296 | } else if (params->n > command->max_args) { | |
297 | error = xasprintf("\"%s\" command takes at most %d arguments", | |
298 | request->method, command->max_args); | |
064af421 | 299 | } else { |
bde9f75d EJ |
300 | struct svec argv = SVEC_EMPTY_INITIALIZER; |
301 | int i; | |
302 | ||
303 | svec_add(&argv, request->method); | |
304 | for (i = 0; i < params->n; i++) { | |
305 | if (params->elems[i]->type != JSON_STRING) { | |
306 | error = xasprintf("\"%s\" command has non-string argument", | |
307 | request->method); | |
308 | break; | |
309 | } | |
310 | svec_add(&argv, json_string(params->elems[i])); | |
311 | } | |
312 | svec_terminate(&argv); | |
313 | ||
314 | if (!error) { | |
0e15264f BP |
315 | command->cb(conn, argv.n, (const char **) argv.names, |
316 | command->aux); | |
317 | } | |
318 | ||
bde9f75d | 319 | svec_destroy(&argv); |
064af421 | 320 | } |
0e15264f | 321 | |
bde9f75d EJ |
322 | if (error) { |
323 | unixctl_command_reply_error(conn, error); | |
324 | free(error); | |
325 | } | |
064af421 BP |
326 | } |
327 | ||
328 | static int | |
bde9f75d | 329 | run_connection(struct unixctl_conn *conn) |
064af421 | 330 | { |
bde9f75d | 331 | int error, i; |
064af421 | 332 | |
bde9f75d EJ |
333 | jsonrpc_run(conn->rpc); |
334 | error = jsonrpc_get_status(conn->rpc); | |
335 | if (error || jsonrpc_get_backlog(conn->rpc)) { | |
336 | return error; | |
337 | } | |
064af421 | 338 | |
bde9f75d EJ |
339 | for (i = 0; i < 10; i++) { |
340 | struct jsonrpc_msg *msg; | |
064af421 | 341 | |
bde9f75d EJ |
342 | if (error || conn->request_id) { |
343 | break; | |
064af421 BP |
344 | } |
345 | ||
bde9f75d EJ |
346 | jsonrpc_recv(conn->rpc, &msg); |
347 | if (msg) { | |
348 | if (msg->type == JSONRPC_REQUEST) { | |
349 | process_command(conn, msg); | |
064af421 | 350 | } else { |
bde9f75d EJ |
351 | VLOG_WARN_RL(&rl, "%s: received unexpected %s message", |
352 | jsonrpc_get_name(conn->rpc), | |
353 | jsonrpc_msg_type_to_string(msg->type)); | |
354 | error = EINVAL; | |
064af421 | 355 | } |
bde9f75d | 356 | jsonrpc_msg_destroy(msg); |
064af421 | 357 | } |
bde9f75d | 358 | error = error ? error : jsonrpc_get_status(conn->rpc); |
064af421 | 359 | } |
064af421 | 360 | |
bde9f75d | 361 | return error; |
064af421 BP |
362 | } |
363 | ||
364 | static void | |
365 | kill_connection(struct unixctl_conn *conn) | |
366 | { | |
417e7e66 | 367 | ovs_list_remove(&conn->node); |
bde9f75d EJ |
368 | jsonrpc_close(conn->rpc); |
369 | json_destroy(conn->request_id); | |
064af421 BP |
370 | free(conn); |
371 | } | |
372 | ||
373 | void | |
374 | unixctl_server_run(struct unixctl_server *server) | |
375 | { | |
614c4892 BP |
376 | if (!server) { |
377 | return; | |
378 | } | |
379 | ||
71f21279 | 380 | for (int i = 0; i < 10; i++) { |
bde9f75d EJ |
381 | struct stream *stream; |
382 | int error; | |
383 | ||
384 | error = pstream_accept(server->listener, &stream); | |
385 | if (!error) { | |
386 | struct unixctl_conn *conn = xzalloc(sizeof *conn); | |
417e7e66 | 387 | ovs_list_push_back(&server->conns, &conn->node); |
bde9f75d EJ |
388 | conn->rpc = jsonrpc_open(stream); |
389 | } else if (error == EAGAIN) { | |
064af421 | 390 | break; |
bde9f75d EJ |
391 | } else { |
392 | VLOG_WARN_RL(&rl, "%s: accept failed: %s", | |
393 | pstream_get_name(server->listener), | |
10a89ef0 | 394 | ovs_strerror(error)); |
064af421 | 395 | } |
064af421 BP |
396 | } |
397 | ||
71f21279 | 398 | struct unixctl_conn *conn, *next; |
4e8e4213 | 399 | LIST_FOR_EACH_SAFE (conn, next, node, &server->conns) { |
064af421 BP |
400 | int error = run_connection(conn); |
401 | if (error && error != EAGAIN) { | |
402 | kill_connection(conn); | |
403 | } | |
404 | } | |
405 | } | |
406 | ||
407 | void | |
408 | unixctl_server_wait(struct unixctl_server *server) | |
409 | { | |
410 | struct unixctl_conn *conn; | |
411 | ||
614c4892 BP |
412 | if (!server) { |
413 | return; | |
414 | } | |
415 | ||
bde9f75d | 416 | pstream_wait(server->listener); |
4e8e4213 | 417 | LIST_FOR_EACH (conn, node, &server->conns) { |
bde9f75d EJ |
418 | jsonrpc_wait(conn->rpc); |
419 | if (!jsonrpc_get_backlog(conn->rpc)) { | |
420 | jsonrpc_recv_wait(conn->rpc); | |
064af421 BP |
421 | } |
422 | } | |
423 | } | |
424 | ||
425 | /* Destroys 'server' and stops listening for connections. */ | |
426 | void | |
427 | unixctl_server_destroy(struct unixctl_server *server) | |
428 | { | |
429 | if (server) { | |
430 | struct unixctl_conn *conn, *next; | |
431 | ||
4e8e4213 | 432 | LIST_FOR_EACH_SAFE (conn, next, node, &server->conns) { |
064af421 BP |
433 | kill_connection(conn); |
434 | } | |
435 | ||
bde9f75d | 436 | pstream_close(server->listener); |
064af421 BP |
437 | free(server); |
438 | } | |
439 | } | |
440 | \f | |
cb54a8c5 GS |
441 | /* On POSIX based systems, connects to a unixctl server socket. 'path' should |
442 | * be the name of a unixctl server socket. If it does not start with '/', it | |
443 | * will be prefixed with the rundir (e.g. /usr/local/var/run/openvswitch). | |
444 | * | |
922247c6 AS |
445 | * On Windows, connects to a local named pipe. A file which resides in |
446 | * 'path' is used to mimic the behavior of a Unix domain socket. | |
cb54a8c5 | 447 | * 'path' should be an absolute path of the file. |
064af421 BP |
448 | * |
449 | * Returns 0 if successful, otherwise a positive errno value. If successful, | |
bde9f75d | 450 | * sets '*client' to the new jsonrpc, otherwise to NULL. */ |
064af421 | 451 | int |
bde9f75d | 452 | unixctl_client_create(const char *path, struct jsonrpc **client) |
064af421 | 453 | { |
bde9f75d EJ |
454 | char *abs_path, *unix_path; |
455 | struct stream *stream; | |
064af421 | 456 | int error; |
cb54a8c5 | 457 | |
e3f512b0 | 458 | #ifdef _WIN32 |
13a233f7 | 459 | abs_path = xstrdup(path); |
cb54a8c5 | 460 | #else |
bde9f75d | 461 | abs_path = abs_file_name(ovs_rundir(), path); |
cb54a8c5 | 462 | #endif |
e3f512b0 | 463 | unix_path = xasprintf("unix:%s", abs_path); |
cb54a8c5 GS |
464 | |
465 | *client = NULL; | |
466 | ||
f125905c MM |
467 | error = stream_open_block(stream_open(unix_path, &stream, DSCP_DEFAULT), |
468 | &stream); | |
bde9f75d EJ |
469 | free(unix_path); |
470 | free(abs_path); | |
064af421 | 471 | |
bde9f75d EJ |
472 | if (error) { |
473 | VLOG_WARN("failed to connect to %s", path); | |
474 | return error; | |
064af421 | 475 | } |
bde9f75d EJ |
476 | |
477 | *client = jsonrpc_open(stream); | |
478 | return 0; | |
064af421 BP |
479 | } |
480 | ||
bde9f75d EJ |
481 | /* Executes 'command' on the server with an argument vector 'argv' containing |
482 | * 'argc' elements. If successfully communicated with the server, returns 0 | |
483 | * and sets '*result', or '*err' (not both) to the result or error the server | |
484 | * returned. Otherwise, sets '*result' and '*err' to NULL and returns a | |
485 | * positive errno value. The caller is responsible for freeing '*result' or | |
486 | * '*err' if not NULL. */ | |
064af421 | 487 | int |
bde9f75d EJ |
488 | unixctl_client_transact(struct jsonrpc *client, const char *command, int argc, |
489 | char *argv[], char **result, char **err) | |
064af421 | 490 | { |
bde9f75d EJ |
491 | struct jsonrpc_msg *request, *reply; |
492 | struct json **json_args, *params; | |
493 | int error, i; | |
064af421 | 494 | |
bde9f75d EJ |
495 | *result = NULL; |
496 | *err = NULL; | |
497 | ||
498 | json_args = xmalloc(argc * sizeof *json_args); | |
499 | for (i = 0; i < argc; i++) { | |
500 | json_args[i] = json_string_create(argv[i]); | |
064af421 | 501 | } |
bde9f75d EJ |
502 | params = json_array_create(json_args, argc); |
503 | request = jsonrpc_create_request(command, params, NULL); | |
504 | ||
505 | error = jsonrpc_transact_block(client, request, &reply); | |
506 | if (error) { | |
507 | VLOG_WARN("error communicating with %s: %s", jsonrpc_get_name(client), | |
2bf1d3cc | 508 | ovs_retval_to_string(error)); |
bde9f75d | 509 | return error; |
064af421 BP |
510 | } |
511 | ||
bde9f75d EJ |
512 | if (reply->error) { |
513 | if (reply->error->type == JSON_STRING) { | |
514 | *err = xstrdup(json_string(reply->error)); | |
515 | } else { | |
516 | VLOG_WARN("%s: unexpected error type in JSON RPC reply: %s", | |
517 | jsonrpc_get_name(client), | |
518 | json_type_to_string(reply->error->type)); | |
519 | error = EINVAL; | |
064af421 | 520 | } |
bde9f75d EJ |
521 | } else if (reply->result) { |
522 | if (reply->result->type == JSON_STRING) { | |
523 | *result = xstrdup(json_string(reply->result)); | |
064af421 | 524 | } else { |
bde9f75d EJ |
525 | VLOG_WARN("%s: unexpected result type in JSON rpc reply: %s", |
526 | jsonrpc_get_name(client), | |
527 | json_type_to_string(reply->result->type)); | |
528 | error = EINVAL; | |
064af421 BP |
529 | } |
530 | } | |
064af421 | 531 | |
bde9f75d EJ |
532 | jsonrpc_msg_destroy(reply); |
533 | return error; | |
064af421 | 534 | } |