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