2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-10-17 18:02:04 +00:00
|
|
|
#include "libc/assert.h"
|
2021-01-30 21:09:11 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/calls/struct/fd.internal.h"
|
2021-05-14 12:36:58 +00:00
|
|
|
#include "libc/calls/struct/flock.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall-nt.internal.h"
|
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/calls/wincrash.internal.h"
|
|
|
|
#include "libc/errno.h"
|
2022-10-17 18:02:04 +00:00
|
|
|
#include "libc/intrin/kmalloc.h"
|
2022-09-15 04:29:50 +00:00
|
|
|
#include "libc/intrin/weaken.h"
|
2022-10-17 18:02:04 +00:00
|
|
|
#include "libc/limits.h"
|
2022-09-15 04:29:50 +00:00
|
|
|
#include "libc/log/backtrace.internal.h"
|
2022-10-17 18:02:04 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2021-05-14 12:36:58 +00:00
|
|
|
#include "libc/nt/enum/filelockflags.h"
|
2021-06-12 07:01:55 +00:00
|
|
|
#include "libc/nt/errors.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/files.h"
|
2021-06-12 07:01:55 +00:00
|
|
|
#include "libc/nt/runtime.h"
|
2021-05-14 12:36:58 +00:00
|
|
|
#include "libc/nt/struct/byhandlefileinformation.h"
|
|
|
|
#include "libc/nt/struct/overlapped.h"
|
2023-06-10 16:15:19 +00:00
|
|
|
#include "libc/stdckdint.h"
|
2022-10-17 18:02:04 +00:00
|
|
|
#include "libc/str/str.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/f.h"
|
|
|
|
#include "libc/sysv/consts/fd.h"
|
2020-11-28 20:01:51 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2022-10-17 18:02:04 +00:00
|
|
|
#include "libc/thread/thread.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-10-17 18:02:04 +00:00
|
|
|
struct FileLock {
|
|
|
|
struct FileLock *next;
|
|
|
|
int64_t off;
|
|
|
|
int64_t len;
|
|
|
|
int fd;
|
|
|
|
bool exc;
|
|
|
|
};
|
2021-08-12 07:42:14 +00:00
|
|
|
|
2022-10-17 18:02:04 +00:00
|
|
|
struct FileLocks {
|
|
|
|
pthread_mutex_t mu;
|
|
|
|
struct FileLock *list;
|
|
|
|
struct FileLock *free;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct FileLocks g_locks;
|
2022-09-15 04:29:50 +00:00
|
|
|
|
2022-10-17 18:02:04 +00:00
|
|
|
static textwindows struct FileLock *NewFileLock(void) {
|
|
|
|
struct FileLock *fl;
|
|
|
|
if (g_locks.free) {
|
|
|
|
fl = g_locks.free;
|
|
|
|
g_locks.free = fl->next;
|
|
|
|
} else {
|
|
|
|
fl = kmalloc(sizeof(*fl));
|
2022-09-15 04:29:50 +00:00
|
|
|
}
|
2022-10-17 18:02:04 +00:00
|
|
|
bzero(fl, sizeof(*fl));
|
|
|
|
fl->next = g_locks.list;
|
|
|
|
g_locks.list = fl;
|
|
|
|
return fl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows void FreeFileLock(struct FileLock *fl) {
|
|
|
|
fl->next = g_locks.free;
|
|
|
|
g_locks.free = fl;
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows bool OverlapsFileLock(struct FileLock *fl, int64_t off,
|
|
|
|
int64_t len) {
|
|
|
|
uint64_t BegA, EndA, BegB, EndB;
|
|
|
|
BegA = off;
|
|
|
|
EndA = off + (len - 1);
|
|
|
|
BegB = fl->off;
|
|
|
|
EndB = fl->off + (fl->len - 1);
|
|
|
|
return MAX(BegA, BegB) < MIN(EndA, EndB);
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows bool EncompassesFileLock(struct FileLock *fl, int64_t off,
|
|
|
|
int64_t len) {
|
|
|
|
return off <= fl->off && fl->off + fl->len <= off + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows bool EqualsFileLock(struct FileLock *fl, int64_t off,
|
|
|
|
int64_t len) {
|
|
|
|
return fl->off == off && off + len == fl->off + fl->len;
|
|
|
|
}
|
2022-09-15 04:29:50 +00:00
|
|
|
|
2022-11-08 19:39:50 +00:00
|
|
|
_Hide textwindows void sys_fcntl_nt_lock_cleanup(int fd) {
|
2022-10-17 18:02:04 +00:00
|
|
|
struct FileLock *fl, *ft, **flp;
|
|
|
|
pthread_mutex_lock(&g_locks.mu);
|
|
|
|
for (flp = &g_locks.list, fl = *flp; fl;) {
|
|
|
|
if (fl->fd == fd) {
|
|
|
|
*flp = fl->next;
|
|
|
|
ft = fl->next;
|
|
|
|
FreeFileLock(fl);
|
|
|
|
fl = ft;
|
|
|
|
} else {
|
|
|
|
flp = &fl->next;
|
|
|
|
fl = *flp;
|
|
|
|
}
|
2022-09-15 04:29:50 +00:00
|
|
|
}
|
2022-10-17 18:02:04 +00:00
|
|
|
pthread_mutex_unlock(&g_locks.mu);
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows int sys_fcntl_nt_lock(struct Fd *f, int fd, int cmd,
|
|
|
|
uintptr_t arg) {
|
|
|
|
int e;
|
|
|
|
struct flock *l;
|
|
|
|
uint32_t flags, err;
|
|
|
|
struct FileLock *fl, *ft, **flp;
|
|
|
|
int64_t pos, off, len, end, size;
|
2022-09-15 04:29:50 +00:00
|
|
|
|
2021-05-14 12:36:58 +00:00
|
|
|
l = (struct flock *)arg;
|
|
|
|
len = l->l_len;
|
|
|
|
off = l->l_start;
|
2022-09-15 04:29:50 +00:00
|
|
|
|
2021-06-12 07:01:55 +00:00
|
|
|
switch (l->l_whence) {
|
|
|
|
case SEEK_SET:
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
2022-10-17 18:02:04 +00:00
|
|
|
pos = 0;
|
|
|
|
if (SetFilePointerEx(f->handle, 0, &pos, SEEK_CUR)) {
|
|
|
|
off = pos + off;
|
|
|
|
} else {
|
|
|
|
return __winerr();
|
|
|
|
}
|
2021-06-12 07:01:55 +00:00
|
|
|
break;
|
|
|
|
case SEEK_END:
|
2022-10-17 18:02:04 +00:00
|
|
|
off = INT64_MAX - off;
|
2021-06-12 07:01:55 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-05-14 12:36:58 +00:00
|
|
|
return einval();
|
|
|
|
}
|
2022-09-15 04:29:50 +00:00
|
|
|
|
|
|
|
if (!len) {
|
2022-10-17 18:02:04 +00:00
|
|
|
len = INT64_MAX - off;
|
2022-09-15 04:29:50 +00:00
|
|
|
}
|
|
|
|
|
2023-06-10 16:15:19 +00:00
|
|
|
if (off < 0 || len < 0 || ckd_add(&end, off, len)) {
|
2022-09-15 04:29:50 +00:00
|
|
|
return einval();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool32 ok;
|
|
|
|
struct NtOverlapped ov = {.hEvent = f->handle,
|
|
|
|
.Pointer = (void *)(uintptr_t)off};
|
|
|
|
|
2021-05-14 12:36:58 +00:00
|
|
|
if (l->l_type == F_RDLCK || l->l_type == F_WRLCK) {
|
2022-10-17 18:02:04 +00:00
|
|
|
|
|
|
|
if (cmd == F_SETLK || cmd == F_SETLKW) {
|
|
|
|
// make it possible to transition read locks to write locks
|
|
|
|
for (flp = &g_locks.list, fl = *flp; fl;) {
|
|
|
|
if (fl->fd == fd) {
|
|
|
|
if (EqualsFileLock(fl, off, len)) {
|
|
|
|
if (fl->exc == l->l_type == F_WRLCK) {
|
|
|
|
// we already have this lock
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
// unlock our read lock and acquire write lock below
|
|
|
|
if (UnlockFileEx(f->handle, 0, len, len >> 32, &ov)) {
|
|
|
|
*flp = fl->next;
|
|
|
|
ft = fl->next;
|
|
|
|
FreeFileLock(fl);
|
|
|
|
fl = ft;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else if (OverlapsFileLock(fl, off, len)) {
|
|
|
|
return enotsup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flp = &fl->next;
|
|
|
|
fl = *flp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return better information on conflicting locks if possible
|
|
|
|
if (cmd == F_GETLK) {
|
|
|
|
for (fl = g_locks.list; fl; fl = fl->next) {
|
|
|
|
if (fl->fd == fd && //
|
|
|
|
OverlapsFileLock(fl, off, len) &&
|
|
|
|
(l->l_type == F_WRLCK || !fl->exc)) {
|
|
|
|
l->l_whence = SEEK_SET;
|
|
|
|
l->l_start = fl->off;
|
|
|
|
l->l_len = fl->len;
|
|
|
|
l->l_type == fl->exc ? F_WRLCK : F_RDLCK;
|
|
|
|
l->l_pid = getpid();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 12:36:58 +00:00
|
|
|
flags = 0;
|
2022-09-15 04:29:50 +00:00
|
|
|
if (cmd != F_SETLKW) {
|
2022-10-17 18:02:04 +00:00
|
|
|
// TODO(jart): we should use expo backoff in wrapper function
|
|
|
|
// should not matter since sqlite doesn't need it
|
2022-09-15 04:29:50 +00:00
|
|
|
flags |= kNtLockfileFailImmediately;
|
|
|
|
}
|
|
|
|
if (l->l_type == F_WRLCK) {
|
|
|
|
flags |= kNtLockfileExclusiveLock;
|
|
|
|
}
|
|
|
|
ok = LockFileEx(f->handle, flags, 0, len, len >> 32, &ov);
|
|
|
|
if (cmd == F_GETLK) {
|
|
|
|
if (ok) {
|
|
|
|
l->l_type = F_UNLCK;
|
|
|
|
if (!UnlockFileEx(f->handle, 0, len, len >> 32, &ov)) {
|
2022-10-17 18:02:04 +00:00
|
|
|
return -1;
|
2022-09-15 04:29:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
l->l_pid = -1;
|
|
|
|
ok = true;
|
|
|
|
}
|
2022-10-17 18:02:04 +00:00
|
|
|
} else if (ok) {
|
|
|
|
fl = NewFileLock();
|
|
|
|
fl->off = off;
|
|
|
|
fl->len = len;
|
|
|
|
fl->exc = l->l_type == F_WRLCK;
|
|
|
|
fl->fd = fd;
|
2022-09-15 04:29:50 +00:00
|
|
|
}
|
2022-10-13 20:44:41 +00:00
|
|
|
return ok ? 0 : -1;
|
2022-09-15 04:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (l->l_type == F_UNLCK) {
|
|
|
|
if (cmd == F_GETLK) return einval();
|
2022-10-17 18:02:04 +00:00
|
|
|
|
|
|
|
// allow a big range to unlock many small ranges
|
|
|
|
for (flp = &g_locks.list, fl = *flp; fl;) {
|
|
|
|
if (fl->fd == fd && EncompassesFileLock(fl, off, len)) {
|
|
|
|
struct NtOverlapped ov = {.hEvent = f->handle,
|
|
|
|
.Pointer = (void *)(uintptr_t)fl->off};
|
|
|
|
if (UnlockFileEx(f->handle, 0, fl->len, fl->len >> 32, &ov)) {
|
|
|
|
*flp = fl->next;
|
|
|
|
ft = fl->next;
|
|
|
|
FreeFileLock(fl);
|
|
|
|
fl = ft;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
flp = &fl->next;
|
|
|
|
fl = *flp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// win32 won't let us carve up existing locks
|
|
|
|
int overlap_count = 0;
|
|
|
|
for (fl = g_locks.list; fl; fl = fl->next) {
|
|
|
|
if (fl->fd == fd && //
|
|
|
|
OverlapsFileLock(fl, off, len)) {
|
|
|
|
++overlap_count;
|
|
|
|
}
|
2021-05-14 12:36:58 +00:00
|
|
|
}
|
2022-10-17 18:02:04 +00:00
|
|
|
|
|
|
|
// try to handle the carving cases needed by sqlite
|
|
|
|
if (overlap_count == 1) {
|
|
|
|
for (fl = g_locks.list; fl; fl = fl->next) {
|
|
|
|
if (fl->fd == fd && //
|
|
|
|
off <= fl->off && //
|
|
|
|
off + len >= fl->off && //
|
|
|
|
off + len < fl->off + fl->len) {
|
|
|
|
// cleave left side of lock
|
|
|
|
struct NtOverlapped ov = {.hEvent = f->handle,
|
|
|
|
.Pointer = (void *)(uintptr_t)fl->off};
|
|
|
|
if (!UnlockFileEx(f->handle, 0, fl->len, fl->len >> 32, &ov)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fl->len = (fl->off + fl->len) - (off + len);
|
|
|
|
fl->off = off + len;
|
|
|
|
ov.Pointer = (void *)(uintptr_t)fl->off;
|
|
|
|
if (!LockFileEx(f->handle, kNtLockfileExclusiveLock, 0, fl->len,
|
|
|
|
fl->len >> 32, &ov)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overlap_count) {
|
|
|
|
return enotsup();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2021-05-14 12:36:58 +00:00
|
|
|
}
|
2022-09-15 04:29:50 +00:00
|
|
|
|
|
|
|
return einval();
|
2021-05-14 12:36:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 18:02:04 +00:00
|
|
|
static textwindows int sys_fcntl_nt_dupfd(int fd, int cmd, int start) {
|
|
|
|
if (start < 0) return einval();
|
|
|
|
return sys_dup_nt(fd, -1, (cmd == F_DUPFD_CLOEXEC ? O_CLOEXEC : 0), start);
|
|
|
|
}
|
|
|
|
|
2021-05-14 12:36:58 +00:00
|
|
|
textwindows int sys_fcntl_nt(int fd, int cmd, uintptr_t arg) {
|
2022-10-17 18:02:04 +00:00
|
|
|
int rc;
|
2020-10-27 10:39:46 +00:00
|
|
|
uint32_t flags;
|
2021-01-25 21:08:05 +00:00
|
|
|
if (__isfdkind(fd, kFdFile) || __isfdkind(fd, kFdSocket)) {
|
2021-05-14 12:36:58 +00:00
|
|
|
if (cmd == F_GETFL) {
|
2022-10-17 18:02:04 +00:00
|
|
|
rc = g_fds.p[fd].flags &
|
|
|
|
(O_ACCMODE | O_APPEND | O_ASYNC | O_DIRECT | O_NOATIME | O_NONBLOCK |
|
|
|
|
O_RANDOM | O_SEQUENTIAL);
|
2021-05-14 12:36:58 +00:00
|
|
|
} else if (cmd == F_SETFL) {
|
2022-04-15 06:39:48 +00:00
|
|
|
// O_APPEND doesn't appear to be tunable at cursory glance
|
|
|
|
// O_NONBLOCK might require we start doing all i/o in threads
|
|
|
|
// O_DSYNC / O_RSYNC / O_SYNC maybe if we fsync() everything
|
2022-08-20 19:32:51 +00:00
|
|
|
// O_DIRECT | O_RANDOM | O_SEQUENTIAL | O_NDELAY possible but
|
|
|
|
// not worth it.
|
2022-11-02 06:14:14 +00:00
|
|
|
rc = enosys();
|
2021-05-14 12:36:58 +00:00
|
|
|
} else if (cmd == F_GETFD) {
|
|
|
|
if (g_fds.p[fd].flags & O_CLOEXEC) {
|
2022-10-17 18:02:04 +00:00
|
|
|
rc = FD_CLOEXEC;
|
2021-05-14 12:36:58 +00:00
|
|
|
} else {
|
2022-10-17 18:02:04 +00:00
|
|
|
rc = 0;
|
2021-05-14 12:36:58 +00:00
|
|
|
}
|
|
|
|
} else if (cmd == F_SETFD) {
|
|
|
|
if (arg & FD_CLOEXEC) {
|
|
|
|
g_fds.p[fd].flags |= O_CLOEXEC;
|
|
|
|
} else {
|
|
|
|
g_fds.p[fd].flags &= ~O_CLOEXEC;
|
|
|
|
}
|
2022-11-02 06:14:14 +00:00
|
|
|
rc = 0;
|
2022-08-17 06:23:34 +00:00
|
|
|
} else if (cmd == F_SETLK || cmd == F_SETLKW || cmd == F_GETLK) {
|
2022-10-17 18:02:04 +00:00
|
|
|
pthread_mutex_lock(&g_locks.mu);
|
|
|
|
rc = sys_fcntl_nt_lock(g_fds.p + fd, fd, cmd, arg);
|
|
|
|
pthread_mutex_unlock(&g_locks.mu);
|
2021-08-12 07:42:14 +00:00
|
|
|
} else if (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC) {
|
2022-10-17 18:02:04 +00:00
|
|
|
rc = sys_fcntl_nt_dupfd(fd, cmd, arg);
|
2021-05-14 12:36:58 +00:00
|
|
|
} else {
|
2022-10-17 18:02:04 +00:00
|
|
|
rc = einval();
|
2021-01-25 21:08:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-10-17 18:02:04 +00:00
|
|
|
rc = ebadf();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-10-17 18:02:04 +00:00
|
|
|
return rc;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|