1 /* Copyright (c) 2015 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
20 #include "command-line.h"
22 #include "fatal-signal.h"
23 #include "openvswitch/vlog.h"
25 #include "poll-loop.h"
29 VLOG_DEFINE_THIS_MODULE(test_unixctl);
31 static void parse_options(int *argc, char **argvp[], char **unixctl_pathp);
32 OVS_NO_RETURN static void usage(void);
35 test_unixctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
36 const char *argv[] OVS_UNUSED, void *exiting_)
38 bool *exiting = exiting_;
40 unixctl_command_reply(conn, NULL);
44 test_unixctl_echo(struct unixctl_conn *conn, int argc OVS_UNUSED,
45 const char *argv[], void *aux OVS_UNUSED)
47 unixctl_command_reply(conn, argv[1]);
51 test_unixctl_echo_error(struct unixctl_conn *conn, int argc OVS_UNUSED,
52 const char *argv[], void *aux OVS_UNUSED)
54 unixctl_command_reply_error(conn, argv[1]);
58 test_unixctl_log(struct unixctl_conn *conn, int argc OVS_UNUSED,
59 const char *argv[], void *aux OVS_UNUSED)
61 VLOG_INFO("%s", argv[1]);
62 unixctl_command_reply(conn, NULL);
66 test_unixctl_block(struct unixctl_conn *conn OVS_UNUSED, int argc OVS_UNUSED,
67 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
69 VLOG_INFO("%s", argv[1]);
70 unixctl_command_reply(conn, NULL);
74 test_unixctl_main(int argc, char *argv[])
76 char *unixctl_path = NULL;
77 struct unixctl_server *unixctl;
80 ovs_cmdl_proctitle_init(argc, argv);
81 set_program_name(argv[0]);
82 service_start(&argc, &argv);
83 fatal_ignore_sigpipe();
84 parse_options(&argc, &argv, &unixctl_path);
86 daemonize_start(false);
87 int retval = unixctl_server_create(unixctl_path, &unixctl);
91 unixctl_command_register("exit", "", 0, 0, test_unixctl_exit, &exiting);
92 unixctl_command_register("echo", "ARG", 1, 1, test_unixctl_echo, NULL);
93 unixctl_command_register("echo_error", "ARG", 1, 1,
94 test_unixctl_echo_error, NULL);
95 unixctl_command_register("log", "ARG", 1, 1, test_unixctl_log, NULL);
96 unixctl_command_register("block", "", 0, 0, test_unixctl_block, NULL);
99 VLOG_INFO("Entering run loop.");
101 unixctl_server_run(unixctl);
102 unixctl_server_wait(unixctl);
104 poll_immediate_wake();
108 unixctl_server_destroy(unixctl);
115 parse_options(int *argcp, char **argvp[], char **unixctl_pathp)
118 OPT_REMOTE = UCHAR_MAX + 1,
123 static const struct option long_options[] = {
124 {"unixctl", required_argument, NULL, OPT_UNIXCTL},
125 {"help", no_argument, NULL, 'h'},
126 {"version", no_argument, NULL, 'V'},
131 char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
133 char **argv = *argvp;
138 c = getopt_long(argc, argv, short_options, long_options, NULL);
145 *unixctl_pathp = optarg;
152 ovs_print_version(0, 0);
156 DAEMON_OPTION_HANDLERS
174 printf("%s: Open vSwitch unixctl test program\n"
175 "usage: %s [OPTIONS]\n",
176 program_name, program_name);
179 printf("\nOther options:\n"
180 " --unixctl=SOCKET override default control socket name\n"
181 " -h, --help display this help message\n"
182 " -V, --version display version information\n");
186 OVSTEST_REGISTER("test-unixctl", test_unixctl_main);