From: "Daniel P. Berrange" <berrange@xxxxxxxxxx> Add some simple wrappers around the fcntl() discretionary file locking capability. * src/util/util.c, src/util/util.h, src/libvirt_private.syms: Add virFileLock and virFileUnlock APIs --- src/libvirt_private.syms | 2 ++ src/util/util.c | 33 +++++++++++++++++++++++++++++++++ src/util/util.h | 3 +++ 3 files changed, 38 insertions(+), 0 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ae0d199..0746dee 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -993,6 +993,7 @@ virFileFindMountPoint; virFileHasSuffix; virFileIsExecutable; virFileLinkPointsTo; +virFileLock; virFileMakePath; virFileMatchesNameSuffix; virFileOpenAs; @@ -1004,6 +1005,7 @@ virFileReadPid; virFileResolveLink; virFileSanitizePath; virFileStripSuffix; +virFileUnlock; virFileWaitForDevices; virFileWriteStr; virFindFileInPath; diff --git a/src/util/util.c b/src/util/util.c index 4710fc5..1d8083f 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -1175,6 +1175,39 @@ int virFileOpenTtyAt(const char *ptmx ATTRIBUTE_UNUSED, } #endif + +int virFileLock(int fd, bool shared, off_t start, off_t len) +{ + struct flock fl = { + .l_type = shared ? F_RDLCK : F_WRLCK, + .l_whence = SEEK_SET, + .l_start = start, + .l_len = len, + }; + + if (fcntl(fd, F_SETLK, &fl) < 0) + return -errno; + + return 0; +} + + +int virFileUnlock(int fd, off_t start, off_t len) +{ + struct flock fl = { + .l_type = F_UNLCK, + .l_whence = SEEK_SET, + .l_start = start, + .l_len = len, + }; + + if (fcntl(fd, F_SETLK, &fl) < 0) + return -errno; + + return 0; +} + + char* virFilePid(const char *dir, const char* name) { char *pidfile; diff --git a/src/util/util.h b/src/util/util.h index 1555653..820c9ff 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -117,6 +117,9 @@ int virFileOpenTtyAt(const char *ptmx, char **ttyName, int rawmode); +int virFileLock(int fd, bool shared, off_t start, off_t len); +int virFileUnlock(int fd, off_t start, off_t len); + char* virFilePid(const char *dir, const char *name); -- 1.7.6 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list