/*
- * Copyright (c) 2009, 2010 Nicira Networks.
+ * Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
#include <config.h>
-
+#undef NDEBUG
#include "jsonrpc.h"
-
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
-
#include "command-line.h"
#include "daemon.h"
#include "json.h"
+#include "ovstest.h"
#include "poll-loop.h"
#include "stream-ssl.h"
#include "stream.h"
#include "timeval.h"
#include "util.h"
-#include "vlog.h"
+#include "openvswitch/vlog.h"
-static struct command all_commands[];
-
-static void usage(void) NO_RETURN;
+OVS_NO_RETURN static void usage(void);
static void parse_options(int argc, char *argv[]);
+static struct ovs_cmdl_command *get_all_commands(void);
-int
-main(int argc, char *argv[])
+static void
+test_jsonrpc_main(int argc, char *argv[])
{
- proctitle_init(argc, argv);
+ struct ovs_cmdl_context ctx = { .argc = 0, };
+ ovs_cmdl_proctitle_init(argc, argv);
set_program_name(argv[0]);
- time_init();
- vlog_init();
+ service_start(&argc, &argv);
parse_options(argc, argv);
- run_command(argc - optind, argv + optind, all_commands);
- return 0;
+ ctx.argc = argc - optind;
+ ctx.argv = argv + optind;
+ ovs_cmdl_run_command(&ctx, get_all_commands());
}
static void
parse_options(int argc, char *argv[])
{
enum {
- OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1
+ OPT_BOOTSTRAP_CA_CERT = UCHAR_MAX + 1,
+ DAEMON_OPTION_ENUMS
};
- static struct option long_options[] = {
- {"verbose", optional_argument, 0, 'v'},
- {"help", no_argument, 0, 'h'},
+ static const struct option long_options[] = {
+ {"verbose", optional_argument, NULL, 'v'},
+ {"help", no_argument, NULL, 'h'},
DAEMON_LONG_OPTIONS,
-#ifdef HAVE_OPENSSL
- {"bootstrap-ca-cert", required_argument, 0, OPT_BOOTSTRAP_CA_CERT},
- STREAM_SSL_LONG_OPTIONS
-#endif
- {0, 0, 0, 0},
+ {"bootstrap-ca-cert", required_argument, NULL, OPT_BOOTSTRAP_CA_CERT},
+ STREAM_SSL_LONG_OPTIONS,
+ {NULL, 0, NULL, 0},
};
- char *short_options = long_options_to_short_options(long_options);
+ char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
for (;;) {
int c = getopt_long(argc, argv, short_options, long_options, NULL);
DAEMON_OPTION_HANDLERS
-#ifdef HAVE_OPENSSL
STREAM_SSL_OPTION_HANDLERS
case OPT_BOOTSTRAP_CA_CERT:
stream_ssl_set_ca_cert_file(optarg, true);
break;
-#endif
case '?':
exit(EXIT_FAILURE);
\f
/* Command implementations. */
-static void
+static int
handle_rpc(struct jsonrpc *rpc, struct jsonrpc_msg *msg, bool *done)
{
- struct jsonrpc_msg *reply = NULL;
if (msg->type == JSONRPC_REQUEST) {
+ struct jsonrpc_msg *reply = NULL;
if (!strcmp(msg->method, "echo")) {
reply = jsonrpc_create_reply(json_clone(msg->params), msg->id);
} else {
reply = jsonrpc_create_error(error, msg->id);
ovs_error(0, "unknown request %s", msg->method);
}
-
+ jsonrpc_send(rpc, reply);
+ return 0;
} else if (msg->type == JSONRPC_NOTIFY) {
if (!strcmp(msg->method, "shutdown")) {
*done = true;
+ return 0;
} else {
- jsonrpc_error(rpc, ENOTTY);
ovs_error(0, "unknown notification %s", msg->method);
+ return ENOTTY;
}
} else {
- jsonrpc_error(rpc, EPROTO);
ovs_error(0, "unsolicited JSON-RPC reply or error");
- }
-
- if (reply) {
- jsonrpc_send(rpc, reply);
+ return EPROTO;
}
}
static void
-do_listen(int argc OVS_UNUSED, char *argv[])
+do_listen(struct ovs_cmdl_context *ctx)
{
struct pstream *pstream;
struct jsonrpc **rpcs;
bool done;
int error;
- die_if_already_running();
-
- error = pstream_open(argv[1], &pstream);
+ error = jsonrpc_pstream_open(ctx->argv[1], &pstream, DSCP_DEFAULT);
if (error) {
- ovs_fatal(error, "could not listen on \"%s\"", argv[1]);
+ ovs_fatal(error, "could not listen on \"%s\"", ctx->argv[1]);
}
daemonize();
if (!jsonrpc_get_backlog(rpc)) {
error = jsonrpc_recv(rpc, &msg);
if (!error) {
- handle_rpc(rpc, msg, &done);
+ error = handle_rpc(rpc, msg, &done);
jsonrpc_msg_destroy(msg);
+ } else if (error == EAGAIN) {
+ error = 0;
}
}
- error = jsonrpc_get_status(rpc);
+ if (!error) {
+ error = jsonrpc_get_status(rpc);
+ }
if (error) {
jsonrpc_close(rpc);
ovs_error(error, "connection closed");
}
static void
-do_request(int argc OVS_UNUSED, char *argv[])
+do_request(struct ovs_cmdl_context *ctx)
{
struct jsonrpc_msg *msg;
struct jsonrpc *rpc;
char *string;
int error;
- method = argv[2];
- params = parse_json(argv[3]);
+ method = ctx->argv[2];
+ params = parse_json(ctx->argv[3]);
msg = jsonrpc_create_request(method, params, NULL);
string = jsonrpc_msg_is_valid(msg);
if (string) {
ovs_fatal(0, "not a valid JSON-RPC request: %s", string);
}
- error = stream_open_block(stream_open(argv[1], &stream), &stream);
+ error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
+ DSCP_DEFAULT), &stream);
if (error) {
- ovs_fatal(error, "could not open \"%s\"", argv[1]);
+ ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]);
}
rpc = jsonrpc_open(stream);
}
static void
-do_notify(int argc OVS_UNUSED, char *argv[])
+do_notify(struct ovs_cmdl_context *ctx)
{
struct jsonrpc_msg *msg;
struct jsonrpc *rpc;
char *string;
int error;
- method = argv[2];
- params = parse_json(argv[3]);
+ method = ctx->argv[2];
+ params = parse_json(ctx->argv[3]);
msg = jsonrpc_create_notify(method, params);
string = jsonrpc_msg_is_valid(msg);
if (string) {
ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
}
- error = stream_open_block(stream_open(argv[1], &stream), &stream);
+ error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
+ DSCP_DEFAULT), &stream);
if (error) {
- ovs_fatal(error, "could not open \"%s\"", argv[1]);
+ ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]);
}
rpc = jsonrpc_open(stream);
error = jsonrpc_send_block(rpc, msg);
if (error) {
- ovs_fatal(error, "could not send request");
+ ovs_fatal(error, "could not send notification");
}
jsonrpc_close(rpc);
}
static void
-do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+do_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
{
usage();
}
-static struct command all_commands[] = {
- { "listen", 1, 1, do_listen },
- { "request", 3, 3, do_request },
- { "notify", 3, 3, do_notify },
- { "help", 0, INT_MAX, do_help },
- { NULL, 0, 0, NULL },
+static struct ovs_cmdl_command all_commands[] = {
+ { "listen", NULL, 1, 1, do_listen },
+ { "request", NULL, 3, 3, do_request },
+ { "notify", NULL, 3, 3, do_notify },
+ { "help", NULL, 0, INT_MAX, do_help },
+ { NULL, NULL, 0, 0, NULL },
};
+
+static struct ovs_cmdl_command *
+get_all_commands(void)
+{
+ return all_commands;
+}
+
+OVSTEST_REGISTER("test-jsonrpc", test_jsonrpc_main);