[1] | 1 | /* $Id: filelock-posix.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - File Locking, POSIX.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[5999] | 29 | * CDDL are applicable instead of those of the GPL.
|
---|
| 30 | *
|
---|
| 31 | * You may elect to license modified versions of this file under the
|
---|
| 32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
| 37 |
|
---|
[57358] | 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 41 | #define LOG_GROUP RTLOGGROUP_FILE
|
---|
| 42 |
|
---|
| 43 | #include <errno.h>
|
---|
| 44 | #include <sys/types.h>
|
---|
| 45 | #include <sys/ioctl.h>
|
---|
| 46 | #include <fcntl.h>
|
---|
[1387] | 47 | #include <unistd.h>
|
---|
| 48 | #include <sys/time.h>
|
---|
[1] | 49 |
|
---|
| 50 | #include <iprt/file.h>
|
---|
| 51 | #include <iprt/assert.h>
|
---|
| 52 | #include <iprt/string.h>
|
---|
| 53 | #include <iprt/err.h>
|
---|
| 54 | #include <iprt/log.h>
|
---|
| 55 | #include "internal/file.h"
|
---|
| 56 | #include "internal/fs.h"
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 |
|
---|
[37596] | 61 | RTR3DECL(int) RTFileLock(RTFILE hFile, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
[1] | 62 | {
|
---|
| 63 | Assert(offLock >= 0);
|
---|
| 64 |
|
---|
| 65 | /* Check arguments. */
|
---|
| 66 | if (fLock & ~RTFILE_LOCK_MASK)
|
---|
| 67 | {
|
---|
| 68 | AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
|
---|
| 69 | return VERR_INVALID_PARAMETER;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /*
|
---|
| 73 | * Validate offset.
|
---|
| 74 | */
|
---|
| 75 | if ( sizeof(off_t) < sizeof(cbLock)
|
---|
| 76 | && ( (offLock >> 32) != 0
|
---|
| 77 | || (cbLock >> 32) != 0
|
---|
| 78 | || ((offLock + cbLock) >> 32) != 0))
|
---|
| 79 | {
|
---|
| 80 | AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
|
---|
| 81 | return VERR_NOT_SUPPORTED;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /* Prepare flock structure. */
|
---|
| 85 | struct flock fl;
|
---|
| 86 | Assert(RTFILE_LOCK_WRITE);
|
---|
| 87 | fl.l_type = (fLock & RTFILE_LOCK_WRITE) ? F_WRLCK : F_RDLCK;
|
---|
| 88 | fl.l_whence = SEEK_SET;
|
---|
| 89 | fl.l_start = (off_t)offLock;
|
---|
| 90 | fl.l_len = (off_t)cbLock;
|
---|
| 91 | fl.l_pid = 0;
|
---|
| 92 |
|
---|
| 93 | Assert(RTFILE_LOCK_WAIT);
|
---|
[37596] | 94 | if (fcntl(RTFileToNative(hFile), (fLock & RTFILE_LOCK_WAIT) ? F_SETLKW : F_SETLK, &fl) >= 0)
|
---|
[1] | 95 | return VINF_SUCCESS;
|
---|
| 96 |
|
---|
| 97 | int iErr = errno;
|
---|
| 98 | if ( iErr == EAGAIN
|
---|
| 99 | || iErr == EACCES)
|
---|
| 100 | return VERR_FILE_LOCK_VIOLATION;
|
---|
| 101 |
|
---|
| 102 | return RTErrConvertFromErrno(iErr);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
[37596] | 106 | RTR3DECL(int) RTFileChangeLock(RTFILE hFile, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
[1] | 107 | {
|
---|
[1387] | 108 | /** @todo We never returns VERR_FILE_NOT_LOCKED for now. */
|
---|
[37596] | 109 | return RTFileLock(hFile, fLock, offLock, cbLock);
|
---|
[1] | 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
[37596] | 113 | RTR3DECL(int) RTFileUnlock(RTFILE hFile, int64_t offLock, uint64_t cbLock)
|
---|
[1] | 114 | {
|
---|
| 115 | Assert(offLock >= 0);
|
---|
| 116 |
|
---|
| 117 | /*
|
---|
| 118 | * Validate offset.
|
---|
| 119 | */
|
---|
| 120 | if ( sizeof(off_t) < sizeof(cbLock)
|
---|
| 121 | && ( (offLock >> 32) != 0
|
---|
| 122 | || (cbLock >> 32) != 0
|
---|
| 123 | || ((offLock + cbLock) >> 32) != 0))
|
---|
| 124 | {
|
---|
| 125 | AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
|
---|
| 126 | return VERR_NOT_SUPPORTED;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /* Prepare flock structure. */
|
---|
| 130 | struct flock fl;
|
---|
| 131 | fl.l_type = F_UNLCK;
|
---|
| 132 | fl.l_whence = SEEK_SET;
|
---|
| 133 | fl.l_start = (off_t)offLock;
|
---|
| 134 | fl.l_len = (off_t)cbLock;
|
---|
| 135 | fl.l_pid = 0;
|
---|
| 136 |
|
---|
[37596] | 137 | if (fcntl(RTFileToNative(hFile), F_SETLK, &fl) >= 0)
|
---|
[1] | 138 | return VINF_SUCCESS;
|
---|
| 139 |
|
---|
[63561] | 140 | /** @todo check error codes for non existing lock. */
|
---|
[1] | 141 | int iErr = errno;
|
---|
| 142 | if ( iErr == EAGAIN
|
---|
| 143 | || iErr == EACCES)
|
---|
| 144 | return VERR_FILE_LOCK_VIOLATION;
|
---|
| 145 |
|
---|
| 146 | return RTErrConvertFromErrno(iErr);
|
---|
| 147 | }
|
---|
| 148 |
|
---|