]> git.proxmox.com Git - mirror_ovs.git/blame - utilities/ovs-appctl.c
socket-util: Don't try to listen to a UDP socket.
[mirror_ovs.git] / utilities / ovs-appctl.c
CommitLineData
064af421 1/*
e3c17733 2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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"
064af421
BP
29#include "timeval.h"
30#include "unixctl.h"
31#include "util.h"
32
3fbe1d30
BP
33static void usage(void);
34static const char *parse_command_line(int argc, char *argv[]);
35static struct unixctl_client *connect_to_target(const char *target);
064af421 36
3fbe1d30
BP
37int
38main(int argc, char *argv[])
064af421 39{
3fbe1d30
BP
40 struct unixctl_client *client;
41 const char *target;
42 struct ds request;
43 int code, error;
064af421 44 char *reply;
3fbe1d30
BP
45 int i;
46
47 set_program_name(argv[0]);
3fbe1d30
BP
48
49 /* Parse command line and connect to target. */
50 target = parse_command_line(argc, argv);
51 client = connect_to_target(target);
52
53 /* Compose request. */
54 ds_init(&request);
55 for (i = optind; i < argc; i++) {
56 if (i != optind) {
57 ds_put_char(&request, ' ');
064af421 58 }
3fbe1d30 59 ds_put_cstr(&request, argv[i]);
064af421 60 }
064af421 61
3fbe1d30
BP
62 /* Transact request and process reply. */
63 error = unixctl_client_transact(client, ds_cstr(&request), &code, &reply);
064af421 64 if (error) {
3fbe1d30
BP
65 ovs_fatal(error, "%s: transaction error", target);
66 }
67 if (code / 100 != 2) {
3080205e 68 fputs(reply, stderr);
3fbe1d30
BP
69 ovs_error(0, "%s: server returned reply code %03d", target, code);
70 exit(2);
064af421 71 }
3fbe1d30
BP
72 fputs(reply, stdout);
73
65f92a50
BP
74 unixctl_client_destroy(client);
75 free(reply);
76 ds_destroy(&request);
77
3fbe1d30 78 return 0;
064af421
BP
79}
80
81static void
3fbe1d30 82usage(void)
064af421 83{
c1a543a8
BP
84 printf("\
85%s, for querying and controlling Open vSwitch daemon\n\
86usage: %s [TARGET] COMMAND [ARG...]\n\
87Targets:\n\
88 -t, --target=TARGET pidfile or socket to contact\n\
89Common commands:\n\
90 help List commands supported by the target\n\
d5e1e5ed 91 version Print version of the target\n\
c1a543a8
BP
92 vlog/list List current logging levels\n\
93 vlog/set MODULE[:FACILITY[:LEVEL]]\n\
94 Set MODULE and FACILITY log level to LEVEL\n\
95 MODULE may be any valid module name or 'ANY'\n\
96 FACILITY may be 'syslog', 'console', 'file', or 'ANY' (default)\n\
97 LEVEL may be 'off', 'emer', 'err', 'warn', 'info', or 'dbg' (default)\n\
98 vlog/reopen Make the program reopen its log file\n\
99Other options:\n\
100 -h, --help Print this helpful information\n\
d5e1e5ed 101 -V, --version Display ovs-appctl version information\n",
3fbe1d30
BP
102 program_name, program_name);
103 exit(EXIT_SUCCESS);
064af421
BP
104}
105
3fbe1d30
BP
106static const char *
107parse_command_line(int argc, char *argv[])
064af421
BP
108{
109 static const struct option long_options[] = {
064af421 110 {"target", required_argument, NULL, 't'},
3fbe1d30 111 {"execute", no_argument, NULL, 'e'},
064af421
BP
112 {"help", no_argument, NULL, 'h'},
113 {"version", no_argument, NULL, 'V'},
e3c17733 114 {NULL, 0, NULL, 0},
064af421 115 };
3fbe1d30
BP
116 const char *target;
117 int e_options;
064af421 118
3fbe1d30
BP
119 target = NULL;
120 e_options = 0;
064af421
BP
121 for (;;) {
122 int option;
064af421 123
3fbe1d30 124 option = getopt_long(argc, argv, "+t:hVe", long_options, NULL);
064af421
BP
125 if (option == -1) {
126 break;
127 }
064af421
BP
128 switch (option) {
129 case 't':
3fbe1d30
BP
130 if (target) {
131 ovs_fatal(0, "-t or --target may be specified only once");
064af421 132 }
3fbe1d30 133 target = optarg;
064af421
BP
134 break;
135
136 case 'e':
3fbe1d30
BP
137 /* We ignore -e for compatibility. Older versions specified the
138 * command as the argument to -e. Since the current version takes
139 * the command as non-option arguments and we say that -e has no
140 * arguments, this just works in the common case. */
141 if (e_options++) {
142 ovs_fatal(0, "-e or --execute may be speciifed only once");
064af421
BP
143 }
144 break;
145
146 case 'h':
3fbe1d30 147 usage();
064af421
BP
148 break;
149
150 case 'V':
55d5bb44 151 ovs_print_version(0, 0);
064af421
BP
152 exit(EXIT_SUCCESS);
153
154 case '?':
155 exit(EXIT_FAILURE);
156
157 default:
158 NOT_REACHED();
159 }
160 }
3fbe1d30
BP
161
162 if (optind >= argc) {
163 ovs_fatal(0, "at least one non-option argument is required "
164 "(use --help for help)");
165 }
166
167 return target ? target : "ovs-vswitchd";
168}
169
170static struct unixctl_client *
171connect_to_target(const char *target)
172{
173 struct unixctl_client *client;
174 char *socket_name;
175 int error;
176
177 if (target[0] != '/') {
178 char *pidfile_name;
3fbe1d30
BP
179 pid_t pid;
180
b43c6fe2 181 pidfile_name = xasprintf("%s/%s.pid", ovs_rundir(), target);
3fbe1d30
BP
182 pid = read_pidfile(pidfile_name);
183 if (pid < 0) {
184 ovs_fatal(-pid, "cannot read pidfile \"%s\"", pidfile_name);
185 }
186 free(pidfile_name);
187 socket_name = xasprintf("%s/%s.%ld.ctl",
b43c6fe2 188 ovs_rundir(), target, (long int) pid);
3fbe1d30
BP
189 } else {
190 socket_name = xstrdup(target);
191 }
192
193 error = unixctl_client_create(socket_name, &client);
194 if (error) {
195 ovs_fatal(error, "cannot connect to \"%s\"", socket_name);
064af421 196 }
3fbe1d30
BP
197 free(socket_name);
198
199 return client;
064af421 200}
3fbe1d30 201