Skip to contentSkip to conversation

Instead sem_wait by fcntl for sigaction test.

PATCH In review Bian Naimeng 3 participants 6 messages Nov 19, 2010 – Nov 25, 2010 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Instead sem_wait by fcntl for sigaction test.

Some days before, mitani-san reported some sigaction test failed at linux,
http://sourceforge.net/mailarchive/message.php?msg_id=000001cb798d%241aaa0210%244ffe0630%24%40co.jp

As Garrett said, add Linux-isms is not advisably. So i want try to fix it by another method.

I have seen the assertions.xml, these tests want to test sigaction with SA_RESTART not sem_wait(it looks like there's a bug at older linux kernel), so i suggest to instead sem_wait by fcntl.

What about this one? If you agree with me, i will post other patchs.

-------------------------------------------------------------------------------------
At older linux kernel(2.6.22 before), a signal handler always interrupts a blocked call to sem_wait, regardless of the use of the sigaction SA_RESTART flag. So instead it by fcntl.

---
 .../conformance/interfaces/sigaction/16-1.c        |  201 ++++++++++----------
 1 files changed, 103 insertions(+), 98 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +103 -98
index 706dbfc..752dcb9 100644
@@ -21,14 +21,15 @@
 * shall restart silently.
 
 * The steps are:
-* -> create a child thread 
-* -> child registers a handler for SIGABRT with SA_RESTART, then waits for the semaphore
-* -> parent kills the child with SIGABRT, then post the semaphore.
+* -> acquired a file lock
+* -> create a child process
+* -> child registers a handler for SIGABRT with SA_RESTART, then blocked at fcntl
+* -> parent kills the child with SIGABRT, then release the lock
 
-* The test fails if the sem_wait function returns EINTR
+* The test fails if the fcntl function returns EINTR
 
 *Note:
-This test uses sem_wait to check if EINTR is returned. As the function is not required to
+This test uses fcntl to check if EINTR is returned. As the function is not required to
 fail with EINTR, the test may return PASS and the feature not be correct (false positive).
 Anyway, a false negative status cannot be returned.
 
@@ -51,9 +52,11 @@ Anyway, a false negative status cannot be returned.
 #include <string.h>
 #include <unistd.h>
 
-#include <semaphore.h>
 #include <signal.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 /******************************************************************************/
 /***************************   Test framework   *******************************/
@@ -92,128 +95,130 @@ Anyway, a false negative status cannot be returned.
 /***************************    Test case   ***********************************/
 /******************************************************************************/
 
-volatile sig_atomic_t caught = 0;
-sem_t sem;
-
 /* Handler function */
 void handler( int signo )
 {
 	printf( "Caught signal %d\n", signo );
-	caught++;
-}
-
-/* Thread function */
-void * threaded ( void * arg )
-{
-	int ret = 0;
-
-	ret = sem_wait( &sem );
-
-	if ( ret != 0 )
-	{
-		if ( errno == EINTR )
-		{
-			FAILED( "The function returned EINTR while SA_RESTART is set" );
-		}
-		else
-		{
-			UNRESOLVED( errno, "sem_wait failed" );
-		}
-	}
-
-	return NULL;
 }
 
 /* main function */
 int main()
 {
 	int ret;
-	pthread_t child;
-
+	pid_t pid;
+	int lockfd;
+	struct flock fl;
+	char tmpfname[256];
+	int status; 
 
-	struct sigaction sa;
+	snprintf(tmpfname, sizeof(tmpfname), "/tmp/sigaction_16_1_%d", getpid());
+	unlink(tmpfname);
 
 	/* Initialize output */
 	output_init();
 
-	/* Set the signal handler */
-	sa.sa_flags = SA_RESTART;
-	sa.sa_handler = handler;
-	ret = sigemptyset( &sa.sa_mask );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to empty signal set" );
-	}
-
-	/* Install the signal handler for SIGNAL */
-	ret = sigaction( SIGNAL, &sa, 0 );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to set signal handler" );
+	/*create a tmp file for test.*/
+	lockfd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
+	if (lockfd == -1) {
+		printf("Error at create file: %s\n", strerror(errno));
+		return PTS_UNRESOLVED;
 	}
-
-	/* Initialize the semaphore */
-	ret = sem_init( &sem, 0, 0 );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to init a semaphore" );
+	unlink(tmpfname);
+
+	/* lock the file first */
+	memset(&fl, 0, sizeof(struct flock));
+	fl.l_type = F_WRLCK;
+	fl.l_whence = SEEK_SET;
+	fl.l_start = 0;
+	fl.l_len = 0;
+	fl.l_pid = getpid();
+	if (-1 == fcntl(lockfd, F_SETLKW, &fl)) {
+		printf("Failed to acquire write lock: %s\n", strerror(errno));
+		return PTS_UNRESOLVED;
 	}
 
-	/* Create the child thread */
-	ret = pthread_create( &child, NULL, threaded, NULL );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to create a child thread" );
-	}
-
-	/* Let the child thread enter the wait routine...
-	  we use sched_yield as there is no certain way to test that the child
-	  is waiting for the semaphore... */
-	sched_yield();
+	pid = fork();
+	if (pid < 0) {
+		printf("Failed to fork: %s\n", strerror(errno));
+		return PTS_UNRESOLVED;
+	} else if (pid == 0) {
+		struct sigaction sa;
+
+		/* Set the signal handler */
+		sa.sa_flags = SA_RESTART;
+		sa.sa_flags = 0;
+		sa.sa_handler = handler;
+
+		ret = sigemptyset(&sa.sa_mask);
+		if(ret != 0) {
+			printf("Failed to empty signal set: %s\n",
+				strerror(errno));
+			exit(PTS_UNRESOLVED);
+		}
 
-	sched_yield();
+		/* Install the signal handler for SIGNAL */
+		ret = sigaction(SIGNAL, &sa, 0);
+		if (ret != 0) {
+			printf("Failed to set signal handler: %s\n",
+				strerror(errno));
+			exit(PTS_UNRESOLVED);
+		}
 
-	sched_yield();
+		/* block self */
+		memset(&fl, 0, sizeof(struct flock));
+		fl.l_type = F_WRLCK;
+		fl.l_whence = SEEK_SET;
+		fl.l_start = 0;
+		fl.l_len = 0;
+		fl.l_pid = getpid();
+
+		if (-1 == fcntl(lockfd, F_SETLKW, &fl)) {
+			if (errno == EINTR) {
+				printf("The function returned EINTR while "
+				       "SA_RESTART is set\n");
+			} else {
+				printf("Failed with unexpected error: %s\n",
+				       strerror(errno));
+			}
+			exit(PTS_FAIL);
+		}
 
-	/* Ok, now kill the child */
-	ret = pthread_kill( child, SIGNAL );
+		fl.l_type = F_UNLCK;
+		if (-1 == fcntl(lockfd, F_SETLKW, &fl)) {
+			printf("Failed to remove lock: %s\n", strerror(errno));
+			exit(PTS_UNRESOLVED);
+		}
 
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to kill the child thread" );
+		exit(PTS_PASS);
 	}
 
-	/* wait that the child receives the signal */
-	while ( !caught )
-		sched_yield();
+	/* make sure child has blocked at fcntl */
+	sleep(2);
 
-	/* Now let the child run and terminate */
-	ret = sem_post( &sem );
+	kill(pid, SIGNAL);
 
-	if ( ret != 0 )
-	{
-		UNRESOLVED( errno, "Failed to post the semaphore" );
-	}
+	/* make sure fcntl has enough time to restart */
+	sleep(2);
 
-	ret = pthread_join( child, NULL );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to join the thread" );
+	/* release lock to make sure child can acquire it */
+	fl.l_type = F_UNLCK;
+	if (fcntl(lockfd, F_SETLKW, &fl) == -1) {
+		printf("Failed to remove lock: %s\n", strerror(errno));
+		return PTS_UNRESOLVED;
 	}
 
-	/* terminate */
-	ret = sem_destroy( &sem );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to destroy the semaphore" );
+	ret = waitpid(pid, &status, 0);
+	if (ret != pid) {
+		printf("Failed to waitpid\n");
+		return PTS_UNRESOLVED;
 	}
 
+	if (WIFEXITED(status)) {
+		if ((ret = WEXITSTATUS(status)) != PTS_PASS)
+			return ret;
+	} else {
+		return PTS_UNRESOLVED;
+	}
 
 	/* Test passed */
 #if VERBOSE > 0
@@ -222,5 +227,5 @@ int main()
 
 #endif
 
-	PASSED;
+	return PTS_PASS;
 }
Signature · 21 lines
1.7.0.4





-- 
Regards
Bian Naimeng


------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today
http://p.sf.net/sfu/msIE9-sfdev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#1 lore raw
Quoted 342 lines

-----Original Message-----
From: Bian Naimeng [mailto:[email protected]]
Sent: Friday, November 19, 2010 3:56 PM
To: Garrett Cooper
Cc: Mitani; [email protected]
Subject: [LTP][POSIX][PATCH]Instead sem_wait by fcntl for sigaction test.

Some days before, mitani-san reported some sigaction test failed at linux,
http://sourceforge.net/mailarchive/message.php?msg_id=000001cb798d
%241aaa0210%244ffe0630%24%40co.jp

As Garrett said, add Linux-isms is not advisably. So i want try to fix it by another method.

I have seen the assertions.xml, these tests want to test sigaction with SA_RESTART
not sem_wait(it looks like there's a bug at older linux kernel), so i suggest to
instead sem_wait by fcntl.

What about this one? If you agree with me, i will post other patchs.

------------------------------------------------------------------
-------------------
At older linux kernel(2.6.22 before), a signal handler always interrupts a
blocked call to sem_wait, regardless of the use of the sigaction SA_RESTART flag. So instead it by fcntl.

---
 .../conformance/interfaces/sigaction/16-1.c        |  201
++++++++++----------
 1 files changed, 103 insertions(+), 98 deletions(-)

diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sigaction/ 16-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sigaction/ 16-1.c
index 706dbfc..752dcb9 100644
---
a/testcases/open_posix_testsuite/conformance/interfaces/sigaction/ 16-1.c
+++
b/testcases/open_posix_testsuite/conformance/interfaces/sigaction/ 16-1.c

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +0 -2
@@ -21,14 +21,15 @@
 * shall restart silently.
 
 * The steps are:
-* -> create a child thread
-* -> child registers a handler for SIGABRT with SA_RESTART, then waits

for the semaphore

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +3 -1
-* -> parent kills the child with SIGABRT, then post the semaphore.
+* -> acquired a file lock
+* -> create a child process
+* -> child registers a handler for SIGABRT with SA_RESTART, then

blocked at fcntl

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +2 -2
+* -> parent kills the child with SIGABRT, then release the lock
 
-* The test fails if the sem_wait function returns EINTR
+* The test fails if the fcntl function returns EINTR
 
 *Note:
-This test uses sem_wait to check if EINTR is returned. As the function

is not required to
+This test uses fcntl to check if EINTR is returned. As the function is not required to
fail with EINTR, the test may return PASS and the feature not be
correct (false positive).
Anyway, a false negative status cannot be returned.

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +3 -1
@@ -51,9 +52,11 @@ Anyway, a false negative status cannot be returned.
 #include <string.h>
 #include <unistd.h>
 
-#include <semaphore.h>
 #include <signal.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 
 /*****************************************************************

*************/
/*************************** Test framework
*******************************/

(quoted diff) +0 -0
@@ -92,128 +95,130 @@ Anyway, a false negative status cannot be

returned.
/*************************** Test case
***********************************/

/***************************************************************** *************/

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +0 -18
-volatile sig_atomic_t caught = 0;
-sem_t sem;
-
 /* Handler function */
 void handler( int signo )
 {
 	printf( "Caught signal %d\n", signo );
-	caught++;
-}
-
-/* Thread function */
-void * threaded ( void * arg )
-{
-	int ret = 0;
-
-	ret = sem_wait( &sem );
-
-	if ( ret != 0 )
-	{
-		if ( errno == EINTR )
-		{
-			FAILED( "The function returned EINTR while

SA_RESTART is set" );

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +6 -11
-		}
-		else
-		{
-			UNRESOLVED( errno, "sem_wait failed" );
-		}
-	}
-
-	return NULL;
 }
 
 /* main function */
 int main()
 {
 	int ret;
-	pthread_t child;
-
+	pid_t pid;
+	int lockfd;
+	struct flock fl;
+	char tmpfname[256];
+	int status;
 
-	struct sigaction sa;
+	snprintf(tmpfname, sizeof(tmpfname),

"/tmp/sigaction_16_1_%d", getpid());

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +3 -16
+	unlink(tmpfname);
 
 	/* Initialize output */
 	output_init();
 
-	/* Set the signal handler */
-	sa.sa_flags = SA_RESTART;
-	sa.sa_handler = handler;
-	ret = sigemptyset( &sa.sa_mask );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to empty signal set" );
-	}
-
-	/* Install the signal handler for SIGNAL */
-	ret = sigaction( SIGNAL, &sa, 0 );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to set signal handler" );
+	/*create a tmp file for test.*/
+	lockfd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR |

S_IWUSR);

(quoted diff) +2 -0
+	if (lockfd == -1) {
+		printf("Error at create file: %s\n",

strerror(errno));

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +12 -7
+		return PTS_UNRESOLVED;
 	}
-
-	/* Initialize the semaphore */
-	ret = sem_init( &sem, 0, 0 );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to init a semaphore" );
+	unlink(tmpfname);
+
+	/* lock the file first */
+	memset(&fl, 0, sizeof(struct flock));
+	fl.l_type = F_WRLCK;
+	fl.l_whence = SEEK_SET;
+	fl.l_start = 0;
+	fl.l_len = 0;
+	fl.l_pid = getpid();
+	if (-1 == fcntl(lockfd, F_SETLKW, &fl)) {
+		printf("Failed to acquire write lock: %s\n",

strerror(errno));

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +1 -10
+		return PTS_UNRESOLVED;
 	}
 
-	/* Create the child thread */
-	ret = pthread_create( &child, NULL, threaded, NULL );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to create a child thread" );
-	}
-
-	/* Let the child thread enter the wait routine...
-	  we use sched_yield as there is no certain way to test that

the child

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +36 -4
-	  is waiting for the semaphore... */
-	sched_yield();
+	pid = fork();
+	if (pid < 0) {
+		printf("Failed to fork: %s\n", strerror(errno));
+		return PTS_UNRESOLVED;
+	} else if (pid == 0) {
+		struct sigaction sa;
+
+		/* Set the signal handler */
+		sa.sa_flags = SA_RESTART;
+		sa.sa_flags = 0;
+		sa.sa_handler = handler;
+
+		ret = sigemptyset(&sa.sa_mask);
+		if(ret != 0) {
+			printf("Failed to empty signal set: %s\n",
+				strerror(errno));
+			exit(PTS_UNRESOLVED);
+		}
 
-	sched_yield();
+		/* Install the signal handler for SIGNAL */
+		ret = sigaction(SIGNAL, &sa, 0);
+		if (ret != 0) {
+			printf("Failed to set signal handler: %s\n",
+				strerror(errno));
+			exit(PTS_UNRESOLVED);
+		}
 
-	sched_yield();
+		/* block self */
+		memset(&fl, 0, sizeof(struct flock));
+		fl.l_type = F_WRLCK;
+		fl.l_whence = SEEK_SET;
+		fl.l_start = 0;
+		fl.l_len = 0;
+		fl.l_pid = getpid();
+
+		if (-1 == fcntl(lockfd, F_SETLKW, &fl)) {
+			if (errno == EINTR) {
+				printf("The function returned EINTR

while "

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +3 -0
+				       "SA_RESTART is set\n");
+			} else {
+				printf("Failed with unexpected

error: %s\n",

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +7 -2
+				       strerror(errno));
+			}
+			exit(PTS_FAIL);
+		}
 
-	/* Ok, now kill the child */
-	ret = pthread_kill( child, SIGNAL );
+		fl.l_type = F_UNLCK;
+		if (-1 == fcntl(lockfd, F_SETLKW, &fl)) {
+			printf("Failed to remove lock: %s\n",

strerror(errno));

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +12 -17
+			exit(PTS_UNRESOLVED);
+		}
 
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to kill the child thread" );
+		exit(PTS_PASS);
 	}
 
-	/* wait that the child receives the signal */
-	while ( !caught )
-		sched_yield();
+	/* make sure child has blocked at fcntl */
+	sleep(2);
 
-	/* Now let the child run and terminate */
-	ret = sem_post( &sem );
+	kill(pid, SIGNAL);
 
-	if ( ret != 0 )
-	{
-		UNRESOLVED( errno, "Failed to post the semaphore" );
-	}
+	/* make sure fcntl has enough time to restart */
+	sleep(2);
 
-	ret = pthread_join( child, NULL );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to join the thread" );
+	/* release lock to make sure child can acquire it */
+	fl.l_type = F_UNLCK;
+	if (fcntl(lockfd, F_SETLKW, &fl) == -1) {
+		printf("Failed to remove lock: %s\n",

strerror(errno));

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +12 -8
+		return PTS_UNRESOLVED;
 	}
 
-	/* terminate */
-	ret = sem_destroy( &sem );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to destroy the semaphore" );
+	ret = waitpid(pid, &status, 0);
+	if (ret != pid) {
+		printf("Failed to waitpid\n");
+		return PTS_UNRESOLVED;
 	}
 
+	if (WIFEXITED(status)) {
+		if ((ret = WEXITSTATUS(status)) != PTS_PASS)
+			return ret;
+	} else {
+		return PTS_UNRESOLVED;
+	}
 
 	/* Test passed */
 #if VERBOSE > 0
@@ -222,5 +227,5 @@ int main()
 
 #endif
 
-	PASSED;
+	return PTS_PASS;
 }
--

1.7.0.4

Quoted 3 lines

--
Regards
Bian Naimeng

I tried Bian's patch:
------------
[root@RHEL55-LTP-x86 sigaction]# make conformance/interfaces/sigaction/16-1 compile PASSED [root@RHEL55-LTP-x86 sigaction]# ./16-1.run-test Caught signal 6
The function returned EINTR while SA_RESTART is set [root@RHEL55-LTP-x86 sigaction]# echo $?
1
[root@RHEL55-LTP-x86 sigaction]#
------------

It failed, but "Caught signal 6" indicated. It shows that a signal is pitched and was received.

Thank you--

-Tomonori Mitani

------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports standards for HTML5, CSS3, SVG 1.1, ECMAScript5, and DOM L2 & L3. Spend less time writing and rewriting code and more time creating great experiences on the web. Be a part of the beta today
http://p.sf.net/sfu/msIE9-sfdev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

#2 lore raw
Quoted 214 lines · Mitani
Quoted 214 lines

-----Original Message-----
From: Bian Naimeng [mailto:[email protected]]
Sent: Friday, November 19, 2010 3:56 PM
To: Garrett Cooper
Cc: Mitani; [email protected]
Subject: [LTP][POSIX][PATCH]Instead sem_wait by fcntl for sigaction test.

Some days before, mitani-san reported some sigaction test failed at linux,
http://sourceforge.net/mailarchive/message.php?msg_id=000001cb798d
%241aaa0210%244ffe0630%24%40co.jp

As Garrett said, add Linux-isms is not advisably. So i want try to fix it by another method.

I have seen the assertions.xml, these tests want to test sigaction with SA_RESTART
not sem_wait(it looks like there's a bug at older linux kernel), so i suggest to
instead sem_wait by fcntl.

What about this one? If you agree with me, i will post other patchs.

------------------------------------------------------------------
-------------------
At older linux kernel(2.6.22 before), a signal handler always interrupts a
blocked call to sem_wait, regardless of the use of the sigaction SA_RESTART flag. So instead it by fcntl.

---
 .../conformance/interfaces/sigaction/16-1.c        |  201
++++++++++----------
 1 files changed, 103 insertions(+), 98 deletions(-)

diff --git a/testcases/open_posix_testsuite/conformance/interfaces/sigaction/ 16-1.c b/testcases/open_posix_testsuite/conformance/interfaces/sigaction/ 16-1.c
index 706dbfc..752dcb9 100644
---
a/testcases/open_posix_testsuite/conformance/interfaces/sigaction/ 16-1.c
+++
b/testcases/open_posix_testsuite/conformance/interfaces/sigaction/ 16-1.c

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +0 -2
@@ -21,14 +21,15 @@
 * shall restart silently.
 
 * The steps are:
-* -> create a child thread
-* -> child registers a handler for SIGABRT with SA_RESTART, then waits

for the semaphore

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +3 -1
-* -> parent kills the child with SIGABRT, then post the semaphore.
+* -> acquired a file lock
+* -> create a child process
+* -> child registers a handler for SIGABRT with SA_RESTART, then

blocked at fcntl

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +2 -2
+* -> parent kills the child with SIGABRT, then release the lock
 
-* The test fails if the sem_wait function returns EINTR
+* The test fails if the fcntl function returns EINTR
 
 *Note:
-This test uses sem_wait to check if EINTR is returned. As the function

is not required to
+This test uses fcntl to check if EINTR is returned. As the function is not required to
fail with EINTR, the test may return PASS and the feature not be
correct (false positive).
Anyway, a false negative status cannot be returned.

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +3 -1
@@ -51,9 +52,11 @@ Anyway, a false negative status cannot be returned.
 #include <string.h>
 #include <unistd.h>
 
-#include <semaphore.h>
 #include <signal.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 
 /*****************************************************************

*************/
/*************************** Test framework
*******************************/

(quoted diff) +0 -0
@@ -92,128 +95,130 @@ Anyway, a false negative status cannot be

returned.
/*************************** Test case
***********************************/

/***************************************************************** *************/

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +0 -18
-volatile sig_atomic_t caught = 0;
-sem_t sem;
-
 /* Handler function */
 void handler( int signo )
 {
 	printf( "Caught signal %d\n", signo );
-	caught++;
-}
-
-/* Thread function */
-void * threaded ( void * arg )
-{
-	int ret = 0;
-
-	ret = sem_wait( &sem );
-
-	if ( ret != 0 )
-	{
-		if ( errno == EINTR )
-		{
-			FAILED( "The function returned EINTR while

SA_RESTART is set" );

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +6 -11
-		}
-		else
-		{
-			UNRESOLVED( errno, "sem_wait failed" );
-		}
-	}
-
-	return NULL;
 }
 
 /* main function */
 int main()
 {
 	int ret;
-	pthread_t child;
-
+	pid_t pid;
+	int lockfd;
+	struct flock fl;
+	char tmpfname[256];
+	int status;
 
-	struct sigaction sa;
+	snprintf(tmpfname, sizeof(tmpfname),

"/tmp/sigaction_16_1_%d", getpid());

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +3 -16
+	unlink(tmpfname);
 
 	/* Initialize output */
 	output_init();
 
-	/* Set the signal handler */
-	sa.sa_flags = SA_RESTART;
-	sa.sa_handler = handler;
-	ret = sigemptyset( &sa.sa_mask );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to empty signal set" );
-	}
-
-	/* Install the signal handler for SIGNAL */
-	ret = sigaction( SIGNAL, &sa, 0 );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to set signal handler" );
+	/*create a tmp file for test.*/
+	lockfd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR |

S_IWUSR);

(quoted diff) +2 -0
+	if (lockfd == -1) {
+		printf("Error at create file: %s\n",

strerror(errno));

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +12 -7
+		return PTS_UNRESOLVED;
 	}
-
-	/* Initialize the semaphore */
-	ret = sem_init( &sem, 0, 0 );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to init a semaphore" );
+	unlink(tmpfname);
+
+	/* lock the file first */
+	memset(&fl, 0, sizeof(struct flock));
+	fl.l_type = F_WRLCK;
+	fl.l_whence = SEEK_SET;
+	fl.l_start = 0;
+	fl.l_len = 0;
+	fl.l_pid = getpid();
+	if (-1 == fcntl(lockfd, F_SETLKW, &fl)) {
+		printf("Failed to acquire write lock: %s\n",

strerror(errno));

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +1 -10
+		return PTS_UNRESOLVED;
 	}
 
-	/* Create the child thread */
-	ret = pthread_create( &child, NULL, threaded, NULL );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to create a child thread" );
-	}
-
-	/* Let the child thread enter the wait routine...
-	  we use sched_yield as there is no certain way to test that

the child

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +2 -2
-	  is waiting for the semaphore... */
-	sched_yield();
+	pid = fork();
+	if (pid < 0) {
Quoted 8 lines

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c Anyway, a false negative status cannot be returned. · line 149

Quoted 8 lines
testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +8 -0
+		printf("Failed to fork: %s\n", strerror(errno));
+		return PTS_UNRESOLVED;
+	} else if (pid == 0) {
+		struct sigaction sa;
+
+		/* Set the signal handler */
+		sa.sa_flags = SA_RESTART;
+		sa.sa_flags = 0;

Hi mitani-san,

Would you like to retry it after remove this line "sa.sa_flags = 0",

Thanks
Bian

Quoted 148 lines
Quoted 120 lines
testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +26 -2
+		sa.sa_handler = handler;
+
+		ret = sigemptyset(&sa.sa_mask);
+		if(ret != 0) {
+			printf("Failed to empty signal set: %s\n",
+				strerror(errno));
+			exit(PTS_UNRESOLVED);
+		}
 
-	sched_yield();
+		/* Install the signal handler for SIGNAL */
+		ret = sigaction(SIGNAL, &sa, 0);
+		if (ret != 0) {
+			printf("Failed to set signal handler: %s\n",
+				strerror(errno));
+			exit(PTS_UNRESOLVED);
+		}
 
-	sched_yield();
+		/* block self */
+		memset(&fl, 0, sizeof(struct flock));
+		fl.l_type = F_WRLCK;
+		fl.l_whence = SEEK_SET;
+		fl.l_start = 0;
+		fl.l_len = 0;
+		fl.l_pid = getpid();
+
+		if (-1 == fcntl(lockfd, F_SETLKW, &fl)) {
+			if (errno == EINTR) {
+				printf("The function returned EINTR

while "

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +3 -0
+				       "SA_RESTART is set\n");
+			} else {
+				printf("Failed with unexpected

error: %s\n",

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +7 -2
+				       strerror(errno));
+			}
+			exit(PTS_FAIL);
+		}
 
-	/* Ok, now kill the child */
-	ret = pthread_kill( child, SIGNAL );
+		fl.l_type = F_UNLCK;
+		if (-1 == fcntl(lockfd, F_SETLKW, &fl)) {
+			printf("Failed to remove lock: %s\n",

strerror(errno));

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +12 -17
+			exit(PTS_UNRESOLVED);
+		}
 
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to kill the child thread" );
+		exit(PTS_PASS);
 	}
 
-	/* wait that the child receives the signal */
-	while ( !caught )
-		sched_yield();
+	/* make sure child has blocked at fcntl */
+	sleep(2);
 
-	/* Now let the child run and terminate */
-	ret = sem_post( &sem );
+	kill(pid, SIGNAL);
 
-	if ( ret != 0 )
-	{
-		UNRESOLVED( errno, "Failed to post the semaphore" );
-	}
+	/* make sure fcntl has enough time to restart */
+	sleep(2);
 
-	ret = pthread_join( child, NULL );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to join the thread" );
+	/* release lock to make sure child can acquire it */
+	fl.l_type = F_UNLCK;
+	if (fcntl(lockfd, F_SETLKW, &fl) == -1) {
+		printf("Failed to remove lock: %s\n",

strerror(errno));

testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +12 -8
+		return PTS_UNRESOLVED;
 	}
 
-	/* terminate */
-	ret = sem_destroy( &sem );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to destroy the semaphore" );
+	ret = waitpid(pid, &status, 0);
+	if (ret != pid) {
+		printf("Failed to waitpid\n");
+		return PTS_UNRESOLVED;
 	}
 
+	if (WIFEXITED(status)) {
+		if ((ret = WEXITSTATUS(status)) != PTS_PASS)
+			return ret;
+	} else {
+		return PTS_UNRESOLVED;
+	}
 
 	/* Test passed */
 #if VERBOSE > 0
@@ -222,5 +227,5 @@ int main()
 
 #endif
 
-	PASSED;
+	return PTS_PASS;
 }
--

1.7.0.4

Quoted 3 lines

--
Regards
Bian Naimeng

I tried Bian's patch:
------------
[root@RHEL55-LTP-x86 sigaction]# make conformance/interfaces/sigaction/16-1 compile PASSED [root@RHEL55-LTP-x86 sigaction]# ./16-1.run-test Caught signal 6
The function returned EINTR while SA_RESTART is set [root@RHEL55-LTP-x86 sigaction]# echo $?
1
[root@RHEL55-LTP-x86 sigaction]#
------------

It failed, but "Caught signal 6" indicated. It shows that a signal is pitched and was received.

Thank you--

-Tomonori Mitani

Signature · 14 lines
Regards
Bian Naimeng


------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today
http://p.sf.net/sfu/msIE9-sfdev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#3 lore raw
In reply to Mitani
Quoted 8 lines
Quoted 8 lines

-----Original Message-----
From: Bian Naimeng [mailto:[email protected]]
Sent: Friday, November 19, 2010 3:56 PM
To: Garrett Cooper
Cc: Mitani; [email protected]
Subject: [LTP][POSIX][PATCH]Instead sem_wait by fcntl for sigaction test.

... snip ...

Quoted 26 lines
Quoted 26 lines
(quoted diff) +24 -1
+	if (pid < 0) {
+		printf("Failed to fork: %s\n", strerror(errno));
+		return PTS_UNRESOLVED;
+	} else if (pid == 0) {
+		struct sigaction sa;
+
+		/* Set the signal handler */
+		sa.sa_flags = SA_RESTART;
+		sa.sa_flags = 0;
+		sa.sa_handler = handler;
+
+		ret = sigemptyset(&sa.sa_mask);
+		if(ret != 0) {
+			printf("Failed to empty signal set: %s\n",
+				strerror(errno));
+			exit(PTS_UNRESOLVED);
+		}
 
-	sched_yield();
+		/* Install the signal handler for SIGNAL */
+		ret = sigaction(SIGNAL, &sa, 0);
+		if (ret != 0) {
+			printf("Failed to set signal handler: %s\n",
+				strerror(errno));
+			exit(PTS_UNRESOLVED);
+		}

... snip ...

Quoted 14 lines

I tried Bian's patch:
------------
[root@RHEL55-LTP-x86 sigaction]# make conformance/interfaces/sigaction/16-1 compile PASSED [root@RHEL55-LTP-x86 sigaction]# ./16-1.run-test Caught signal 6
The function returned EINTR while SA_RESTART is set [root@RHEL55-LTP-x86 sigaction]# echo $?
1
[root@RHEL55-LTP-x86 sigaction]#
------------

Quoted 3 lines

It failed, but "Caught signal 6" indicated. It shows that a signal is pitched and was received.

Sorry, i forgot to remove some debug code. please retry the new one.

----

At older linux kernel(2.6.22 before), a signal handler always interrupts a blocked call to sem_wait, regardless of the use of the sigaction SA_RESTART flag. So instead it by fcntl.

---
 .../conformance/interfaces/sigaction/16-1.c        |  200 ++++++++++----------
 1 files changed, 102 insertions(+), 98 deletions(-)
testcases/open_posix_testsuite/conformance/interfaces/sigaction/16-1.c +102 -98
index 706dbfc..07ab98a 100644
@@ -21,14 +21,15 @@
 * shall restart silently.
 
 * The steps are:
-* -> create a child thread 
-* -> child registers a handler for SIGABRT with SA_RESTART, then waits for the semaphore
-* -> parent kills the child with SIGABRT, then post the semaphore.
+* -> acquired a file lock
+* -> create a child process
+* -> child registers a handler for SIGABRT with SA_RESTART, then blocked at fcntl
+* -> parent kills the child with SIGABRT, then release the lock
 
-* The test fails if the sem_wait function returns EINTR
+* The test fails if the fcntl function returns EINTR
 
 *Note:
-This test uses sem_wait to check if EINTR is returned. As the function is not required to
+This test uses fcntl to check if EINTR is returned. As the function is not required to
 fail with EINTR, the test may return PASS and the feature not be correct (false positive).
 Anyway, a false negative status cannot be returned.
 
@@ -51,9 +52,11 @@ Anyway, a false negative status cannot be returned.
 #include <string.h>
 #include <unistd.h>
 
-#include <semaphore.h>
 #include <signal.h>
 #include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 /******************************************************************************/
 /***************************   Test framework   *******************************/
@@ -92,128 +95,129 @@ Anyway, a false negative status cannot be returned.
 /***************************    Test case   ***********************************/
 /******************************************************************************/
 
-volatile sig_atomic_t caught = 0;
-sem_t sem;
-
 /* Handler function */
 void handler( int signo )
 {
 	printf( "Caught signal %d\n", signo );
-	caught++;
-}
-
-/* Thread function */
-void * threaded ( void * arg )
-{
-	int ret = 0;
-
-	ret = sem_wait( &sem );
-
-	if ( ret != 0 )
-	{
-		if ( errno == EINTR )
-		{
-			FAILED( "The function returned EINTR while SA_RESTART is set" );
-		}
-		else
-		{
-			UNRESOLVED( errno, "sem_wait failed" );
-		}
-	}
-
-	return NULL;
 }
 
 /* main function */
 int main()
 {
 	int ret;
-	pthread_t child;
-
+	pid_t pid;
+	int lockfd;
+	struct flock fl;
+	char tmpfname[256];
+	int status; 
 
-	struct sigaction sa;
+	snprintf(tmpfname, sizeof(tmpfname), "/tmp/sigaction_16_1_%d", getpid());
+	unlink(tmpfname);
 
 	/* Initialize output */
 	output_init();
 
-	/* Set the signal handler */
-	sa.sa_flags = SA_RESTART;
-	sa.sa_handler = handler;
-	ret = sigemptyset( &sa.sa_mask );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to empty signal set" );
-	}
-
-	/* Install the signal handler for SIGNAL */
-	ret = sigaction( SIGNAL, &sa, 0 );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to set signal handler" );
+	/*create a tmp file for test.*/
+	lockfd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
+	if (lockfd == -1) {
+		printf("Error at create file: %s\n", strerror(errno));
+		return PTS_UNRESOLVED;
 	}
-
-	/* Initialize the semaphore */
-	ret = sem_init( &sem, 0, 0 );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to init a semaphore" );
+	unlink(tmpfname);
+
+	/* lock the file first */
+	memset(&fl, 0, sizeof(struct flock));
+	fl.l_type = F_WRLCK;
+	fl.l_whence = SEEK_SET;
+	fl.l_start = 0;
+	fl.l_len = 0;
+	fl.l_pid = getpid();
+	if (-1 == fcntl(lockfd, F_SETLKW, &fl)) {
+		printf("Failed to acquire write lock: %s\n", strerror(errno));
+		return PTS_UNRESOLVED;
 	}
 
-	/* Create the child thread */
-	ret = pthread_create( &child, NULL, threaded, NULL );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to create a child thread" );
-	}
-
-	/* Let the child thread enter the wait routine...
-	  we use sched_yield as there is no certain way to test that the child
-	  is waiting for the semaphore... */
-	sched_yield();
+	pid = fork();
+	if (pid < 0) {
+		printf("Failed to fork: %s\n", strerror(errno));
+		return PTS_UNRESOLVED;
+	} else if (pid == 0) {
+		struct sigaction sa;
+
+		/* Set the signal handler */
+		sa.sa_flags = SA_RESTART;
+		sa.sa_handler = handler;
+
+		ret = sigemptyset(&sa.sa_mask);
+		if(ret != 0) {
+			printf("Failed to empty signal set: %s\n",
+				strerror(errno));
+			exit(PTS_UNRESOLVED);
+		}
 
-	sched_yield();
+		/* Install the signal handler for SIGNAL */
+		ret = sigaction(SIGNAL, &sa, 0);
+		if (ret != 0) {
+			printf("Failed to set signal handler: %s\n",
+				strerror(errno));
+			exit(PTS_UNRESOLVED);
+		}
 
-	sched_yield();
+		/* block self */
+		memset(&fl, 0, sizeof(struct flock));
+		fl.l_type = F_WRLCK;
+		fl.l_whence = SEEK_SET;
+		fl.l_start = 0;
+		fl.l_len = 0;
+		fl.l_pid = getpid();
+
+		if (-1 == fcntl(lockfd, F_SETLKW, &fl)) {
+			if (errno == EINTR) {
+				printf("The function returned EINTR while "
+				       "SA_RESTART is set\n");
+			} else {
+				printf("Failed with unexpected error: %s\n",
+				       strerror(errno));
+			}
+			exit(PTS_FAIL);
+		}
 
-	/* Ok, now kill the child */
-	ret = pthread_kill( child, SIGNAL );
+		fl.l_type = F_UNLCK;
+		if (-1 == fcntl(lockfd, F_SETLKW, &fl)) {
+			printf("Failed to remove lock: %s\n", strerror(errno));
+			exit(PTS_UNRESOLVED);
+		}
 
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to kill the child thread" );
+		exit(PTS_PASS);
 	}
 
-	/* wait that the child receives the signal */
-	while ( !caught )
-		sched_yield();
+	/* make sure child has blocked at fcntl */
+	sleep(2);
 
-	/* Now let the child run and terminate */
-	ret = sem_post( &sem );
+	kill(pid, SIGNAL);
 
-	if ( ret != 0 )
-	{
-		UNRESOLVED( errno, "Failed to post the semaphore" );
-	}
+	/* make sure fcntl has enough time to restart */
+	sleep(2);
 
-	ret = pthread_join( child, NULL );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to join the thread" );
+	/* release lock to make sure child can acquire it */
+	fl.l_type = F_UNLCK;
+	if (fcntl(lockfd, F_SETLKW, &fl) == -1) {
+		printf("Failed to remove lock: %s\n", strerror(errno));
+		return PTS_UNRESOLVED;
 	}
 
-	/* terminate */
-	ret = sem_destroy( &sem );
-
-	if ( ret != 0 )
-	{
-		UNRESOLVED( ret, "Failed to destroy the semaphore" );
+	ret = waitpid(pid, &status, 0);
+	if (ret != pid) {
+		printf("Failed to waitpid\n");
+		return PTS_UNRESOLVED;
 	}
 
+	if (WIFEXITED(status)) {
+		if ((ret = WEXITSTATUS(status)) != PTS_PASS)
+			return ret;
+	} else {
+		return PTS_UNRESOLVED;
+	}
 
 	/* Test passed */
 #if VERBOSE > 0
@@ -222,5 +226,5 @@ int main()
 
 #endif
 
-	PASSED;
+	return PTS_PASS;
 }
Signature · 19 lines
1.7.0.4



-- 
Regards
Bian Naimeng


------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today
http://p.sf.net/sfu/msIE9-sfdev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#4 lore raw
In reply to Bian Naimeng

Hi!

Quoted 10 lines

Some days before, mitani-san reported some sigaction test failed at linux,
http://sourceforge.net/mailarchive/message.php?msg_id=000001cb798d%241aaa0210%244ffe0630%24%40co.jp

As Garrett said, add Linux-isms is not advisably. So i want try to fix it by another method.

I have seen the assertions.xml, these tests want to test sigaction with SA_RESTART not sem_wait(it looks like there's a bug at older linux kernel), so i suggest to instead sem_wait by fcntl.

What about this one? If you agree with me, i will post other patchs.

As far as I understand this, you are trying to avoid a fact, that older kernels are not comforming to the POSIX specification. This is a case where the test is failing for a good reason, all we could do here is print message like:

"this test is known to fail on linux kernel < 2.6.22"

Before the test reports failure.

On the other end I do not object to adding test for file locks with SA_RESTART. But you should not remove the sem_lock() test (as it's not broken).

Signature · 13 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today
http://p.sf.net/sfu/msIE9-sfdev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

Hi!

Quoted 29 lines
Quoted 29 lines
Quoted 10 lines
Quoted 10 lines

Some days before, mitani-san reported some sigaction test failed at linux,
http://sourceforge.net/mailarchive/message.php?msg_id=000001cb798d%241aaa0210%244ffe0630%24%40co.jp

As Garrett said, add Linux-isms is not advisably. So i want try to fix it by another method.

I have seen the assertions.xml, these tests want to test sigaction with SA_RESTART not sem_wait(it looks like there's a bug at older linux kernel), so i suggest to instead sem_wait by fcntl.

What about this one? If you agree with me, i will post other patchs.

Quoted 8 lines

As far as I understand this, you are trying to avoid a fact, that older kernels are not comforming to the POSIX specification. This is a case where the test is failing for a good reason, all we could do here is print message like:

"this test is known to fail on linux kernel < 2.6.22"

Before the test reports failure.

Well... I would actually record these items in a file somewhere. Once upon a time the ltp-results@ list was a good source for this data, but I don't know how useful this data is anymore or how much/well this data is reported and subsequently mined (I'm not on the list for instance)...

Quoted 3 lines

On the other end I do not object to adding test for file locks with SA_RESTART. But you should not remove the sem_lock() test (as it's not broken).

Quoted 5 lines
Quoted 2 lines

Agreed.

Well...
Then, do you have a plan to do it?

So far there are more important things on my TODO.

Signature · 13 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list