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