xref: /netbsd-src/tests/kernel/t_lockf.c (revision d56fcfc956401ef8b3dad803db409f1910ae4dc9)
1*d56fcfc9Schristos /*	$NetBSD: t_lockf.c,v 1.9 2013/10/19 17:45:00 christos Exp $	*/
272067301Spgoyette 
372067301Spgoyette /*-
472067301Spgoyette  * Copyright (c) 2000 The NetBSD Foundation, Inc.
572067301Spgoyette  * All rights reserved.
672067301Spgoyette  *
772067301Spgoyette  * Redistribution and use in source and binary forms, with or without
872067301Spgoyette  * modification, are permitted provided that the following conditions
972067301Spgoyette  * are met:
1072067301Spgoyette  * 1. Redistributions of source code must retain the above copyright
1172067301Spgoyette  *    notice, this list of conditions and the following disclaimer.
1272067301Spgoyette  * 2. Redistributions in binary form must reproduce the above copyright
1372067301Spgoyette  *    notice, this list of conditions and the following disclaimer in the
1472067301Spgoyette  *    documentation and/or other materials provided with the distribution.
1572067301Spgoyette  *
1672067301Spgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1772067301Spgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1872067301Spgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1972067301Spgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2072067301Spgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2172067301Spgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2272067301Spgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2372067301Spgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2472067301Spgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2572067301Spgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2672067301Spgoyette  * POSSIBILITY OF SUCH DAMAGE.
2772067301Spgoyette  */
2872067301Spgoyette 
2972067301Spgoyette #include <atf-c.h>
3072067301Spgoyette #include <err.h>
3172067301Spgoyette #include <errno.h>
3272067301Spgoyette #include <fcntl.h>
3372067301Spgoyette #include <signal.h>
3472067301Spgoyette #include <stdio.h>
3572067301Spgoyette #include <stdlib.h>
3672067301Spgoyette #include <string.h>
3772067301Spgoyette #include <unistd.h>
3872067301Spgoyette 
3972067301Spgoyette #include <sys/types.h>
4072067301Spgoyette #include <sys/wait.h>
4172067301Spgoyette #include <sys/ptrace.h>
4272067301Spgoyette 
4372067301Spgoyette /*
4472067301Spgoyette  * lockf1 regression test:
4572067301Spgoyette  *
4672067301Spgoyette  * Tests:
4772067301Spgoyette  * Fork N child processes, each of which gets M random byte range locks
4872067301Spgoyette  * on a common file.  We ignore all lock errors (practically speaking,
4972067301Spgoyette  * this means EDEADLK or ENOLOCK), but we make numerous passes over all
5072067301Spgoyette  * the children to make sure that they are still awake.  (We do this by
5172067301Spgoyette  * verifying that we can ptrace(ATTACH/DETACH) to the children and get
5272067301Spgoyette  * their status via waitpid().)
5372067301Spgoyette  * When finished, reap all the children.
5472067301Spgoyette  */
5572067301Spgoyette 
567dec8010Spgoyette #define	nlocks		500	/* number of locks per thread */
577dec8010Spgoyette #define	nprocs		10	/* number of processes to spawn */
587dec8010Spgoyette #define	npasses		50	/* number of passes to make over the children */
597dec8010Spgoyette #define	sleeptime	150000	/* sleep time between locks, usec */
607dec8010Spgoyette #define	filesize 	8192	/* size of file to lock */
6172067301Spgoyette 
6272067301Spgoyette const char *lockfile = "lockf_test";
6372067301Spgoyette 
6472067301Spgoyette static u_int32_t
random_uint32(void)6572067301Spgoyette random_uint32(void)
6672067301Spgoyette {
6772067301Spgoyette 	return lrand48();
6872067301Spgoyette }
6972067301Spgoyette 
7072067301Spgoyette static void
trylocks(int id)7172067301Spgoyette trylocks(int id)
7272067301Spgoyette {
73*d56fcfc9Schristos 	int i, fd;
7472067301Spgoyette 
7572067301Spgoyette 	srand48(getpid());
7672067301Spgoyette 
7772067301Spgoyette 	fd = open (lockfile, O_RDWR, 0);
7872067301Spgoyette 
7972067301Spgoyette 	if (fd < 0)
8072067301Spgoyette 		err(1, "%s", lockfile);
8172067301Spgoyette 
8272067301Spgoyette 	printf("%d: start\n", id);
8372067301Spgoyette 
8472067301Spgoyette 	for (i = 0; i < nlocks; i++) {
8572067301Spgoyette 		struct flock fl;
8672067301Spgoyette 
877dec8010Spgoyette 		fl.l_start = random_uint32() % filesize;
887dec8010Spgoyette 		fl.l_len = random_uint32() % filesize;
8972067301Spgoyette 		switch (random_uint32() % 3) {
9072067301Spgoyette 		case 0:
9172067301Spgoyette 			fl.l_type = F_RDLCK;
9272067301Spgoyette 			break;
9372067301Spgoyette 		case 1:
9472067301Spgoyette 			fl.l_type = F_WRLCK;
9572067301Spgoyette 			break;
9672067301Spgoyette 		case 2:
9772067301Spgoyette 			fl.l_type = F_UNLCK;
9872067301Spgoyette 			break;
9972067301Spgoyette 		}
10072067301Spgoyette 		fl.l_whence = SEEK_SET;
10172067301Spgoyette 
102*d56fcfc9Schristos 		(void)fcntl(fd, F_SETLKW, &fl);
10372067301Spgoyette 
10472067301Spgoyette 		if (usleep(sleeptime) < 0)
10572067301Spgoyette 		  err(1, "usleep");
10672067301Spgoyette 	}
10772067301Spgoyette 	printf("%d: done\n", id);
10872067301Spgoyette 	close (fd);
10972067301Spgoyette }
11072067301Spgoyette 
11172067301Spgoyette ATF_TC(randlock);
ATF_TC_HEAD(randlock,tc)11272067301Spgoyette ATF_TC_HEAD(randlock, tc)
11372067301Spgoyette {
11472067301Spgoyette 
11572067301Spgoyette 	atf_tc_set_md_var(tc, "timeout", "300");
11672067301Spgoyette 	atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) locking");
11772067301Spgoyette }
11872067301Spgoyette 
ATF_TC_BODY(randlock,tc)11972067301Spgoyette ATF_TC_BODY(randlock, tc)
12072067301Spgoyette {
12172067301Spgoyette 	int i, j, fd;
122a3217a37Spgoyette 	int pipe_fd[2];
12372067301Spgoyette 	pid_t *pid;
12472067301Spgoyette 	int status;
125a3217a37Spgoyette 	char pipe_in, pipe_out;
126d5bb255fSpgoyette 	const char pipe_errmsg[] = "child: pipe write failed\n";
12772067301Spgoyette 
12872067301Spgoyette 	(void)unlink(lockfile);
12972067301Spgoyette 
13072067301Spgoyette 	fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
13172067301Spgoyette 	ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
13272067301Spgoyette 
1337dec8010Spgoyette 	ATF_REQUIRE_MSG(ftruncate(fd, filesize) >= 0,
13472067301Spgoyette 	    "ftruncate(%s): %s", lockfile, strerror(errno));
13572067301Spgoyette 
136a3217a37Spgoyette 	ATF_REQUIRE_MSG(pipe(pipe_fd) == 0, "pipe: %s", strerror(errno));
137a3217a37Spgoyette 
13872067301Spgoyette 	fsync(fd);
13972067301Spgoyette 	close(fd);
14072067301Spgoyette 
14172067301Spgoyette 	pid = malloc(nprocs * sizeof(pid_t));
14272067301Spgoyette 
14372067301Spgoyette 	for (i = 0; i < nprocs; i++) {
144a3217a37Spgoyette 		pipe_out = (char)('A' + i);
14572067301Spgoyette 		pid[i] = fork();
14672067301Spgoyette 		switch (pid[i]) {
14772067301Spgoyette 		case 0:
148a3217a37Spgoyette 			if (write(pipe_fd[1], &pipe_out, 1) != 1)
149ab7a5974Spgoyette 				write(STDERR_FILENO, pipe_errmsg,
150d5bb255fSpgoyette 				    __arraycount(pipe_errmsg) - 1);
151a3217a37Spgoyette 			else
15272067301Spgoyette 				trylocks(i);
15372067301Spgoyette 			_exit(0);
15472067301Spgoyette 			break;
15572067301Spgoyette 		case -1:
15672067301Spgoyette 			atf_tc_fail("fork %d failed", i);
15772067301Spgoyette 			break;
15872067301Spgoyette 		default:
159a3217a37Spgoyette 			ATF_REQUIRE_MSG(read(pipe_fd[0], &pipe_in, 1) == 1,
160a3217a37Spgoyette 			    "parent: read_pipe(%i): %s", i, strerror(errno));
161a3217a37Spgoyette 			ATF_REQUIRE_MSG(pipe_in == pipe_out,
162a3217a37Spgoyette 			    "parent: pipe does not match");
16372067301Spgoyette 			break;
16472067301Spgoyette 		}
16572067301Spgoyette 	}
166593938d6Spgoyette 	for (j = 0; j < npasses; j++) {
16772067301Spgoyette 		printf("parent: run %i\n", j+1);
16872067301Spgoyette 		for (i = 0; i < nprocs; i++) {
16972067301Spgoyette 			ATF_REQUIRE_MSG(ptrace(PT_ATTACH, pid[i], 0, 0) >= 0,
17072067301Spgoyette 			    "ptrace attach %d", pid[i]);
17172067301Spgoyette 			ATF_REQUIRE_MSG(waitpid(pid[i], &status, WUNTRACED) >= 0,
17272067301Spgoyette 			    "waitpid(ptrace)");
17372067301Spgoyette 			usleep(sleeptime / 3);
17472067301Spgoyette 			ATF_REQUIRE_MSG(ptrace(PT_DETACH, pid[i], (caddr_t)1,
17572067301Spgoyette 					       0) >= 0,
17672067301Spgoyette 			    "ptrace detach %d", pid[i]);
17772067301Spgoyette 			usleep(sleeptime / 3);
17872067301Spgoyette 		}
17972067301Spgoyette 	}
18072067301Spgoyette 	for (i = 0; i < nprocs; i++) {
18172067301Spgoyette 		printf("reap %d: ", i);
18272067301Spgoyette 		fflush(stdout);
18372067301Spgoyette 		kill(pid[i], SIGINT);
18472067301Spgoyette 		waitpid(pid[i], &status, 0);
18572067301Spgoyette 		printf(" status %d\n", status);
18672067301Spgoyette 	}
18772067301Spgoyette 	atf_tc_pass();
18872067301Spgoyette }
18972067301Spgoyette 
19072067301Spgoyette static int
dolock(int fd,int op,off_t lk_off,off_t lk_size)19172067301Spgoyette dolock(int fd, int op, off_t lk_off, off_t lk_size)
19272067301Spgoyette {
19372067301Spgoyette 	off_t result;
19472067301Spgoyette 	int ret;
19572067301Spgoyette 
19672067301Spgoyette 	result = lseek(fd, lk_off, SEEK_SET);
19772067301Spgoyette 	if (result == -1) {
19872067301Spgoyette 		return errno;
19972067301Spgoyette 	}
20072067301Spgoyette 	ATF_REQUIRE_MSG(result == lk_off, "lseek to wrong offset");
20172067301Spgoyette 	ret = lockf(fd, op, lk_size);
20272067301Spgoyette 	if (ret == -1) {
20372067301Spgoyette 		return errno;
20472067301Spgoyette 	}
20572067301Spgoyette 	return 0;
20672067301Spgoyette }
20772067301Spgoyette 
20872067301Spgoyette ATF_TC(deadlock);
ATF_TC_HEAD(deadlock,tc)20972067301Spgoyette ATF_TC_HEAD(deadlock, tc)
21072067301Spgoyette {
21172067301Spgoyette 
21272067301Spgoyette 	atf_tc_set_md_var(tc, "timeout", "30");
21372067301Spgoyette 	atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) deadlock detection");
21472067301Spgoyette }
21572067301Spgoyette 
ATF_TC_BODY(deadlock,tc)21672067301Spgoyette ATF_TC_BODY(deadlock, tc)
21772067301Spgoyette {
21872067301Spgoyette 	int fd;
21972067301Spgoyette 	int error;
22072067301Spgoyette 	int ret;
22172067301Spgoyette 	pid_t pid;
22272067301Spgoyette 
22372067301Spgoyette 	(void)unlink(lockfile);
22472067301Spgoyette 
22572067301Spgoyette 	fd = open (lockfile, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0666);
22672067301Spgoyette 	ATF_REQUIRE_MSG(fd >= 0, "open(%s): %s", lockfile, strerror(errno));
22772067301Spgoyette 
2287dec8010Spgoyette 	ATF_REQUIRE_MSG(ftruncate(fd, filesize) >= 0,
22972067301Spgoyette 	    "ftruncate(%s): %s", lockfile, strerror(errno));
23072067301Spgoyette 
23172067301Spgoyette 	fsync(fd);
23272067301Spgoyette 
23372067301Spgoyette 	error = dolock(fd, F_LOCK, 0, 1);
23472067301Spgoyette 	ATF_REQUIRE_MSG(error == 0, "initial dolock: %s", strerror(errno));
23572067301Spgoyette 
23672067301Spgoyette 	pid = fork();
23772067301Spgoyette 	ATF_REQUIRE_MSG(pid != -1, "fork failed: %s", strerror(errno));
23872067301Spgoyette 	if (pid == 0) {
23972067301Spgoyette 		error = dolock(fd, F_LOCK, 1, 1);
24072067301Spgoyette 		ATF_REQUIRE_MSG(error == 0, "child dolock: %s",
24172067301Spgoyette 		    strerror(errno));
24272067301Spgoyette 		dolock(fd, F_LOCK, 0, 1);	/* will block */
24372067301Spgoyette 		atf_tc_fail("child did not block");
24472067301Spgoyette 	}
24572067301Spgoyette 	sleep(1);	/* give child time to grab its lock then block */
24672067301Spgoyette 
24772067301Spgoyette 	error = dolock(fd, F_LOCK, 1, 1);
248706bd0b3Spgoyette 	ATF_REQUIRE_MSG(error == EDEADLK, "parent did not detect deadlock: %s",
24972067301Spgoyette 	    strerror(errno));
25072067301Spgoyette 	ret = kill(pid, SIGKILL);
25172067301Spgoyette 	ATF_REQUIRE_MSG(ret != -1, "failed to kill child: %s", strerror(errno));
25272067301Spgoyette 
25372067301Spgoyette 	atf_tc_pass();
25472067301Spgoyette }
25572067301Spgoyette 
ATF_TP_ADD_TCS(tp)25672067301Spgoyette ATF_TP_ADD_TCS(tp)
25772067301Spgoyette {
25872067301Spgoyette 	ATF_TP_ADD_TC(tp, randlock);
25972067301Spgoyette 	ATF_TP_ADD_TC(tp, deadlock);
26072067301Spgoyette 
26172067301Spgoyette 	return atf_no_error();
26272067301Spgoyette }
263