From fc2b47ad979a87bfbd11aeea3f67c26e7fb39e30 Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Thu, 7 Jan 2016 19:40:08 +0000
Subject: [PATCH] Check if __GLIBC_PREREQ is defined before using it

__GLIBC_PREREQ is specific to glibc so it should be checked if it is
defined or not.

Signed-off-by: Khem Raj <raj.khem@gmail.com>

Upstream-Status: Pending
---
 .../kernel/syscalls/accept4/accept4_01.c      |  9 ++++++-
 .../sched_getaffinity/sched_getaffinity01.c   | 26 +++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/testcases/kernel/syscalls/accept4/accept4_01.c b/testcases/kernel/syscalls/accept4/accept4_01.c
index dec4ef93b..c5d74b07f 100644
--- a/testcases/kernel/syscalls/accept4/accept4_01.c
+++ b/testcases/kernel/syscalls/accept4/accept4_01.c
@@ -64,6 +64,7 @@ static void cleanup(void)
 	tst_rmdir();
 }
 
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
 #if !(__GLIBC_PREREQ(2, 10))
 static int
 accept4_01(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags)
@@ -82,7 +83,6 @@ accept4_01(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags)
 	}
 	tst_resm(TINFO, "\n");
 #endif
-
 #if USE_SOCKETCALL
 	long args[6];
 
@@ -97,6 +97,7 @@ accept4_01(int fd, struct sockaddr *sockaddr, socklen_t *addrlen, int flags)
 #endif
 }
 #endif
+#endif
 
 static void
 do_test(int lfd, struct sockaddr_in *conn_addr,
@@ -119,9 +120,15 @@ do_test(int lfd, struct sockaddr_in *conn_addr,
 		die("Connect Error");
 
 	addrlen = sizeof(struct sockaddr_in);
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
 #if !(__GLIBC_PREREQ(2, 10))
 	acceptfd = accept4_01(lfd, (struct sockaddr *)&claddr, &addrlen,
 			      closeonexec_flag | nonblock_flag);
+
+#else
+	acceptfd = accept4(lfd, (struct sockaddr *)&claddr, &addrlen,
+			   closeonexec_flag | nonblock_flag);
+#endif
 #else
 	acceptfd = accept4(lfd, (struct sockaddr *)&claddr, &addrlen,
 			   closeonexec_flag | nonblock_flag);
diff --git a/testcases/kernel/syscalls/sched_getaffinity/sched_getaffinity01.c b/testcases/kernel/syscalls/sched_getaffinity/sched_getaffinity01.c
index 02f04b909..d906d7e09 100644
--- a/testcases/kernel/syscalls/sched_getaffinity/sched_getaffinity01.c
+++ b/testcases/kernel/syscalls/sched_getaffinity/sched_getaffinity01.c
@@ -67,9 +67,11 @@ do { \
 	tst_resm((TEST_RETURN == -1 ? TPASS : TFAIL) | TTERRNO, #t); \
 } while (0)
 
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
 #if !(__GLIBC_PREREQ(2, 7))
 #define CPU_FREE(ptr)	free(ptr)
 #endif
+#endif
 
 int main(int ac, char **av)
 {
@@ -96,17 +98,26 @@ static void do_test(void)
 	pid_t unused_pid;
 	unsigned len;
 
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
 #if __GLIBC_PREREQ(2, 7)
 realloc:
 	mask = CPU_ALLOC(nrcpus);
+#else
+	mask = malloc(sizeof(cpu_set_t));
+#endif
 #else
 	mask = malloc(sizeof(cpu_set_t));
 #endif
 	if (mask == NULL)
 		tst_brkm(TFAIL | TTERRNO, cleanup, "fail to get enough memory");
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
 #if __GLIBC_PREREQ(2, 7)
 	len = CPU_ALLOC_SIZE(nrcpus);
 	CPU_ZERO_S(len, mask);
+#else
+	len = sizeof(cpu_set_t);
+	CPU_ZERO(mask);
+#endif
 #else
 	len = sizeof(cpu_set_t);
 	CPU_ZERO(mask);
@@ -115,11 +126,18 @@ realloc:
 	TEST(sched_getaffinity(0, len, mask));
 	if (TEST_RETURN == -1) {
 		CPU_FREE(mask);
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
 #if __GLIBC_PREREQ(2, 7)
 		if (errno == EINVAL && nrcpus < (1024 << 8)) {
 			nrcpus = nrcpus << 2;
 			goto realloc;
 		}
+#else
+		if (errno == EINVAL)
+			tst_resm(TFAIL, "NR_CPUS > 1024, we'd better use a "
+				 "newer glibc(>= 2.7)");
+		else
+#endif
 #else
 		if (errno == EINVAL)
 			tst_resm(TFAIL, "NR_CPUS > 1024, we'd better use a "
@@ -132,8 +150,12 @@ realloc:
 		tst_resm(TINFO, "cpusetsize is %d", len);
 		tst_resm(TINFO, "mask.__bits[0] = %lu ", mask->__bits[0]);
 		for (i = 0; i < num; i++) {
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
 #if __GLIBC_PREREQ(2, 7)
 			TEST(CPU_ISSET_S(i, len, mask));
+#else
+			TEST(CPU_ISSET(i, mask));
+#endif
 #else
 			TEST(CPU_ISSET(i, mask));
 #endif
@@ -144,8 +166,12 @@ realloc:
 		}
 	}
 
+#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
 #if __GLIBC_PREREQ(2, 7)
 	CPU_ZERO_S(len, mask);
+#else
+	CPU_ZERO(mask);
+#endif
 #else
 	CPU_ZERO(mask);
 #endif
-- 
2.17.1

