diff options
author | Neal H Walfield <neal@cs.uml.edu> | 2019-10-30 00:03:13 +0100 |
---|---|---|
committer | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2019-10-30 00:04:40 +0100 |
commit | 3a8234d2440a464f5401b3444d454ac913d6b3d6 (patch) | |
tree | 409ff15b23279ea508c18fc4d1909da55fc9ca6e /libfshelp-tests/race.c | |
parent | 14639382938714f07f76abd3d221c6d472710502 (diff) | |
download | hurd-3a8234d2440a464f5401b3444d454ac913d6b3d6.tar.gz hurd-3a8234d2440a464f5401b3444d454ac913d6b3d6.tar.bz2 hurd-3a8234d2440a464f5401b3444d454ac913d6b3d6.zip |
libfshelp-tests: Tests for file record locking
2019-03-04 Svante Signell <svante.signell@gmail.com>
* test-*.c: Update code, remove test results.
* README.new_tests: New file, summarize new test results.
2019-02-12 Svante Signell <svante.signell@gmail.com>
* test-*.c: Update code, add some test results.
* Makefile: Remove extra flags.
2019-02-01 Svante Signell <svante.signell@gmail.com>
* Update copyright years.
2016-05-23 Svante Signell <svante.signell@gmail.com>
* Makefile: Add sub-directory libfshelp-tests.
2018-12-07 Svante Signell <svante.signell@gmail.com>
* Update copyright years.
* locks.c(cmd_lock): Call fshelp_rlock_tweak()
with new last argument rendezvous = MACH_PORT_NULL.
2017-01-05 Svante Signell <svante.signell@gmail.com>
* Update copyright years and headers.
2016-12-28 Svante Signell <svante.signell@gmail.com>
* Makefile: test-flock.c, test-lockf.c and test-fcntl.c
* test-lockf.c: New file
* Rename set-flock.c, set-fcntl.c to test-flock.c test-fcntl.c
* TODO: Update README
2016-05-23 Svante Signell <svante.signell@gmail.com>
* Makefile: Link with pthread, add build of set-flock.c and set-fcntl.c
* define temporary CPP_FLAGS until glibc is updated
* set-flock.c, set-fcntl.c: New files.
* Fix typos in README
2001-04-11 Neal H Walfield <neal@cs.uml.edu>
* ChangeLog: New file, mentioning itself in this sentence.
* Makefile: New file.
* README: Likewise.
* fork.c: Likewise.
* locks: Likewise.
* locks-tests: Likewise.
* locks.c: Likewise.
* race.c: Likewise.
Diffstat (limited to 'libfshelp-tests/race.c')
-rw-r--r-- | libfshelp-tests/race.c | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/libfshelp-tests/race.c b/libfshelp-tests/race.c new file mode 100644 index 00000000..a1d398ab --- /dev/null +++ b/libfshelp-tests/race.c @@ -0,0 +1,83 @@ +/* Test races in the record locking code. + + Copyright (C) 2001 Free Software Foundation, Inc. + + Written by Neal H Walfield <neal@cs.uml.edu> + + 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 the Free Software Foundation; either version 2, or (at + your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */ + +#include <stdio.h> +#include <error.h> +#include <fcntl.h> +#include <unistd.h> +#include "fs_U.h" +#include <hurd.h> +#include "io_U.h" + +int main (int argc, char **argv) +{ + error_t err; + struct flock64 lock; + mach_port_t rendezvous = MACH_PORT_NULL; + int fd; + int i; + uint v; + int blocked = 0; + char buf[10] = ""; + char *bufp; + + if (argc != 4) + error (1, 0, "Usage: %s file start len", argv[0]); + + lock.l_whence = SEEK_SET; + lock.l_start = atoi (argv[2]); + lock.l_len = atoi (argv[3]); + + fd = file_name_lookup (argv[1], O_READ | O_WRITE | O_CREAT, 0666); + if (fd == MACH_PORT_NULL) + error (1, errno, "file_name_lookup"); + + for (i = 0; i < 10000; i ++) + { + lock.l_type = F_WRLCK; + err = file_record_lock (fd, F_SETLK64, &lock, rendezvous, MACH_MSG_TYPE_MAKE_SEND); + if (err) + { + blocked ++; + err = file_record_lock (fd, F_SETLKW64, &lock, rendezvous, MACH_MSG_TYPE_MAKE_SEND); + } + if (err) + error (1, err, "file_record_lock"); + + v = sizeof (buf); + bufp = buf; + io_read (fd, &bufp, &v, 0, v); + + v = atoi (bufp); + sprintf (buf, "%d\n", v + 1); + + v = 10; + io_write (fd, buf, sizeof (buf), 0, &v); + if (v == 0) + error (1, errno, "write (%d)", i); + + lock.l_type = F_UNLCK; + file_record_lock (fd, F_SETLK64, &lock, rendezvous, MACH_MSG_TYPE_MAKE_SEND); + } + + mach_port_deallocate (mach_task_self (), fd); + + printf ("Was blocked %d times\n", blocked); + return 0; +} |