1 | /* $Id: filelock-os2.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - File Locking, OS/2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
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 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
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
|
---|
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.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #define LOG_GROUP RTLOGGROUP_FILE
|
---|
42 |
|
---|
43 | #include <errno.h>
|
---|
44 | #include <sys/types.h>
|
---|
45 | #include <sys/ioctl.h>
|
---|
46 | #include <sys/fcntl.h>
|
---|
47 | #include <fcntl.h>
|
---|
48 | #include <unistd.h>
|
---|
49 | #include <sys/time.h>
|
---|
50 |
|
---|
51 | #include <iprt/file.h>
|
---|
52 | #include <iprt/assert.h>
|
---|
53 | #include <iprt/string.h>
|
---|
54 | #include <iprt/err.h>
|
---|
55 | #include <iprt/log.h>
|
---|
56 | #include "internal/file.h"
|
---|
57 | #include "internal/fs.h"
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 | RTR3DECL(int) RTFileLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
63 | {
|
---|
64 | Assert(offLock >= 0);
|
---|
65 |
|
---|
66 | /* Check arguments. */
|
---|
67 | if (fLock & ~RTFILE_LOCK_MASK)
|
---|
68 | {
|
---|
69 | AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
|
---|
70 | return VERR_INVALID_PARAMETER;
|
---|
71 | }
|
---|
72 |
|
---|
73 | /*
|
---|
74 | * Validate offset.
|
---|
75 | */
|
---|
76 | if ( sizeof(off_t) < sizeof(cbLock)
|
---|
77 | && ( (offLock >> 32) != 0
|
---|
78 | || (cbLock >> 32) != 0
|
---|
79 | || ((offLock + cbLock) >> 32) != 0))
|
---|
80 | {
|
---|
81 | AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
|
---|
82 | return VERR_NOT_SUPPORTED;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /* Prepare flock structure. */
|
---|
86 | struct flock fl;
|
---|
87 | Assert(RTFILE_LOCK_WRITE);
|
---|
88 | fl.l_type = (fLock & RTFILE_LOCK_WRITE) ? F_WRLCK : F_RDLCK;
|
---|
89 | fl.l_whence = SEEK_SET;
|
---|
90 | fl.l_start = (off_t)offLock;
|
---|
91 | fl.l_len = (off_t)cbLock;
|
---|
92 | fl.l_pid = 0;
|
---|
93 |
|
---|
94 | Assert(RTFILE_LOCK_WAIT);
|
---|
95 | if (fcntl(RTFileToNative(File), (fLock & RTFILE_LOCK_WAIT) ? F_SETLKW : F_SETLK, &fl) >= 0)
|
---|
96 | return VINF_SUCCESS;
|
---|
97 |
|
---|
98 | int iErr = errno;
|
---|
99 | if ( iErr == EAGAIN
|
---|
100 | || iErr == EACCES)
|
---|
101 | return VERR_FILE_LOCK_VIOLATION;
|
---|
102 |
|
---|
103 | return RTErrConvertFromErrno(iErr);
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | RTR3DECL(int) RTFileChangeLock(RTFILE File, unsigned fLock, int64_t offLock, uint64_t cbLock)
|
---|
108 | {
|
---|
109 | /** @todo copied from ../win/fileio-win.cpp for now but a proper solution
|
---|
110 | * would probably be to modify kLIBC so that __fcntl_locking() first
|
---|
111 | * assumes a change lock request is made (e.g. the same region was
|
---|
112 | * previously F_RDLCK'ed and now needs to be F_WRLCK'ed or vice versa) and
|
---|
113 | * tries to use atomic locking, and only if it fails, it does the regular
|
---|
114 | * lock procedure. The alternative is to use DosSetFileLocks directly here
|
---|
115 | * which basically means copy-pasting the __fcntl_locking() source
|
---|
116 | * code :) Note that the first attempt to call RTFileLock() below assumes
|
---|
117 | * that kLIBC is patched as described above one day and gives it a chance;
|
---|
118 | * on failure, we fall back to the Win-like unlock-then-lock approach. */
|
---|
119 |
|
---|
120 | int rc = RTFileLock(File, fLock, offLock, cbLock);
|
---|
121 | if (RT_FAILURE(rc) && rc != VERR_FILE_LOCK_VIOLATION)
|
---|
122 | return rc;
|
---|
123 |
|
---|
124 | /* Check arguments. */
|
---|
125 | if (fLock & ~RTFILE_LOCK_MASK)
|
---|
126 | {
|
---|
127 | AssertMsgFailed(("Invalid fLock=%08X\n", fLock));
|
---|
128 | return VERR_INVALID_PARAMETER;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /* Remove old lock. */
|
---|
132 | rc = RTFileUnlock(File, offLock, cbLock);
|
---|
133 | if (RT_FAILURE(rc))
|
---|
134 | return rc;
|
---|
135 |
|
---|
136 | /* Set new lock. */
|
---|
137 | rc = RTFileLock(File, fLock, offLock, cbLock);
|
---|
138 | if (RT_SUCCESS(rc))
|
---|
139 | return rc;
|
---|
140 |
|
---|
141 | /* Try to restore old lock. */
|
---|
142 | unsigned fLockOld = (fLock & RTFILE_LOCK_WRITE) ? fLock & ~RTFILE_LOCK_WRITE : fLock | RTFILE_LOCK_WRITE;
|
---|
143 | rc = RTFileLock(File, fLockOld, offLock, cbLock);
|
---|
144 | if (RT_SUCCESS(rc))
|
---|
145 | return VERR_FILE_LOCK_VIOLATION;
|
---|
146 | else
|
---|
147 | return VERR_FILE_LOCK_LOST;
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | RTR3DECL(int) RTFileUnlock(RTFILE File, int64_t offLock, uint64_t cbLock)
|
---|
152 | {
|
---|
153 | Assert(offLock >= 0);
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * Validate offset.
|
---|
157 | */
|
---|
158 | if ( sizeof(off_t) < sizeof(cbLock)
|
---|
159 | && ( (offLock >> 32) != 0
|
---|
160 | || (cbLock >> 32) != 0
|
---|
161 | || ((offLock + cbLock) >> 32) != 0))
|
---|
162 | {
|
---|
163 | AssertMsgFailed(("64-bit file i/o not supported! offLock=%lld cbLock=%lld\n", offLock, cbLock));
|
---|
164 | return VERR_NOT_SUPPORTED;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /* Prepare flock structure. */
|
---|
168 | struct flock fl;
|
---|
169 | fl.l_type = F_UNLCK;
|
---|
170 | fl.l_whence = SEEK_SET;
|
---|
171 | fl.l_start = (off_t)offLock;
|
---|
172 | fl.l_len = (off_t)cbLock;
|
---|
173 | fl.l_pid = 0;
|
---|
174 |
|
---|
175 | if (fcntl(RTFileToNative(File), F_SETLK, &fl) >= 0)
|
---|
176 | return VINF_SUCCESS;
|
---|
177 |
|
---|
178 | /** @todo check error codes for non existing lock. */
|
---|
179 | int iErr = errno;
|
---|
180 | if ( iErr == EAGAIN
|
---|
181 | || iErr == EACCES)
|
---|
182 | return VERR_FILE_LOCK_VIOLATION;
|
---|
183 |
|
---|
184 | return RTErrConvertFromErrno(iErr);
|
---|
185 | }
|
---|
186 |
|
---|