Skip to content

Commit 467a332

Browse files
committed
Introduce sigtimedwait() and sigwaitinfo()
This change also invents sigcountset() and strsignal_r() and improves the quality of siginfo_t handling.
1 parent 7ae5564 commit 467a332

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+887
-345
lines changed

libc/calls/calls.mk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ $(LIBC_CALLS_A).pkg: \
6565
$(LIBC_CALLS_A_OBJS) \
6666
$(foreach x,$(LIBC_CALLS_A_DIRECTDEPS),$($(x)_A).pkg)
6767

68+
# we can't use asan because:
69+
# siginfo_t memory is owned by kernels
70+
o/$(MODE)/libc/calls/siginfo2cosmo.o: private \
71+
OVERRIDE_COPTS += \
72+
-ffreestanding \
73+
-fno-sanitize=address
74+
6875
# we can't use asan because:
6976
# ucontext_t memory is owned by xnu kernel
7077
o/$(MODE)/libc/calls/sigenter-xnu.o: private \

libc/calls/pledge-linux.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ static const uint16_t kPledgeStdio[] = {
556556
__NR_linux_prlimit | STDIO, //
557557
__NR_linux_sched_getaffinity, //
558558
__NR_linux_sched_setaffinity, //
559+
__NR_linux_sigtimedwait, //
559560
};
560561

561562
static const uint16_t kPledgeFlock[] = {

libc/calls/sigenter-freebsd.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "libc/calls/struct/sigaction-freebsd.internal.h"
2323
#include "libc/calls/struct/sigaction.h"
2424
#include "libc/calls/struct/siginfo-freebsd.internal.h"
25+
#include "libc/calls/struct/siginfo-meta.internal.h"
2526
#include "libc/calls/struct/siginfo.h"
2627
#include "libc/calls/struct/ucontext-freebsd.internal.h"
2728
#include "libc/calls/ucontext.h"
@@ -75,16 +76,7 @@ privileged void __sigenter_freebsd(int sig, struct siginfo_freebsd *freebsdinfo,
7576
g.uc.uc_mcontext.err = ctx->uc_mcontext.mc_err;
7677
g.uc.uc_mcontext.trapno = ctx->uc_mcontext.mc_trapno;
7778
__repmovsb(&g.uc.__fpustate, &ctx->uc_mcontext.mc_fpstate, 512);
78-
g.si.si_signo = freebsdinfo->si_signo;
79-
g.si.si_errno = freebsdinfo->si_errno;
80-
g.si.si_code = freebsdinfo->si_code;
81-
if (freebsdinfo->si_pid) {
82-
g.si.si_pid = freebsdinfo->si_pid;
83-
g.si.si_uid = freebsdinfo->si_uid;
84-
} else {
85-
g.si.si_addr = (void *)freebsdinfo->si_addr;
86-
}
87-
g.si.si_value = freebsdinfo->si_value;
79+
__siginfo2cosmo(&g.si, (void *)freebsdinfo);
8880
((sigaction_f)(_base + rva))(sig, &g.si, &g.uc);
8981
ctx->uc_stack.ss_sp = g.uc.uc_stack.ss_sp;
9082
ctx->uc_stack.ss_size = g.uc.uc_stack.ss_size;

libc/calls/sigenter-netbsd.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "libc/calls/state.internal.h"
2222
#include "libc/calls/struct/sigaction-freebsd.internal.h"
2323
#include "libc/calls/struct/sigaction.h"
24+
#include "libc/calls/struct/siginfo-meta.internal.h"
2425
#include "libc/calls/struct/siginfo-netbsd.internal.h"
2526
#include "libc/calls/struct/siginfo.h"
2627
#include "libc/calls/struct/ucontext-netbsd.internal.h"
@@ -43,13 +44,7 @@ privileged void __sigenter_netbsd(int sig, struct siginfo_netbsd *si,
4344
((sigaction_f)(_base + rva))(sig, 0, 0);
4445
} else {
4546
__repstosb(&uc, 0, sizeof(uc));
46-
__repstosb(&si2, 0, sizeof(si2));
47-
si2.si_signo = si->si_signo;
48-
si2.si_code = si->si_code;
49-
si2.si_errno = si->si_errno;
50-
si2.si_pid = si->si_pid;
51-
si2.si_uid = si->si_uid;
52-
si2.si_value = si->si_value;
47+
__siginfo2cosmo(&si2, (void *)si);
5348
uc.uc_mcontext.fpregs = &uc.__fpustate;
5449
uc.uc_flags = ctx->uc_flags;
5550
uc.uc_stack.ss_sp = ctx->uc_stack.ss_sp;

libc/calls/sigenter-openbsd.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "libc/calls/state.internal.h"
2222
#include "libc/calls/struct/sigaction-freebsd.internal.h"
2323
#include "libc/calls/struct/sigaction.h"
24+
#include "libc/calls/struct/siginfo-meta.internal.h"
2425
#include "libc/calls/struct/siginfo-openbsd.internal.h"
2526
#include "libc/calls/struct/siginfo.h"
2627
#include "libc/calls/struct/ucontext-openbsd.internal.h"
@@ -44,17 +45,8 @@ privileged void __sigenter_openbsd(int sig, struct siginfo_openbsd *openbsdinfo,
4445
if (~flags & SA_SIGINFO) {
4546
((sigaction_f)(_base + rva))(sig, 0, 0);
4647
} else {
47-
__repstosb(&g, 0, sizeof(g));
48-
g.si.si_signo = openbsdinfo->si_signo;
49-
g.si.si_code = openbsdinfo->si_code;
50-
g.si.si_errno = openbsdinfo->si_errno;
51-
if (openbsdinfo->si_pid) {
52-
g.si.si_pid = openbsdinfo->si_pid;
53-
g.si.si_uid = openbsdinfo->si_uid;
54-
} else {
55-
g.si.si_addr = (void *)openbsdinfo->si_addr;
56-
}
57-
g.si.si_value = openbsdinfo->si_value;
48+
__repstosb(&g.uc, 0, sizeof(g.uc));
49+
__siginfo2cosmo(&g.si, (void *)openbsdinfo);
5850
g.uc.uc_mcontext.fpregs = &g.uc.__fpustate;
5951
__repmovsb(&g.uc.uc_sigmask, &ctx->sc_mask,
6052
MIN(sizeof(g.uc.uc_sigmask), sizeof(ctx->sc_mask)));

libc/calls/sigenter-xnu.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "libc/calls/state.internal.h"
2222
#include "libc/calls/struct/metasigaltstack.h"
2323
#include "libc/calls/struct/sigaction.h"
24+
#include "libc/calls/struct/siginfo-meta.internal.h"
2425
#include "libc/calls/struct/siginfo-xnu.internal.h"
2526
#include "libc/calls/struct/siginfo.h"
2627
#include "libc/calls/ucontext.h"
@@ -492,16 +493,7 @@ privileged void __sigenter_xnu(void *fn, int infostyle, int sig,
492493
}
493494
}
494495
if (xnuinfo) {
495-
g.si.si_signo = xnuinfo->si_signo;
496-
g.si.si_errno = xnuinfo->si_errno;
497-
g.si.si_code = xnuinfo->si_code;
498-
if (xnuinfo->si_pid) {
499-
g.si.si_pid = xnuinfo->si_pid;
500-
g.si.si_uid = xnuinfo->si_uid;
501-
} else {
502-
g.si.si_addr = (void *)xnuinfo->si_addr;
503-
}
504-
g.si.si_value = xnuinfo->si_value;
496+
__siginfo2cosmo(&g.si, (void *)xnuinfo);
505497
}
506498
((sigaction_f)(_base + rva))(sig, &g.si, &g.uc);
507499
if (xnuctx) {

libc/calls/siginfo2cosmo.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2022 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/calls/struct/siginfo-meta.internal.h"
20+
#include "libc/calls/struct/siginfo.h"
21+
#include "libc/dce.h"
22+
#include "libc/sysv/consts/sig.h"
23+
24+
privileged void __siginfo2cosmo(struct siginfo *si,
25+
const union siginfo_meta *m) {
26+
void *si_addr;
27+
int32_t si_signo;
28+
int32_t si_errno;
29+
int32_t si_code;
30+
int32_t si_pid;
31+
int32_t si_uid;
32+
int32_t si_status;
33+
int32_t si_timerid;
34+
int32_t si_overrun;
35+
union sigval si_value;
36+
37+
if (IsLinux()) {
38+
*si = m->linux;
39+
return;
40+
} else if (IsFreebsd()) {
41+
si_signo = m->freebsd.si_signo;
42+
si_errno = m->freebsd.si_errno;
43+
si_code = m->freebsd.si_code;
44+
si_pid = m->freebsd.si_pid;
45+
si_uid = m->freebsd.si_uid;
46+
si_status = m->freebsd.si_status;
47+
si_timerid = m->freebsd.si_timerid;
48+
si_overrun = m->freebsd.si_overrun;
49+
si_addr = m->freebsd.si_addr;
50+
si_value = m->freebsd.si_value;
51+
} else if (IsXnu()) {
52+
si_signo = m->xnu.si_signo;
53+
si_errno = m->xnu.si_errno;
54+
si_code = m->xnu.si_code;
55+
si_pid = m->xnu.si_pid;
56+
si_uid = m->xnu.si_uid;
57+
si_status = m->xnu.si_status;
58+
si_timerid = 0;
59+
si_overrun = 0;
60+
si_addr = m->xnu.si_addr;
61+
si_value = m->xnu.si_value;
62+
} else if (IsOpenbsd()) {
63+
si_signo = m->openbsd.si_signo;
64+
si_errno = m->openbsd.si_errno;
65+
si_code = m->openbsd.si_code;
66+
si_pid = m->openbsd.si_pid;
67+
si_uid = m->openbsd.si_uid;
68+
si_status = m->openbsd.si_status;
69+
si_timerid = 0;
70+
si_overrun = 0;
71+
si_addr = m->openbsd.si_addr;
72+
si_value = m->openbsd.si_value;
73+
} else if (IsNetbsd()) {
74+
si_signo = m->netbsd.si_signo;
75+
si_errno = m->netbsd.si_errno;
76+
si_code = m->netbsd.si_code;
77+
si_pid = m->netbsd.si_pid;
78+
si_uid = m->netbsd.si_uid;
79+
si_status = m->netbsd.si_status;
80+
si_timerid = 0;
81+
si_overrun = 0;
82+
si_addr = m->netbsd.si_addr;
83+
si_value = m->netbsd.si_value;
84+
} else {
85+
notpossible;
86+
}
87+
88+
*si = (struct siginfo){0};
89+
si->si_signo = si_signo;
90+
si->si_errno = si_errno;
91+
si->si_code = si_code;
92+
si->si_value = si_value;
93+
if (si_signo == SIGILL || //
94+
si_signo == SIGFPE || //
95+
si_signo == SIGSEGV || //
96+
si_signo == SIGBUS || //
97+
si_signo == SIGTRAP) {
98+
si->si_addr = si_addr;
99+
} else if (si_signo == SIGALRM) {
100+
si->si_timerid = si_timerid;
101+
si->si_overrun = si_overrun;
102+
} else {
103+
si->si_pid = si_pid;
104+
si->si_uid = si_uid;
105+
}
106+
}

libc/calls/sigpending.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,44 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/calls/sig.internal.h"
20-
#include "libc/intrin/strace.internal.h"
2120
#include "libc/calls/struct/sigset.h"
2221
#include "libc/calls/struct/sigset.internal.h"
2322
#include "libc/dce.h"
2423
#include "libc/intrin/asan.internal.h"
2524
#include "libc/intrin/describeflags.internal.h"
25+
#include "libc/intrin/strace.internal.h"
2626
#include "libc/sysv/errfuns.h"
2727

2828
/**
2929
* Determines the blocked pending signals
3030
*
3131
* @param pending is where the bitset of pending signals is returned,
32-
* may not be NULL.
33-
* @return -1 w/ EFAULT
32+
* which may not be null
33+
* @return 0 on success, or -1 w/ errno
34+
* @raise EFAULT if `pending` points to invalid memory
3435
* @asyncsignalsafe
3536
*/
3637
int sigpending(sigset_t *pending) {
3738
int rc;
38-
if (IsAsan() && !__asan_is_valid(pending, sizeof(*pending))) {
39+
if (!pending || (IsAsan() && !__asan_is_valid(pending, sizeof(*pending)))) {
3940
rc = efault();
4041
} else if (IsLinux() || IsNetbsd() || IsOpenbsd() || IsFreebsd() || IsXnu()) {
42+
// 128 signals on NetBSD and FreeBSD, 64 on Linux, 32 on OpenBSD and XNU
4143
rc = sys_sigpending(pending, 8);
42-
// 128 signals on NetBSD and FreeBSD, 64 on Linux, 32 on OpenBSD and XNU.
44+
// OpenBSD passes signal sets in words rather than pointers
4345
if (IsOpenbsd()) {
4446
pending->__bits[0] = (unsigned)rc;
4547
rc = 0;
46-
} else if (IsXnu()) {
47-
pending->__bits[0] &= 0xFFFFFFFF;
4848
}
49-
if (IsLinux() || IsOpenbsd() || IsXnu()) {
50-
pending->__bits[1] = 0;
49+
// only modify memory on success
50+
if (!rc) {
51+
// clear unsupported bits
52+
if (IsXnu()) {
53+
pending->__bits[0] &= 0xFFFFFFFF;
54+
}
55+
if (IsLinux() || IsOpenbsd() || IsXnu()) {
56+
pending->__bits[1] = 0;
57+
}
5158
}
5259
} else if (IsWindows()) {
5360
sigemptyset(pending);

libc/calls/sigtimedwait.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2022 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/calls/asan.internal.h"
20+
#include "libc/calls/sigtimedwait.h"
21+
#include "libc/calls/sigtimedwait.internal.h"
22+
#include "libc/calls/struct/siginfo.internal.h"
23+
#include "libc/calls/struct/sigset.internal.h"
24+
#include "libc/calls/struct/timespec.internal.h"
25+
#include "libc/dce.h"
26+
#include "libc/intrin/asan.internal.h"
27+
#include "libc/intrin/strace.internal.h"
28+
#include "libc/str/str.h"
29+
#include "libc/sysv/errfuns.h"
30+
31+
/**
32+
* Waits for signal synchronously, w/ timeout.
33+
*
34+
* @param set is signals for which we'll be waiting
35+
* @param info if not null shall receive info about signal
36+
* @param timeout is relative deadline and null means wait forever
37+
* @return signal number on success, or -1 w/ errno
38+
* @raise EINTR if an asynchronous signal was delivered instead
39+
* @raise EINVAL if nanoseconds parameter was out of range
40+
* @raise EAGAIN if deadline expired
41+
* @raise ENOSYS on Windows, XNU, OpenBSD, Metal
42+
* @raise EFAULT if invalid memory was supplied
43+
*/
44+
int sigtimedwait(const sigset_t *set, siginfo_t *info,
45+
const struct timespec *timeout) {
46+
int rc;
47+
char strsig[15];
48+
struct timespec ts;
49+
union siginfo_meta si = {0};
50+
51+
if (IsAsan() && (!__asan_is_valid(set, sizeof(*set)) ||
52+
(info && !__asan_is_valid(info, sizeof(*info))) ||
53+
(timeout && !__asan_is_valid_timespec(timeout)))) {
54+
rc = efault();
55+
} else if (IsLinux() || IsFreebsd() || IsNetbsd()) {
56+
if (timeout) {
57+
// 1. Linux needs its size parameter
58+
// 2. NetBSD modifies timeout argument
59+
ts = *timeout;
60+
rc = sys_sigtimedwait(set, &si, &ts, 8);
61+
} else {
62+
rc = sys_sigtimedwait(set, &si, 0, 8);
63+
}
64+
if (rc != -1 && info) {
65+
__siginfo2cosmo(info, &si);
66+
}
67+
} else {
68+
rc = enosys();
69+
}
70+
71+
STRACE("sigtimedwait(%s, [%s], %s) → %s% m", DescribeSigset(0, set),
72+
DescribeSiginfo(rc, info), DescribeTimespec(0, timeout),
73+
strsignal_r(rc, strsig));
74+
return rc;
75+
}

libc/calls/sigtimedwait.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef COSMOPOLITAN_LIBC_CALLS_SIGTIMEDWAIT_H_
2+
#define COSMOPOLITAN_LIBC_CALLS_SIGTIMEDWAIT_H_
3+
#include "libc/calls/struct/siginfo.h"
4+
#include "libc/calls/struct/sigset.h"
5+
#include "libc/calls/struct/timespec.h"
6+
#if !(__ASSEMBLER__ + __LINKER__ + 0)
7+
COSMOPOLITAN_C_START_
8+
9+
int sigtimedwait(const sigset_t *, siginfo_t *, const struct timespec *);
10+
int sigwaitinfo(const sigset_t *, siginfo_t *);
11+
12+
COSMOPOLITAN_C_END_
13+
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
14+
#endif /* COSMOPOLITAN_LIBC_CALLS_SIGTIMEDWAIT_H_ */

0 commit comments

Comments
 (0)