Elevate Windows production worthiness

- SQLite file locking now works on Windows
- SQLite will now use fdatasync() on non-Apple platforms
- Fix Ctrl-C handler on Windows to not crash with TLS
- Signals now work in multithreaded apps on Windows
- fcntl() will now accurately report EINVAL errors
- fcntl() now has excellent --strace logging
- Token bucket replenish now go 100x faster
- *NSYNC cancellations now work on Windows
- Support closefrom() on NetBSD
This commit is contained in:
Justine Tunney 2022-10-13 13:44:41 -07:00
parent d38700687a
commit 997ce29ddc
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
95 changed files with 959 additions and 418 deletions

View file

@ -30,6 +30,8 @@
#include "libc/calls/weirdtypes.h"
#include "libc/errno.h"
#include "libc/sysv/consts/f.h"
#include "libc/sysv/errfuns.h"
#include "third_party/musl/lockf.h"
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
@ -44,25 +46,20 @@ int lockf(int fd, int op, off_t size)
.l_whence = SEEK_CUR,
.l_len = size,
};
if (op == F_TEST){
switch (op) {
case F_TEST:
l.l_type = F_RDLCK;
if (fcntl(fd, F_GETLK, &l) < 0)
return -1;
if (l.l_type == F_UNLCK || l.l_pid == getpid())
return 0;
errno = EACCES;
return -1;
}
if (op == F_ULOCK) {
return eacces();
case F_ULOCK:
l.l_type = F_UNLCK;
case F_TLOCK:
return fcntl(fd, F_SETLK, &l);
}
if (op == F_TLOCK) {
return fcntl(fd, F_SETLK, &l);
}
if (op == F_LOCK) {
case F_LOCK:
return fcntl(fd, F_SETLKW, &l);
}
errno = EINVAL;
return -1;
return einval();
}

16
third_party/musl/lockf.h vendored Normal file
View file

@ -0,0 +1,16 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_MUSL_LOCKF_H_
#define COSMOPOLITAN_THIRD_PARTY_MUSL_LOCKF_H_
#define F_ULOCK 0
#define F_LOCK 1
#define F_TLOCK 2
#define F_TEST 3
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
int lockf(int, int, int64_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_MUSL_LOCKF_H_ */

View file

@ -18,6 +18,7 @@
*/
#include "libc/atomic.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/struct/timespec.h"
@ -166,15 +167,19 @@ int nsync_futex_wait_ (int *p, int expect, char pshare, struct timespec *timeout
if (pshare) {
goto Polyfill;
}
if (timeout) {
ms = _timespec_tomillis (*timeout);
if (_check_interrupts (false, 0)) {
rc = -EINTR;
} else {
ms = -1;
}
if (WaitOnAddress (p, &expect, sizeof(int), ms)) {
rc = 0;
} else {
rc = -GetLastError ();
if (timeout) {
ms = _timespec_tomillis (*timeout);
} else {
ms = -1;
}
if (WaitOnAddress (p, &expect, sizeof(int), ms)) {
rc = 0;
} else {
rc = -GetLastError ();
}
}
} else if (IsFreebsd ()) {
rc = sys_umtx_timedwait_uint (
@ -193,7 +198,7 @@ int nsync_futex_wait_ (int *p, int expect, char pshare, struct timespec *timeout
__get_tls()->tib_flags &= ~TIB_FLAG_TIME_CRITICAL;
}
STRACE ("futex(%t, %s, %d, %s) → %s",
STRACE ("futex(%t, %s, %#x, %s) → %s",
p, DescribeFutexOp (op), expect,
DescribeTimespec (0, timeout),
DescribeErrnoResult (rc));

View file

@ -16,6 +16,7 @@
limitations under the License.
*/
#include "libc/errno.h"
#include "libc/runtime/runtime.h"
#include "third_party/nsync/atomic.h"
#include "third_party/nsync/atomic.internal.h"
#include "third_party/nsync/common.internal.h"
@ -81,5 +82,3 @@ int nsync_sem_wait_with_cancel_ (waiter *w, nsync_time abs_deadline,
}
return (sem_outcome);
}

View file

@ -1,33 +0,0 @@
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi
Copyright 2016 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 │
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "third_party/nsync/common.internal.h"
#include "third_party/nsync/mu_semaphore.h"
asm(".ident\t\"\\n\\n\
*NSYNC (Apache 2.0)\\n\
Copyright 2016 Google, Inc.\\n\
https://github.com/google/nsync\"");
// clang-format off
/* Wait until one of:
w->sem is non-zero----decrement it and return 0.
abs_deadline expires---return ETIMEDOUT.
Ignores cancel_note. */
int nsync_sem_wait_with_cancel_ (waiter *w, nsync_time abs_deadline, nsync_note cancel_note) {
return (nsync_mu_semaphore_p_with_deadline (&w->sem, abs_deadline));
}

View file

@ -62,6 +62,7 @@
#include "libc/time/struct/utimbuf.h"
#include "libc/time/time.h"
#include "libc/x/x.h"
#include "third_party/musl/lockf.h"
#include "third_party/musl/passwd.h"
#include "third_party/python/Include/abstract.h"
#include "third_party/python/Include/boolobject.h"
@ -11894,10 +11895,10 @@ all_ins(PyObject *m)
if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
/* constants for lockf */
if (F_LOCK && PyModule_AddIntMacro(m, F_LOCK)) return -1;
if (F_TLOCK && PyModule_AddIntMacro(m, F_TLOCK)) return -1;
if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
if (F_TEST && PyModule_AddIntMacro(m, F_TEST)) return -1;
if (PyModule_AddIntMacro(m, F_TEST)) return -1;
#ifdef HAVE_SPAWNV
if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;

View file

@ -227,7 +227,7 @@
/* #undef HAVE_LINUX_CAN_RAW_FD_FRAMES */
/* Define to 1 if you have the 'lockf' function and the F_LOCK macro. */
/* #undef HAVE_LOCKF */
#define HAVE_LOCKF 1
/* #define HAVE_DEVICE_MACROS 1 */
#define HAVE_MAKEDEV 1

View file

@ -10,3 +10,7 @@ ORIGIN
LICENSE
Public Domain or MIT
LOCAL CHANGES
- Changed the fsync() code to be configured at runtime.

View file

@ -3105,10 +3105,6 @@ static int openDatabase(
char *zErrMsg = 0; /* Error message from sqlite3ParseUri() */
int i; /* Loop counter */
// TODO(jart): Fix SQLite.
extern bool __force_sqlite_to_work_until_we_can_fix_it;
__force_sqlite_to_work_until_we_can_fix_it = true;
#ifdef SQLITE_ENABLE_API_ARMOR
if( ppDb==0 ) return SQLITE_MISUSE_BKPT;
#endif

View file

@ -53,6 +53,8 @@
#include "third_party/sqlite3/sqlite3.h"
#include "third_party/sqlite3/mutex.internal.h"
#include "third_party/sqlite3/mutex.internal.h"
#include "libc/sysv/consts/f.h"
#include "libc/dce.h"
#include "third_party/sqlite3/sqliteInt.inc"
#if SQLITE_OS_UNIX /* This file is used on unix only */
@ -3612,8 +3614,9 @@ static int full_fsync(int fd, int fullSync, int dataOnly){
struct stat buf;
rc = osFstat(fd, &buf);
}
#elif HAVE_FULLFSYNC
if( fullSync ){
#elif HAVE_FULLFSYNC || defined(__COSMOPOLITAN__)
/* [jart] use runtime os detection */
if( fullSync && F_FULLFSYNC != -1 ){
rc = osFcntl(fd, F_FULLFSYNC, 0);
}else{
rc = 1;
@ -3626,7 +3629,13 @@ static int full_fsync(int fd, int fullSync, int dataOnly){
** It'd be better to detect fullfsync support once and avoid
** the fcntl call every time sync is called.
*/
if( rc ) rc = fsync(fd);
if( rc ) {
if( IsXnu() ){
rc = fsync(fd);
}else{
rc = fdatasync(fd);
}
}
#elif defined(__APPLE__)
/* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly

View file

@ -59,6 +59,7 @@
#include "libc/sysv/consts/rusage.h"
#include "libc/time/time.h"
#include "libc/runtime/runtime.h"
#include "libc/errno.h"
#if SQLITE_USER_AUTHENTICATION
#include "third_party/sqlite3/sqlite3userauth.inc"
#endif
@ -10573,6 +10574,7 @@ static void process_sqliterc(
p->in = inSaved;
p->lineno = savedLineno;
sqlite3_free(zBuf);
errno = 0; /* [jart] clear error */
}
/*