~lynxman/ubuntu/natty/openvswitch/lp_822142

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>

#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "command-line.h"
#include "daemon.h"
#include "dirs.h"
#include "dynamic-string.h"
#include "timeval.h"
#include "unixctl.h"
#include "util.h"

static void usage(void);
static const char *parse_command_line(int argc, char *argv[]);
static struct unixctl_client *connect_to_target(const char *target);

int
main(int argc, char *argv[])
{
    struct unixctl_client *client;
    const char *target;
    struct ds request;
    int code, error;
    char *reply;
    int i;

    set_program_name(argv[0]);

    /* Parse command line and connect to target. */
    target = parse_command_line(argc, argv);
    client = connect_to_target(target);

    /* Compose request. */
    ds_init(&request);
    for (i = optind; i < argc; i++) {
        if (i != optind) {
            ds_put_char(&request, ' ');
        }
        ds_put_cstr(&request, argv[i]);
    }

    /* Transact request and process reply. */
    error = unixctl_client_transact(client, ds_cstr(&request), &code, &reply);
    if (error) {
        ovs_fatal(error, "%s: transaction error", target);
    }
    if (code / 100 != 2) {
        ovs_error(0, "%s: server returned reply code %03d", target, code);
        exit(2);
    }
    fputs(reply, stdout);

    unixctl_client_destroy(client);
    free(reply);
    ds_destroy(&request);

    return 0;
}

static void
usage(void)
{
    printf("%s, for querying and controlling Open vSwitch daemon\n"
           "usage: %s [TARGET] COMMAND [ARG...]\n"
           "Targets:\n"
           "  -t, --target=TARGET  pidfile or socket to contact\n"
           "Common commands:"
           "  help               List commands supported by the target\n"
           "  vlog/list          List current logging levels\n"
           "  vlog/set MODULE[:FACILITY[:LEVEL]]\n"
           "        Set MODULE and FACILITY log level to LEVEL\n"
           "        MODULE may be any valid module name or 'ANY'\n"
           "        FACILITY may be 'syslog', 'console', 'file', or 'ANY' (default)\n"
           "        LEVEL may be 'emer', 'err', 'warn', 'info', or 'dbg' (default)\n"
           "  vlog/reopen        Make the program reopen its log file\n"
           "Other options:\n"
           "  -h, --help         Print this helpful information\n"
           "  -V, --version      Display version information\n",
           program_name, program_name);
    exit(EXIT_SUCCESS);
}

static const char *
parse_command_line(int argc, char *argv[])
{
    static const struct option long_options[] = {
        {"target", required_argument, NULL, 't'},
        {"execute", no_argument, NULL, 'e'},
        {"help", no_argument, NULL, 'h'},
        {"version", no_argument, NULL, 'V'},
        {0, 0, 0, 0},
    };
    const char *target;
    int e_options;

    target = NULL;
    e_options = 0;
    for (;;) {
        int option;

        option = getopt_long(argc, argv, "+t:hVe", long_options, NULL);
        if (option == -1) {
            break;
        }
        switch (option) {
        case 't':
            if (target) {
                ovs_fatal(0, "-t or --target may be specified only once");
            }
            target = optarg;
            break;

        case 'e':
            /* We ignore -e for compatibility.  Older versions specified the
             * command as the argument to -e.  Since the current version takes
             * the command as non-option arguments and we say that -e has no
             * arguments, this just works in the common case. */
            if (e_options++) {
                ovs_fatal(0, "-e or --execute may be speciifed only once");
            }
            break;

        case 'h':
            usage();
            break;

        case 'V':
            OVS_PRINT_VERSION(0, 0);
            exit(EXIT_SUCCESS);

        case '?':
            exit(EXIT_FAILURE);

        default:
            NOT_REACHED();
        }
    }

    if (optind >= argc) {
        ovs_fatal(0, "at least one non-option argument is required "
                  "(use --help for help)");
    }

    return target ? target : "ovs-vswitchd";
}

static struct unixctl_client *
connect_to_target(const char *target)
{
    struct unixctl_client *client;
    char *socket_name;
    int error;

    if (target[0] != '/') {
        char *pidfile_name;
        pid_t pid;

        pidfile_name = xasprintf("%s/%s.pid", ovs_rundir, target);
        pid = read_pidfile(pidfile_name);
        if (pid < 0) {
            ovs_fatal(-pid, "cannot read pidfile \"%s\"", pidfile_name);
        }
        free(pidfile_name);
        socket_name = xasprintf("%s/%s.%ld.ctl",
                                ovs_rundir, target, (long int) pid);
    } else {
        socket_name = xstrdup(target);
    }

    error = unixctl_client_create(socket_name, &client);
    if (error) {
        ovs_fatal(error, "cannot connect to \"%s\"", socket_name);
    }
    free(socket_name);

    return client;
}