Module Name:    src
Committed By:   pgoyette
Date:           Wed Nov  7 14:00:38 UTC 2012

Modified Files:
        src/tests/kernel: Makefile
Added Files:
        src/tests/kernel: t_lockf.c

Log Message:
Add ATF version of the file locking test


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/tests/kernel/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/kernel/t_lockf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/tests/kernel/Makefile
diff -u src/tests/kernel/Makefile:1.29 src/tests/kernel/Makefile:1.30
--- src/tests/kernel/Makefile:1.29	Wed Nov  7 05:13:45 2012
+++ src/tests/kernel/Makefile	Wed Nov  7 14:00:38 2012
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.29 2012/11/07 05:13:45 nakayama Exp $
+# $NetBSD: Makefile,v 1.30 2012/11/07 14:00:38 pgoyette Exp $
 
 NOMAN=		# defined
 
@@ -8,6 +8,7 @@ TESTSDIR=	${TESTSBASE}/kernel
 
 TESTS_SUBDIRS=	kqueue
 TESTS_C=	t_lock
+TESTS_C=	t_lockf
 TESTS_C+=	t_pty
 TESTS_C+=	t_mqueue
 TESTS_C+=	t_sysv

Added files:

Index: src/tests/kernel/t_lockf.c
diff -u /dev/null src/tests/kernel/t_lockf.c:1.1
--- /dev/null	Wed Nov  7 14:00:39 2012
+++ src/tests/kernel/t_lockf.c	Wed Nov  7 14:00:38 2012
@@ -0,0 +1,247 @@
+/*	$NetBSD: t_lockf.c,v 1.1 2012/11/07 14:00:38 pgoyette Exp $	*/
+
+/*-
+ * Copyright (c) 2000 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <atf-c.h>
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/ptrace.h> 
+
+/*
+ * lockf1 regression test:
+ *
+ * Tests:
+ * Fork N child processes, each of which gets M random byte range locks
+ * on a common file.  We ignore all lock errors (practically speaking,
+ * this means EDEADLK or ENOLOCK), but we make numerous passes over all
+ * the children to make sure that they are still awake.  (We do this by
+ * verifying that we can ptrace(ATTACH/DETACH) to the children and get
+ * their status via waitpid().)
+ * When finished, reap all the children.
+ */
+
+int nlocks = 500;		/* number of locks per thread */
+int nprocs = 10;		/* number of processes to spawn */
+int sleeptime = 150000;		/* sleep time between locks, usec */
+off_t size = 8192;		/* size of file to lock */
+
+const char *lockfile = "lockf_test";
+
+static u_int32_t
+random_uint32(void)
+{
+	return lrand48();
+}
+
+static void
+trylocks(int id)
+{
+	int i, ret, fd;
+
+	srand48(getpid());
+
+	fd = open (lockfile, O_RDWR, 0);
+        
+	if (fd < 0)
+		err(1, "%s", lockfile);
+
+	printf("%d: start\n", id);
+
+	for (i=0; i<nlocks; i++) {
+		struct flock fl;
+
+		fl.l_start = random_uint32() % size;
+		fl.l_len = random_uint32() % size;
+		switch (random_uint32() % 3) {
+		case 0:
+			fl.l_type = F_RDLCK;
+			break;
+		case 1:
+			fl.l_type = F_WRLCK;
+			break;
+		case 2:
+			fl.l_type = F_UNLCK;
+			break;
+		}
+		fl.l_whence = SEEK_SET;
+
+		ret = fcntl(fd, F_SETLKW, &fl);
+
+		if (usleep(sleeptime) < 0) 
+		  err(1, "usleep");
+	}
+	printf("%d: done\n", id);
+	close (fd);
+}
+
+ATF_TC(randlock);
+ATF_TC_HEAD(randlock, tc)
+{  
+
+	atf_tc_set_md_var(tc, "timeout", "300");
+	atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) locking");
+}
+
+ATF_TC_BODY(randlock, tc)
+{
+	int i, j, fd;
+	pid_t *pid;
+	int status;
+
+	(void)unlink(lockfile);
+
+	fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
+	ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
+
+	ATF_REQUIRE_MSG(ftruncate(fd, size) >= 0,
+	    "ftruncate(%s): %s", lockfile, strerror(errno));
+
+	fsync(fd);
+	close(fd);
+
+	pid = malloc(nprocs * sizeof(pid_t));
+	
+	for (i=0; i<nprocs; i++) {
+		pid[i] = fork();
+		switch (pid[i]) {
+		case 0:
+			trylocks(i);
+			_exit(0);
+			break;
+		case -1:
+			atf_tc_fail("fork %d failed", i);
+			break;
+		default:
+			break;
+		}
+	}
+	for (j=0; j<100; j++) {
+		printf("parent: run %i\n", j+1);
+		for (i=0; i<nprocs; i++) {
+			ATF_REQUIRE_MSG(ptrace(PT_ATTACH, pid[i], 0, 0) >= 0,
+			    "ptrace attach %d", pid[i]);
+			ATF_REQUIRE_MSG(waitpid(pid[i], &status, WUNTRACED) >= 0,
+			    "waitpid(ptrace)");
+			usleep(sleeptime/3);
+			ATF_REQUIRE_MSG(ptrace(PT_DETACH, pid[i], (caddr_t)1,
+					       0) >= 0,
+			    "ptrace detach %d", pid[i]);
+			usleep(sleeptime/3);
+		}
+	}
+	for (i=0; i<nprocs; i++) {
+		printf("reap %d: ", i);
+		fflush(stdout);
+		kill(pid[i], SIGINT);
+		waitpid(pid[i], &status, 0);
+		printf(" status %d\n", status);
+	}
+	atf_tc_pass();
+}
+
+static int
+dolock(int fd, int op, off_t lk_off, off_t lk_size)
+{
+	off_t result;
+	int ret;
+
+	result = lseek(fd, lk_off, SEEK_SET);
+	if (result == -1) {
+		return errno;
+	}
+	ATF_REQUIRE_MSG(result == lk_off, "lseek to wrong offset");
+	ret = lockf(fd, op, lk_size);
+	if (ret == -1) {
+		return errno;
+	}
+	return 0;
+}
+
+ATF_TC(deadlock);
+ATF_TC_HEAD(deadlock, tc)
+{  
+
+	atf_tc_set_md_var(tc, "timeout", "30");
+	atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) deadlock detection");
+}
+
+ATF_TC_BODY(deadlock, tc)
+{
+	int fd;
+	int error;
+	int ret;
+	pid_t pid;
+
+	(void)unlink(lockfile);
+
+	fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
+	ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
+
+	ATF_REQUIRE_MSG(ftruncate(fd, size) >= 0,
+	    "ftruncate(%s): %s", lockfile, strerror(errno));
+
+	fsync(fd);
+
+	error = dolock(fd, F_LOCK, 0, 1);
+	ATF_REQUIRE_MSG(error == 0, "initial dolock: %s", strerror(errno));
+
+	pid = fork();
+	ATF_REQUIRE_MSG(pid != -1, "fork failed: %s", strerror(errno));
+	if (pid == 0) {
+		error = dolock(fd, F_LOCK, 1, 1);
+		ATF_REQUIRE_MSG(error == 0, "child dolock: %s",
+		    strerror(errno));
+		dolock(fd, F_LOCK, 0, 1);	/* will block */
+		atf_tc_fail("child did not block");
+	}
+	sleep(1);	/* give child time to grab its lock then block */
+
+	error = dolock(fd, F_LOCK, 1, 1);
+	ATF_CHECK_MSG(error != EDEADLK, "parent did not deadlock: %s",
+	    strerror(errno));
+	ret = kill(pid, SIGKILL);
+	ATF_REQUIRE_MSG(ret != -1, "failed to kill child: %s", strerror(errno));
+
+	atf_tc_pass();
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, randlock); 
+	ATF_TP_ADD_TC(tp, deadlock); 
+
+	return atf_no_error();
+}

Reply via email to