From owner-svn-src-all@FreeBSD.ORG Wed Jul 1 13:07:02 2009 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D2AE9106564A; Wed, 1 Jul 2009 13:07:02 +0000 (UTC) (envelope-from stas@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id C08608FC27; Wed, 1 Jul 2009 13:07:02 +0000 (UTC) (envelope-from stas@FreeBSD.org) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id n61D72ow013395; Wed, 1 Jul 2009 13:07:02 GMT (envelope-from stas@svn.freebsd.org) Received: (from stas@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n61D72ma013392; Wed, 1 Jul 2009 13:07:02 GMT (envelope-from stas@svn.freebsd.org) Message-Id: <200907011307.n61D72ma013392@svn.freebsd.org> From: Stanislav Sedov Date: Wed, 1 Jul 2009 13:07:02 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r195237 - head/usr.sbin/wake X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Jul 2009 13:07:03 -0000 Author: stas Date: Wed Jul 1 13:07:02 2009 New Revision: 195237 URL: http://svn.freebsd.org/changeset/base/195237 Log: - Fix the bug in write(2) called with incorrect parameters resulting in writes always started from the start of the packet. - Fix usage string (multiple addresses can be specified). - Make the source more style(9) compliant. - Improve error reporting (do not silently fail if something goes wrong). - Make functions static. - Use warns level 6. Approved by: re (kib) Discussed with: Marc Balmer , brian, mbr Modified: head/usr.sbin/wake/Makefile head/usr.sbin/wake/wake.c Modified: head/usr.sbin/wake/Makefile ============================================================================== --- head/usr.sbin/wake/Makefile Wed Jul 1 12:44:23 2009 (r195236) +++ head/usr.sbin/wake/Makefile Wed Jul 1 13:07:02 2009 (r195237) @@ -2,5 +2,6 @@ PROG= wake MAN= wake.8 +WARNS?= 6 .include Modified: head/usr.sbin/wake/wake.c ============================================================================== --- head/usr.sbin/wake/wake.c Wed Jul 1 12:44:23 2009 (r195236) +++ head/usr.sbin/wake/wake.c Wed Jul 1 13:07:02 2009 (r195237) @@ -27,18 +27,6 @@ #include __FBSDID("$FreeBSD$"); -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - #include #include #include @@ -50,70 +38,89 @@ __FBSDID("$FreeBSD$"); #include #include -#define _PATH_BPF "/dev/bpf" +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define _PATH_BPF "/dev/bpf" #ifndef SYNC_LEN -#define SYNC_LEN 6 +#define SYNC_LEN 6 #endif #ifndef DESTADDR_COUNT -#define DESTADDR_COUNT 16 +#define DESTADDR_COUNT 16 #endif -void usage(void); +static void usage(void); +static int wake(const char *iface, const char *host); +static int bind_if_to_bpf(char const *ifname, int bpf); +static int get_ether(char const *text, struct ether_addr *addr); +static int send_wakeup(int bpf, struct ether_addr const *addr); -int wake(const char *iface, const char *host); -int bind_if_to_bpf(char const *ifname, int bpf); -int get_ether(char const *text, struct ether_addr *addr); -int send_wakeup(int bpf, struct ether_addr const *addr); - -void +static void usage(void) { - (void)fprintf(stderr, "usage: wake interface lladdr\n"); + + (void)fprintf(stderr, "usage: wake interface lladdr...\n"); exit(0); } -int +static int wake(const char *iface, const char *host) { - int res, bpf; struct ether_addr macaddr; + int bpf, res; bpf = open(_PATH_BPF, O_RDWR); if (bpf == -1) { - printf("no bpf\n"); - return -1; + warn("no bpf"); + return (-1); } if (bind_if_to_bpf(iface, bpf) == -1 || get_ether(host, &macaddr) == -1) { (void)close(bpf); - return -1; + return (-1); } res = send_wakeup(bpf, &macaddr); (void)close(bpf); - return res; + return (res); } -int +static int bind_if_to_bpf(char const *ifname, int bpf) { struct ifreq ifr; u_int dlt; if (strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)) >= - sizeof(ifr.ifr_name)) - return -1; - if (ioctl(bpf, BIOCSETIF, &ifr) == -1) - return -1; - if (ioctl(bpf, BIOCGDLT, &dlt) == -1) - return -1; - if (dlt != DLT_EN10MB) - return -1; - return 0; + sizeof(ifr.ifr_name)) { + warnx("interface name too long: %s", ifname); + return (-1); + } + if (ioctl(bpf, BIOCSETIF, &ifr) == -1) { + warn("ioctl(%s)", "BIOCSETIF"); + return (-1); + } + if (ioctl(bpf, BIOCGDLT, &dlt) == -1) { + warn("ioctl(%s)", "BIOCGDLT"); + return (-1); + } + if (dlt != DLT_EN10MB) { + warnx("incompatible media"); + return (-1); + } + return (0); } -int +static int get_ether(char const *text, struct ether_addr *addr) { struct ether_addr *paddr; @@ -121,24 +128,26 @@ get_ether(char const *text, struct ether paddr = ether_aton(text); if (paddr != NULL) { *addr = *paddr; - return 0; + return (0); } - if (ether_hostton(text, addr)) - return -1; - return 0; + if (ether_hostton(text, addr)) { + warnx("no match for host %s found", text); + return (-1); + } + return (0); } -int +static int send_wakeup(int bpf, struct ether_addr const *addr) { struct { struct ether_header hdr; u_char data[SYNC_LEN + ETHER_ADDR_LEN * DESTADDR_COUNT]; - } pkt; - u_char *p; - int i; + } __packed pkt; ssize_t bw; ssize_t len; + int i; + u_char *p; (void)memset(pkt.hdr.ether_dhost, 0xff, sizeof(pkt.hdr.ether_dhost)); pkt.hdr.ether_type = htons(0); @@ -150,12 +159,14 @@ send_wakeup(int bpf, struct ether_addr c len = sizeof(pkt); bw = 0; while (len) { - if ((bw = write(bpf, &pkt, sizeof(pkt))) == -1) - return -1; + if ((bw = write(bpf, p, len)) == -1) { + warn("write()"); + return (-1); + } len -= bw; p += bw; } - return 0; + return (0); } int @@ -170,5 +181,5 @@ main(int argc, char *argv[]) if (wake(argv[1], argv[n])) warnx("error sending Wake on LAN frame over %s to %s", argv[1], argv[n]); - return 0; + return (0); }