From 04eec97b390621f2b3794b0d774b77429eb88cfd Mon Sep 17 00:00:00 2001 From: Andrea Adami Date: Wed, 2 May 2018 23:14:19 +0200 Subject: [PATCH] add if_nameindex from musl Taken from musl, minimal changes. klibc lacks struct and func Fix ifdown.o: In function `ifdown': ifdown.c (.text+0x30): undefined reference to `if_nameindex' While there add klibc-specific guard and include sys/types.h to fix : /kexec/if_nameindex.c:2: /usr/lib/klibc/include/linux/types.h:22:0: warning: "__bitwise" redefined #define __bitwise __bitwise__ Signed-off-by: Andrea Adami --- Upstream-Status: Pending kexec/Makefile | 2 +- kexec/if_nameindex.c | 64 ++++++++++++++++++++++++++++++++++++++++++++ kexec/if_nameindex.h | 15 +++++++++++ kexec/ifdown.c | 3 +++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 kexec/if_nameindex.c create mode 100644 kexec/if_nameindex.h diff --git a/kexec/Makefile b/kexec/Makefile index 4db84d8..fb7520b 100644 --- a/kexec/Makefile +++ b/kexec/Makefile @@ -11,7 +11,7 @@ KEXEC_SRCS = $(KEXEC_SRCS_base) KEXEC_GENERATED_SRCS = KEXEC_SRCS_base += kexec/kexec.c -KEXEC_SRCS_base += kexec/ifdown.c +KEXEC_SRCS_base += kexec/if_nameindex kexec/ifdown.c KEXEC_SRCS_base += kexec/kexec-elf.c KEXEC_SRCS_base += kexec/kexec-elf-exec.c KEXEC_SRCS_base += kexec/kexec-elf-core.c diff --git a/kexec/if_nameindex.c b/kexec/if_nameindex.c new file mode 100644 index 0000000..e586e41 --- /dev/null +++ b/kexec/if_nameindex.c @@ -0,0 +1,64 @@ +#define _GNU_SOURCE +#ifdef __KLIBC__ +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __KLIBC__ +#include "if_nameindex.h" +#endif + +static void *do_nameindex(int s, size_t n) +{ + size_t i, len, k; + struct ifconf conf; + struct if_nameindex *idx; + + idx = malloc(n * (sizeof(struct if_nameindex)+sizeof(struct ifreq))); + if (!idx) return 0; + + conf.ifc_buf = (void *)&idx[n]; + conf.ifc_len = len = n * sizeof(struct ifreq); + if (ioctl(s, SIOCGIFCONF, &conf) < 0) { + free(idx); + return 0; + } + if (conf.ifc_len == len) { + free(idx); + return (void *)-1; + } + + n = conf.ifc_len / sizeof(struct ifreq); + for (i=k=0; i=0) { + for (n=0; (p=do_nameindex(s, n)) == (void *)-1; n++); +/* __syscall(SYS_close, s); */ + close(s); + } + errno = ENOBUFS; + return p; +} diff --git a/kexec/if_nameindex.h b/kexec/if_nameindex.h new file mode 100644 index 0000000..cf1c061 --- /dev/null +++ b/kexec/if_nameindex.h @@ -0,0 +1,15 @@ +#ifndef _NET_IF__NAMEINDEX_H +#define _NET_IF_NAMEINDEX_H + +struct if_nameindex +{ + unsigned int if_index; + char *if_name; +}; + +unsigned int if_nametoindex (const char *); +char *if_indextoname (unsigned int, char *); +struct if_nameindex *if_nameindex (void); +void if_freenameindex (struct if_nameindex *); + +#endif diff --git a/kexec/ifdown.c b/kexec/ifdown.c index 82c6141..cc3ca9f 100644 --- a/kexec/ifdown.c +++ b/kexec/ifdown.c @@ -18,6 +18,9 @@ char *v_ifdown = "@(#)ifdown.c 1.11 02-Jun-1998 miquels@cistron.nl"; #include #include +#ifdef __KLIBC__ +#include "if_nameindex.h" +#endif /* * First, we find all shaper devices and down them. Then we