public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH v3] posix/conformance/interfaces/sem_timedwait/2-1: Fix test
@ 2024-08-11 12:33 chenguanxi11234
  2024-08-28 13:33 ` Cyril Hrubis
  0 siblings, 1 reply; 2+ messages in thread
From: chenguanxi11234 @ 2024-08-11 12:33 UTC (permalink / raw)
  To: ltp; +Cc: yang.guang5, chen.haonan2

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(-)

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;
-- 
2.25.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [LTP] [PATCH v3] posix/conformance/interfaces/sem_timedwait/2-1: Fix test
  2024-08-11 12:33 [LTP] [PATCH v3] posix/conformance/interfaces/sem_timedwait/2-1: Fix test chenguanxi11234
@ 2024-08-28 13:33 ` Cyril Hrubis
  0 siblings, 0 replies; 2+ messages in thread
From: Cyril Hrubis @ 2024-08-28 13:33 UTC (permalink / raw)
  To: chenguanxi11234; +Cc: yang.guang5, ltp, chen.haonan2

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");

-- 
Cyril Hrubis
chrubis@suse.cz

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-08-28 13:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-11 12:33 [LTP] [PATCH v3] posix/conformance/interfaces/sem_timedwait/2-1: Fix test chenguanxi11234
2024-08-28 13:33 ` Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox