[gPXE-devel] [gPXE] PATCH: Allow "dhcp" command to iterate over multiple interfaces like autoboot()
Lars Kellogg-Stedman
lars at oddbit.com
Fri Jul 30 21:59:37 EDT 2010
> Please use gPXE coding style, see net/netdevice.c for examples.
> [...]
Stefan,
I've reworked the code a little bit, trying to take your suggestions
into account. I've also removed the "-c" option because it resulted
in simpler code. You can see the diffs here:
http://github.com/larsks/gpxe/compare/master...lars
And the patch follows:
diff --git a/src/hci/commands/dhcp_cmd.c b/src/hci/commands/dhcp_cmd.c
index 96aac8d..593cbcb 100644
--- a/src/hci/commands/dhcp_cmd.c
+++ b/src/hci/commands/dhcp_cmd.c
@@ -31,6 +31,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <gpxe/in.h>
#include <gpxe/command.h>
#include <usr/dhcpmgmt.h>
+#include <usr/ifmgmt.h>
/** @file
*
@@ -45,10 +46,76 @@ FILE_LICENCE ( GPL2_OR_LATER );
*/
static void dhcp_syntax ( char **argv ) {
printf ( "Usage:\n"
- " %s <interface>\n"
+ " %s <interface> [<interface> ...]\n"
+ " %s any\n"
"\n"
"Configure a network interface using DHCP\n",
- argv[0] );
+ argv[0], argv[0] );
+}
+
+/**
+ * Attempt to configure a device with dhcp.
+ *
+ * @netdev Device to configure.
+ */
+static int dhcp_one_device ( struct net_device *netdev ) {
+ int rc;
+
+ /* Perform DHCP */
+ if ( ( rc = dhcp ( netdev ) ) != 0 ) {
+ /* close the device on error to avoid out-of-memory
+ * condition. */
+ netdev_close( netdev );
+
+ printf ( "Could not configure %s: %s\n", netdev->name,
+ strerror ( rc ) );
+ return 1;
+ }
+
+ return 0;
+}
+
+/**
+ * Call dhcp_one_device() for each name in argv.
+ *
+ * @argc Number of devices
+ * @argv List of device names
+ */
+static int dhcp_each_device_name ( int argc, char *argv[] ) {
+ int i;
+ char *netdev_name;
+ struct net_device *netdev;
+
+ for ( i=0; i<argc; i++ ) {
+ netdev_name = argv[i];
+ netdev = find_netdev( netdev_name );
+
+ if ( ! netdev ) {
+ printf ( "No such interface: %s\n", netdev_name );
+ continue;
+ }
+
+ if ( dhcp_one_device ( netdev ) == 0 )
+ return 0;
+ }
+
+ printf( "Could not configure any interface.\n" );
+ return 1;
+}
+
+/**
+ * Call dhcp_one_device() for each device in net_devices.
+ */
+static int dhcp_each_device ( void ) {
+ struct net_device *netdev;
+
+ for_each_netdev ( netdev ) {
+ if ( dhcp_one_device ( netdev ) == 0 )
+ return 0;
+ }
+
+ printf( "Could not configure any interface.\n" );
+ return 1;
}
/**
@@ -63,10 +130,7 @@ static int dhcp_exec ( int argc, char **argv ) {
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
- const char *netdev_txt;
- struct net_device *netdev;
int c;
- int rc;
/* Parse options */
while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
@@ -81,27 +145,15 @@ static int dhcp_exec ( int argc, char **argv ) {
}
/* Need exactly one interface name remaining after the options */
- if ( optind != ( argc - 1 ) ) {
+ if ( ( argc - optind ) < 1 ) {
dhcp_syntax ( argv );
return 1;
}
- netdev_txt = argv[optind];
-
- /* Parse arguments */
- netdev = find_netdev ( netdev_txt );
- if ( ! netdev ) {
- printf ( "No such interface: %s\n", netdev_txt );
- return 1;
- }
- /* Perform DHCP */
- if ( ( rc = dhcp ( netdev ) ) != 0 ) {
- printf ( "Could not configure %s: %s\n", netdev->name,
- strerror ( rc ) );
- return 1;
- }
+ if ( strcmp( argv[optind], "any" ) == 0 )
+ return dhcp_each_device();
- return 0;
+ return dhcp_each_device_name ( argc-optind, argv+optind );
}
/**
More information about the gPXE-devel
mailing list