aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2016-08-19 08:54:29 +1000
committerSteve Bennett <steveb@workware.net.au>2016-08-20 11:24:28 +1000
commitdef038d816120c9281e8001c24170047996ebfbc (patch)
tree17afdef43c7f1b46cb5130eca47bd349cf341c2c
parentdc7c2751eb01e8aa37740ce494e2857ce11b5ad3 (diff)
downloadjimtcl-def038d816120c9281e8001c24170047996ebfbc.zip
jimtcl-def038d816120c9281e8001c24170047996ebfbc.tar.gz
jimtcl-def038d816120c9281e8001c24170047996ebfbc.tar.bz2
Fix aio locking on non-linux platforms
And improve the tests Signed-off-by: Steve Bennett <steveb@workware.net.au>
-rw-r--r--auto.def4
-rw-r--r--jim-aio.c19
-rw-r--r--tests/lock.test35
3 files changed, 32 insertions, 26 deletions
diff --git a/auto.def b/auto.def
index fcf4a80..bb50330 100644
--- a/auto.def
+++ b/auto.def
@@ -112,6 +112,10 @@ if {[cc-check-functions sysinfo]} {
}
}
+cc-with {-includes fcntl.h} {
+ cc-check-types "struct flock"
+}
+
cc-check-lfs
cc-check-functions fseeko ftello
diff --git a/jim-aio.c b/jim-aio.c
index 7619e3d..fb8577f 100644
--- a/jim-aio.c
+++ b/jim-aio.c
@@ -1159,10 +1159,16 @@ static int aio_cmd_verify(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
}
#endif /* JIM_BOOTSTRAP */
+#if defined(HAVE_STRUCT_FLOCK) && !defined(JIM_BOOTSTRAP)
static int aio_cmd_lock(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
- struct flock fl = { F_WRLCK, SEEK_SET, 0, 0 };
+ struct flock fl;
+
+ fl.l_start = 0;
+ fl.l_len = 0;
+ fl.l_type = F_WRLCK;
+ fl.l_whence = SEEK_SET;
switch (fcntl(af->fd, F_SETLK, &fl))
{
@@ -1190,11 +1196,16 @@ static int aio_cmd_lock(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
static int aio_cmd_unlock(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
AioFile *af = Jim_CmdPrivData(interp);
- struct flock fl = { F_UNLCK, SEEK_SET, 0, 0 };
+ struct flock fl;
+ fl.l_start = 0;
+ fl.l_len = 0;
+ fl.l_type = F_UNLCK;
+ fl.l_whence = SEEK_SET;
Jim_SetResultInt(interp, fcntl(af->fd, F_SETLK, &fl) == 0);
return JIM_OK;
}
+#endif /* JIM_BOOTSTRAP */
static const jim_subcmd_type aio_command_table[] = {
{ "read",
@@ -1370,8 +1381,9 @@ static const jim_subcmd_type aio_command_table[] = {
/* Description: Verifies the certificate of a SSL/TLS channel */
},
#endif /* JIM_BOOTSTRAP */
+#if defined(HAVE_STRUCT_FLOCK) && !defined(JIM_BOOTSTRAP)
{ "lock",
- NULL,
+ NULL,
aio_cmd_lock,
0,
0,
@@ -1384,6 +1396,7 @@ static const jim_subcmd_type aio_command_table[] = {
0,
/* Description: Relase a lock. */
},
+#endif /* JIM_BOOTSTRAP */
{ NULL }
};
diff --git a/tests/lock.test b/tests/lock.test
index 2fe6ee4..92778d1 100644
--- a/tests/lock.test
+++ b/tests/lock.test
@@ -10,6 +10,11 @@
source [file dirname [info script]]/testing.tcl
+needs constraint jim
+# Really we want to check if locking is supported, but there
+# is no easy way to do that, so use the existence of os.wait as a proxy
+needs cmd os.wait
+
set fh [open locktest.file w]
test lock-1.1 {grab lock} {
@@ -29,32 +34,16 @@ test lock-1.4 {release lock again} {
} 1
test lock-1.5 {grab lock from sub-process} {
- switch [set pid [os.fork]] {
- -1 { error "Fork error." }
- 0 {
- # Child process
- $fh lock
- sleep 2
- puts $fh [$fh unlock]
- exit
- }
- default {
- sleep 1
- set stat [$fh lock]
- set stat
- }
- }
+ # Run a child process that grabs the lock for 0.5 seconds
+ set pid [exec [info nameofexecutable] -e {set fh [open locktest.file r+]; $fh lock; sleep 0.5} >/dev/null &]
+ sleep 0.1
+ # Try to grab the lock - should fail
+ set stat [$fh lock]
+ sleep 0.5
+ set stat
} 0
-# fcntl() allows unlock on non-held lock so unlock will always return 1
-#set x [os.wait $pid]
-#test lock-1.6 {check unlock in child process} {
-# $fh seek 0 start
-# $fh read 1
-#} 1
-
$fh close
file delete locktest.file
testreport
-