1 //===-- LockFilePosix.cpp ---------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Host/posix/LockFilePosix.h" 10 11 #include <fcntl.h> 12 #include <unistd.h> 13 14 using namespace lldb; 15 using namespace lldb_private; 16 17 namespace { 18 19 Status fileLock(int fd, int cmd, int lock_type, const uint64_t start, 20 const uint64_t len) { 21 struct flock fl; 22 23 fl.l_type = lock_type; 24 fl.l_whence = SEEK_SET; 25 fl.l_start = start; 26 fl.l_len = len; 27 fl.l_pid = ::getpid(); 28 29 Status error; 30 if (::fcntl(fd, cmd, &fl) == -1) 31 error.SetErrorToErrno(); 32 33 return error; 34 } 35 36 } // namespace 37 38 LockFilePosix::LockFilePosix(int fd) : LockFileBase(fd) {} 39 40 LockFilePosix::~LockFilePosix() { Unlock(); } 41 42 Status LockFilePosix::DoWriteLock(const uint64_t start, const uint64_t len) { 43 return fileLock(m_fd, F_SETLKW, F_WRLCK, start, len); 44 } 45 46 Status LockFilePosix::DoTryWriteLock(const uint64_t start, const uint64_t len) { 47 return fileLock(m_fd, F_SETLK, F_WRLCK, start, len); 48 } 49 50 Status LockFilePosix::DoReadLock(const uint64_t start, const uint64_t len) { 51 return fileLock(m_fd, F_SETLKW, F_RDLCK, start, len); 52 } 53 54 Status LockFilePosix::DoTryReadLock(const uint64_t start, const uint64_t len) { 55 return fileLock(m_fd, F_SETLK, F_RDLCK, start, len); 56 } 57 58 Status LockFilePosix::DoUnlock() { 59 return fileLock(m_fd, F_SETLK, F_UNLCK, m_start, m_len); 60 } 61