]>
git.proxmox.com Git - mirror_ovs.git/blob - utilities/ovs-appctl.c
ba0c172e6dadd208f01ec740d05689311e75a161
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2014 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 "openvswitch/dynamic-string.h"
34 #include "openvswitch/vlog.h"
36 static void usage(void);
37 static const char *parse_command_line(int argc
, char *argv
[]);
38 static struct jsonrpc
*connect_to_target(const char *target
);
41 main(int argc
, char *argv
[])
43 char *cmd_result
, *cmd_error
;
44 struct jsonrpc
*client
;
45 char *cmd
, **cmd_argv
;
50 set_program_name(argv
[0]);
52 /* Parse command line and connect to target. */
53 target
= parse_command_line(argc
, argv
);
54 client
= connect_to_target(target
);
56 /* Transact request and process reply. */
58 cmd_argc
= argc
- optind
;
59 cmd_argv
= cmd_argc
? argv
+ optind
: NULL
;
60 error
= unixctl_client_transact(client
, cmd
, cmd_argc
, cmd_argv
,
61 &cmd_result
, &cmd_error
);
63 ovs_fatal(error
, "%s: transaction error", target
);
67 jsonrpc_close(client
);
68 fputs(cmd_error
, stderr
);
69 ovs_error(0, "%s: server returned an error", target
);
71 } else if (cmd_result
) {
72 fputs(cmd_result
, stdout
);
77 jsonrpc_close(client
);
87 %s, for querying and controlling Open vSwitch daemon\n\
88 usage: %s [TARGET] COMMAND [ARG...]\n\
90 -t, --target=TARGET pidfile or socket to contact\n\
92 list-commands List commands supported by the target\n\
93 version Print version of the target\n\
94 vlog/list List current logging levels\n\
95 vlog/list-pattern List logging patterns for each destination.\n\
97 Set log levels as detailed in SPEC, which may include:\n\
98 A valid module name (all modules, by default)\n\
99 'syslog', 'console', 'file' (all destinations, by default))\n\
100 'off', 'emer', 'err', 'warn', 'info', or 'dbg' ('dbg', bydefault)\n\
101 vlog/reopen Make the program reopen its log file\n\
103 --timeout=SECS wait at most SECS seconds for a response\n\
104 -h, --help Print this helpful information\n\
105 -V, --version Display ovs-appctl version information\n",
106 program_name
, program_name
);
111 parse_command_line(int argc
, char *argv
[])
114 OPT_START
= UCHAR_MAX
+ 1,
117 static const struct option long_options
[] = {
118 {"target", required_argument
, NULL
, 't'},
119 {"execute", no_argument
, NULL
, 'e'},
120 {"help", no_argument
, NULL
, 'h'},
121 {"option", no_argument
, NULL
, 'o'},
122 {"version", no_argument
, NULL
, 'V'},
123 {"timeout", required_argument
, NULL
, 'T'},
127 char *short_options_
= ovs_cmdl_long_options_to_short_options(long_options
);
128 char *short_options
= xasprintf("+%s", short_options_
);
131 unsigned int timeout
= 0;
138 option
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
145 ovs_fatal(0, "-t or --target may be specified only once");
151 /* We ignore -e for compatibility. Older versions specified the
152 * command as the argument to -e. Since the current version takes
153 * the command as non-option arguments and we say that -e has no
154 * arguments, this just works in the common case. */
156 ovs_fatal(0, "-e or --execute may be speciifed only once");
165 ovs_cmdl_print_options(long_options
);
169 if (!str_to_uint(optarg
, 10, &timeout
) || !timeout
) {
170 ovs_fatal(0, "value %s on -T or --timeout is invalid", optarg
);
175 ovs_print_version(0, 0);
187 free(short_options_
);
190 ctl_timeout_setup(timeout
);
192 if (optind
>= argc
) {
193 ovs_fatal(0, "at least one non-option argument is required "
194 "(use --help for help)");
197 return target
? target
: "ovs-vswitchd";
200 static struct jsonrpc
*
201 connect_to_target(const char *target
)
203 struct jsonrpc
*client
;
208 if (target
[0] != '/') {
212 pidfile_name
= xasprintf("%s/%s.pid", ovs_rundir(), target
);
213 pid
= read_pidfile(pidfile_name
);
215 ovs_fatal(-pid
, "cannot read pidfile \"%s\"", pidfile_name
);
218 socket_name
= xasprintf("%s/%s.%ld.ctl",
219 ovs_rundir(), target
, (long int) pid
);
221 /* On windows, if the 'target' contains ':', we make an assumption that
222 * it is an absolute path. */
223 if (!strchr(target
, ':')) {
224 socket_name
= xasprintf("%s/%s.ctl", ovs_rundir(), target
);
227 socket_name
= xstrdup(target
);
230 error
= unixctl_client_create(socket_name
, &client
);
232 ovs_fatal(error
, "cannot connect to \"%s\"", socket_name
);