]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
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. | |
064af421 | 15 | */ |
3fbe1d30 | 16 | |
064af421 | 17 | #include <config.h> |
064af421 | 18 | |
064af421 BP |
19 | #include <errno.h> |
20 | #include <getopt.h> | |
064af421 BP |
21 | #include <stdio.h> |
22 | #include <string.h> | |
23 | #include <stdlib.h> | |
24 | ||
25 | #include "command-line.h" | |
3fbe1d30 BP |
26 | #include "daemon.h" |
27 | #include "dirs.h" | |
28 | #include "dynamic-string.h" | |
bde9f75d | 29 | #include "jsonrpc.h" |
0e15264f | 30 | #include "process.h" |
064af421 BP |
31 | #include "timeval.h" |
32 | #include "unixctl.h" | |
33 | #include "util.h" | |
34 | ||
3fbe1d30 BP |
35 | static void usage(void); |
36 | static const char *parse_command_line(int argc, char *argv[]); | |
bde9f75d | 37 | static struct jsonrpc *connect_to_target(const char *target); |
064af421 | 38 | |
3fbe1d30 BP |
39 | int |
40 | main(int argc, char *argv[]) | |
064af421 | 41 | { |
bde9f75d EJ |
42 | char *cmd_result, *cmd_error; |
43 | struct jsonrpc *client; | |
44 | char *cmd, **cmd_argv; | |
3fbe1d30 | 45 | const char *target; |
bde9f75d EJ |
46 | int cmd_argc; |
47 | int error; | |
3fbe1d30 BP |
48 | |
49 | set_program_name(argv[0]); | |
3fbe1d30 BP |
50 | |
51 | /* Parse command line and connect to target. */ | |
52 | target = parse_command_line(argc, argv); | |
53 | client = connect_to_target(target); | |
54 | ||
3fbe1d30 | 55 | /* Transact request and process reply. */ |
bde9f75d EJ |
56 | cmd = argv[optind++]; |
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); | |
064af421 | 61 | if (error) { |
3fbe1d30 BP |
62 | ovs_fatal(error, "%s: transaction error", target); |
63 | } | |
bde9f75d EJ |
64 | |
65 | if (cmd_error) { | |
66 | fputs(cmd_error, stderr); | |
67 | ovs_error(0, "%s: server returned an error", target); | |
3fbe1d30 | 68 | exit(2); |
bde9f75d EJ |
69 | } else if (cmd_result) { |
70 | fputs(cmd_result, stdout); | |
71 | } else { | |
428b2edd | 72 | OVS_NOT_REACHED(); |
064af421 | 73 | } |
65f92a50 | 74 | |
bde9f75d EJ |
75 | jsonrpc_close(client); |
76 | free(cmd_result); | |
77 | free(cmd_error); | |
3fbe1d30 | 78 | return 0; |
064af421 BP |
79 | } |
80 | ||
81 | static void | |
3fbe1d30 | 82 | usage(void) |
064af421 | 83 | { |
c1a543a8 BP |
84 | printf("\ |
85 | %s, for querying and controlling Open vSwitch daemon\n\ | |
86 | usage: %s [TARGET] COMMAND [ARG...]\n\ | |
87 | Targets:\n\ | |
88 | -t, --target=TARGET pidfile or socket to contact\n\ | |
89 | Common commands:\n\ | |
90 | help List commands supported by the target\n\ | |
d5e1e5ed | 91 | version Print version of the target\n\ |
c1a543a8 | 92 | vlog/list List current logging levels\n\ |
2a3e30b2 BP |
93 | vlog/set [SPEC]\n\ |
94 | Set log levels as detailed in SPEC, which may include:\n\ | |
95 | A valid module name (all modules, by default)\n\ | |
96 | 'syslog', 'console', 'file' (all facilities, by default))\n\ | |
97 | 'off', 'emer', 'err', 'warn', 'info', or 'dbg' ('dbg', bydefault)\n\ | |
c1a543a8 BP |
98 | vlog/reopen Make the program reopen its log file\n\ |
99 | Other options:\n\ | |
f4ec6ff4 | 100 | --timeout=SECS wait at most SECS seconds for a response\n\ |
c1a543a8 | 101 | -h, --help Print this helpful information\n\ |
d5e1e5ed | 102 | -V, --version Display ovs-appctl version information\n", |
3fbe1d30 BP |
103 | program_name, program_name); |
104 | exit(EXIT_SUCCESS); | |
064af421 BP |
105 | } |
106 | ||
3fbe1d30 BP |
107 | static const char * |
108 | parse_command_line(int argc, char *argv[]) | |
064af421 BP |
109 | { |
110 | static const struct option long_options[] = { | |
064af421 | 111 | {"target", required_argument, NULL, 't'}, |
3fbe1d30 | 112 | {"execute", no_argument, NULL, 'e'}, |
064af421 BP |
113 | {"help", no_argument, NULL, 'h'}, |
114 | {"version", no_argument, NULL, 'V'}, | |
f4ec6ff4 | 115 | {"timeout", required_argument, NULL, 'T'}, |
e3c17733 | 116 | {NULL, 0, NULL, 0}, |
064af421 | 117 | }; |
3fbe1d30 BP |
118 | const char *target; |
119 | int e_options; | |
064af421 | 120 | |
3fbe1d30 BP |
121 | target = NULL; |
122 | e_options = 0; | |
064af421 BP |
123 | for (;;) { |
124 | int option; | |
064af421 | 125 | |
3fbe1d30 | 126 | option = getopt_long(argc, argv, "+t:hVe", long_options, NULL); |
064af421 BP |
127 | if (option == -1) { |
128 | break; | |
129 | } | |
064af421 BP |
130 | switch (option) { |
131 | case 't': | |
3fbe1d30 BP |
132 | if (target) { |
133 | ovs_fatal(0, "-t or --target may be specified only once"); | |
064af421 | 134 | } |
3fbe1d30 | 135 | target = optarg; |
064af421 BP |
136 | break; |
137 | ||
138 | case 'e': | |
3fbe1d30 BP |
139 | /* We ignore -e for compatibility. Older versions specified the |
140 | * command as the argument to -e. Since the current version takes | |
141 | * the command as non-option arguments and we say that -e has no | |
142 | * arguments, this just works in the common case. */ | |
143 | if (e_options++) { | |
144 | ovs_fatal(0, "-e or --execute may be speciifed only once"); | |
064af421 BP |
145 | } |
146 | break; | |
147 | ||
148 | case 'h': | |
3fbe1d30 | 149 | usage(); |
064af421 BP |
150 | break; |
151 | ||
f4ec6ff4 EJ |
152 | case 'T': |
153 | time_alarm(atoi(optarg)); | |
154 | break; | |
155 | ||
064af421 | 156 | case 'V': |
55d5bb44 | 157 | ovs_print_version(0, 0); |
064af421 BP |
158 | exit(EXIT_SUCCESS); |
159 | ||
160 | case '?': | |
161 | exit(EXIT_FAILURE); | |
162 | ||
163 | default: | |
428b2edd | 164 | OVS_NOT_REACHED(); |
064af421 BP |
165 | } |
166 | } | |
3fbe1d30 BP |
167 | |
168 | if (optind >= argc) { | |
169 | ovs_fatal(0, "at least one non-option argument is required " | |
170 | "(use --help for help)"); | |
171 | } | |
172 | ||
173 | return target ? target : "ovs-vswitchd"; | |
174 | } | |
175 | ||
bde9f75d | 176 | static struct jsonrpc * |
3fbe1d30 BP |
177 | connect_to_target(const char *target) |
178 | { | |
bde9f75d | 179 | struct jsonrpc *client; |
3fbe1d30 BP |
180 | char *socket_name; |
181 | int error; | |
182 | ||
183 | if (target[0] != '/') { | |
184 | char *pidfile_name; | |
3fbe1d30 BP |
185 | pid_t pid; |
186 | ||
b43c6fe2 | 187 | pidfile_name = xasprintf("%s/%s.pid", ovs_rundir(), target); |
3fbe1d30 BP |
188 | pid = read_pidfile(pidfile_name); |
189 | if (pid < 0) { | |
190 | ovs_fatal(-pid, "cannot read pidfile \"%s\"", pidfile_name); | |
191 | } | |
192 | free(pidfile_name); | |
193 | socket_name = xasprintf("%s/%s.%ld.ctl", | |
b43c6fe2 | 194 | ovs_rundir(), target, (long int) pid); |
3fbe1d30 BP |
195 | } else { |
196 | socket_name = xstrdup(target); | |
197 | } | |
198 | ||
199 | error = unixctl_client_create(socket_name, &client); | |
200 | if (error) { | |
201 | ovs_fatal(error, "cannot connect to \"%s\"", socket_name); | |
064af421 | 202 | } |
3fbe1d30 BP |
203 | free(socket_name); |
204 | ||
205 | return client; | |
064af421 | 206 | } |
3fbe1d30 | 207 |