1From 04eec97b390621f2b3794b0d774b77429eb88cfd Mon Sep 17 00:00:00 2001 2From: Andrea Adami <andrea.adami@gmail.com> 3Date: Wed, 2 May 2018 23:14:19 +0200 4Subject: [PATCH] add if_nameindex from musl 5 6Taken from musl, minimal changes. 7klibc lacks struct and func 8 9Fix 10 11 ifdown.o: In function `ifdown': 12 ifdown.c (.text+0x30): undefined reference to `if_nameindex' 13 14While there add klibc-specific guard and include sys/types.h 15to fix : 16 17 /kexec/if_nameindex.c:2: 18 /usr/lib/klibc/include/linux/types.h:22:0: 19 warning: "__bitwise" redefined 20 #define __bitwise __bitwise__ 21 22Signed-off-by: Andrea Adami <andrea.adami@gmail.com> 23 24--- 25 kexec/Makefile | 2 +- 26 kexec/if_nameindex.c | 64 ++++++++++++++++++++++++++++++++++++++++++++ 27 kexec/if_nameindex.h | 15 +++++++++++ 28 kexec/ifdown.c | 3 +++ 29 4 files changed, 83 insertions(+), 1 deletion(-) 30 create mode 100644 kexec/if_nameindex.c 31 create mode 100644 kexec/if_nameindex.h 32 33diff --git a/kexec/Makefile b/kexec/Makefile 34index 4db84d8..fb7520b 100644 35--- a/kexec/Makefile 36+++ b/kexec/Makefile 37@@ -11,7 +11,7 @@ KEXEC_SRCS = $(KEXEC_SRCS_base) 38 KEXEC_GENERATED_SRCS = 39 40 KEXEC_SRCS_base += kexec/kexec.c 41-KEXEC_SRCS_base += kexec/ifdown.c 42+KEXEC_SRCS_base += kexec/if_nameindex kexec/ifdown.c 43 KEXEC_SRCS_base += kexec/kexec-elf.c 44 KEXEC_SRCS_base += kexec/kexec-elf-exec.c 45 KEXEC_SRCS_base += kexec/kexec-elf-core.c 46diff --git a/kexec/if_nameindex.c b/kexec/if_nameindex.c 47new file mode 100644 48index 0000000..e586e41 49--- /dev/null 50+++ b/kexec/if_nameindex.c 51@@ -0,0 +1,64 @@ 52+#define _GNU_SOURCE 53+#ifdef __KLIBC__ 54+#include <sys/types.h> 55+#endif 56+#include <netinet/in.h> 57+#include <net/if.h> 58+#include <stdlib.h> 59+#include <sys/socket.h> 60+#include <sys/ioctl.h> 61+#include <errno.h> 62+#include <sys/syscall.h> 63+#include <stdio.h> 64+#ifdef __KLIBC__ 65+#include "if_nameindex.h" 66+#endif 67+ 68+static void *do_nameindex(int s, size_t n) 69+{ 70+ size_t i, len, k; 71+ struct ifconf conf; 72+ struct if_nameindex *idx; 73+ 74+ idx = malloc(n * (sizeof(struct if_nameindex)+sizeof(struct ifreq))); 75+ if (!idx) return 0; 76+ 77+ conf.ifc_buf = (void *)&idx[n]; 78+ conf.ifc_len = len = n * sizeof(struct ifreq); 79+ if (ioctl(s, SIOCGIFCONF, &conf) < 0) { 80+ free(idx); 81+ return 0; 82+ } 83+ if (conf.ifc_len == len) { 84+ free(idx); 85+ return (void *)-1; 86+ } 87+ 88+ n = conf.ifc_len / sizeof(struct ifreq); 89+ for (i=k=0; i<n; i++) { 90+ if (ioctl(s, SIOCGIFINDEX, &conf.ifc_req[i]) < 0) { 91+ k++; 92+ continue; 93+ } 94+ idx[i-k].if_index = conf.ifc_req[i].ifr_ifindex; 95+ idx[i-k].if_name = conf.ifc_req[i].ifr_name; 96+ } 97+ idx[i-k].if_name = 0; 98+ idx[i-k].if_index = 0; 99+ 100+ return idx; 101+} 102+ 103+struct if_nameindex *if_nameindex() 104+{ 105+ size_t n; 106+ void *p = 0; 107+ int s = socket(AF_UNIX, SOCK_DGRAM, 0); 108+ if (s>=0) { 109+ for (n=0; (p=do_nameindex(s, n)) == (void *)-1; n++); 110+/* __syscall(SYS_close, s); */ 111+ close(s); 112+ } 113+ errno = ENOBUFS; 114+ return p; 115+} 116diff --git a/kexec/if_nameindex.h b/kexec/if_nameindex.h 117new file mode 100644 118index 0000000..cf1c061 119--- /dev/null 120+++ b/kexec/if_nameindex.h 121@@ -0,0 +1,15 @@ 122+#ifndef _NET_IF__NAMEINDEX_H 123+#define _NET_IF_NAMEINDEX_H 124+ 125+struct if_nameindex 126+{ 127+ unsigned int if_index; 128+ char *if_name; 129+}; 130+ 131+unsigned int if_nametoindex (const char *); 132+char *if_indextoname (unsigned int, char *); 133+struct if_nameindex *if_nameindex (void); 134+void if_freenameindex (struct if_nameindex *); 135+ 136+#endif 137diff --git a/kexec/ifdown.c b/kexec/ifdown.c 138index 82c6141..cc3ca9f 100644 139--- a/kexec/ifdown.c 140+++ b/kexec/ifdown.c 141@@ -18,6 +18,9 @@ char *v_ifdown = "@(#)ifdown.c 1.11 02-Jun-1998 miquels@cistron.nl"; 142 143 #include <netinet/in.h> 144 #include <net/if.h> 145+#ifdef __KLIBC__ 146+#include "if_nameindex.h" 147+#endif 148 149 /* 150 * First, we find all shaper devices and down them. Then we 151