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