ifmgmt_cmd.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stdio.h>
00022 #include <getopt.h>
00023 #include <gpxe/netdevice.h>
00024 #include <gpxe/command.h>
00025 #include <usr/ifmgmt.h>
00026 #include <hci/ifmgmt_cmd.h>
00027 
00028 /** @file
00029  *
00030  * Network interface management commands
00031  *
00032  */
00033 
00034 /** Options shared by all if<xxx> commands */
00035 static struct option ifcommon_longopts[] = {
00036         { "help", 0, NULL, 'h' },
00037         { NULL, 0, NULL, 0 },
00038 };
00039 
00040 /**
00041  * Print syntax of if<xxx> command
00042  *
00043  * @v argv              Command arguments
00044  * @v verb              Verb describing the action of the command
00045  */
00046 static void ifcommon_syntax ( char **argv, const char *verb ) {
00047         printf ( "Usage:\n"
00048                  "  %s [<interface>] [<interface>...]\n"
00049                  "\n"
00050                  "%s the specified network interfaces\n",
00051                  argv[0], verb );
00052 }
00053 
00054 /**
00055  * Execute if<xxx> command over all network devices
00056  *
00057  * @v payload           Command to execute
00058  * @ret rc              Exit code
00059  */
00060 static int ifcommon_do_all ( int ( * payload ) ( struct net_device * ) ) {
00061         struct net_device *netdev;
00062         int rc = 0;
00063 
00064         /* Execute payload for each network device */
00065         for_each_netdev ( netdev ) {
00066                 if ( payload ( netdev ) != 0 )
00067                         rc = 1;
00068         }
00069         return rc;
00070 }
00071 
00072 /**
00073  * Execute if<xxx> command over list of network devices
00074  *
00075  * @v payload           Command to execute
00076  * @ret rc              Exit code
00077  */
00078 static int ifcommon_do_list ( int ( * payload ) ( struct net_device * ),
00079                               char **list, unsigned int count ) {
00080         const char *netdev_name;
00081         struct net_device *netdev;
00082         int rc = 0;
00083 
00084         while ( count-- ) {
00085                 netdev_name = *(list++);
00086                 netdev = find_netdev ( netdev_name );
00087                 if ( ! netdev ) {
00088                         printf ( "%s: no such interface\n", netdev_name );
00089                         rc = 1;
00090                         continue;
00091                 }
00092                 if ( payload ( netdev ) != 0 )
00093                         rc = 1;
00094         }
00095         return rc;
00096 }
00097 
00098 /**
00099  * Execute if<xxx> command
00100  *
00101  * @v argc              Argument count
00102  * @v argv              Argument list
00103  * @v payload           Command to execute
00104  * @v verb              Verb describing the action of the command
00105  * @ret rc              Exit code
00106  */
00107 int ifcommon_exec ( int argc, char **argv,
00108                     int ( * payload ) ( struct net_device * ),
00109                     const char *verb ) {
00110         int c;
00111 
00112         /* Parse options */
00113         while ( ( c = getopt_long ( argc, argv, "h", ifcommon_longopts,
00114                                     NULL ) ) >= 0 ) {
00115                 switch ( c ) {
00116                 case 'h':
00117                         /* Display help text */
00118                 default:
00119                         /* Unrecognised/invalid option */
00120                         ifcommon_syntax ( argv, verb );
00121                         return 1;
00122                 }
00123         }
00124 
00125         if ( optind == argc ) {
00126                 return ifcommon_do_all ( payload );
00127         } else {
00128                 return ifcommon_do_list ( payload, &argv[optind],
00129                                           ( argc - optind ) );
00130         }
00131 }
00132 
00133 /* "ifopen" command */
00134 
00135 static int ifopen_payload ( struct net_device *netdev ) {
00136         return ifopen ( netdev );
00137 }
00138 
00139 static int ifopen_exec ( int argc, char **argv ) {
00140         return ifcommon_exec ( argc, argv, ifopen_payload, "Open" );
00141 }
00142 
00143 /* "ifclose" command */
00144 
00145 static int ifclose_payload ( struct net_device *netdev ) {
00146         ifclose ( netdev );
00147         return 0;
00148 }
00149 
00150 static int ifclose_exec ( int argc, char **argv ) {
00151         return ifcommon_exec ( argc, argv, ifclose_payload, "Close" );
00152 }
00153 
00154 /* "ifstat" command */
00155 
00156 static int ifstat_payload ( struct net_device *netdev ) {
00157         ifstat ( netdev );
00158         return 0;
00159 }
00160 
00161 static int ifstat_exec ( int argc, char **argv ) {
00162         return ifcommon_exec ( argc, argv,
00163                                ifstat_payload, "Display status of" );
00164 }
00165 
00166 /** Interface management commands */
00167 struct command ifmgmt_commands[] __command = {
00168         {
00169                 .name = "ifopen",
00170                 .exec = ifopen_exec,
00171         },
00172         {
00173                 .name = "ifclose",
00174                 .exec = ifclose_exec,
00175         },
00176         {
00177                 .name = "ifstat",
00178                 .exec = ifstat_exec,
00179         },
00180 };

Generated on Tue Apr 6 20:01:06 2010 for gPXE by  doxygen 1.5.7.1