From 3f0b38402424deb0dfa4a2dfb590c285f80105fb Mon Sep 17 00:00:00 2001
From: Jiri Palecek <jirka@debian.(none)>
Date: Wed, 3 Dec 2008 01:16:55 +0100
Subject: [PATCH] Trap signals in aiodio_sparse and dio_sparse tests to ensure cleanup of temporary files

---
 testcases/kernel/io/ltp-aiodio/Makefile        |    4 +-
 testcases/kernel/io/ltp-aiodio/aiodio_sparse.c |   79 ++++++++++++++++++++++--
 testcases/kernel/io/ltp-aiodio/dio_sparse.c    |   66 ++++++++++++++++++-
 3 files changed, 137 insertions(+), 12 deletions(-)

diff --git a/testcases/kernel/io/ltp-aiodio/Makefile b/testcases/kernel/io/ltp-aiodio/Makefile
index 7b89fd2..1e3bf79 100644
--- a/testcases/kernel/io/ltp-aiodio/Makefile
+++ b/testcases/kernel/io/ltp-aiodio/Makefile
@@ -1,5 +1,5 @@
-CFLAGS+= -Wall -O -g -DAIO
-LDLIBS+= -laio
+CFLAGS+= -Wall -O -g -DAIO -I../../../../include
+LDLIBS+= -L../../../../lib -laio -lltp
 
 TARGETS=ltp-diorh aiocp aiodio_append aiodio_sparse dio_append dio_sparse dio_truncate dirty read_checkzero aio-stress
 SRCS=$(wildcard *.c)
diff --git a/testcases/kernel/io/ltp-aiodio/aiodio_sparse.c b/testcases/kernel/io/ltp-aiodio/aiodio_sparse.c
index 2f4c303..c040994 100644
--- a/testcases/kernel/io/ltp-aiodio/aiodio_sparse.c
+++ b/testcases/kernel/io/ltp-aiodio/aiodio_sparse.c
@@ -39,9 +39,30 @@
 
 #include <libaio.h>
 
+#include "test.h"
+
 #define NUM_CHILDREN 1000
 
 int debug;
+const char* filename1=NULL;
+const char* filename2=NULL;
+
+static void setup(void);
+static void cleanup(void);
+
+char *TCID="aiodio_sparse";	/* Test program identifier.    */
+int TST_TOTAL=1;	/* Total number of test cases. */
+
+#define barrier() __asm__ __volatile__("": : :"memory")
+#define WITH_SIGNALS_BLOCKED(code) {											\
+		sigset_t held_sigs_;																	\
+		sigfillset(&held_sigs_);															\
+		sigprocmask(SIG_SETMASK, &held_sigs_, &held_sigs_);	\
+		barrier(); \
+		code;																									\
+		barrier(); \
+		sigprocmask(SIG_SETMASK, &held_sigs_, NULL);					\
+	}
 
 /*
  * aiodio_sparse - issue async O_DIRECT writes to holes is a file while
@@ -152,7 +170,11 @@ void aiodio_sparse(char *filename, int align, int writesize, int filesize, int n
 		}
 	}
 
-	fd = open(filename, O_DIRECT|O_WRONLY|O_CREAT, 0666);
+	WITH_SIGNALS_BLOCKED(
+		fd = open(filename, O_DIRECT|O_WRONLY|O_CREAT|O_EXCL, 0600);
+		if(fd > 0)
+			filename1=filename;
+	);
 
 	if (fd < 0) {
 		perror("cannot create file");
@@ -171,7 +193,11 @@ void aiodio_sparse(char *filename, int align, int writesize, int filesize, int n
 		if (posix_memalign(&bufptr, align, writesize)) {
 			perror("cannot malloc aligned memory");
 			close(fd);
-			unlink(filename);
+
+			WITH_SIGNALS_BLOCKED(
+				filename1=NULL;
+				unlink(filename);
+			);
 			return;
 		}
 		memset(bufptr, 0, writesize);
@@ -185,7 +211,11 @@ void aiodio_sparse(char *filename, int align, int writesize, int filesize, int n
 	if ((w = io_submit(myctx, num_aio, iocbs)) < 0) {
 		printf("io_submit failed error=%d\n", w);
 		close(fd);
-		unlink(filename);
+
+		WITH_SIGNALS_BLOCKED(
+			filename1=NULL;
+			unlink(filename);
+		);
 		return;
 	}
 	if (debug) 
@@ -270,7 +300,11 @@ void aiodio_sparse(char *filename, int align, int writesize, int filesize, int n
 	if (debug)
 		fprintf(stderr, "AIO DIO write done unlinking file\n");
 	close(fd);
-	unlink(filename);
+
+	WITH_SIGNALS_BLOCKED(
+		filename1=NULL;
+		unlink(filename);
+	);
 }
 
 
@@ -284,7 +318,13 @@ void dirty_freeblocks(int size)
 	pg = getpagesize();
 	size = ((size + pg - 1) / pg) * pg;
 	sprintf(filename, "file.xx.%d", getpid());
-	fd = open(filename, O_CREAT|O_RDWR, 0666);
+
+	WITH_SIGNALS_BLOCKED(
+		fd = open(filename, O_CREAT|O_RDWR|O_EXCL, 0600);
+		if(fd != -1)
+			filename2 = filename;
+	);
+
 	if (fd < 0) {
 		perror("cannot open file");
 		exit(2);
@@ -293,13 +333,22 @@ void dirty_freeblocks(int size)
 	p = mmap(0, size, PROT_WRITE|PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
 	if (p == MAP_FAILED) {
 		perror("cannot mmap");
+		close(fd);
+
+		WITH_SIGNALS_BLOCKED(
+			filename2=NULL;
+			unlink(filename);
+		);
 		exit(2);
 	}
 	memset(p, 0xaa, size);
 	msync(p, size, MS_SYNC);
 	munmap(p, size);
 	close(fd);
-	unlink(filename);
+	WITH_SIGNALS_BLOCKED(
+		filename2=NULL;
+		unlink(filename);
+	);
 }
 
 int usage()
@@ -399,6 +448,8 @@ int main(int argc, char **argv)
 		}
 	}
 
+	setup();
+
 	/*
 	 * Create some dirty free blocks by allocating, writing, syncing,
 	 * and then unlinking and freeing.
@@ -455,3 +506,19 @@ int main(int argc, char **argv)
 		exit(10);
 	return 0;
 }
+
+static void setup(void)
+{
+	tst_sig(FORK, DEF_HANDLER, cleanup);
+	signal(SIGTERM, cleanup);
+}
+
+static void cleanup(void)
+{
+	if(filename1)
+		unlink(filename1);
+	if(filename2)
+		unlink(filename2);
+
+	tst_exit();
+}
diff --git a/testcases/kernel/io/ltp-aiodio/dio_sparse.c b/testcases/kernel/io/ltp-aiodio/dio_sparse.c
index 78f1429..7bcf4a3 100644
--- a/testcases/kernel/io/ltp-aiodio/dio_sparse.c
+++ b/testcases/kernel/io/ltp-aiodio/dio_sparse.c
@@ -37,10 +37,31 @@
 #include <sys/mman.h>
 #include <sys/wait.h>
 
+#include "test.h"
+
 #define NUM_CHILDREN 1000
 
 int debug;
 
+const char* filename1=NULL;
+const char* filename2=NULL;
+
+static void setup(void);
+static void cleanup(void);
+
+#define barrier() __asm__ __volatile__("": : :"memory")
+#define WITH_SIGNALS_BLOCKED(code) {											\
+		sigset_t held_sigs_;																	\
+		sigfillset(&held_sigs_);															\
+		sigprocmask(SIG_SETMASK, &held_sigs_, &held_sigs_);	\
+		barrier(); \
+		code;																									\
+		barrier(); \
+		sigprocmask(SIG_SETMASK, &held_sigs_, NULL);					\
+	}
+
+char *TCID="dio_sparse";	/* Test program identifier.    */
+int TST_TOTAL=1;	/* Total number of test cases. */
 /*
  * dio_sparse - issue O_DIRECT writes to holes is a file while concurrently
  *	reading the file and checking that the read never reads uninitailized
@@ -130,7 +148,11 @@ void dio_sparse(char *filename, int align, int writesize, int filesize)
 	s.sa_flags = SA_SIGINFO;
 	sigaction(SIGTERM, &s, 0);
 
-	fd = open(filename, O_DIRECT|O_WRONLY|O_CREAT, 0666);
+	WITH_SIGNALS_BLOCKED(
+		fd = open(filename, O_DIRECT|O_WRONLY|O_CREAT|O_EXCL, 0600);
+		if(fd >= 0)
+			filename1 = filename;
+	);
 
 	if (fd < 0) {
 		perror("cannot create file");
@@ -141,6 +163,11 @@ void dio_sparse(char *filename, int align, int writesize, int filesize)
 
 	if (posix_memalign(&bufptr, align, writesize)) {
 		perror("cannot malloc aligned memory");
+		close(fd);
+		WITH_SIGNALS_BLOCKED(
+			filename1=NULL;
+			unlink(filename);
+		);
 		return;
 	}
 
@@ -163,7 +190,10 @@ void dio_sparse(char *filename, int align, int writesize, int filesize)
 	if (debug)
 		fprintf(stderr, "DIO write done unlinking file\n");
 	close(fd);
-	unlink(filename);
+	WITH_SIGNALS_BLOCKED(
+		filename1=NULL;
+		unlink(filename);
+	);
 }
 
 
@@ -177,7 +207,12 @@ void dirty_freeblocks(int size)
 	pg = getpagesize();
 	size = ((size + pg - 1) / pg) * pg;
 	sprintf(filename, "file.xx.%d", getpid());
-	fd = open(filename, O_CREAT|O_RDWR, 0666);
+
+	WITH_SIGNALS_BLOCKED(
+		fd = open(filename, O_CREAT|O_RDWR|O_EXCL, 0600);
+		if(fd >= 0)
+			filename2=filename;
+	);
 	if (fd < 0) {
 		perror("cannot open file");
 		exit(2);
@@ -186,13 +221,21 @@ void dirty_freeblocks(int size)
 	p = mmap(0, size, PROT_WRITE|PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
 	if (p == MAP_FAILED) {
 		perror("cannot mmap");
+		close(fd);
+		WITH_SIGNALS_BLOCKED(
+			filename2=NULL;
+			unlink(filename);
+		);
 		exit(2);
 	}
 	memset(p, 0xaa, size);
 	msync(p, size, MS_SYNC);
 	munmap(p, size);
 	close(fd);
-	unlink(filename);
+	WITH_SIGNALS_BLOCKED(
+		filename2=NULL;
+		unlink(filename);
+	);
 }
 
 int usage()
@@ -284,6 +327,7 @@ int main(int argc, char **argv)
 		}
 	}
 
+	setup();
 	/*
 	 * Create some dirty free blocks by allocating, writing, syncing,
 	 * and then unlinking and freeing.
@@ -339,3 +383,17 @@ int main(int argc, char **argv)
 		exit(10);
 	return 0;
 }
+
+static void setup(void)
+{
+	tst_sig(FORK, DEF_HANDLER, cleanup);
+	signal(SIGTERM, SIG_DFL);
+}
+
+static void cleanup(void)
+{
+	if(filename1)
+		unlink(filename1);
+	if(filename2)
+		unlink(filename2);
+}
-- 
1.6.0.2

Signed-off-by: Jiri Palecek <jpalecek@web.de>