Skip to contentSkip to conversation

syscalls/pwrite: pwrite02 new test library refactoring

PATCH New Cyril Hrubis 1 participant 4 messages Jan 23, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 syscalls/pwrite: pwrite02 new test library refactoring

From: Carlo Marcelo Arenas Belón <[email protected]>

---
 testcases/kernel/syscalls/pwrite/pwrite02.c | 188 +++++++++-------------------
 1 file changed, 56 insertions(+), 132 deletions(-)
testcases/kernel/syscalls/pwrite/pwrite02.c +56 -132
index d4592fb1d..df506afd6 100644
@@ -35,58 +35,33 @@
 
 #include <errno.h>
 #include <unistd.h>
-#include <fcntl.h>
-
-#include "test.h"
-#include "safe_macros.h"
-
-#define TEMPFILE	"pwrite_file"
-#define K1		1024
-
-TCID_DEFINE(pwrite02);
-
-static char write_buf[K1];
-
-static void setup(void);
-static void cleanup(void);
-
-static void test_espipe(void);
-static void test_einval(void);
-static void test_ebadf1(void);
-static void test_ebadf2(void);
-
-#if !defined(UCLINUX)
-static void test_efault(void);
-#endif
-
-static void (*testfunc[])(void) = {
-	test_espipe, test_einval, test_ebadf1, test_ebadf2,
-#if !defined(UCLINUX)
-	test_efault
-#endif
+#include <string.h>
+
+#include "tst_test.h"
+
+#define TEMPFILE "pwrite_file"
+#define BS 1024
+
+static int fd;
+static int fd_ro;
+static int inv_fd = -1;
+static int pipe_fds[2];
+static char buf[BS];
+
+static struct tcase {
+	void *buf;
+	size_t size;
+	int *fd;
+	off_t off;
+	int exp_errno;
+} tcases[] = {
+	{buf, sizeof(buf), &pipe_fds[1], 0, ESPIPE},
+	{buf, sizeof(buf), &fd, -1, EINVAL},
+	{buf, sizeof(buf), &inv_fd, 0, EBADF},
+	{buf, sizeof(buf), &fd_ro, 0, EBADF},
+	{NULL, sizeof(buf), &fd, 0, EFAULT},
 };
 
-int TST_TOTAL = ARRAY_SIZE(testfunc);
-
-int main(int ac, char **av)
-{
-	int i, lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++)
-			(*testfunc[i])();
-	}
-
-	cleanup();
-	tst_exit();
-}
-
 /*
  * sighandler - handle SIGXFSZ
  *
@@ -109,106 +84,55 @@ static void sighandler(int sig)
 	(void)ret;
 }
 
-static void setup(void)
+static void verify_pwrite(unsigned int i)
 {
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	/* see the comment in the sighandler() function */
-	/* call signal() to trap the signal generated */
-	if (signal(SIGXFSZ, sighandler) == SIG_ERR)
-		tst_brkm(TBROK, cleanup, "signal() failed");
+	struct tcase *tc = &tcases[i];
 
-	TEST_PAUSE;
+	TEST(pwrite(*tc->fd, tc->buf, BS, tc->off));
 
-	tst_tmpdir();
-
-	memset(write_buf, 'a', K1);
-}
-
-static void print_test_result(int err, int exp_errno)
-{
-	if (err == 0) {
-		tst_resm(TFAIL, "call succeeded unexpectedly");
+	if (TEST_RETURN >= 0) {
+		tst_res(TFAIL, "call succeeded unexpectedly");
 		return;
 	}
 
-	if (err == exp_errno) {
-		tst_resm(TPASS, "pwrite failed as expected: %d - %s",
-			 err, strerror(err));
-	} else {
-		tst_resm(TFAIL, "pwrite failed unexpectedly; expected: %d - %s"
-			 "return: %d - %s", exp_errno, strerror(exp_errno),
-			 err, strerror(err));
+	if (TEST_ERRNO != tc->exp_errno) {
+		tst_res(TFAIL | TTERRNO,
+			"pwrite failed unexpectedly, expected %s",
+			tst_strerrno(tc->exp_errno));
 	}
-}
 
-static void test_espipe(void)
-{
-	int pipe_fds[2];
-
-	SAFE_PIPE(cleanup, pipe_fds);
-
-	TEST(pwrite(pipe_fds[1], write_buf, K1, 0));
-
-	print_test_result(errno, ESPIPE);
-
-	SAFE_CLOSE(cleanup, pipe_fds[0]);
-	SAFE_CLOSE(cleanup, pipe_fds[1]);
-}
-
-static void test_einval(void)
-{
-	int fd;
-
-	fd = SAFE_OPEN(cleanup, TEMPFILE, O_RDWR | O_CREAT, 0666);
-
-	/* the specified offset was invalid */
-	TEST(pwrite(fd, write_buf, K1, -1));
-
-	print_test_result(errno, EINVAL);
-
-	SAFE_CLOSE(cleanup, fd);
-}
-
-static void test_ebadf1(void)
-{
-	int fd = -1;
-
-	TEST(pwrite(fd, write_buf, K1, 0));
-
-	print_test_result(errno, EBADF);
+	tst_res(TPASS | TTERRNO, "pwrite failed as expected");
 }
 
-static void test_ebadf2(void)
+static void setup(void)
 {
-	int fd;
-
-	fd = SAFE_OPEN(cleanup, TEMPFILE, O_RDONLY | O_CREAT, 0666);
-
-	TEST(pwrite(fd, write_buf, K1, 0));
+	SAFE_SIGNAL(SIGXFSZ, sighandler);
 
-	print_test_result(errno, EBADF);
+	SAFE_PIPE(pipe_fds);
 
-	SAFE_CLOSE(cleanup, fd);
+	fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
+	fd_ro = SAFE_OPEN(TEMPFILE, O_RDONLY | O_CREAT, 0666);
 }
 
-#if !defined(UCLINUX)
-static void test_efault(void)
+static void cleanup(void)
 {
-	int fd;
-	char *buf = sbrk(0);
-
-	fd = SAFE_OPEN(cleanup, TEMPFILE, O_RDWR | O_CREAT, 0666);
+	if (fd > 0)
+		SAFE_CLOSE(fd);
 
-	TEST(pwrite(fd, buf, K1, 0));
+	if (fd_ro > 0)
+		SAFE_CLOSE(fd_ro);
 
-	print_test_result(errno, EFAULT);
+	if (pipe_fds[0] > 0)
+		SAFE_CLOSE(pipe_fds[0]);
 
-	SAFE_CLOSE(cleanup, fd);
+	if (pipe_fds[1] > 0)
+		SAFE_CLOSE(pipe_fds[1]);
 }
-#endif
 
-static void cleanup(void)
-{
-	tst_rmdir();
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_pwrite,
+	.tcnt = ARRAY_SIZE(tcases),
+};
Signature · 1 line
2.13.6
#1 lore raw

PATCH 2/4 syscalls/write: write04 new test library refactoring

From: Carlo Marcelo Arenas Belón <[email protected]>

---
 testcases/kernel/syscalls/write/write04.c | 223 +++++-------------------------
 1 file changed, 38 insertions(+), 185 deletions(-)
testcases/kernel/syscalls/write/write04.c +38 -185
index d6a51460c..36ef97e8a 100644
@@ -18,31 +18,12 @@
  */
 
 /*
- * NAME
- *	write04.c
- *
  * DESCRIPTION
  *	Testcase to check that write() sets errno to EAGAIN
  *
  * ALGORITHM
  *	Create a named pipe (fifo), open it in O_NONBLOCK mode, and
  *	attempt to write to it when it is full, write(2) should fail
- *	with EAGAIN.
- *
- * USAGE:  <for command-line>
- *      write04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *      where,  -c n : Run n copies concurrently.
- *              -e   : Turn on errno logging.
- *              -i n : Execute test n times.
- *              -I x : Execute test for x seconds.
- *              -P x : Pause for x seconds between iterations.
- *              -t   : Turn on syscall timing.
- *
- * HISTORY
- *      ??/???? someone made this testcase but didn't add HISTORY
- *
- * RESTRICTIONS
- *	NONE
  */
 
 #include <sys/stat.h>
@@ -51,189 +32,61 @@
 #include <setjmp.h>
 #include <errno.h>
 #include <string.h>
-#include "test.h"
+#include <stdio.h>
+#include "tst_test.h"
 
-#define PIPE_SIZE_TEST getpagesize()
+static char fifo[100];
+static int rfd, wfd;
+static long page_size;
 
-void alarm_handler();
-void setup();
-void cleanup();
+static void verify_write(void)
+{
+	char wbuf[8 * page_size];
 
-char *TCID = "write04";
-int TST_TOTAL = 1;
+	TEST(write(wfd, wbuf, sizeof(wbuf)));
 
-char fifo[100] = "fifo";
-static sigjmp_buf jmp;
-int rfd, wfd;
+	if (TEST_RETURN != -1) {
+		tst_res(TFAIL, "write() succeeded unexpectedly");
+		return;
+	}
 
-int main(int argc, char **argv)
-{
-	int lc;
-
-	struct stat buf;
-	int fail;
-	int cnt;
-	char wbuf[17 * PIPE_SIZE_TEST];
-	struct sigaction sigptr;	/* set up signal handler */
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	/* global setup */
-	setup();
-
-	/*
-	 * The following loop checks looping state if -i option given
-	 */
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		/* reset tst_count in case we are looping */
-		tst_count = 0;
-
-		if (mknod(fifo, S_IFIFO | 0777, 0) < 0) {
-			tst_resm(TBROK, "mknod() failed, errno: %d", errno);
-			cleanup();
-		}
-		if (stat(fifo, &buf) != 0) {
-			tst_resm(TBROK, "stat() failed, errno: %d", errno);
-			cleanup();
-		}
-		if ((buf.st_mode & S_IFIFO) == 0) {
-			tst_resm(TBROK, "Mode does not indicate fifo file");
-			cleanup();
-		}
-#if 0
-		sigset(SIGALRM, alarm_handler);
-#endif
-		sigptr.sa_handler = (void (*)(int signal))alarm_handler;
-		sigfillset(&sigptr.sa_mask);
-		sigptr.sa_flags = 0;
-		sigaddset(&sigptr.sa_mask, SIGALRM);
-		if (sigaction(SIGALRM, &sigptr, NULL) == -1) {
-			tst_resm(TBROK, "sigaction(): Failed");
-			cleanup();
-		}
-//block1:
-		tst_resm(TINFO, "Enter block 1: test for EAGAIN in write()");
-		fail = 0;
-
-		(void)memset((void *)wbuf, 'A', 17 * PIPE_SIZE_TEST);
-
-		/*
-		 * open the read end of the pipe
-		 */
-		if (sigsetjmp(jmp, 1)) {
-			tst_resm(TBROK, "Error reading fifo, read blocked");
-			fail = 1;
-		}
-		(void)alarm(10);	/* set alarm for 10 seconds */
-		rfd = open(fifo, O_RDONLY | O_NONBLOCK);
-		(void)alarm(0);
-		if (rfd < 0) {
-			tst_resm(TBROK, "open() for reading the pipe failed");
-			fail = 1;
-		}
-
-		/*
-		 * open the write end of the pipe
-		 */
-		if (sigsetjmp(jmp, 1)) {
-			tst_resm(TBROK, "setjmp() failed");
-			cleanup();
-		}
-		(void)alarm(10);	/* set alarm for 10 seconds */
-		wfd = open(fifo, O_WRONLY | O_NONBLOCK);
-		(void)alarm(0);
-		if (wfd < 0) {
-			tst_resm(TBROK, "open() for writing the pipe failed");
-			fail = 1;
-		}
-
-		/*
-		 * attempt to fill the pipe with some data
-		 */
-		if (sigsetjmp(jmp, 1)) {
-			tst_resm(TBROK, "sigsetjmp() failed");
-			fail = 1;
-		}
-		(void)alarm(10);
-		cnt = write(wfd, wbuf, 17 * PIPE_SIZE_TEST);
-		(void)alarm(0);
-		if (cnt == 17 * PIPE_SIZE_TEST) {
-			tst_resm(TBROK, "Error reading fifo, nozero read");
-			fail = 1;
-		}
-
-		/*
-		 * Now that the fifo is full try and send some more
-		 */
-		if (sigsetjmp(jmp, 1)) {
-			tst_resm(TBROK, "sigsetjmp() failed");
-			fail = 1;
-		}
-		(void)alarm(10);
-		cnt = write(wfd, wbuf, 8 * PIPE_SIZE_TEST);
-		(void)alarm(0);
-		if (cnt != -1) {
-			tst_resm(TBROK, "write() failed to fail when pipe "
-				 "is full");
-			fail = 1;
-		} else {
-			if (errno != EAGAIN) {
-				tst_resm(TBROK, "write set bad errno, expected "
-					 "EAGAIN, got %d", errno);
-				fail = 1;
-			}
-			tst_resm(TINFO, "read() succeded in setting errno to "
-				 "EAGAIN");
-		}
-		if (fail) {
-			tst_resm(TFAIL, "Block 1 FAILED");
-		} else {
-			tst_resm(TPASS, "Block 1 PASSED");
-		}
-		tst_resm(TINFO, "Exit block 1");
-
-		/* unlink fifo in case we are looping. */
-		unlink(fifo);
+	if (TEST_ERRNO != EAGAIN) {
+		tst_res(TFAIL | TTERRNO,
+			"write() failed unexpectedly, expected EAGAIN");
+		return;
 	}
-	cleanup();
-	tst_exit();
-}
 
-void alarm_handler(void)
-{
-	siglongjmp(jmp, 1);
+	tst_res(TPASS | TTERRNO, "write() failed expectedly");
 }
 
-/*
- * setup()
- *	performs all ONE TIME setup for this test
- */
-void setup(void)
+static void setup(void)
 {
+	page_size = getpagesize();
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
+	char wbuf[17 * page_size];
 
-	/* Pause if that option was specified
-	 * TEST_PAUSE contains the code to fork the test with the -i option.
-	 * You want to make sure you do this before you create your temporary
-	 * directory.
-	 */
-	TEST_PAUSE;
+	sprintf(fifo, "%s.%d", fifo, getpid());
 
-	/* Create a unique temporary directory and chdir() to it. */
-	tst_tmpdir();
+	SAFE_MKNOD(fifo, S_IFIFO | 0777, 0);
 
-	/* create a temporary filename */
-	sprintf(fifo, "%s.%d", fifo, getpid());
+	rfd = SAFE_OPEN(fifo, O_RDONLY | O_NONBLOCK);
+	wfd = SAFE_OPEN(fifo, O_WRONLY | O_NONBLOCK);
 
+	SAFE_WRITE(0, wfd, wbuf, sizeof(wbuf));
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
+	if (rfd > 0)
+		SAFE_CLOSE(rfd);
 
-	close(rfd);
-	close(wfd);
-	unlink(fifo);
-	tst_rmdir();
-
+	if (wfd > 0)
+		SAFE_CLOSE(wfd);
 }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_write,
+};
Signature · 1 line
2.13.6
#2 lore raw

PATCH 3/4 syscalls/write: write05 new test library refactoring

In reply to Cyril Hrubis

From: Carlo Marcelo Arenas Belón <[email protected]>

---
 testcases/kernel/syscalls/write/write05.c | 235 +++++++++---------------------
 1 file changed, 70 insertions(+), 165 deletions(-)
testcases/kernel/syscalls/write/write05.c +70 -165
index bcbe85874..ec157ae28 100644
@@ -1,6 +1,9 @@
 /*
  *
  *   Copyright (c) International Business Machines  Corp., 2001
+ *	07/2001 Ported by John George
+ *      04/2002 wjhuie sigset cleanups
+ *      08/2007 Ricardo Salveti de Araujo <[email protected]>
  *
  *   This program is free software;  you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
@@ -18,199 +21,101 @@
  */
 
 /*
- * NAME
- *	write05.c
- *
  * DESCRIPTION
  *	Check the return value, and errnos of write(2)
  *	- when the file descriptor is invalid - EBADF
  *	- when the buf parameter is invalid - EFAULT
  *	- on an attempt to write to a pipe that is not open for reading - EPIPE
- *
- * ALGORITHM
- *	Attempt to write on a file with negative file descriptor, check for -1
- *	Attempt to write on a file with invalid buffer, check for -1
- *	Open a pipe and close the read end, attempt to write to the write
- *	end, check for -1.
- *
- * USAGE:  <for command-line>
- *      write05 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- *      where,  -c n : Run n copies concurrently.
- *              -e   : Turn on errno logging.
- *              -i n : Execute test n times.
- *              -I x : Execute test for x seconds.
- *              -P x : Pause for x seconds between iterations.
- *              -t   : Turn on syscall timing.
- *
- * History
- *	07/2001 John George
- *		-Ported
- *      04/2002 wjhuie sigset cleanups
- *      08/2007 Ricardo Salveti de Araujo <[email protected]>
- *		- Closing the fd before removing the file
- *
- * Restrictions
- *	None
  */
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/wait.h>
 #include <sys/mman.h>
-#include "test.h"
-
-void setup(void);
-void cleanup(void);
-
-char *TCID = "write05";
-int TST_TOTAL = 1;
-char filename[100];
-int fd;
-
-char *bad_addr = 0;
-
-int main(int argc, char **argv)
+#include "tst_test.h"
+
+static int fd;
+static int inv_fd = -1;
+static char b[32];
+static char *buf = b;
+static char *bad_addr;
+static int pipefd[2];
+
+static struct tcase {
+	int *fd;
+	char **buf;
+	size_t size;
+	int exp_errno;
+} tcases[] = {
+	{&inv_fd, &buf, sizeof(buf), EBADF},
+	{&fd, &bad_addr, sizeof(buf), EFAULT},
+	{&pipefd[1], &buf, sizeof(buf), EPIPE},
+};
+
+static int sigpipe_cnt;
+
+static void sighandler(int sig)
 {
-	int lc;
-
-	char pbuf[BUFSIZ];
-	int pipefildes[2];
-	int status, pid;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	/* global setup */
-	setup();
-
-	/* The following loop checks looping state if -i option given */
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		/* reset tst_count in case we are looping */
-		tst_count = 0;
-
-//block1:
-		tst_resm(TINFO, "Enter Block 1: test with bad fd");
-		if (write(-1, pbuf, 1) != -1) {
-			tst_resm(TFAIL, "write of invalid fd passed");
-		} else {
-			if (errno != EBADF) {
-				tst_resm(TFAIL, "expected EBADF got %d", errno);
-			}
-			tst_resm(TPASS, "received EBADF as expected.");
-		}
-		tst_resm(TINFO, "Exit Block 1");
-
-//block2:
-		tst_resm(TINFO, "Enter Block 2: test with a bad address");
-		fd = creat(filename, 0644);
-		if (fd < 0) {
-			tst_resm(TFAIL, "creating a new file failed");
-			cleanup();
-		}
-		if (write(fd, bad_addr, 10) != -1) {
-			tst_resm(TFAIL, "write() on an invalid buffer "
-				 "succeeded, but should have failed");
-			cleanup();
-		} else {
-			if (errno != EFAULT) {
-				tst_resm(TFAIL, "write() returned illegal "
-					 "errno: expected EFAULT, got %d",
-					 errno);
-				cleanup();
-			}
-			tst_resm(TPASS, "received EFAULT as expected.");
-		}
-		tst_resm(TINFO, "Exit Block 2");
-
-//block3:
-		tst_resm(TINFO, "Enter Block 3: test with invalid pipe");
-		if ((pid = FORK_OR_VFORK()) == 0) {	/* child */
-			if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
-				tst_resm(TINFO, "signal failed");
-			}
-			if (pipe(pipefildes) == -1) {
-				tst_brkm(TBROK, NULL, "can't open pipe");
-				exit(errno);
-			}
-			close(pipefildes[0]);
-			if (write(pipefildes[1], pbuf, 1) != -1) {
-				tst_resm(TFAIL, "write on read-closed"
-					 "pipe succeeded");
-				exit(-1);
-			} else {
-				if (errno != EPIPE) {
-					tst_resm(TFAIL, "write() failed to set"
-						 " errno to EPIPE, got: %d",
-						 errno);
-					exit(errno);
-				}
-				exit(0);
-			}
-		} else {
-			if (pid < 0) {
-				tst_resm(TFAIL, "Fork failed");
-			}
-			wait(&status);
-			if (WIFSIGNALED(status) &&
-				WTERMSIG(status) == SIGPIPE) {
-				tst_resm(TFAIL, "child set SIGPIPE in exit");
-			} else if (WEXITSTATUS(status) != 0) {
-				tst_resm(TFAIL, "exit status from child "
-					 "expected 0 got %d", status >> 8);
-			} else {
-				tst_resm(TPASS, "received EPIPE as expected.");
-			}
-			tst_resm(TINFO, "Exit Block 3");
-		}
-		close(fd);
-	}
-	cleanup();
-	tst_exit();
+	if (sig == SIGPIPE)
+		sigpipe_cnt++;
 }
 
-/*
- * setup()
- *	performs all ONE TIME setup for this test
- */
-void setup(void)
+static void verify_write(unsigned int i)
 {
+	struct tcase *tc = &tcases[i];
 
-	tst_sig(FORK, DEF_HANDLER, cleanup);
+	sigpipe_cnt = 0;
 
-	/* Pause if that option was specified
-	 * TEST_PAUSE contains the code to fork the test with the -i option.
-	 * You want to make sure you do this before you create your temporary
-	 * directory.
-	 */
-	TEST_PAUSE;
+	TEST(write(*tc->fd, *tc->buf, tc->size));
 
-	/* Create a unique temporary directory and chdir() to it. */
-	tst_tmpdir();
+	if (TEST_RETURN != -1) {
+		tst_res(TFAIL, "write() succeeded unexpectedly");
+		return;
+	}
 
-	sprintf(filename, "write05.%d", getpid());
+	if (TEST_ERRNO != tc->exp_errno) {
+		tst_res(TFAIL | TTERRNO,
+			"write() failed unexpectedly, expected %s",
+			tst_strerrno(tc->exp_errno));
+		return;
+	}
 
-	bad_addr = mmap(0, 1, PROT_NONE,
-			MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
-	if (bad_addr == MAP_FAILED) {
-		printf("mmap failed\n");
+	if (tc->exp_errno == EPIPE && sigpipe_cnt != 1) {
+		tst_res(TFAIL, "sigpipe_cnt = %i", sigpipe_cnt);
+		return;
 	}
 
+	tst_res(TPASS | TTERRNO, "write() failed expectedly");
 }
 
-/*
- * cleanup()
- *	performs all ONE TIME cleanup for this test at
- *	completion or premature exit
- */
-void cleanup(void)
+static void setup(void)
 {
+	fd = SAFE_OPEN("write_test", O_RDWR | O_CREAT, 0644);
+
+	bad_addr = SAFE_MMAP(0, 1, PROT_NONE,
+			MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+
+	SAFE_PIPE(pipefd);
+	SAFE_CLOSE(pipefd[0]);
 
-	/* Close the file descriptor befor removing the file */
-	close(fd);
+	SAFE_SIGNAL(SIGPIPE, sighandler);
+}
 
-	unlink(filename);
-	tst_rmdir();
+static void cleanup(void)
+{
+	if (fd > 0)
+		SAFE_CLOSE(fd);
 
+	SAFE_MUNMAP(bad_addr, 1);
 }
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_write,
+	.tcnt = ARRAY_SIZE(tcases),
+};
Signature · 1 line
2.13.6
#3 lore raw

PATCH 4/4 syscalls/[p]write: check for special case with NULL buffer and 0 nsize

In reply to Cyril Hrubis

From: Carlo Marcelo Arenas Belón <[email protected]>

As documented, calling write with 0 as nsize is valid and should return 0.

Passing NULL as a buffer is also valid, and when combined with a size of 0 has been shown historically to result in a returned value of 0.

SysV and BSD make a point to always return 0, regardless of the buffer and therefore some software expects a call like write(fd, NULL, 0) to never fail, so test for that combination as an exception to this test that looks for an EFAULT.

---
 runtest/syscalls                            |  3 ++
 testcases/kernel/syscalls/.gitignore        |  3 ++
 testcases/kernel/syscalls/pwrite/pwrite03.c | 56 +++++++++++++++++++++++++++++
 testcases/kernel/syscalls/write/write02.c   | 56 +++++++++++++++++++++++++++++
 4 files changed, 118 insertions(+)
 create mode 100644 testcases/kernel/syscalls/pwrite/pwrite03.c
 create mode 100644 testcases/kernel/syscalls/write/write02.c
runtest/syscalls +3 -0
index 94efd2305..2a4fad0a8 100644
@@ -851,10 +851,12 @@ ptrace07 ptrace07
 
 pwrite01 pwrite01
 pwrite02 pwrite02
+pwrite03 pwrite03
 pwrite04 pwrite04
 
 pwrite01_64 pwrite01_64
 pwrite02_64 pwrite02_64
+pwrite03_64 pwrite03_64
 pwrite04_64 pwrite04_64
 
 pwritev01 pwritev01
@@ -1455,6 +1457,7 @@ waitid01 waitid01
 waitid02 waitid02
 
 write01 write01
+write02 write02
 write03 write03
 write04 write04
 write05 write05
testcases/kernel/syscalls/.gitignore +3 -0
index 061948125..67211cad4 100644
@@ -716,6 +716,8 @@
 /pwrite/pwrite01_64
 /pwrite/pwrite02
 /pwrite/pwrite02_64
+/pwrite/pwrite03
+/pwrite/pwrite03_64
 /pwrite/pwrite04
 /pwrite/pwrite04_64
 /pwritev/pwritev01
@@ -1123,6 +1125,7 @@
 /waitpid/waitpid12
 /waitpid/waitpid13
 /write/write01
+/write/write02
 /write/write03
 /write/write04
 /write/write05
testcases/kernel/syscalls/pwrite/pwrite03.c new file +56 -0

56 lines not on this page · raw message

testcases/kernel/syscalls/write/write02.c new file +56 -0

56 lines not on this page · raw message

Signature · 1 line
2.13.6