From af2ef0d223b94b1434821aab61067e56280c3412 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Fri, 14 Oct 2022 09:52:35 -0700 Subject: [PATCH] Fix the build --- libc/calls/struct/stat.macros.h | 9 +++---- libc/stdio/__fseterr.c | 24 ++++++++++++++++++ libc/stdio/stdio_ext.h | 1 + libc/thread/sem_name.c | 7 ++++-- libc/thread/sem_open.c | 7 +++++- test/libc/thread/sem_open_test.c | 42 +++++++++++++++++++------------- 6 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 libc/stdio/__fseterr.c 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/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/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/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)); }