From 5a740a2a51cfca981dc3d3d7e6b72b776514f295 Mon Sep 17 00:00:00 2001 From: dyoung Date: Wed, 5 Dec 2007 01:10:47 +0000 Subject: [PATCH] Extract common code into subroutine svr4_count_ifnum(). Don't open-code queue(3) macros (x = ifnet.tqh_first; y = x.if_list.tqe_next). Instead, use the macros themselves. Use IFNET_FOREACH() and IFADDR_FOREACH(). --- sys/compat/svr4/svr4_sockio.c | 39 +++++++++++++---------------- sys/compat/svr4/svr4_sockio.h | 6 ++++- sys/compat/svr4_32/svr4_32_sockio.c | 16 +++--------- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/sys/compat/svr4/svr4_sockio.c b/sys/compat/svr4/svr4_sockio.c index b75092317cf2..cdd591d31239 100644 --- a/sys/compat/svr4/svr4_sockio.c +++ b/sys/compat/svr4/svr4_sockio.c @@ -1,4 +1,4 @@ -/* $NetBSD: svr4_sockio.c,v 1.31 2007/12/04 18:40:24 dsl Exp $ */ +/* $NetBSD: svr4_sockio.c,v 1.32 2007/12/05 01:10:47 dyoung Exp $ */ /*- * Copyright (c) 1995 The NetBSD Foundation, Inc. @@ -37,7 +37,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: svr4_sockio.c,v 1.31 2007/12/04 18:40:24 dsl Exp $"); +__KERNEL_RCSID(0, "$NetBSD: svr4_sockio.c,v 1.32 2007/12/05 01:10:47 dyoung Exp $"); #include #include @@ -91,6 +91,18 @@ bsd_to_svr4_flags(bf) return sf; } +int +svr4_count_ifnum(struct ifnet *ifp) +{ + struct ifaddr *ifa; + int ifnum = 0; + + IFADDR_FOREACH(ifa, ifp) + ifnum++; + + return MAX(1, ifnum); +} + int svr4_sock_ioctl(struct file *fp, struct lwp *l, register_t *retval, int fd, u_long cmd, void *data) @@ -105,7 +117,6 @@ svr4_sock_ioctl(struct file *fp, struct lwp *l, register_t *retval, case SVR4_SIOCGLIFNUM: { struct ifnet *ifp; - struct ifaddr *ifa; struct svr4_lifnum lifnum; error = copyin(data, &lifnum, sizeof(lifnum)); @@ -114,14 +125,8 @@ svr4_sock_ioctl(struct file *fp, struct lwp *l, register_t *retval, lifnum.lifn_count = 0; /* XXX: We don't pay attention to family or flags */ - for (ifp = ifnet.tqh_first; - ifp != 0; ifp = ifp->if_list.tqe_next) - if ((ifa = ifp->if_addrlist.tqh_first) == NULL) - lifnum.lifn_count++; - else - for (;ifa != NULL; - ifa = ifa->ifa_list.tqe_next) - lifnum.lifn_count++; + IFNET_FOREACH(ifp) + lifnum.lifn_count += svr4_count_ifnum(ifp); DPRINTF(("SIOCGLIFNUM [family=%d,flags=%d,count=%d]\n", lifnum.lifn_family, lifnum.lifn_flags, @@ -132,7 +137,6 @@ svr4_sock_ioctl(struct file *fp, struct lwp *l, register_t *retval, case SVR4_SIOCGIFNUM: { struct ifnet *ifp; - struct ifaddr *ifa; int ifnum = 0; /* @@ -147,15 +151,8 @@ svr4_sock_ioctl(struct file *fp, struct lwp *l, register_t *retval, * entry per physical interface? */ - for (ifp = ifnet.tqh_first; - ifp != 0; ifp = ifp->if_list.tqe_next) - if ((ifa = ifp->if_addrlist.tqh_first) == NULL) - ifnum++; - else - for (;ifa != NULL; - ifa = ifa->ifa_list.tqe_next) - ifnum++; - + IFNET_FOREACH(ifp) + ifnum += svr4_count_ifnum(ifp); DPRINTF(("SIOCGIFNUM %d\n", ifnum)); return copyout(&ifnum, data, sizeof(ifnum)); diff --git a/sys/compat/svr4/svr4_sockio.h b/sys/compat/svr4/svr4_sockio.h index 74de1c67f0f7..c982e4706bb1 100644 --- a/sys/compat/svr4/svr4_sockio.h +++ b/sys/compat/svr4/svr4_sockio.h @@ -1,4 +1,4 @@ -/* $NetBSD: svr4_sockio.h,v 1.7 2007/03/04 06:01:33 christos Exp $ */ +/* $NetBSD: svr4_sockio.h,v 1.8 2007/12/05 01:10:47 dyoung Exp $ */ /*- * Copyright (c) 1995 The NetBSD Foundation, Inc. @@ -195,4 +195,8 @@ struct svr4_ifconf { #define SVR4_SIOCGLIFFLAGS SVR4_IOWR('i', 117, struct svr4_lifreq) #define SVR4_SIOCGLIFNUM SVR4_IOWR('i', 130, struct svr4_lifnum) +#ifdef _KERNEL +int svr4_count_ifnum(struct ifnet *); +#endif /* _KERNEL */ + #endif /* !_SVR4_SOCKIO_H_ */ diff --git a/sys/compat/svr4_32/svr4_32_sockio.c b/sys/compat/svr4_32/svr4_32_sockio.c index c78f7ec9c3cb..d88c72f89034 100644 --- a/sys/compat/svr4_32/svr4_32_sockio.c +++ b/sys/compat/svr4_32/svr4_32_sockio.c @@ -1,4 +1,4 @@ -/* $NetBSD: svr4_32_sockio.c,v 1.17 2007/12/04 18:40:26 dsl Exp $ */ +/* $NetBSD: svr4_32_sockio.c,v 1.18 2007/12/05 01:10:47 dyoung Exp $ */ /*- * Copyright (c) 1995 The NetBSD Foundation, Inc. @@ -37,7 +37,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: svr4_32_sockio.c,v 1.17 2007/12/04 18:40:26 dsl Exp $"); +__KERNEL_RCSID(0, "$NetBSD: svr4_32_sockio.c,v 1.18 2007/12/05 01:10:47 dyoung Exp $"); #include #include @@ -110,7 +110,6 @@ svr4_32_sock_ioctl(fp, l, retval, fd, cmd, data) case SVR4_SIOCGIFNUM: { struct ifnet *ifp; - struct ifaddr *ifa; int ifnum = 0; /* @@ -125,15 +124,8 @@ svr4_32_sock_ioctl(fp, l, retval, fd, cmd, data) * entry per physical interface? */ - for (ifp = ifnet.tqh_first; - ifp != 0; ifp = ifp->if_list.tqe_next) - if ((ifa = ifp->if_addrlist.tqh_first) == NULL) - ifnum++; - else - for (;ifa != NULL; - ifa = ifa->ifa_list.tqe_next) - ifnum++; - + IFNET_FOREACH(ifp) + ifnum += svr4_count_ifnum(ifp) DPRINTF(("SIOCGIFNUM %d\n", ifnum)); return copyout(&ifnum, data, sizeof(ifnum));