2 * Copyright (c) 2008, 2009 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.
27 #include "command-line.h"
34 usage(char *prog_name, int exit_code)
36 printf("Usage: %s [TARGET] [ACTION...]\n"
38 " -t, --target=TARGET Path to Unix domain socket\n"
40 " -l, --list List current settings\n"
41 " -s, --set=MODULE[:FACILITY[:LEVEL]]\n"
42 " Set MODULE and FACILITY log level to LEVEL\n"
43 " MODULE may be any valid module name or 'ANY'\n"
44 " FACILITY may be 'syslog', 'console', 'file', or 'ANY' (default)\n"
45 " LEVEL may be 'emer', 'err', 'warn', 'info', or 'dbg' (default)\n"
46 " -r, --reopen Make the program reopen its log file\n"
47 " -e, --execute=COMMAND Execute control COMMAND and print its output\n"
49 " -h, --help Print this helpful information\n"
50 " -V, --version Display version information\n",
56 transact(struct unixctl_client *client, const char *request, bool *ok)
60 int error = unixctl_client_transact(client, request, &code, &reply);
62 fprintf(stderr, "%s: transaction error: %s\n",
63 unixctl_client_target(client), strerror(error));
67 if (code / 100 != 2) {
68 fprintf(stderr, "%s: server returned reply code %03d\n",
69 unixctl_client_target(client), code);
76 transact_ack(struct unixctl_client *client, const char *request, bool *ok)
78 free(transact(client, request, ok));
82 execute_command(struct unixctl_client *client, const char *request, bool *ok)
86 int error = unixctl_client_transact(client, request, &code, &reply);
88 fprintf(stderr, "%s: transaction error: %s\n",
89 unixctl_client_target(client), strerror(error));
92 if (code / 100 != 2) {
93 fprintf(stderr, "%s: server returned reply code %03d\n",
94 unixctl_client_target(client), code);
104 add_target(struct unixctl_client ***clients, size_t *n_clients,
105 const char *path, bool *ok)
107 struct unixctl_client *client;
108 int error = unixctl_client_create(path, &client);
110 fprintf(stderr, "Error connecting to \"%s\": %s\n",
111 path, strerror(error));
114 *clients = xrealloc(*clients, sizeof *clients * (*n_clients + 1));
115 (*clients)[*n_clients] = client;
120 int main(int argc, char *argv[])
122 static const struct option long_options[] = {
123 /* Target options must come first. */
124 {"target", required_argument, NULL, 't'},
125 {"help", no_argument, NULL, 'h'},
126 {"version", no_argument, NULL, 'V'},
128 /* Action options come afterward. */
129 {"list", no_argument, NULL, 'l'},
130 {"set", required_argument, NULL, 's'},
131 {"reopen", no_argument, NULL, 'r'},
132 {"execute", required_argument, NULL, 'e'},
137 /* Determine targets. */
140 struct unixctl_client **clients = NULL;
141 size_t n_clients = 0;
143 set_program_name(argv[0]);
146 short_options = long_options_to_short_options(long_options);
151 option = getopt_long(argc, argv, short_options, long_options, NULL);
155 if (!strchr("thV", option) && n_clients == 0) {
156 ovs_fatal(0, "no targets specified (use --help for help)");
162 add_target(&clients, &n_clients, optarg, &ok);
166 for (i = 0; i < n_clients; i++) {
167 struct unixctl_client *client = clients[i];
170 printf("%s:\n", unixctl_client_target(client));
171 reply = transact(client, "vlog/list", &ok);
172 fputs(reply, stdout);
178 for (i = 0; i < n_clients; i++) {
179 struct unixctl_client *client = clients[i];
180 char *request = xasprintf("vlog/set %s", optarg);
181 transact_ack(client, request, &ok);
187 for (i = 0; i < n_clients; i++) {
188 struct unixctl_client *client = clients[i];
189 char *request = xstrdup("vlog/reopen");
190 transact_ack(client, request, &ok);
196 for (i = 0; i < n_clients; i++) {
197 execute_command(clients[i], optarg, &ok);
202 usage(argv[0], EXIT_SUCCESS);
206 OVS_PRINT_VERSION(0, 0);
218 "warning: no actions specified (use --help for help)\n");