2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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"
34 static void usage(void);
35 static const char *parse_command_line(int argc, char *argv[]);
36 static struct unixctl_client *connect_to_target(const char *target);
39 main(int argc, char *argv[])
41 struct unixctl_client *client;
47 set_program_name(argv[0]);
49 /* Parse command line and connect to target. */
50 target = parse_command_line(argc, argv);
51 client = connect_to_target(target);
53 /* Transact request and process reply. */
54 request = process_escape_args(argv + optind);
55 error = unixctl_client_transact(client, request, &code, &reply);
58 ovs_fatal(error, "%s: transaction error", target);
60 if (code / 100 != 2) {
62 ovs_error(0, "%s: server returned reply code %03d", target, code);
67 unixctl_client_destroy(client);
77 %s, for querying and controlling Open vSwitch daemon\n\
78 usage: %s [TARGET] COMMAND [ARG...]\n\
80 -t, --target=TARGET pidfile or socket to contact\n\
82 help List commands supported by the target\n\
83 version Print version of the target\n\
84 vlog/list List current logging levels\n\
85 vlog/set MODULE[:FACILITY[:LEVEL]]\n\
86 Set MODULE and FACILITY log level to LEVEL\n\
87 MODULE may be any valid module name or 'ANY'\n\
88 FACILITY may be 'syslog', 'console', 'file', or 'ANY' (default)\n\
89 LEVEL may be 'off', 'emer', 'err', 'warn', 'info', or 'dbg' (default)\n\
90 vlog/reopen Make the program reopen its log file\n\
92 -h, --help Print this helpful information\n\
93 -V, --version Display ovs-appctl version information\n",
94 program_name, program_name);
99 parse_command_line(int argc, char *argv[])
101 static const struct option long_options[] = {
102 {"target", required_argument, NULL, 't'},
103 {"execute", no_argument, NULL, 'e'},
104 {"help", no_argument, NULL, 'h'},
105 {"version", no_argument, NULL, 'V'},
116 option = getopt_long(argc, argv, "+t:hVe", long_options, NULL);
123 ovs_fatal(0, "-t or --target may be specified only once");
129 /* We ignore -e for compatibility. Older versions specified the
130 * command as the argument to -e. Since the current version takes
131 * the command as non-option arguments and we say that -e has no
132 * arguments, this just works in the common case. */
134 ovs_fatal(0, "-e or --execute may be speciifed only once");
143 ovs_print_version(0, 0);
154 if (optind >= argc) {
155 ovs_fatal(0, "at least one non-option argument is required "
156 "(use --help for help)");
159 return target ? target : "ovs-vswitchd";
162 static struct unixctl_client *
163 connect_to_target(const char *target)
165 struct unixctl_client *client;
169 if (target[0] != '/') {
173 pidfile_name = xasprintf("%s/%s.pid", ovs_rundir(), target);
174 pid = read_pidfile(pidfile_name);
176 ovs_fatal(-pid, "cannot read pidfile \"%s\"", pidfile_name);
179 socket_name = xasprintf("%s/%s.%ld.ctl",
180 ovs_rundir(), target, (long int) pid);
182 socket_name = xstrdup(target);
185 error = unixctl_client_create(socket_name, &client);
187 ovs_fatal(error, "cannot connect to \"%s\"", socket_name);