LCOV - code coverage report
Current view: top level - utilities - ovs-appctl.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 58 72 80.6 %
Date: 2016-09-14 01:02:56 Functions: 3 4 75.0 %
Branches: 24 36 66.7 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2014 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 "openvswitch/dynamic-string.h"
      29                 :            : #include "jsonrpc.h"
      30                 :            : #include "process.h"
      31                 :            : #include "timeval.h"
      32                 :            : #include "unixctl.h"
      33                 :            : #include "util.h"
      34                 :            : #include "openvswitch/vlog.h"
      35                 :            : 
      36                 :            : static void usage(void);
      37                 :            : static const char *parse_command_line(int argc, char *argv[]);
      38                 :            : static struct jsonrpc *connect_to_target(const char *target);
      39                 :            : 
      40                 :            : int
      41                 :      10982 : main(int argc, char *argv[])
      42                 :            : {
      43                 :            :     char *cmd_result, *cmd_error;
      44                 :            :     struct jsonrpc *client;
      45                 :            :     char *cmd, **cmd_argv;
      46                 :            :     const char *target;
      47                 :            :     int cmd_argc;
      48                 :            :     int error;
      49                 :            : 
      50                 :      10982 :     set_program_name(argv[0]);
      51                 :            : 
      52                 :            :     /* Parse command line and connect to target. */
      53                 :      10982 :     target = parse_command_line(argc, argv);
      54                 :      10966 :     client = connect_to_target(target);
      55                 :            : 
      56                 :            :     /* Transact request and process reply. */
      57                 :      10946 :     cmd = argv[optind++];
      58                 :      10946 :     cmd_argc = argc - optind;
      59         [ +  + ]:      10946 :     cmd_argv = cmd_argc ? argv + optind : NULL;
      60                 :      10946 :     error = unixctl_client_transact(client, cmd, cmd_argc, cmd_argv,
      61                 :            :                                     &cmd_result, &cmd_error);
      62         [ -  + ]:      10946 :     if (error) {
      63                 :          0 :         ovs_fatal(error, "%s: transaction error", target);
      64                 :            :     }
      65                 :            : 
      66         [ +  + ]:      10946 :     if (cmd_error) {
      67                 :         68 :         jsonrpc_close(client);
      68                 :         68 :         fputs(cmd_error, stderr);
      69                 :         68 :         ovs_error(0, "%s: server returned an error", target);
      70                 :         68 :         exit(2);
      71         [ +  - ]:      10878 :     } else if (cmd_result) {
      72                 :      10878 :         fputs(cmd_result, stdout);
      73                 :            :     } else {
      74                 :          0 :         OVS_NOT_REACHED();
      75                 :            :     }
      76                 :            : 
      77                 :      10878 :     jsonrpc_close(client);
      78                 :      10878 :     free(cmd_result);
      79                 :      10878 :     free(cmd_error);
      80                 :      10878 :     return 0;
      81                 :            : }
      82                 :            : 
      83                 :            : static void
      84                 :          0 : usage(void)
      85                 :            : {
      86                 :          0 :     printf("\
      87                 :            : %s, for querying and controlling Open vSwitch daemon\n\
      88                 :            : usage: %s [TARGET] COMMAND [ARG...]\n\
      89                 :            : Targets:\n\
      90                 :            :   -t, --target=TARGET  pidfile or socket to contact\n\
      91                 :            : Common commands:\n\
      92                 :            :   list-commands      List commands supported by the target\n\
      93                 :            :   version            Print version of the target\n\
      94                 :            :   vlog/list          List current logging levels\n\
      95                 :            :   vlog/list-pattern  List logging patterns for each destination.\n\
      96                 :            :   vlog/set [SPEC]\n\
      97                 :            :       Set log levels as detailed in SPEC, which may include:\n\
      98                 :            :       A valid module name (all modules, by default)\n\
      99                 :            :       'syslog', 'console', 'file' (all destinations, by default))\n\
     100                 :            :       'off', 'emer', 'err', 'warn', 'info', or 'dbg' ('dbg', bydefault)\n\
     101                 :            :   vlog/reopen        Make the program reopen its log file\n\
     102                 :            : Other options:\n\
     103                 :            :   --timeout=SECS     wait at most SECS seconds for a response\n\
     104                 :            :   -h, --help         Print this helpful information\n\
     105                 :            :   -V, --version      Display ovs-appctl version information\n",
     106                 :            :            program_name, program_name);
     107                 :          0 :     exit(EXIT_SUCCESS);
     108                 :            : }
     109                 :            : 
     110                 :            : static const char *
     111                 :      10982 : parse_command_line(int argc, char *argv[])
     112                 :            : {
     113                 :            :     enum {
     114                 :            :         OPT_START = UCHAR_MAX + 1,
     115                 :            :         VLOG_OPTION_ENUMS
     116                 :            :     };
     117                 :            :     static const struct option long_options[] = {
     118                 :            :         {"target", required_argument, NULL, 't'},
     119                 :            :         {"execute", no_argument, NULL, 'e'},
     120                 :            :         {"help", no_argument, NULL, 'h'},
     121                 :            :         {"option", no_argument, NULL, 'o'},
     122                 :            :         {"version", no_argument, NULL, 'V'},
     123                 :            :         {"timeout", required_argument, NULL, 'T'},
     124                 :            :         VLOG_LONG_OPTIONS,
     125                 :            :         {NULL, 0, NULL, 0},
     126                 :            :     };
     127                 :      10982 :     char *short_options_ = ovs_cmdl_long_options_to_short_options(long_options);
     128                 :      10982 :     char *short_options = xasprintf("+%s", short_options_);
     129                 :            :     const char *target;
     130                 :            :     int e_options;
     131                 :            : 
     132                 :      10982 :     target = NULL;
     133                 :      10982 :     e_options = 0;
     134                 :            :     for (;;) {
     135                 :            :         int option;
     136                 :            : 
     137                 :      13769 :         option = getopt_long(argc, argv, short_options, long_options, NULL);
     138         [ +  + ]:      13769 :         if (option == -1) {
     139                 :      10966 :             break;
     140                 :            :         }
     141   [ +  +  -  +  :       2803 :         switch (option) {
          +  -  +  -  -  
                -  -  - ]
     142                 :            :         case 't':
     143         [ -  + ]:       2621 :             if (target) {
     144                 :          0 :                 ovs_fatal(0, "-t or --target may be specified only once");
     145                 :            :             }
     146                 :       2621 :             target = optarg;
     147                 :       2621 :             break;
     148                 :            : 
     149                 :            :         case 'e':
     150                 :            :             /* We ignore -e for compatibility.  Older versions specified the
     151                 :            :              * command as the argument to -e.  Since the current version takes
     152                 :            :              * the command as non-option arguments and we say that -e has no
     153                 :            :              * arguments, this just works in the common case. */
     154         [ -  + ]:         41 :             if (e_options++) {
     155                 :          0 :                 ovs_fatal(0, "-e or --execute may be speciifed only once");
     156                 :            :             }
     157                 :         41 :             break;
     158                 :            : 
     159                 :            :         case 'h':
     160                 :          0 :             usage();
     161                 :          0 :             break;
     162                 :            : 
     163                 :            :         case 'o':
     164                 :         16 :             ovs_cmdl_print_options(long_options);
     165                 :         16 :             exit(EXIT_SUCCESS);
     166                 :            : 
     167                 :            :         case 'T':
     168                 :        119 :             time_alarm(atoi(optarg));
     169                 :        119 :             break;
     170                 :            : 
     171                 :            :         case 'V':
     172                 :          0 :             ovs_print_version(0, 0);
     173                 :          0 :             exit(EXIT_SUCCESS);
     174                 :            : 
     175                 :          6 :         VLOG_OPTION_HANDLERS
     176                 :            : 
     177                 :            :         case '?':
     178                 :          0 :             exit(EXIT_FAILURE);
     179                 :            : 
     180                 :            :         default:
     181                 :          0 :             OVS_NOT_REACHED();
     182                 :            :         }
     183                 :       2787 :     }
     184                 :      10966 :     free(short_options_);
     185                 :      10966 :     free(short_options);
     186                 :            : 
     187         [ -  + ]:      10966 :     if (optind >= argc) {
     188                 :          0 :         ovs_fatal(0, "at least one non-option argument is required "
     189                 :            :                   "(use --help for help)");
     190                 :            :     }
     191                 :            : 
     192         [ +  + ]:      10966 :     return target ? target : "ovs-vswitchd";
     193                 :            : }
     194                 :            : 
     195                 :            : static struct jsonrpc *
     196                 :      10966 : connect_to_target(const char *target)
     197                 :            : {
     198                 :            :     struct jsonrpc *client;
     199                 :            :     char *socket_name;
     200                 :            :     int error;
     201                 :            : 
     202                 :            : #ifndef _WIN32
     203         [ +  + ]:      10966 :     if (target[0] != '/') {
     204                 :            :         char *pidfile_name;
     205                 :            :         pid_t pid;
     206                 :            : 
     207                 :      10453 :         pidfile_name = xasprintf("%s/%s.pid", ovs_rundir(), target);
     208                 :      10453 :         pid = read_pidfile(pidfile_name);
     209         [ +  + ]:      10453 :         if (pid < 0) {
     210                 :         10 :             ovs_fatal(-pid, "cannot read pidfile \"%s\"", pidfile_name);
     211                 :            :         }
     212                 :      10443 :         free(pidfile_name);
     213                 :      10443 :         socket_name = xasprintf("%s/%s.%ld.ctl",
     214                 :            :                                 ovs_rundir(), target, (long int) pid);
     215                 :            : #else
     216                 :            :     /* On windows, if the 'target' contains ':', we make an assumption that
     217                 :            :      * it is an absolute path. */
     218                 :            :     if (!strchr(target, ':')) {
     219                 :            :         socket_name = xasprintf("%s/%s.ctl", ovs_rundir(), target);
     220                 :            : #endif
     221                 :            :     } else {
     222                 :        513 :         socket_name = xstrdup(target);
     223                 :            :     }
     224                 :            : 
     225                 :      10956 :     error = unixctl_client_create(socket_name, &client);
     226         [ +  + ]:      10956 :     if (error) {
     227                 :         10 :         ovs_fatal(error, "cannot connect to \"%s\"", socket_name);
     228                 :            :     }
     229                 :      10946 :     free(socket_name);
     230                 :            : 
     231                 :      10946 :     return client;
     232                 :            : }
     233                 :            : 

Generated by: LCOV version 1.12