[BusyBox] [PATCH] macadddr command
systems engineer
ozone at cname.com
Thu Jul 21 19:35:42 UTC 2005
the warewulf project uses busybox for its ramdisk image (which works
great), with which they have a standalone mac-addr binary that simply
emits the mac address of an interface specified on the command line. i'm
working on better integration, so have ported mac-addr to busybox (but
without the hypen :) resulting in the following patch, submitted in hopes
that it will be useful.
two pieces: macaddr.c (goes in networking) and the glue. (new to
subversion; it didn't give me macaddr.c in diff, probably showing where i
didn't know to include an additional step...)
-------------- next part --------------
Index: networking/Makefile.in
===================================================================
--- networking/Makefile.in (revision 10867)
+++ networking/Makefile.in (working copy)
@@ -39,6 +39,7 @@
NETWORKING-$(CONFIG_IPLINK) += iplink.o
NETWORKING-$(CONFIG_IPROUTE) += iproute.o
NETWORKING-$(CONFIG_IPTUNNEL) += iptunnel.o
+NETWORKING-$(CONFIG_MACADDR) += macaddr.o
NETWORKING-$(CONFIG_NAMEIF) += nameif.o
NETWORKING-$(CONFIG_NC) += nc.o
NETWORKING-$(CONFIG_NETSTAT) += netstat.o
Index: networking/Config.in
===================================================================
--- networking/Config.in (revision 10867)
+++ networking/Config.in (working copy)
@@ -407,6 +407,12 @@
help
Equivalent to selecting tunnel support to "ip", above.
+config CONFIG_MACADDR
+ bool "macaddr"
+ default n
+ help
+ Return the mac address of a given interface, for use in scripts.
+
config CONFIG_NAMEIF
bool "nameif"
default n
Index: include/usage.h
===================================================================
--- include/usage.h (revision 10867)
+++ include/usage.h (working copy)
@@ -1648,6 +1648,11 @@
#define lsmod_full_usage \
"List the currently loaded kernel modules."
+#define macaddr_trivial_usage \
+ "[INTERFACE]"
+#define macaddr_full_usage \
+ "Returns the MAC address of a given interface."
+
#ifdef CONFIG_FEATURE_MAKEDEVS_LEAF
#define makedevs_trivial_usage \
"NAME TYPE MAJOR MINOR FIRST LAST [s]"
Index: include/applets.h
===================================================================
--- include/applets.h (revision 10867)
+++ include/applets.h (working copy)
@@ -389,6 +389,9 @@
#ifdef CONFIG_LSMOD
APPLET(lsmod, lsmod_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
+#ifdef CONFIG_MACADDR
+ APPLET(macaddr, macaddr_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
+#endif
#ifdef CONFIG_MAKEDEVS
APPLET(makedevs, makedevs_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
#endif
-------------- next part --------------
/*
* macaddr.c print the mac address of a given interface
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Authors: Lance Davis
*
* Changes:
*
* Greg Kurtzer <greg at caosity.org> added argument support
* ozone <ozone at cname.com> grafted it into busybox, rewrote for(printf()) loop
*/
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include "busybox.h"
int macaddr_main(int argc, char **argv) {
int s;
struct ifreq devea;
if (argc == 2) {
strcpy(devea.ifr_name,argv[1]);
} else {
printf("Usage: %s device\n",argv[0]);
exit(1);
}
s = socket(AF_INET,SOCK_DGRAM,0);
if (s < 0) {
perror("socket");
exit(1);
}
if (ioctl(s,SIOCGIFHWADDR,&devea) < 0) {
perror(&devea.ifr_name[0]);
exit(1);
}
for (s = 0; s < 6; s++) {
printf("%.2X%c",
devea.ifr_ifru.ifru_hwaddr.sa_data[s]&0xff,
s == 5 ? '\n' : ':');
}
return (0);
}
More information about the busybox
mailing list