X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=utilities%2Fovs-appctl.c;h=a6fbebd0ec03f826e07637deaf4268e13d0810f3;hb=HEAD;hp=060e8e286db91c413721d7e67c23e501c547fef0;hpb=3fbe1d307e27ed99528d83cf866c7df497e58b77;p=sliver-openvswitch.git diff --git a/utilities/ovs-appctl.c b/utilities/ovs-appctl.c index 060e8e286..a6fbebd0e 100644 --- a/utilities/ovs-appctl.c +++ b/utilities/ovs-appctl.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009 Nicira Networks. + * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,73 +26,81 @@ #include "daemon.h" #include "dirs.h" #include "dynamic-string.h" +#include "jsonrpc.h" +#include "process.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); +static struct jsonrpc *connect_to_target(const char *target); int main(int argc, char *argv[]) { - struct unixctl_client *client; + char *cmd_result, *cmd_error; + struct jsonrpc *client; + char *cmd, **cmd_argv; const char *target; - struct ds request; - int code, error; - char *reply; - int i; + int cmd_argc; + int error; set_program_name(argv[0]); - time_init(); /* 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); + cmd = argv[optind++]; + cmd_argc = argc - optind; + cmd_argv = cmd_argc ? argv + optind : NULL; + error = unixctl_client_transact(client, cmd, cmd_argc, cmd_argv, + &cmd_result, &cmd_error); if (error) { ovs_fatal(error, "%s: transaction error", target); } - if (code / 100 != 2) { - ovs_error(0, "%s: server returned reply code %03d", target, code); + + if (cmd_error) { + jsonrpc_close(client); + fputs(cmd_error, stderr); + ovs_error(0, "%s: server returned an error", target); exit(2); + } else if (cmd_result) { + fputs(cmd_result, stdout); + } else { + OVS_NOT_REACHED(); } - fputs(reply, stdout); + jsonrpc_close(client); + free(cmd_result); + free(cmd_error); 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", + 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:\n\ + help List commands supported by the target\n\ + version Print version of the target\n\ + vlog/list List current logging levels\n\ + vlog/set [SPEC]\n\ + Set log levels as detailed in SPEC, which may include:\n\ + A valid module name (all modules, by default)\n\ + 'syslog', 'console', 'file' (all facilities, by default))\n\ + 'off', 'emer', 'err', 'warn', 'info', or 'dbg' ('dbg', bydefault)\n\ + vlog/reopen Make the program reopen its log file\n\ +Other options:\n\ + --timeout=SECS wait at most SECS seconds for a response\n\ + -h, --help Print this helpful information\n\ + -V, --version Display ovs-appctl version information\n", program_name, program_name); exit(EXIT_SUCCESS); } @@ -105,7 +113,8 @@ parse_command_line(int argc, char *argv[]) {"execute", no_argument, NULL, 'e'}, {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, - {0, 0, 0, 0}, + {"timeout", required_argument, NULL, 'T'}, + {NULL, 0, NULL, 0}, }; const char *target; int e_options; @@ -141,15 +150,19 @@ parse_command_line(int argc, char *argv[]) usage(); break; + case 'T': + time_alarm(atoi(optarg)); + break; + case 'V': - OVS_PRINT_VERSION(0, 0); + ovs_print_version(0, 0); exit(EXIT_SUCCESS); case '?': exit(EXIT_FAILURE); default: - NOT_REACHED(); + OVS_NOT_REACHED(); } } @@ -161,26 +174,32 @@ parse_command_line(int argc, char *argv[]) return target ? target : "ovs-vswitchd"; } -static struct unixctl_client * +static struct jsonrpc * connect_to_target(const char *target) { - struct unixctl_client *client; + struct jsonrpc *client; char *socket_name; int error; +#ifndef _WIN32 if (target[0] != '/') { char *pidfile_name; - char *socket_name; pid_t pid; - pidfile_name = xasprintf("%s/%s.pid", ovs_rundir, target); + 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); + ovs_rundir(), target, (long int) pid); +#else + /* On windows, if the 'target' contains ':', we make an assumption that + * it is an absolute path. */ + if (!strchr(target, ':')) { + socket_name = xasprintf("%s/%s.ctl", ovs_rundir(), target); +#endif } else { socket_name = xstrdup(target); }