diff mbox series

[v3] posix/conformance/interfaces/sem_timedwait/2-1: Fix test

Message ID c82e787a5e77e6c7bc7cbac6338f4ce33b1079b2.1722929348.git.chen.haonan2@zte.com.cn
State Accepted
Headers show
Series [v3] posix/conformance/interfaces/sem_timedwait/2-1: Fix test | expand

Commit Message

chenguanxi11234@163.com Aug. 11, 2024, 12:33 p.m. UTC
From: Chen Haonan <chen.haonan2@zte.com.cn>

Since the parent and child processes are not operating on the 
same semaphore, this code wasn't doing its job correctly before,
so we mapped the semaphore to a piece of shared memory and 
changed some implementation details in the original code to make it work.

Signed-off-by: Chen Haonan <chen.haonan2@zte.com.cn>
---
 .../interfaces/sem_timedwait/2-1.c            | 24 +++++++++++++------
 1 file changed, 17 insertions(+), 7 deletions(-)

Comments

Cyril Hrubis Aug. 28, 2024, 1:33 p.m. UTC | #1
Hi!
Pushed with a minor change, thanks.

> -		if (!WEXITSTATUS(i)) {
> +		if (WEXITSTATUS(i)) {
>  			return PTS_FAIL;
>  		}

The return value from WEXITSTATUS() is only valid if WIFEXITED() is true
so I've changed this part as:

@@ -71,7 +71,7 @@ int main(void)
                        return PTS_UNRESOLVED;
                }

-               if (WEXITSTATUS(i)) {
+               if (!WIFEXITED(i) || WEXITSTATUS(i)) {
                        return PTS_FAIL;
                }
                puts("TEST PASSED");
diff mbox series

Patch

diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sem_timedwait/2-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sem_timedwait/2-1.c
index 655e35108..46e9b696a 100644
--- a/testcases/open_posix_testsuite/conformance/interfaces/sem_timedwait/2-1.c
+++ b/testcases/open_posix_testsuite/conformance/interfaces/sem_timedwait/2-1.c
@@ -20,6 +20,7 @@ 
 #include <fcntl.h>
 #include <signal.h>
 #include <time.h>
+#include <sys/mman.h>
 #include "posixtest.h"
 
 #define TEST "2-1"
@@ -28,12 +29,17 @@ 
 
 int main(void)
 {
-	sem_t mysemp;
+	sem_t *mysemp;
 	struct timespec ts;
 	int pid;
+	mysemp = mmap(NULL, sizeof(*mysemp), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+	if (mysemp == MAP_FAILED) {
+		perror(ERROR_PREFIX "mmap");
+		return PTS_UNRESOLVED;
+	}
 
 	/* Semaphore started out locked */
-	if (sem_init(&mysemp, 0, 0) == -1) {
+	if (sem_init(mysemp, 1, 0) == -1) {
 		perror(ERROR_PREFIX "sem_init");
 		return PTS_UNRESOLVED;
 	}
@@ -44,19 +50,19 @@  int main(void)
 		ts.tv_sec = time(NULL) + 2;
 		ts.tv_nsec = 0;
 
-		if (sem_timedwait(&mysemp, &ts) == -1) {
+		if (sem_timedwait(mysemp, &ts) == -1) {
 			puts("TEST FAILED");
 			return PTS_FAIL;
 		} else {
 			puts("TEST PASSED");
-			sem_destroy(&mysemp);
+			sem_destroy(mysemp);
 			return PTS_PASS;
 		}
 	} else if (pid > 0)	// parent to unlock semaphore
 	{
 		int i;
 		sleep(1);
-		if (sem_post(&mysemp) == -1) {
+		if (sem_post(mysemp) == -1) {
 			perror(ERROR_PREFIX "sem_post");
 			return PTS_FAIL;
 		}
@@ -65,11 +71,15 @@  int main(void)
 			return PTS_UNRESOLVED;
 		}
 
-		if (!WEXITSTATUS(i)) {
+		if (WEXITSTATUS(i)) {
 			return PTS_FAIL;
 		}
 		puts("TEST PASSED");
-		sem_destroy(&mysemp);
+		sem_destroy(mysemp);
+		if (munmap(mysemp, sizeof(*mysemp)) == -1) {
+			perror(ERROR_PREFIX "munmap");
+			return PTS_UNRESOLVED;
+		}
 		return PTS_PASS;
 	}
 	return PTS_UNRESOLVED;