Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | From a3cbee31daae2466bc8dcac36b33a01352693346 Mon Sep 17 00:00:00 2001 |
| 2 | From: Khem Raj <raj.khem@gmail.com> |
| 3 | Date: Thu, 7 Jan 2016 19:40:08 +0000 |
| 4 | Subject: [PATCH 01/26] Check if __GLIBC_PREREQ is defined before using it |
| 5 | |
| 6 | __GLIBC_PREREQ is specific to glibc so it should be checked if it is |
| 7 | defined or not. |
| 8 | |
| 9 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 10 | |
| 11 | --- |
| 12 | testcases/kernel/syscalls/accept4/accept4_01.c | 9 ++++- |
| 13 | testcases/kernel/syscalls/getcpu/getcpu01.c | 40 +++++++++++++++++++++- |
| 14 | .../sched_getaffinity/sched_getaffinity01.c | 26 ++++++++++++++ |
| 15 | 3 files changed, 73 insertions(+), 2 deletions(-) |
| 16 | |
| 17 | diff --git a/testcases/kernel/syscalls/accept4/accept4_01.c b/testcases/kernel/syscalls/accept4/accept4_01.c |
| 18 | index 6072bfa..2b090cb 100644 |
| 19 | --- a/testcases/kernel/syscalls/accept4/accept4_01.c |
| 20 | +++ b/testcases/kernel/syscalls/accept4/accept4_01.c |
| 21 | @@ -64,6 +64,7 @@ static void cleanup(void) |
| 22 | tst_rmdir(); |
| 23 | } |
| 24 | |
| 25 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 26 | #if !(__GLIBC_PREREQ(2, 10)) |
| 27 | static int |
| 28 | accept4_01(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags) |
| 29 | @@ -82,7 +83,6 @@ accept4_01(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags) |
| 30 | } |
| 31 | tst_resm(TINFO, "\n"); |
| 32 | #endif |
| 33 | - |
| 34 | #if USE_SOCKETCALL |
| 35 | long args[6]; |
| 36 | |
| 37 | @@ -97,6 +97,7 @@ accept4_01(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags) |
| 38 | #endif |
| 39 | } |
| 40 | #endif |
| 41 | +#endif |
| 42 | |
| 43 | static void |
| 44 | do_test(int lfd, struct sockaddr_in *conn_addr, |
| 45 | @@ -119,9 +120,15 @@ do_test(int lfd, struct sockaddr_in *conn_addr, |
| 46 | die("Connect Error"); |
| 47 | |
| 48 | addrlen = sizeof(struct sockaddr_in); |
| 49 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 50 | #if !(__GLIBC_PREREQ(2, 10)) |
| 51 | acceptfd = accept4_01(lfd, (struct sockaddr *)&claddr, &addrlen, |
| 52 | closeonexec_flag | nonblock_flag); |
| 53 | + |
| 54 | +#else |
| 55 | + acceptfd = accept4(lfd, (struct sockaddr *)&claddr, &addrlen, |
| 56 | + closeonexec_flag | nonblock_flag); |
| 57 | +#endif |
| 58 | #else |
| 59 | acceptfd = accept4(lfd, (struct sockaddr *)&claddr, &addrlen, |
| 60 | closeonexec_flag | nonblock_flag); |
| 61 | diff --git a/testcases/kernel/syscalls/getcpu/getcpu01.c b/testcases/kernel/syscalls/getcpu/getcpu01.c |
| 62 | index c927512..921b107 100644 |
| 63 | --- a/testcases/kernel/syscalls/getcpu/getcpu01.c |
| 64 | +++ b/testcases/kernel/syscalls/getcpu/getcpu01.c |
| 65 | @@ -62,6 +62,7 @@ |
| 66 | #include <dirent.h> |
| 67 | |
| 68 | #if defined(__i386__) || defined(__x86_64__) |
| 69 | +#if defined(__GLIBC__) |
| 70 | #if __GLIBC_PREREQ(2,6) |
| 71 | #if defined(__x86_64__) |
| 72 | #include <utmpx.h> |
| 73 | @@ -75,10 +76,17 @@ int sys_support = 0; |
| 74 | #else |
| 75 | int sys_support = 0; |
| 76 | #endif |
| 77 | +#else |
| 78 | +int sys_support = 0; |
| 79 | +#endif |
| 80 | |
| 81 | +#if defined(__GLIBC__) |
| 82 | #if !(__GLIBC_PREREQ(2, 7)) |
| 83 | #define CPU_FREE(ptr) free(ptr) |
| 84 | #endif |
| 85 | +#else |
| 86 | +#define CPU_FREE(ptr) free(ptr) |
| 87 | +#endif |
| 88 | |
| 89 | void cleanup(void); |
| 90 | void setup(void); |
| 91 | @@ -164,9 +172,14 @@ static inline int getcpu(unsigned *cpu_id, unsigned *node_id, |
| 92 | { |
| 93 | #if defined(__i386__) |
| 94 | return syscall(318, cpu_id, node_id, cache_struct); |
| 95 | -#elif __GLIBC_PREREQ(2,6) |
| 96 | +#if defined(__GLIBC__) |
| 97 | +#if __GLIBC_PREREQ(2,6) |
| 98 | + *cpu_id = sched_getcpu(); |
| 99 | +#endif |
| 100 | +#else |
| 101 | *cpu_id = sched_getcpu(); |
| 102 | #endif |
| 103 | +#endif |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | @@ -191,15 +204,20 @@ unsigned int set_cpu_affinity(void) |
| 108 | cpu_set_t *set; |
| 109 | size_t size; |
| 110 | int nrcpus = 1024; |
| 111 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 112 | #if __GLIBC_PREREQ(2, 7) |
| 113 | realloc: |
| 114 | set = CPU_ALLOC(nrcpus); |
| 115 | #else |
| 116 | set = malloc(sizeof(cpu_set_t)); |
| 117 | #endif |
| 118 | +#else |
| 119 | + set = malloc(sizeof(cpu_set_t)); |
| 120 | +#endif |
| 121 | if (set == NULL) { |
| 122 | tst_brkm(TFAIL, NULL, "CPU_ALLOC:errno:%d", errno); |
| 123 | } |
| 124 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 125 | #if __GLIBC_PREREQ(2, 7) |
| 126 | size = CPU_ALLOC_SIZE(nrcpus); |
| 127 | CPU_ZERO_S(size, set); |
| 128 | @@ -207,8 +225,13 @@ realloc: |
| 129 | size = sizeof(cpu_set_t); |
| 130 | CPU_ZERO(set); |
| 131 | #endif |
| 132 | +#else |
| 133 | + size = sizeof(cpu_set_t); |
| 134 | + CPU_ZERO(set); |
| 135 | +#endif |
| 136 | if (sched_getaffinity(0, size, set) < 0) { |
| 137 | CPU_FREE(set); |
| 138 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 139 | #if __GLIBC_PREREQ(2, 7) |
| 140 | if (errno == EINVAL && nrcpus < (1024 << 8)) { |
| 141 | nrcpus = nrcpus << 2; |
| 142 | @@ -220,10 +243,17 @@ realloc: |
| 143 | "NR_CPUS of the kernel is more than 1024, so we'd better use a newer glibc(>= 2.7)"); |
| 144 | else |
| 145 | #endif |
| 146 | +#else |
| 147 | + if (errno == EINVAL) |
| 148 | + tst_resm(TFAIL, |
| 149 | + "NR_CPUS of the kernel is more than 1024, so we'd better use a newer glibc(>= 2.7)"); |
| 150 | + else |
| 151 | +#endif |
| 152 | tst_resm(TFAIL, "sched_getaffinity:errno:%d", errno); |
| 153 | tst_exit(); |
| 154 | } |
| 155 | cpu_max = max_cpuid(size, set); |
| 156 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 157 | #if __GLIBC_PREREQ(2, 7) |
| 158 | CPU_ZERO_S(size, set); |
| 159 | CPU_SET_S(cpu_max, size, set); |
| 160 | @@ -231,6 +261,10 @@ realloc: |
| 161 | CPU_ZERO(set); |
| 162 | CPU_SET(cpu_max, set); |
| 163 | #endif |
| 164 | +#else |
| 165 | + CPU_ZERO(set); |
| 166 | + CPU_SET(cpu_max, set); |
| 167 | +#endif |
| 168 | if (sched_setaffinity(0, size, set) < 0) { |
| 169 | CPU_FREE(set); |
| 170 | tst_brkm(TFAIL, NULL, "sched_setaffinity:errno:%d", errno); |
| 171 | @@ -247,11 +281,15 @@ unsigned int max_cpuid(size_t size, cpu_set_t * set) |
| 172 | { |
| 173 | unsigned int index, max = 0; |
| 174 | for (index = 0; index < size * BITS_PER_BYTE; index++) |
| 175 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 176 | #if __GLIBC_PREREQ(2, 7) |
| 177 | if (CPU_ISSET_S(index, size, set)) |
| 178 | #else |
| 179 | if (CPU_ISSET(index, set)) |
| 180 | #endif |
| 181 | +#else |
| 182 | + if (CPU_ISSET(index, set)) |
| 183 | +#endif |
| 184 | max = index; |
| 185 | return max; |
| 186 | } |
| 187 | diff --git a/testcases/kernel/syscalls/sched_getaffinity/sched_getaffinity01.c b/testcases/kernel/syscalls/sched_getaffinity/sched_getaffinity01.c |
| 188 | index 9d6a81a..4ed13b2 100644 |
| 189 | --- a/testcases/kernel/syscalls/sched_getaffinity/sched_getaffinity01.c |
| 190 | +++ b/testcases/kernel/syscalls/sched_getaffinity/sched_getaffinity01.c |
| 191 | @@ -66,9 +66,11 @@ do { \ |
| 192 | tst_resm((TEST_RETURN == -1 ? TPASS : TFAIL) | TTERRNO, #t); \ |
| 193 | } while (0) |
| 194 | |
| 195 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 196 | #if !(__GLIBC_PREREQ(2, 7)) |
| 197 | #define CPU_FREE(ptr) free(ptr) |
| 198 | #endif |
| 199 | +#endif |
| 200 | |
| 201 | int main(int ac, char **av) |
| 202 | { |
| 203 | @@ -95,14 +97,19 @@ static void do_test(void) |
| 204 | pid_t unused_pid; |
| 205 | unsigned len; |
| 206 | |
| 207 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 208 | #if __GLIBC_PREREQ(2, 7) |
| 209 | realloc: |
| 210 | mask = CPU_ALLOC(nrcpus); |
| 211 | #else |
| 212 | mask = malloc(sizeof(cpu_set_t)); |
| 213 | #endif |
| 214 | +#else |
| 215 | + mask = malloc(sizeof(cpu_set_t)); |
| 216 | +#endif |
| 217 | if (mask == NULL) |
| 218 | tst_brkm(TFAIL | TTERRNO, cleanup, "fail to get enough memory"); |
| 219 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 220 | #if __GLIBC_PREREQ(2, 7) |
| 221 | len = CPU_ALLOC_SIZE(nrcpus); |
| 222 | CPU_ZERO_S(len, mask); |
| 223 | @@ -110,10 +117,15 @@ realloc: |
| 224 | len = sizeof(cpu_set_t); |
| 225 | CPU_ZERO(mask); |
| 226 | #endif |
| 227 | +#else |
| 228 | + len = sizeof(cpu_set_t); |
| 229 | + CPU_ZERO(mask); |
| 230 | +#endif |
| 231 | /* positive test */ |
| 232 | TEST(sched_getaffinity(0, len, mask)); |
| 233 | if (TEST_RETURN == -1) { |
| 234 | CPU_FREE(mask); |
| 235 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 236 | #if __GLIBC_PREREQ(2, 7) |
| 237 | if (errno == EINVAL && nrcpus < (1024 << 8)) { |
| 238 | nrcpus = nrcpus << 2; |
| 239 | @@ -125,17 +137,27 @@ realloc: |
| 240 | "newer glibc(>= 2.7)"); |
| 241 | else |
| 242 | #endif |
| 243 | +#else |
| 244 | + if (errno == EINVAL) |
| 245 | + tst_resm(TFAIL, "NR_CPUS > 1024, we'd better use a " |
| 246 | + "newer glibc(>= 2.7)"); |
| 247 | + else |
| 248 | +#endif |
| 249 | tst_resm(TFAIL | TTERRNO, "fail to get cpu affinity"); |
| 250 | cleanup(); |
| 251 | } else { |
| 252 | tst_resm(TINFO, "cpusetsize is %d", len); |
| 253 | tst_resm(TINFO, "mask.__bits[0] = %lu ", mask->__bits[0]); |
| 254 | for (i = 0; i < num; i++) { |
| 255 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 256 | #if __GLIBC_PREREQ(2, 7) |
| 257 | TEST(CPU_ISSET_S(i, len, mask)); |
| 258 | #else |
| 259 | TEST(CPU_ISSET(i, mask)); |
| 260 | #endif |
| 261 | +#else |
| 262 | + TEST(CPU_ISSET(i, mask)); |
| 263 | +#endif |
| 264 | if (TEST_RETURN != -1) |
| 265 | tst_resm(TPASS, "sched_getaffinity() succeed, " |
| 266 | "this process %d is running " |
| 267 | @@ -143,11 +165,15 @@ realloc: |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | +#if defined(__GLIBC__) && defined(__GLIBC_PREREQ) |
| 272 | #if __GLIBC_PREREQ(2, 7) |
| 273 | CPU_ZERO_S(len, mask); |
| 274 | #else |
| 275 | CPU_ZERO(mask); |
| 276 | #endif |
| 277 | +#else |
| 278 | + CPU_ZERO(mask); |
| 279 | +#endif |
| 280 | /* negative tests */ |
| 281 | QUICK_TEST(sched_getaffinity(0, len, (cpu_set_t *) - 1)); |
| 282 | QUICK_TEST(sched_getaffinity(0, 0, mask)); |
| 283 | -- |
| 284 | 1.9.1 |
| 285 | |