aboutsummaryrefslogtreecommitdiff
path: root/jim-aio.c
diff options
context:
space:
mode:
authorHummyPkg <af123@hummypkg.org.uk>2016-08-19 08:45:08 +1000
committerSteve Bennett <steveb@workware.net.au>2016-08-20 11:01:10 +1000
commitdc7c2751eb01e8aa37740ce494e2857ce11b5ad3 (patch)
tree96ca0096c0182b7de179249246b3ecad0aa40d70 /jim-aio.c
parent278003ca94cf3d3e5f9619e6b2fd08ab590c0899 (diff)
downloadjimtcl-dc7c2751eb01e8aa37740ce494e2857ce11b5ad3.zip
jimtcl-dc7c2751eb01e8aa37740ce494e2857ce11b5ad3.tar.gz
jimtcl-dc7c2751eb01e8aa37740ce494e2857ce11b5ad3.tar.bz2
add POSIX lock/unlock methods to aio objects
Diffstat (limited to 'jim-aio.c')
-rw-r--r--jim-aio.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/jim-aio.c b/jim-aio.c
index 4663afb..7619e3d 100644
--- a/jim-aio.c
+++ b/jim-aio.c
@@ -1159,6 +1159,43 @@ static int aio_cmd_verify(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
}
#endif /* 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 };
+
+ switch (fcntl(af->fd, F_SETLK, &fl))
+ {
+ case 0:
+ Jim_SetResultInt(interp, 1);
+ break;
+ case -1:
+ if (errno == EACCES || errno == EAGAIN)
+ Jim_SetResultInt(interp, 0);
+ else
+ {
+ Jim_SetResultFormatted(interp, "lock failed: %s",
+ strerror(errno));
+ return JIM_ERR;
+ }
+ break;
+ default:
+ Jim_SetResultInt(interp, 0);
+ break;
+ }
+
+ return JIM_OK;
+}
+
+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 };
+
+ Jim_SetResultInt(interp, fcntl(af->fd, F_SETLK, &fl) == 0);
+ return JIM_OK;
+}
+
static const jim_subcmd_type aio_command_table[] = {
{ "read",
"?-nonewline? ?len?",
@@ -1333,6 +1370,20 @@ static const jim_subcmd_type aio_command_table[] = {
/* Description: Verifies the certificate of a SSL/TLS channel */
},
#endif /* JIM_BOOTSTRAP */
+ { "lock",
+ NULL,
+ aio_cmd_lock,
+ 0,
+ 0,
+ /* Description: Attempt to get a lock. */
+ },
+ { "unlock",
+ NULL,
+ aio_cmd_unlock,
+ 0,
+ 0,
+ /* Description: Relase a lock. */
+ },
{ NULL }
};