From e08d71e697ab9dcf0030211a0b72962d1d544dd6 Mon Sep 17 00:00:00 2001 From: Jim Garlick Date: Thu, 16 Jan 2025 18:49:39 -0800 Subject: [PATCH] testsuite: drop record locking kernel tests Problem: tests/kern/t19 and tests/kern/t23 both test POSIX record locking through diod, but diod doesn't support it. Now that there is a unit test that demostrates diod's inability to handle record locks, these tests are not needed. Drop them. Fixes #102. --- tests/kern/Makefile.am | 8 +- tests/kern/t19 | 7 - tests/kern/t19.exp | 87 ------------- tests/kern/t23 | 7 - tests/kern/t23.exp | 127 ------------------ tests/kern/tfcntl2.c | 257 ------------------------------------ tests/kern/tfcntl3.c | 288 ----------------------------------------- 7 files changed, 3 insertions(+), 778 deletions(-) delete mode 100755 tests/kern/t19 delete mode 100644 tests/kern/t19.exp delete mode 100755 tests/kern/t23 delete mode 100644 tests/kern/t23.exp delete mode 100644 tests/kern/tfcntl2.c delete mode 100644 tests/kern/tfcntl3.c diff --git a/tests/kern/Makefile.am b/tests/kern/Makefile.am index c03d611f..6e7a776c 100644 --- a/tests/kern/Makefile.am +++ b/tests/kern/Makefile.am @@ -6,8 +6,6 @@ check_PROGRAMS = \ tstat \ tflock \ tflock2 \ - tfcntl2 \ - tfcntl3 \ tfsgid \ tcreate \ tatomic \ @@ -25,11 +23,11 @@ AM_TESTS_ENVIRONMENT = \ LOG_COMPILER = ${srcdir}/runtest -TESTS = t05 t06 t07 t12 t13 t14 t15 t16 t17 t18 t19 t22 \ - t23 t24 t25 t26 t27 t28 t29 t30 t31 t32 t33 \ +TESTS = t05 t06 t07 t12 t13 t14 t15 t16 t17 t18 t22 \ + t24 t25 t26 t27 t28 t29 t30 t31 t32 t33 \ t35 t36 t37 t38 t39 t40 t41 t42 t43 t44 t45 t46 t47 -XFAIL_TESTS = t19 t23 t37 t43 t48 t49 +XFAIL_TESTS = t37 t43 t48 t49 CLEANFILES = *.out *.diff *.diod *.postmark *.dbench diff --git a/tests/kern/t19 b/tests/kern/t19 deleted file mode 100755 index b80acb94..00000000 --- a/tests/kern/t19 +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -e - -dd if=/dev/zero of=$PATH_EXPDIR/a count=1024 status=noxfer -echo testing host file system -./tfcntl2 $PATH_EXPDIR/a || : -echo testing diod file system -./tfcntl2 $PATH_MNTDIR/a || : diff --git a/tests/kern/t19.exp b/tests/kern/t19.exp deleted file mode 100644 index 1342f476..00000000 --- a/tests/kern/t19.exp +++ /dev/null @@ -1,87 +0,0 @@ -kconjoin: diodmount exited with rc=0 -1024+0 records in -1024+0 records out -testing host file system -tfcntl2: 1. Conflicting write locks cannot be held by two processes -tfcntl2: fd: open -tfcntl2: fd: write-locked -tfcntl2: child forked -tfcntl2: fd2: open (child) -tfcntl2: fd2: write-lock failed: Resource temporarily unavailable -tfcntl2: child exited normally -tfcntl2: fd: closed -tfcntl2: 2. Conflicting read and write locks cannot be held by two processes -tfcntl2: fd: open -tfcntl2: fd: write-locked -tfcntl2: child forked -tfcntl2: fd2: open (child) -tfcntl2: fd2: read-lock failed: Resource temporarily unavailable -tfcntl2: child exited normally -tfcntl2: fd: closed -tfcntl2: 3. Conflicting read locks CAN be held by two processes -tfcntl2: fd: open -tfcntl2: fd: read-locked -tfcntl2: child forked -tfcntl2: fd2: open (child) -tfcntl2: fd2: read-locked -tfcntl2: child exited normally -tfcntl2: fd: closed -tfcntl2: 4. Non-conflicting write locks CAN be held by two processes -tfcntl2: fd: open -tfcntl2: fd: write-locked 1 byte -tfcntl2: child forked -tfcntl2: fd2: open (child) -tfcntl2: fd2: write-locked rest of file -tfcntl2: child exited normally -tfcntl2: fd: closed -tfcntl2: 5. Non-conflicting read and write locks CAN be held by two processes -tfcntl2: fd: open -tfcntl2: fd: write-locked 1 byte -tfcntl2: child forked -tfcntl2: fd2: open (child) -tfcntl2: fd2: read-locked rest of file -tfcntl2: child exited normally -tfcntl2: fd: closed -testing diod file system -tfcntl2: 1. Conflicting write locks cannot be held by two processes -tfcntl2: fd: open -tfcntl2: fd: write-locked -tfcntl2: child forked -tfcntl2: fd2: open (child) -tfcntl2: fd2: write-lock failed: Resource temporarily unavailable -tfcntl2: child exited normally -tfcntl2: fd: closed -tfcntl2: 2. Conflicting read and write locks cannot be held by two processes -tfcntl2: fd: open -tfcntl2: fd: write-locked -tfcntl2: child forked -tfcntl2: fd2: open (child) -tfcntl2: fd2: read-lock failed: Resource temporarily unavailable -tfcntl2: child exited normally -tfcntl2: fd: closed -tfcntl2: 3. Conflicting read locks CAN be held by two processes -tfcntl2: fd: open -tfcntl2: fd: read-locked -tfcntl2: child forked -tfcntl2: fd2: open (child) -tfcntl2: fd2: read-locked -tfcntl2: child exited normally -tfcntl2: fd: closed -tfcntl2: 4. Non-conflicting write locks CAN be held by two processes -tfcntl2: fd: open -tfcntl2: fd: write-locked 1 byte -tfcntl2: child forked -tfcntl2: fd2: open (child) -tfcntl2: fd2: write-locked rest of file -tfcntl2: child exited normally -tfcntl2: fd: closed -tfcntl2: 5. Non-conflicting read and write locks CAN be held by two processes -tfcntl2: fd: open -tfcntl2: fd: write-locked 1 byte -tfcntl2: child forked -tfcntl2: fd2: open (child) -tfcntl2: fd2: read-locked rest of file -tfcntl2: child exited normally -tfcntl2: fd: closed -kconjoin: t19 exited with rc=0 -kconjoin: diod exited with rc=0 diff --git a/tests/kern/t23 b/tests/kern/t23 deleted file mode 100755 index 64d249e0..00000000 --- a/tests/kern/t23 +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -e - -dd if=/dev/zero of=$PATH_EXPDIR/a count=1024 status=noxfer -echo testing host file system -./tfcntl3 $PATH_EXPDIR/a || : -echo testing diod file system -./tfcntl3 $PATH_MNTDIR/a || : diff --git a/tests/kern/t23.exp b/tests/kern/t23.exp deleted file mode 100644 index cd120d19..00000000 --- a/tests/kern/t23.exp +++ /dev/null @@ -1,127 +0,0 @@ -kconjoin: diodmount exited with rc=0 -1024+0 records in -1024+0 records out -testing host file system -tfcntl3: 1. Upgrade read lock to write lock (one fd) -tfcntl3: fd: open O_RDWR -tfcntl3: fd: read-locked -tfcntl3: fd: write-locked -tfcntl3: fd: closed -tfcntl3: 2. Upgrade read lock to write lock (two fds) -tfcntl3: fd: open O_RDWR -tfcntl3: fd: read-locked -tfcntl3: fd2: open O_RDWR -tfcntl3: fd2: write-locked -tfcntl3: fd2: closed -tfcntl3: fd: closed -tfcntl3: 3. Upgrade byte range of write lock (one fd) -tfcntl3: fd: open O_RDWR -tfcntl3: fd: write-locked 1 byte -tfcntl3: fd: write-locked entire file -tfcntl3: fd: closed -tfcntl3: 4. Downgrade byte range of write lock (one fd) -tfcntl3: fd: open O_RDWR -tfcntl3: fd: write-locked entire file -tfcntl3: fd: write-locked 1 byte -tfcntl3: fd: closed -tfcntl3: 5. Downgrade write lock to read lock (one fd) -tfcntl3: fd: open O_RDWR -tfcntl3: fd: write-locked -tfcntl3: fd: read-locked -tfcntl3: fd: closed -tfcntl3: 6. Read lock with O_RDONLY should succeed -tfcntl3: fd: open O_RDONLY -tfcntl3: fd: read-locked -tfcntl3: fd: closed -tfcntl3: 7. Read lock with O_WRONLY should fail -tfcntl3: fd: open O_WRONLY -tfcntl3: fd: fcntl F_SETLK rdlock failed: Bad file descriptor -tfcntl3: fd: closed -tfcntl3: 8. Write lock with O_RDONLY should fail -tfcntl3: fd: open O_RDONLY -tfcntl3: fd: fcntl F_SETLK wrlock failed: Bad file descriptor -tfcntl3: fd: closed -tfcntl3: 9. Write lock with O_WRONLY should succeed -tfcntl3: fd: open O_WRONLY -tfcntl3: fd: write-locked -tfcntl3: fd: closed -tfcntl3: 10. Write lock is not inherited across a fork -tfcntl3: fd: open O_RDWR -tfcntl3: fd: write-locked -tfcntl3: child forked -tfcntl3: fd: read-lock failed (child): Resource temporarily unavailable -tfcntl3: child exited normally -tfcntl3: fd: closed -tfcntl3: 11. Write lock is dropped if another fd to same file is closed -tfcntl3: fd: open O_RDWR -tfcntl3: fd: write-locked -tfcntl3: fd2: open O_RDWR -tfcntl3: fd2: closed -tfcntl3: child forked -tfcntl3: fd: closed (child) -tfcntl3: fd2: open O_RDWR (child) -tfcntl3: fd2: read-locked (child) -tfcntl3: child exited normally -testing diod file system -tfcntl3: 1. Upgrade read lock to write lock (one fd) -tfcntl3: fd: open O_RDWR -tfcntl3: fd: read-locked -tfcntl3: fd: write-locked -tfcntl3: fd: closed -tfcntl3: 2. Upgrade read lock to write lock (two fds) -tfcntl3: fd: open O_RDWR -tfcntl3: fd: read-locked -tfcntl3: fd2: open O_RDWR -tfcntl3: fd2: write-locked -tfcntl3: fd2: closed -tfcntl3: fd: closed -tfcntl3: 3. Upgrade byte range of write lock (one fd) -tfcntl3: fd: open O_RDWR -tfcntl3: fd: write-locked 1 byte -tfcntl3: fd: write-locked entire file -tfcntl3: fd: closed -tfcntl3: 4. Downgrade byte range of write lock (one fd) -tfcntl3: fd: open O_RDWR -tfcntl3: fd: write-locked entire file -tfcntl3: fd: write-locked 1 byte -tfcntl3: fd: closed -tfcntl3: 5. Downgrade write lock to read lock (one fd) -tfcntl3: fd: open O_RDWR -tfcntl3: fd: write-locked -tfcntl3: fd: read-locked -tfcntl3: fd: closed -tfcntl3: 6. Read lock with O_RDONLY should succeed -tfcntl3: fd: open O_RDONLY -tfcntl3: fd: read-locked -tfcntl3: fd: closed -tfcntl3: 7. Read lock with O_WRONLY should fail -tfcntl3: fd: open O_WRONLY -tfcntl3: fd: fcntl F_SETLK rdlock failed: Bad file descriptor -tfcntl3: fd: closed -tfcntl3: 8. Write lock with O_RDONLY should fail -tfcntl3: fd: open O_RDONLY -tfcntl3: fd: fcntl F_SETLK wrlock failed: Bad file descriptor -tfcntl3: fd: closed -tfcntl3: 9. Write lock with O_WRONLY should succeed -tfcntl3: fd: open O_WRONLY -tfcntl3: fd: write-locked -tfcntl3: fd: closed -tfcntl3: 10. Write lock is not inherited across a fork -tfcntl3: fd: open O_RDWR -tfcntl3: fd: write-locked -tfcntl3: child forked -tfcntl3: fd: read-lock failed (child): Resource temporarily unavailable -tfcntl3: child exited normally -tfcntl3: fd: closed -tfcntl3: 11. Write lock is dropped if another fd to same file is closed -tfcntl3: fd: open O_RDWR -tfcntl3: fd: write-locked -tfcntl3: fd2: open O_RDWR -tfcntl3: fd2: closed -tfcntl3: child forked -tfcntl3: fd: closed (child) -tfcntl3: fd2: open O_RDWR (child) -tfcntl3: fd2: read-locked (child) -tfcntl3: child exited normally -kconjoin: t23 exited with rc=0 -kconjoin: diod exited with rc=0 diff --git a/tests/kern/tfcntl2.c b/tests/kern/tfcntl2.c deleted file mode 100644 index 9d6eeb47..00000000 --- a/tests/kern/tfcntl2.c +++ /dev/null @@ -1,257 +0,0 @@ -/************************************************************\ - * Copyright 2010 Lawrence Livermore National Security, LLC - * (c.f. AUTHORS, NOTICE.LLNS, COPYING) - * - * This file is part of the diod 9P server project. - * For details, see https://github.com/chaos/diod. - * - * SPDX-License-Identifier: GPL-2.0-or-later -\************************************************************/ - -/* tfcntl2.c - test POSIX adsvisory record locks with multiple processes */ - -#if HAVE_CONFIG_H -#include "config.h" -#endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "src/libdiod/diod_log.h" - -#include "test.h" - -static const struct flock sf_rdlock = { - .l_type = F_RDLCK, - .l_whence = SEEK_SET, - .l_start = 0, - .l_len = 0 -}; -static const struct flock sf_wrlock = { - .l_type = F_WRLCK, - .l_whence = SEEK_SET, - .l_start = 0, - .l_len = 0 -}; - -int -main (int argc, char *argv[]) -{ - int fd = -1; - int fd2 = -1; - pid_t pid; - int status; - struct flock fl; - - diod_log_init (argv[0]); - - if (argc != 2) { - msg ("Usage: tfcntl2 file"); - exit (1); - } - - msg ("1. Conflicting write locks cannot be held by two processes"); - if ((fd = open (argv[1], O_RDWR)) < 0) - err_exit ("open %s", argv[1]); - msg ("fd: open"); - fl = sf_wrlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: write-lock failed, aborting"); - msg ("fd: write-locked"); - switch (pid = fork ()) { - case -1: - err_exit ("fork"); - case 0: /* child */ - msg ("child forked"); - if ((fd2 = open (argv[1], O_RDWR)) < 0) - err_exit ("fd2 open %s", argv[1]); - msg ("fd2: open (child)"); - fl = sf_wrlock; - if (fcntl (fd2, F_SETLK, &fl) < 0) { - err ("fd2: write-lock failed"); - exit (0); - } - msg_exit ("fd2: write-locked"); - default: /* parent */ - if (waitpid (pid, &status, 0) < 0) - err_exit ("waitpid"); - if (!WIFEXITED (status)) - msg_exit ("child terminated without exit"); - if (WEXITSTATUS (status) != 0) - msg_exit ("child exited with %d, aborting", - WEXITSTATUS (status)); - msg ("child exited normally"); - break; - } - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("2. Conflicting read and write locks cannot be held by two processes"); - if ((fd = open (argv[1], O_RDWR)) < 0) - err_exit ("open %s", argv[1]); - msg ("fd: open"); - fl = sf_wrlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: write-lock failed, aborting"); - msg ("fd: write-locked"); - switch (pid = fork ()) { - case -1: - err_exit ("fork"); - case 0: /* child */ - msg ("child forked"); - if ((fd2 = open (argv[1], O_RDWR)) < 0) - err_exit ("fd2 open %s", argv[1]); - msg ("fd2: open (child)"); - fl = sf_rdlock; - if (fcntl (fd2, F_SETLK, &fl) < 0) { - err ("fd2: read-lock failed"); - exit (0); - } - msg_exit ("fd2: read-locked"); - default: /* parent */ - if (waitpid (pid, &status, 0) < 0) - err_exit ("waitpid"); - if (!WIFEXITED (status)) - msg_exit ("child terminated without exit"); - if (WEXITSTATUS (status) != 0) - msg_exit ("child exited with %d, aborting", - WEXITSTATUS (status)); - msg ("child exited normally"); - break; - } - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("3. Conflicting read locks CAN be held by two processes"); - if ((fd = open (argv[1], O_RDWR)) < 0) - err_exit ("open %s", argv[1]); - msg ("fd: open"); - fl = sf_rdlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: read-lock failed, aborting"); - msg ("fd: read-locked"); - switch (pid = fork ()) { - case -1: - err_exit ("fork"); - case 0: /* child */ - msg ("child forked"); - if ((fd2 = open (argv[1], O_RDWR)) < 0) - err_exit ("fd2 open %s", argv[1]); - msg ("fd2: open (child)"); - fl = sf_rdlock; - if (fcntl (fd2, F_SETLK, &fl) < 0) - err_exit ("fd2: read-lock failed"); - msg ("fd2: read-locked"); - exit (0); - default: /* parent */ - if (waitpid (pid, &status, 0) < 0) - err_exit ("waitpid"); - if (!WIFEXITED (status)) - msg_exit ("child terminated without exit"); - if (WEXITSTATUS (status) != 0) - msg_exit ("child exited with %d, aborting", - WEXITSTATUS (status)); - msg ("child exited normally"); - break; - } - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("4. Non-conflicting write locks CAN be held by two processes"); - if ((fd = open (argv[1], O_RDWR)) < 0) - err_exit ("open %s", argv[1]); - msg ("fd: open"); - fl = sf_wrlock; - fl.l_len = 1; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: write-lock 1 byte failed, aborting"); - msg ("fd: write-locked 1 byte"); - switch (pid = fork ()) { - case -1: - err_exit ("fork"); - case 0: /* child */ - msg ("child forked"); - if ((fd2 = open (argv[1], O_RDWR)) < 0) - err_exit ("fd2 open %s", argv[1]); - msg ("fd2: open (child)"); - fl = sf_wrlock; - fl.l_start = 1; - if (fcntl (fd2, F_SETLK, &fl) < 0) - err_exit ("fd2: write-lock rest of file failed"); - msg ("fd2: write-locked rest of file"); - exit (0); - default: /* parent */ - if (waitpid (pid, &status, 0) < 0) - err_exit ("waitpid"); - if (!WIFEXITED (status)) - msg_exit ("child terminated without exit"); - if (WEXITSTATUS (status) != 0) - msg_exit ("child exited with %d, aborting", - WEXITSTATUS (status)); - msg ("child exited normally"); - break; - } - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("5. Non-conflicting read and write locks CAN be held by two processes"); - if ((fd = open (argv[1], O_RDWR)) < 0) - err_exit ("open %s", argv[1]); - msg ("fd: open"); - fl = sf_wrlock; - fl.l_len = 1; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: write-lock 1 byte failed, aborting"); - msg ("fd: write-locked 1 byte"); - switch (pid = fork ()) { - case -1: - err_exit ("fork"); - case 0: /* child */ - msg ("child forked"); - if ((fd2 = open (argv[1], O_RDWR)) < 0) - err_exit ("fd2 open %s", argv[1]); - msg ("fd2: open (child)"); - fl = sf_rdlock; - fl.l_start = 1; - if (fcntl (fd2, F_SETLK, &fl) < 0) - err_exit ("fd2: read-lock rest of file failed"); - msg ("fd2: read-locked rest of file"); - exit (0); - default: /* parent */ - if (waitpid (pid, &status, 0) < 0) - err_exit ("waitpid"); - if (!WIFEXITED (status)) - msg_exit ("child terminated without exit"); - if (WEXITSTATUS (status) != 0) - msg_exit ("child exited with %d, aborting", - WEXITSTATUS (status)); - msg ("child exited normally"); - break; - } - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - exit (0); -} - -/* - * vi:tabstop=4 shiftwidth=4 expandtab - */ diff --git a/tests/kern/tfcntl3.c b/tests/kern/tfcntl3.c deleted file mode 100644 index b7f3db85..00000000 --- a/tests/kern/tfcntl3.c +++ /dev/null @@ -1,288 +0,0 @@ -/************************************************************\ - * Copyright 2010 Lawrence Livermore National Security, LLC - * (c.f. AUTHORS, NOTICE.LLNS, COPYING) - * - * This file is part of the diod 9P server project. - * For details, see https://github.com/chaos/diod. - * - * SPDX-License-Identifier: GPL-2.0-or-later -\************************************************************/ - -/* tfcntl3.c - test POSIX advisory record locks within one process */ - -#if HAVE_CONFIG_H -#include "config.h" -#endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "src/libdiod/diod_log.h" - -#include "test.h" - -static const struct flock sf_rdlock = { - .l_type = F_RDLCK, - .l_whence = SEEK_SET, - .l_start = 0, - .l_len = 0 -}; -static const struct flock sf_wrlock = { - .l_type = F_WRLCK, - .l_whence = SEEK_SET, - .l_start = 0, - .l_len = 0 -}; - -int -main (int argc, char *argv[]) -{ - int fd = -1; - int fd2 = -1; - pid_t pid; - int status; - struct flock fl; - - diod_log_init (argv[0]); - - if (argc != 2) { - msg ("Usage: tfcntl3 file"); - exit (1); - } - - msg ("1. Upgrade read lock to write lock (one fd)"); - if ((fd = open (argv[1], O_RDWR)) < 0) - err_exit ("fd: open %s", argv[1]); - msg ("fd: open O_RDWR"); - fl = sf_rdlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: fcntl F_SETLK rdlock failed"); - msg ("fd: read-locked"); - fl = sf_wrlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: fcntl F_SETLK wrlock failed"); - msg ("fd: write-locked"); - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("2. Upgrade read lock to write lock (two fds)"); - if ((fd = open (argv[1], O_RDWR)) < 0) - err_exit ("open %s", argv[1]); - msg ("fd: open O_RDWR"); - fl = sf_rdlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: fcntl F_SETLK rdlock failed"); - msg ("fd: read-locked"); - if ((fd2 = open (argv[1], O_RDWR)) < 0) - err_exit ("fd2: open %s", argv[1]); - msg ("fd2: open O_RDWR"); - fl = sf_wrlock; - if (fcntl (fd2, F_SETLK, &fl) < 0) - err_exit ("fd2: fcntl F_SETLK wrlock failed"); - msg ("fd2: write-locked"); - if (close (fd2) < 0) - err_exit ("close fd2"); - msg ("fd2: closed"); - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("3. Upgrade byte range of write lock (one fd)"); - if ((fd = open (argv[1], O_RDWR)) < 0) - err_exit ("fd: open %s", argv[1]); - msg ("fd: open O_RDWR"); - fl = sf_wrlock; - fl.l_len = 1; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: fcntl F_SETLK wrlock (1 byte) failed"); - msg ("fd: write-locked 1 byte"); - fl = sf_wrlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: fcntl F_SETLK wrlock (entire file) failed"); - msg ("fd: write-locked entire file"); - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("4. Downgrade byte range of write lock (one fd)"); - if ((fd = open (argv[1], O_RDWR)) < 0) - err_exit ("fd: open %s", argv[1]); - msg ("fd: open O_RDWR"); - fl = sf_wrlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: fcntl F_SETLK wrlock (entire file) failed"); - msg ("fd: write-locked entire file"); - fl = sf_wrlock; - fl.l_len = 1; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: fcntl F_SETLK wrlock (one byte) failed"); - msg ("fd: write-locked 1 byte"); - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("5. Downgrade write lock to read lock (one fd)"); - if ((fd = open (argv[1], O_RDWR)) < 0) - err_exit ("fd: open %s", argv[1]); - msg ("fd: open O_RDWR"); - fl = sf_wrlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: fcntl F_SETLK wrlock failed"); - msg ("fd: write-locked"); - fl = sf_rdlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: fcntl F_SETLK rdlock failed"); - msg ("fd: read-locked"); - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("6. Read lock with O_RDONLY should succeed"); - if ((fd = open (argv[1], O_RDONLY)) < 0) - err_exit ("fd: open %s", argv[1]); - msg ("fd: open O_RDONLY"); - fl = sf_rdlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: fcntl F_SETLK rdlock failed"); - msg ("fd: read-locked"); - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("7. Read lock with O_WRONLY should fail"); - if ((fd = open (argv[1], O_WRONLY)) < 0) - err_exit ("fd: open %s", argv[1]); - msg ("fd: open O_WRONLY"); - fl = sf_rdlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err ("fd: fcntl F_SETLK rdlock failed"); - else - msg ("fd: read-locked"); - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("8. Write lock with O_RDONLY should fail"); - if ((fd = open (argv[1], O_RDONLY)) < 0) - err_exit ("fd: open %s", argv[1]); - msg ("fd: open O_RDONLY"); - fl = sf_wrlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err ("fd: fcntl F_SETLK wrlock failed"); - else - msg ("fd: write-locked"); - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("9. Write lock with O_WRONLY should succeed"); - if ((fd = open (argv[1], O_WRONLY)) < 0) - err_exit ("fd: open %s", argv[1]); - msg ("fd: open O_WRONLY"); - fl = sf_wrlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err ("fd: fcntl F_SETLK wrlock failed"); - else - msg ("fd: write-locked"); - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("10. Write lock is not inherited across a fork"); - if ((fd = open (argv[1], O_RDWR)) < 0) - err_exit ("fd: open %s", argv[1]); - msg ("fd: open O_RDWR"); - fl = sf_wrlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: write-lock failed, aborting"); - msg ("fd: write-locked"); - switch (pid = fork ()) { - case -1: - err_exit ("fork"); - case 0: /* child */ - msg ("child forked"); - fl = sf_rdlock; - if (fcntl (fd, F_SETLK, &fl) < 0) { - err ("fd: read-lock failed (child)"); - exit (0); - } - msg_exit ("fd: read-locked (child)"); - default: /* parent */ - if (waitpid (pid, &status, 0) < 0) - err_exit ("waitpid"); - if (!WIFEXITED (status)) - msg_exit ("child terminated without exit"); - if (WEXITSTATUS (status) != 0) - msg_exit ("child exited with %d, aborting", - WEXITSTATUS (status)); - msg ("child exited normally"); - break; - } - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed"); - - msg ("11. Write lock is dropped if another fd to same file is closed"); - if ((fd = open (argv[1], O_RDWR)) < 0) - err_exit ("fd: open %s", argv[1]); - msg ("fd: open O_RDWR"); - fl = sf_wrlock; - if (fcntl (fd, F_SETLK, &fl) < 0) - err_exit ("fd: write-lock failed, aborting"); - msg ("fd: write-locked"); - if ((fd2 = open (argv[1], O_RDWR)) < 0) - err_exit ("fd2: open %s", argv[1]); - msg ("fd2: open O_RDWR"); - if (close (fd2) < 0) - err_exit ("close fd2"); - msg ("fd2: closed"); - switch (pid = fork ()) { - case -1: - err_exit ("fork"); - case 0: /* child */ - msg ("child forked"); - if (close (fd) < 0) - err_exit ("close fd"); - msg ("fd: closed (child)"); - if ((fd2 = open (argv[1], O_RDWR)) < 0) - err_exit ("fd2: open %s", argv[1]); - msg ("fd2: open O_RDWR (child)"); - fl = sf_rdlock; - if (fcntl (fd2, F_SETLK, &fl) < 0) { - err ("fd2: read-lock failed (child)"); - exit (1); - } - msg ("fd2: read-locked (child)"); - exit (0); - default: /* parent */ - if (waitpid (pid, &status, 0) < 0) - err_exit ("waitpid"); - if (!WIFEXITED (status)) - msg_exit ("child terminated without exit"); - if (WEXITSTATUS (status) != 0) - msg_exit ("child exited with %d, aborting", - WEXITSTATUS (status)); - msg ("child exited normally"); - break; - } - - exit (0); -} - -/* - * vi:tabstop=4 shiftwidth=4 expandtab - */