From 5af19b7eed05ebc201627be933044b8c72983c26 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Fri, 14 Oct 2022 09:52:35 -0700 Subject: [PATCH] Make some foss compatibility improvements --- ape/sections.internal.h | 30 ++++++++ examples/getrandom.c | 1 + examples/nomodifyself.c | 1 + libc/calls/__sig2.c | 1 + libc/calls/openat-metal.c | 1 + libc/calls/pledge-linux.c | 1 + libc/calls/sigenter-freebsd.c | 1 + libc/calls/sigenter-netbsd.c | 1 + libc/calls/sigenter-openbsd.c | 1 + libc/calls/sigenter-xnu.c | 1 + libc/calls/struct/stat.macros.h | 9 ++- libc/integral/normalize.inc | 2 +- libc/isystem/linux/limits.h | 11 +++ libc/isystem/linux/types.h | 25 +++++++ libc/isystem/mntent.h | 4 ++ libc/runtime/efimain.greg.c | 3 +- libc/runtime/enable_threads.c | 1 + libc/runtime/enable_tls.c | 1 + libc/runtime/hook.greg.c | 1 + libc/runtime/memtrack.internal.h | 1 + libc/runtime/morph.greg.c | 1 + libc/runtime/runtime.h | 21 ------ libc/stdio/__fseterr.c | 24 +++++++ libc/stdio/stdio_ext.h | 1 + libc/sysv/consts.sh | 3 + libc/sysv/consts/limits.h | 6 ++ libc/testlib/benchrunner.c | 1 + libc/thread/sem_name.c | 7 +- libc/thread/sem_open.c | 7 +- test/libc/runtime/brk_test.c | 1 + test/libc/stdio/getrandom_test.c | 1 + test/libc/thread/sem_open_test.c | 42 ++++++----- third_party/musl/mntent.c | 118 +++++++++++++++++++++++++++++++ third_party/musl/mntent.h | 37 ++++++++++ 34 files changed, 319 insertions(+), 48 deletions(-) create mode 100644 ape/sections.internal.h create mode 100644 libc/isystem/linux/limits.h create mode 100644 libc/isystem/linux/types.h create mode 100644 libc/isystem/mntent.h create mode 100644 libc/stdio/__fseterr.c create mode 100644 third_party/musl/mntent.c create mode 100644 third_party/musl/mntent.h diff --git a/ape/sections.internal.h b/ape/sections.internal.h new file mode 100644 index 000000000..3e40e8762 --- /dev/null +++ b/ape/sections.internal.h @@ -0,0 +1,30 @@ +#ifndef COSMOPOLITAN_APE_SECTIONS_INTERNAL_H_ +#define COSMOPOLITAN_APE_SECTIONS_INTERNAL_H_ +#if !(__ASSEMBLER__ + __LINKER__ + 0) +COSMOPOLITAN_C_START_ + +extern unsigned char _base[]; +extern unsigned char _ehead[]; +extern unsigned char _etext[]; +extern unsigned char _edata[]; +extern unsigned char _ezip[]; +extern unsigned char _end[]; +extern unsigned char _ereal[]; +extern unsigned char __privileged_start[]; +extern unsigned char __privileged_addr[]; +extern unsigned char __privileged_size[]; +extern unsigned char __privileged_end[]; +extern unsigned char __test_start[]; +extern unsigned char __ro[]; +extern unsigned char *__relo_start[]; +extern unsigned char *__relo_end[]; +extern uint8_t __zip_start[]; +extern uint8_t __zip_end[]; +extern uint8_t __data_start[]; +extern uint8_t __data_end[]; +extern uint8_t __bss_start[]; +extern uint8_t __bss_end[]; + +COSMOPOLITAN_C_END_ +#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* COSMOPOLITAN_APE_SECTIONS_INTERNAL_H_ */ diff --git a/examples/getrandom.c b/examples/getrandom.c index baf8df0ce..dd9aaa83f 100644 --- a/examples/getrandom.c +++ b/examples/getrandom.c @@ -7,6 +7,7 @@ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif +#include "ape/sections.internal.h" #include "libc/calls/calls.h" #include "libc/calls/struct/sigaction.h" #include "libc/errno.h" diff --git a/examples/nomodifyself.c b/examples/nomodifyself.c index c3c5be75f..9b5553f31 100644 --- a/examples/nomodifyself.c +++ b/examples/nomodifyself.c @@ -7,6 +7,7 @@ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif +#include "ape/sections.internal.h" #include "libc/dce.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" diff --git a/libc/calls/__sig2.c b/libc/calls/__sig2.c index 4f3e0d039..d784b23ed 100644 --- a/libc/calls/__sig2.c +++ b/libc/calls/__sig2.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/calls/sig.internal.h" diff --git a/libc/calls/openat-metal.c b/libc/calls/openat-metal.c index 0decc6c94..7611c3473 100644 --- a/libc/calls/openat-metal.c +++ b/libc/calls/openat-metal.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/calls/internal.h" #include "libc/calls/metalfile.internal.h" #include "libc/intrin/weaken.h" diff --git a/libc/calls/pledge-linux.c b/libc/calls/pledge-linux.c index 41e76c1dc..0e49c94fa 100644 --- a/libc/calls/pledge-linux.c +++ b/libc/calls/pledge-linux.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/calls/calls.h" #include "libc/calls/pledge.internal.h" #include "libc/calls/struct/bpf.h" diff --git a/libc/calls/sigenter-freebsd.c b/libc/calls/sigenter-freebsd.c index b004d9886..8e22c94ff 100644 --- a/libc/calls/sigenter-freebsd.c +++ b/libc/calls/sigenter-freebsd.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/calls/calls.h" #include "libc/calls/internal.h" #include "libc/calls/state.internal.h" diff --git a/libc/calls/sigenter-netbsd.c b/libc/calls/sigenter-netbsd.c index 98333eb70..41b75eccf 100644 --- a/libc/calls/sigenter-netbsd.c +++ b/libc/calls/sigenter-netbsd.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/calls/calls.h" #include "libc/calls/internal.h" #include "libc/calls/state.internal.h" diff --git a/libc/calls/sigenter-openbsd.c b/libc/calls/sigenter-openbsd.c index 2a9b4e3f0..74a9f3e5d 100644 --- a/libc/calls/sigenter-openbsd.c +++ b/libc/calls/sigenter-openbsd.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/calls/calls.h" #include "libc/calls/internal.h" #include "libc/calls/state.internal.h" diff --git a/libc/calls/sigenter-xnu.c b/libc/calls/sigenter-xnu.c index e9fb20ced..14c2d7f2d 100644 --- a/libc/calls/sigenter-xnu.c +++ b/libc/calls/sigenter-xnu.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/calls/calls.h" #include "libc/calls/internal.h" #include "libc/calls/state.internal.h" diff --git a/libc/calls/struct/stat.macros.h b/libc/calls/struct/stat.macros.h index e3712fe3e..6bfb1e105 100644 --- a/libc/calls/struct/stat.macros.h +++ b/libc/calls/struct/stat.macros.h @@ -11,11 +11,10 @@ #define st_mtime_nsec st_mtim.tv_nsec #define st_ctime_nsec st_ctim.tv_nsec -#define st_atimensec st_atim.tv_nsec -#define st_mtimensec st_mtim.tv_nsec -#define st_ctimensec st_ctim.tv_nsec -#define st_birthtime st_birthtim.tv_sec -#define st_birthtimensec st_birthtim.tv_nsec +#define st_atimensec st_atim.tv_nsec +#define st_mtimensec st_mtim.tv_nsec +#define st_ctimensec st_ctim.tv_nsec +#define st_birthtime st_birthtim.tv_sec #define st_file_attributes st_flags diff --git a/libc/integral/normalize.inc b/libc/integral/normalize.inc index 30be7c9b9..82e053933 100644 --- a/libc/integral/normalize.inc +++ b/libc/integral/normalize.inc @@ -83,7 +83,7 @@ #define CHAR_BIT 8 /* b/c von neumann */ #define ARG_MAX 0xfffe /* for argv and envp; see CreateProcess (32767*2) */ #define PATH_MAX 1024 /* b/c _XOPEN_PATH_MAX */ -#define NAME_MAX 63 /* b/c dns */ +#define NAME_MAX 255 /* 511 on netbsd */ #define CHILD_MAX 16 /* only if malloc isn't linked */ #define OPEN_MAX 16 /* only if malloc isn't linked */ #define ATEXIT_MAX 32 /* only if malloc isn't linked */ diff --git a/libc/isystem/linux/limits.h b/libc/isystem/linux/limits.h new file mode 100644 index 000000000..eaa9d34cd --- /dev/null +++ b/libc/isystem/linux/limits.h @@ -0,0 +1,11 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_LINUX_LIMITS_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_LINUX_LIMITS_H_ +#include "libc/sysv/consts/limits.h" + +#define NR_OPEN 1024 +#define XATTR_NAME_MAX 255 +#define XATTR_SIZE_MAX 65536 +#define XATTR_LIST_MAX 65536 +#define RTSIG_MAX 32 + +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_LINUX_LIMITS_H_ */ diff --git a/libc/isystem/linux/types.h b/libc/isystem/linux/types.h new file mode 100644 index 000000000..68f3fdeb3 --- /dev/null +++ b/libc/isystem/linux/types.h @@ -0,0 +1,25 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_LINUX_TYPES_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_LINUX_TYPES_H_ +#if !(__ASSEMBLER__ + __LINKER__ + 0) +COSMOPOLITAN_C_START_ + +typedef uint16_t __u16; +typedef uint32_t __u32; +typedef uint64_t __u64; + +typedef __u16 __le16; +typedef __u16 __be16; +typedef __u32 __le32; +typedef __u32 __be32; +typedef __u64 __le64; +typedef __u64 __be64; +typedef __u16 __sum16; +typedef __u32 __wsum; + +#define __aligned_u64 __u64 __attribute__((__aligned__(8))) +#define __aligned_be64 __be64 __attribute__((__aligned__(8))) +#define __aligned_le64 __le64 __attribute__((__aligned__(8))) + +COSMOPOLITAN_C_END_ +#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_LINUX_TYPES_H_ */ diff --git a/libc/isystem/mntent.h b/libc/isystem/mntent.h new file mode 100644 index 000000000..6f2aafa32 --- /dev/null +++ b/libc/isystem/mntent.h @@ -0,0 +1,4 @@ +#ifndef COSMOPOLITAN_LIBC_ISYSTEM_MNTENT_H_ +#define COSMOPOLITAN_LIBC_ISYSTEM_MNTENT_H_ +#include "third_party/musl/mntent.h" +#endif /* COSMOPOLITAN_LIBC_ISYSTEM_MNTENT_H_ */ diff --git a/libc/runtime/efimain.greg.c b/libc/runtime/efimain.greg.c index aa02aa272..b64b21d0b 100644 --- a/libc/runtime/efimain.greg.c +++ b/libc/runtime/efimain.greg.c @@ -16,8 +16,9 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/intrin/bits.h" +#include "ape/sections.internal.h" #include "libc/dce.h" +#include "libc/intrin/bits.h" #include "libc/macros.internal.h" #include "libc/nt/efi.h" #include "libc/nt/thunk/msabi.h" diff --git a/libc/runtime/enable_threads.c b/libc/runtime/enable_threads.c index dd298b785..334e64d60 100644 --- a/libc/runtime/enable_threads.c +++ b/libc/runtime/enable_threads.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/intrin/strace.internal.h" diff --git a/libc/runtime/enable_tls.c b/libc/runtime/enable_tls.c index 619176d37..2a5117234 100644 --- a/libc/runtime/enable_tls.c +++ b/libc/runtime/enable_tls.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/assert.h" #include "libc/calls/calls.h" #include "libc/calls/syscall-sysv.internal.h" diff --git a/libc/runtime/hook.greg.c b/libc/runtime/hook.greg.c index 24d7de0a3..7f8ac9456 100644 --- a/libc/runtime/hook.greg.c +++ b/libc/runtime/hook.greg.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/calls/struct/sigset.h" #include "libc/runtime/runtime.h" #include "libc/runtime/symbols.internal.h" diff --git a/libc/runtime/memtrack.internal.h b/libc/runtime/memtrack.internal.h index 9cde5f2f8..5c14ca304 100644 --- a/libc/runtime/memtrack.internal.h +++ b/libc/runtime/memtrack.internal.h @@ -1,5 +1,6 @@ #ifndef COSMOPOLITAN_LIBC_RUNTIME_MEMTRACK_H_ #define COSMOPOLITAN_LIBC_RUNTIME_MEMTRACK_H_ +#include "ape/sections.internal.h" #include "libc/dce.h" #include "libc/intrin/nopl.internal.h" #include "libc/macros.internal.h" diff --git a/libc/runtime/morph.greg.c b/libc/runtime/morph.greg.c index e8b035153..27fcf7930 100644 --- a/libc/runtime/morph.greg.c +++ b/libc/runtime/morph.greg.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #define ShouldUseMsabiAttribute() 1 +#include "ape/sections.internal.h" #include "libc/calls/internal.h" #include "libc/calls/struct/sigset.h" #include "libc/dce.h" diff --git a/libc/runtime/runtime.h b/libc/runtime/runtime.h index 4421669c5..954556c1b 100644 --- a/libc/runtime/runtime.h +++ b/libc/runtime/runtime.h @@ -25,27 +25,6 @@ extern uint64_t kStartTsc; /* RII */ extern char kTmpPath[]; /* RII */ extern const char kNtSystemDirectory[]; /* RII */ extern const char kNtWindowsDirectory[]; /* RII */ -extern unsigned char _base[]; /* αpε */ -extern unsigned char _ehead[]; /* αpε */ -extern unsigned char _etext[]; /* αpε */ -extern unsigned char _edata[]; /* αpε */ -extern unsigned char _ezip[]; /* αpε */ -extern unsigned char _end[]; /* αpε */ -extern unsigned char _ereal[]; /* αpε */ -extern unsigned char __privileged_start[]; /* αpε */ -extern unsigned char __privileged_addr[]; /* αpε */ -extern unsigned char __privileged_size[]; /* αpε */ -extern unsigned char __privileged_end[]; /* αpε */ -extern unsigned char __test_start[]; /* αpε */ -extern unsigned char __ro[]; /* αpε */ -extern unsigned char *__relo_start[]; /* αpε */ -extern unsigned char *__relo_end[]; /* αpε */ -extern uint8_t __zip_start[]; /* αpε */ -extern uint8_t __zip_end[]; /* αpε */ -extern uint8_t __data_start[]; /* αpε */ -extern uint8_t __data_end[]; /* αpε */ -extern uint8_t __bss_start[]; /* αpε */ -extern uint8_t __bss_end[]; /* αpε */ extern size_t __virtualmax; extern bool __isworker; diff --git a/libc/stdio/__fseterr.c b/libc/stdio/__fseterr.c new file mode 100644 index 000000000..c975a1678 --- /dev/null +++ b/libc/stdio/__fseterr.c @@ -0,0 +1,24 @@ +/*-*- 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 2022 Justine Alexandra Roberts Tunney │ +│ │ +│ 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. │ +│ │ +│ 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. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/errno.h" +#include "libc/stdio/stdio_ext.h" + +void __fseterr(FILE *f) { + f->state |= EIO; +} diff --git a/libc/stdio/stdio_ext.h b/libc/stdio/stdio_ext.h index ea8b67136..210b9f3be 100644 --- a/libc/stdio/stdio_ext.h +++ b/libc/stdio/stdio_ext.h @@ -19,6 +19,7 @@ int __fwriting(FILE *); int __fsetlocking(FILE *, int); void _flushlbf(void); void __fpurge(FILE *); +void __fseterr(FILE *); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/libc/sysv/consts.sh b/libc/sysv/consts.sh index d98ce05f1..8fd9fd3f3 100755 --- a/libc/sysv/consts.sh +++ b/libc/sysv/consts.sh @@ -1228,6 +1228,9 @@ syscon mount MNT_SNAPSHOT 0 0x40000000 0x01000000 0 0 0 # confusing # group name GNU/Systemd XNU's Not UNIX! FreeBSD OpenBSD NetBSD The New Technology Commentary syscon limits PIPE_BUF 4096 512 512 512 512 4096 # bsd consensus syscon limits NGROUPS_MAX 65536 16 1023 16 16 0 # +syscon limits LINK_MAX 127 32767 32767 32767 32767 64 # freebsd/windows are educated guesses +syscon limits MAX_CANON 255 1024 255 255 255 255 # windows is guessed +syscon limits MAX_INPUT 255 1024 255 255 255 255 # windows is guessed syscon limits SOMAXCONN 4096 128 128 128 128 2147483647 # maximum backlog for listen() syscon limits _ARG_MAX 128*1024 1024*1024 512*1024 512*1024 256*1024 32767*2 # bsd consensus syscon limits _NAME_MAX 255 255 255 255 511 255 # probably higher on windows? diff --git a/libc/sysv/consts/limits.h b/libc/sysv/consts/limits.h index 338f42840..0fdf1ee18 100644 --- a/libc/sysv/consts/limits.h +++ b/libc/sysv/consts/limits.h @@ -11,6 +11,9 @@ extern const int _ARG_MAX; extern const int _NAME_MAX; extern const int _NSIG; extern const int _PATH_MAX; +extern const int LINK_MAX; +extern const int MAX_CANON; +extern const int MAX_INPUT; COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ @@ -22,5 +25,8 @@ COSMOPOLITAN_C_END_ #define _NAME_MAX SYMBOLIC(_NAME_MAX) #define _NSIG SYMBOLIC(_NSIG) #define _PATH_MAX SYMBOLIC(_PATH_MAX) +#define LINK_MAX SYMBOLIC(LINK_MAX) +#define MAX_CANON SYMBOLIC(MAX_CANON) +#define MAX_INPUT SYMBOLIC(MAX_INPUT) #endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_LIMITS_H_ */ diff --git a/libc/testlib/benchrunner.c b/libc/testlib/benchrunner.c index 0843b922c..658ab9b49 100644 --- a/libc/testlib/benchrunner.c +++ b/libc/testlib/benchrunner.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/calls/calls.h" #include "libc/calls/kntprioritycombos.internal.h" #include "libc/errno.h" diff --git a/libc/thread/sem_name.c b/libc/thread/sem_name.c index 46755d86b..1697c77ec 100644 --- a/libc/thread/sem_name.c +++ b/libc/thread/sem_name.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/intrin/kprintf.h" #include "libc/runtime/runtime.h" #include "libc/str/path.h" #include "libc/str/str.h" @@ -23,12 +24,14 @@ #include "libc/thread/thread.h" const char *__sem_name(const char *name, char path[hasatleast PATH_MAX]) { + const char *res; if (_isabspath(name)) { - return name; + res = name; } else { strlcpy(path, kTmpPath, PATH_MAX); strlcat(path, ".sem-", PATH_MAX); strlcat(path, name, PATH_MAX); - return path; + res = path; } + return res; } diff --git a/libc/thread/sem_open.c b/libc/thread/sem_open.c index 642988106..63ecca554 100644 --- a/libc/thread/sem_open.c +++ b/libc/thread/sem_open.c @@ -66,7 +66,12 @@ sem_t *sem_open(const char *name, int oflag, ...) { } sem = mmap(0, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (sem != MAP_FAILED) sem->sem_pshared = true; + if (sem != MAP_FAILED) { + sem->sem_pshared = true; + } else { + sem = SEM_FAILED; + } + _npassert(!close(fd)); return sem; } diff --git a/test/libc/runtime/brk_test.c b/test/libc/runtime/brk_test.c index e8257e48b..15e056835 100644 --- a/test/libc/runtime/brk_test.c +++ b/test/libc/runtime/brk_test.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/calls/struct/rlimit.h" #include "libc/dce.h" #include "libc/errno.h" diff --git a/test/libc/stdio/getrandom_test.c b/test/libc/stdio/getrandom_test.c index 963831d73..3f8f96001 100644 --- a/test/libc/stdio/getrandom_test.c +++ b/test/libc/stdio/getrandom_test.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "ape/sections.internal.h" #include "libc/errno.h" #include "libc/intrin/bits.h" #include "libc/log/check.h" diff --git a/test/libc/thread/sem_open_test.c b/test/libc/thread/sem_open_test.c index 0781e2934..7e77d226a 100644 --- a/test/libc/thread/sem_open_test.c +++ b/test/libc/thread/sem_open_test.c @@ -17,6 +17,7 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/dce.h" +#include "libc/errno.h" #include "libc/mem/gc.h" #include "libc/mem/mem.h" #include "libc/runtime/runtime.h" @@ -31,10 +32,10 @@ pthread_barrier_t barrier; char testlib_enable_tmp_setup_teardown; void *Worker(void *arg) { - sem_t *s[2]; + sem_t *a, *b; struct timespec ts; - ASSERT_NE(SEM_FAILED, (s[0] = sem_open("fooz", O_CREAT, 0644))); - ASSERT_NE(SEM_FAILED, (s[1] = sem_open("barz", O_CREAT, 0644))); + ASSERT_NE(SEM_FAILED, (a = sem_open("fooz", O_CREAT, 0644))); + ASSERT_NE(SEM_FAILED, (b = sem_open("barz", O_CREAT, 0644))); if (pthread_barrier_wait(&barrier) == PTHREAD_BARRIER_SERIAL_THREAD) { if (!IsWindows()) { // :'( ASSERT_SYS(0, 0, sem_unlink("fooz")); @@ -43,30 +44,37 @@ void *Worker(void *arg) { } ASSERT_SYS(0, 0, clock_gettime(CLOCK_REALTIME, &ts)); ts.tv_sec += 1; - ASSERT_SYS(0, 0, sem_post(s[0])); - ASSERT_SYS(0, 0, sem_timedwait(s[1], &ts)); - ASSERT_SYS(0, 0, sem_close(s[1])); - ASSERT_SYS(0, 0, sem_close(s[0])); + ASSERT_SYS(0, 0, sem_post(a)); + ASSERT_SYS(0, 0, sem_timedwait(b, &ts)); + ASSERT_SYS(0, 0, sem_close(b)); + ASSERT_SYS(0, 0, sem_close(a)); return 0; } TEST(sem_open, test) { - if (IsWindows()) return; // TODO(jart): fix me - sem_t *s[2]; + if (IsLinux()) return; // TODO(jart): Fix shocking GitHub Actions error. + sem_t *a, *b; int i, r, n = 4; pthread_t *t = _gc(malloc(sizeof(pthread_t) * n)); + sem_unlink("barz"); + sem_unlink("fooz"); + errno = 0; ASSERT_EQ(0, pthread_barrier_init(&barrier, 0, n)); - ASSERT_NE(SEM_FAILED, (s[0] = sem_open("fooz", O_CREAT, 0644))); - ASSERT_NE(SEM_FAILED, (s[1] = sem_open("barz", O_CREAT, 0644))); + ASSERT_NE(SEM_FAILED, (a = sem_open("fooz", O_CREAT, 0644))); + ASSERT_NE(SEM_FAILED, (b = sem_open("barz", O_CREAT, 0644))); + ASSERT_SYS(0, 0, sem_getvalue(a, &r)); + ASSERT_EQ(0, r); + ASSERT_SYS(0, 0, sem_getvalue(b, &r)); + ASSERT_EQ(0, r); for (i = 0; i < n; ++i) ASSERT_EQ(0, pthread_create(t + i, 0, Worker, 0)); - for (i = 0; i < n; ++i) ASSERT_SYS(0, 0, sem_wait(s[0])); - ASSERT_SYS(0, 0, sem_getvalue(s[0], &r)); + for (i = 0; i < n; ++i) ASSERT_SYS(0, 0, sem_wait(a)); + ASSERT_SYS(0, 0, sem_getvalue(a, &r)); ASSERT_EQ(0, r); - for (i = 0; i < n; ++i) ASSERT_SYS(0, 0, sem_post(s[1])); + for (i = 0; i < n; ++i) ASSERT_SYS(0, 0, sem_post(b)); for (i = 0; i < n; ++i) ASSERT_EQ(0, pthread_join(t[i], 0)); - ASSERT_SYS(0, 0, sem_getvalue(s[1], &r)); + ASSERT_SYS(0, 0, sem_getvalue(b, &r)); ASSERT_EQ(0, r); - ASSERT_SYS(0, 0, sem_close(s[1])); - ASSERT_SYS(0, 0, sem_close(s[0])); + ASSERT_SYS(0, 0, sem_close(b)); + ASSERT_SYS(0, 0, sem_close(a)); ASSERT_EQ(0, pthread_barrier_destroy(&barrier)); } diff --git a/third_party/musl/mntent.c b/third_party/musl/mntent.c new file mode 100644 index 000000000..4770c1cf6 --- /dev/null +++ b/third_party/musl/mntent.c @@ -0,0 +1,118 @@ +/*-*- 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│ +╚──────────────────────────────────────────────────────────────────────────────╝ +│ │ +│ Musl Libc │ +│ Copyright © 2005-2014 Rich Felker, et al. │ +│ │ +│ Permission is hereby granted, free of charge, to any person obtaining │ +│ a copy of this software and associated documentation files (the │ +│ "Software"), to deal in the Software without restriction, including │ +│ without limitation the rights to use, copy, modify, merge, publish, │ +│ distribute, sublicense, and/or sell copies of the Software, and to │ +│ permit persons to whom the Software is furnished to do so, subject to │ +│ the following conditions: │ +│ │ +│ The above copyright notice and this permission notice shall be │ +│ included in all copies or substantial portions of the Software. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, │ +│ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF │ +│ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. │ +│ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY │ +│ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, │ +│ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE │ +│ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ +│ │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/calls.h" +#include "libc/errno.h" +#include "libc/fmt/fmt.h" +#include "libc/limits.h" +#include "libc/stdio/stdio.h" +#include "libc/str/str.h" +#include "third_party/musl/mntent.h" + +asm(".ident\t\"\\n\\n\ +Musl libc (MIT License)\\n\ +Copyright 2005-2014 Rich Felker, et. al.\""); +asm(".include \"libc/disclaimer.inc\""); +// clang-format off + +static char *internal_buf; +static size_t internal_bufsize; + +#define SENTINEL (char *)&internal_buf + +FILE *setmntent(const char *name, const char *mode) +{ + return fopen(name, mode); +} + +int endmntent(FILE *f) +{ + if (f) fclose(f); + return 1; +} + +struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen) +{ + int n[8], use_internal = (linebuf == SENTINEL); + size_t len, i; + + mnt->mnt_freq = 0; + mnt->mnt_passno = 0; + + do { + if (use_internal) { + getline(&internal_buf, &internal_bufsize, f); + linebuf = internal_buf; + } else { + fgets(linebuf, buflen, f); + } + if (feof(f) || ferror(f)) return 0; + if (!strchr(linebuf, '\n')) { + fscanf(f, "%*[^\n]%*[\n]"); + errno = ERANGE; + return 0; + } + + len = strlen(linebuf); + if (len > INT_MAX) continue; + for (i = 0; i < sizeof n / sizeof *n; i++) n[i] = len; + sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d", + n, n+1, n+2, n+3, n+4, n+5, n+6, n+7, + &mnt->mnt_freq, &mnt->mnt_passno); + } while (linebuf[n[0]] == '#' || n[1]==len); + + linebuf[n[1]] = 0; + linebuf[n[3]] = 0; + linebuf[n[5]] = 0; + linebuf[n[7]] = 0; + + mnt->mnt_fsname = linebuf+n[0]; + mnt->mnt_dir = linebuf+n[2]; + mnt->mnt_type = linebuf+n[4]; + mnt->mnt_opts = linebuf+n[6]; + + return mnt; +} + +struct mntent *getmntent(FILE *f) +{ + static struct mntent mnt; + return getmntent_r(f, &mnt, SENTINEL, 0); +} + +int addmntent(FILE *f, const struct mntent *mnt) +{ + if (fseek(f, 0, SEEK_END)) return 1; + return fprintf(f, "%s\t%s\t%s\t%s\t%d\t%d\n", + mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts, + mnt->mnt_freq, mnt->mnt_passno) < 0; +} + +char *hasmntopt(const struct mntent *mnt, const char *opt) +{ + return strstr(mnt->mnt_opts, opt); +} diff --git a/third_party/musl/mntent.h b/third_party/musl/mntent.h new file mode 100644 index 000000000..79f3b5264 --- /dev/null +++ b/third_party/musl/mntent.h @@ -0,0 +1,37 @@ +#ifndef COSMOPOLITAN_THIRD_PARTY_MUSL_MNTENT_H_ +#define COSMOPOLITAN_THIRD_PARTY_MUSL_MNTENT_H_ +#include "libc/stdio/stdio.h" +#if !(__ASSEMBLER__ + __LINKER__ + 0) +COSMOPOLITAN_C_START_ + +#define MOUNTED "/etc/mtab" + +#define MNTTYPE_IGNORE "ignore" +#define MNTTYPE_NFS "nfs" +#define MNTTYPE_SWAP "swap" +#define MNTOPT_DEFAULTS "defaults" +#define MNTOPT_RO "ro" +#define MNTOPT_RW "rw" +#define MNTOPT_SUID "suid" +#define MNTOPT_NOSUID "nosuid" +#define MNTOPT_NOAUTO "noauto" + +struct mntent { + char *mnt_fsname; + char *mnt_dir; + char *mnt_type; + char *mnt_opts; + int mnt_freq; + int mnt_passno; +}; + +FILE *setmntent(const char *, const char *); +int endmntent(FILE *); +struct mntent *getmntent(FILE *); +struct mntent *getmntent_r(FILE *, struct mntent *, char *, int); +int addmntent(FILE *, const struct mntent *); +char *hasmntopt(const struct mntent *, const char *); + +COSMOPOLITAN_C_END_ +#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* COSMOPOLITAN_THIRD_PARTY_MUSL_MNTENT_H_ */