2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include "command-line.h"
28 #include "dynamic-string.h"
35 static void usage(void);
36 static const char *parse_command_line(int argc, char *argv[]);
37 static struct jsonrpc *connect_to_target(const char *target);
40 main(int argc, char *argv[])
42 char *cmd_result, *cmd_error;
43 struct jsonrpc *client;
44 char *cmd, **cmd_argv;
49 set_program_name(argv[0]);
51 /* Parse command line and connect to target. */
52 target = parse_command_line(argc, argv);
53 client = connect_to_target(target);
55 /* Transact request and process reply. */
57 cmd_argc = argc - optind;
58 cmd_argv = cmd_argc ? argv + optind : NULL;
59 error = unixctl_client_transact(client, cmd, cmd_argc, cmd_argv,
60 &cmd_result, &cmd_error);
62 ovs_fatal(error, "%s: transaction error", target);
66 jsonrpc_close(client);
67 fputs(cmd_error, stderr);
68 ovs_error(0, "%s: server returned an error", target);
70 } else if (cmd_result) {
71 fputs(cmd_result, stdout);
76 jsonrpc_close(client);
86 %s, for querying and controlling Open vSwitch daemon\n\
87 usage: %s [TARGET] COMMAND [ARG...]\n\
89 -t, --target=TARGET pidfile or socket to contact\n\
91 help List commands supported by the target\n\
92 version Print version of the target\n\
93 vlog/list List current logging levels\n\
95 Set log levels as detailed in SPEC, which may include:\n\
96 A valid module name (all modules, by default)\n\
97 'syslog', 'console', 'file' (all facilities, by default))\n\
98 'off', 'emer', 'err', 'warn', 'info', or 'dbg' ('dbg', bydefault)\n\
99 vlog/reopen Make the program reopen its log file\n\
101 --timeout=SECS wait at most SECS seconds for a response\n\
102 -h, --help Print this helpful information\n\
103 -V, --version Display ovs-appctl version information\n",
104 program_name, program_name);
109 parse_command_line(int argc, char *argv[])
111 static const struct option long_options[] = {
112 {"target", required_argument, NULL, 't'},
113 {"execute", no_argument, NULL, 'e'},
114 {"help", no_argument, NULL, 'h'},
115 {"version", no_argument, NULL, 'V'},
116 {"timeout", required_argument, NULL, 'T'},
127 option = getopt_long(argc, argv, "+t:hVe", long_options, NULL);
134 ovs_fatal(0, "-t or --target may be specified only once");
140 /* We ignore -e for compatibility. Older versions specified the
141 * command as the argument to -e. Since the current version takes
142 * the command as non-option arguments and we say that -e has no
143 * arguments, this just works in the common case. */
145 ovs_fatal(0, "-e or --execute may be speciifed only once");
154 time_alarm(atoi(optarg));
158 ovs_print_version(0, 0);
169 if (optind >= argc) {
170 ovs_fatal(0, "at least one non-option argument is required "
171 "(use --help for help)");
174 return target ? target : "ovs-vswitchd";
177 static struct jsonrpc *
178 connect_to_target(const char *target)
180 struct jsonrpc *client;
185 if (target[0] != '/') {
189 pidfile_name = xasprintf("%s/%s.pid", ovs_rundir(), target);
190 pid = read_pidfile(pidfile_name);
192 ovs_fatal(-pid, "cannot read pidfile \"%s\"", pidfile_name);
195 socket_name = xasprintf("%s/%s.%ld.ctl",
196 ovs_rundir(), target, (long int) pid);
198 /* On windows, if the 'target' contains ':', we make an assumption that
199 * it is an absolute path. */
200 if (!strchr(target, ':')) {
201 socket_name = xasprintf("%s/%s.ctl", ovs_rundir(), target);
204 socket_name = xstrdup(target);
207 error = unixctl_client_create(socket_name, &client);
209 ovs_fatal(error, "cannot connect to \"%s\"", socket_name);