Make signal handling work well across platforms

- Fix sigsuspend() on XNU
- Fix strsignal() on non-Linux
- Add unit tests for strsignal()
- Add unit tests for setitimer()
- Add unit tests for sigsuspend()
- Rewrite setitimer() for New Technology
- Rewrite nanosleep() for New Technology
- Polyfill SIGALRM on the New Technology
- select(0,0,0,0) on NT now calls pause()
- Remove some NTDLL calls that aren't needed
- Polyfill SA_NOCLDWAIT on the New Technology
- Polyfill SA_RESETHAND on the New Technology
- Polyfill sigprocmask() on the New Technology
- Polyfill SIGCHLD+SIG_IGN on the New Technology
- Polyfill SA_RESTART masking on the New Technology
- Deliver console signals from main thread on New Technology
- Document SA_RESTART behavior w/ @sarestartable / @norestart
- System call trace in MODE=dbg now prints inherited FDs and signal mask
This commit is contained in:
Justine Tunney 2022-03-25 07:11:44 -07:00
parent 3b9e66ecba
commit 072e1d2910
82 changed files with 1388 additions and 450 deletions

View file

@ -26,6 +26,7 @@
* @param inout_addrsize provides and receives addr's byte length
* @return client fd which needs close(), or -1 w/ errno
* @asyncsignalsafe
* @restartable (unless SO_RCVTIMEO)
*/
int accept(int fd, void *out_addr, uint32_t *inout_addrsize) {
return accept4(fd, out_addr, inout_addrsize, 0);

View file

@ -32,6 +32,7 @@
*
* @return 0 on success or -1 w/ errno
* @asyncsignalsafe
* @restartable (unless SO_RCVTIMEO)
*/
int connect(int fd, const void *addr, uint32_t addrsize) {
int rc;

View file

@ -1345,7 +1345,7 @@ static textwindows dontinline int sys_epoll_create1_nt(uint32_t flags) {
}
static textwindows dontinline int sys_epoll_ctl_nt(int epfd, int op, int fd,
struct epoll_event *ev) {
struct epoll_event *ev) {
int r;
struct PortState *port_state;
struct TsTreeNode *tree_node;
@ -1375,9 +1375,9 @@ static textwindows dontinline int sys_epoll_ctl_nt(int epfd, int op, int fd,
}
static textwindows dontinline int sys_epoll_wait_nt(int epfd,
struct epoll_event *events,
int maxevents,
int timeoutms) {
struct epoll_event *events,
int maxevents,
int timeoutms) {
int num_events;
struct PortState *port_state;
struct TsTreeNode *tree_node;
@ -1493,6 +1493,7 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev) {
* @param maxevents is array length of events
* @param timeoutms is milliseconds, 0 to not block, or -1 for forever
* @return number of events stored, 0 on timeout, or -1 w/ errno
* @norestart
*/
int epoll_wait(int epfd, struct epoll_event *events, int maxevents,
int timeoutms) {

View file

@ -19,6 +19,7 @@
#include "libc/bits/bits.h"
#include "libc/bits/weaken.h"
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/macros.internal.h"
#include "libc/nt/struct/pollfd.h"
@ -43,12 +44,8 @@ textwindows int sys_poll_nt(struct pollfd *fds, uint64_t nfds, uint64_t ms) {
}
}
for (;;) {
if (cmpxchg(&__interrupted, true, false) ||
(weaken(_check_sigchld) && weaken(_check_sigchld)()) ||
(weaken(_check_sigwinch) && weaken(_check_sigwinch)(g_fds.p + 0))) {
return eintr();
}
waitfor = MIN(1000, ms); /* for ctrl+c */
if (_check_interrupts(false, g_fds.p)) return eintr();
waitfor = MIN(__SIG_POLLING_INTERVAL_MS, ms); /* for ctrl+c */
if ((got = WSAPoll(ntfds, nfds, waitfor)) != -1) {
if (!got && (ms -= waitfor) > 0) continue;
for (i = 0; i < nfds; ++i) {

View file

@ -37,6 +37,7 @@
* @return fds[𝑖].revents flags can have:
* (fds[𝑖].events & POLL{IN,OUT,PRI,HUP,ERR,NVAL})
* @asyncsignalsafe
* @norestart
*/
int poll(struct pollfd *fds, uint64_t nfds, int32_t timeout_ms) {
int rc;

View file

@ -29,6 +29,7 @@
* @error EINTR, EHOSTUNREACH, ECONNRESET (UDP ICMP Port Unreachable),
* EPIPE (if MSG_NOSIGNAL), EMSGSIZE, ENOTSOCK, EFAULT, etc.
* @asyncsignalsafe
* @restartable (unless SO_RCVTIMEO)
*/
ssize_t recv(int fd, void *buf, size_t size, int flags) {
return recvfrom(fd, buf, size, flags, NULL, 0);

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/calls/struct/iovec.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
@ -40,36 +41,41 @@
* @error EINTR, EHOSTUNREACH, ECONNRESET (UDP ICMP Port Unreachable),
* EPIPE (if MSG_NOSIGNAL), EMSGSIZE, ENOTSOCK, EFAULT, etc.
* @asyncsignalsafe
* @restartable (unless SO_RCVTIMEO)
*/
ssize_t recvfrom(int fd, void *buf, size_t size, uint32_t flags,
void *opt_out_srcaddr, uint32_t *opt_inout_srcaddrsize) {
ssize_t got;
ssize_t rc, got;
if (IsAsan() &&
(!__asan_is_valid(buf, size) ||
(opt_out_srcaddr &&
!__asan_is_valid(opt_out_srcaddr, *opt_inout_srcaddrsize)))) {
return efault();
}
if (!IsWindows()) {
rc = efault();
} else if (IsWindows() && _check_interrupts(false, g_fds.p)) {
rc = eintr();
} else if (!IsWindows()) {
got = sys_recvfrom(fd, buf, size, flags, opt_out_srcaddr,
opt_inout_srcaddrsize);
if (opt_out_srcaddr && IsBsd() && got != -1) {
sockaddr2linux(opt_out_srcaddr);
}
return got;
rc = got;
} else {
if (__isfdopen(fd)) {
if (__isfdkind(fd, kFdSocket)) {
return sys_recvfrom_nt(&g_fds.p[fd], (struct iovec[]){{buf, size}}, 1,
flags, opt_out_srcaddr, opt_inout_srcaddrsize);
rc = sys_recvfrom_nt(&g_fds.p[fd], (struct iovec[]){{buf, size}}, 1,
flags, opt_out_srcaddr, opt_inout_srcaddrsize);
} else if (__isfdkind(fd, kFdFile) && !opt_out_srcaddr) { /* socketpair */
if (flags) return einval();
return sys_read_nt(&g_fds.p[fd], (struct iovec[]){{buf, size}}, 1, -1);
if (flags) rc = einval();
rc = sys_read_nt(&g_fds.p[fd], (struct iovec[]){{buf, size}}, 1, -1);
} else {
return enotsock();
rc = enotsock();
}
} else {
return ebadf();
rc = ebadf();
}
}
STRACE("recvfrom(%d, %#.*hhs, %'zu, %#x) → %'ld% m", fd, size, buf, size,
flags, rc);
return rc;
}

View file

@ -36,6 +36,7 @@
* @error EINTR, EHOSTUNREACH, ECONNRESET (UDP ICMP Port Unreachable),
* EPIPE (if MSG_NOSIGNAL), EMSGSIZE, ENOTSOCK, EFAULT, etc.
* @asyncsignalsafe
* @restartable (unless SO_RCVTIMEO)
*/
ssize_t recvmsg(int fd, struct msghdr *msg, int flags) {
ssize_t got;

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/popcnt.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
@ -119,12 +120,13 @@ int sys_select_nt(int nfds, fd_set *readfds, fd_set *writefds,
} else if (timeout) {
req.tv_sec = timeout->tv_sec;
req.tv_nsec = timeout->tv_usec * 1000;
if ((rc = sys_nanosleep_nt(&req, &rem)) != -1) {
timeout->tv_sec = rem.tv_sec;
timeout->tv_usec = rem.tv_nsec / 1000;
}
rem.tv_sec = 0;
rem.tv_nsec = 0;
rc = sys_nanosleep_nt(&req, &rem);
timeout->tv_sec = rem.tv_sec;
timeout->tv_usec = rem.tv_nsec / 1000;
} else {
rc = einval();
rc = pause();
}
return rc;
}

View file

@ -29,6 +29,7 @@
* @error EINTR, EHOSTUNREACH, ECONNRESET (UDP ICMP Port Unreachable),
* EPIPE (if MSG_NOSIGNAL), EMSGSIZE, ENOTSOCK, EFAULT, etc.
* @asyncsignalsafe
* @restartable (unless SO_RCVTIMEO)
*/
ssize_t send(int fd, const void *buf, size_t size, int flags) {
return sendto(fd, buf, size, flags, NULL, 0);

View file

@ -36,6 +36,7 @@
* @error EINTR, EHOSTUNREACH, ECONNRESET (UDP ICMP Port Unreachable),
* EPIPE (if MSG_NOSIGNAL), EMSGSIZE, ENOTSOCK, EFAULT, etc.
* @asyncsignalsafe
* @restartable (unless SO_RCVTIMEO)
*/
ssize_t sendmsg(int fd, const struct msghdr *msg, int flags) {
if (!IsWindows()) {

View file

@ -45,6 +45,7 @@
* @error EINTR, EHOSTUNREACH, ECONNRESET (UDP ICMP Port Unreachable),
* EPIPE (if MSG_NOSIGNAL), EMSGSIZE, ENOTSOCK, EFAULT, etc.
* @asyncsignalsafe
* @restartable (unless SO_RCVTIMEO)
*/
ssize_t sendto(int fd, const void *buf, size_t size, uint32_t flags,
const void *opt_addr, uint32_t addrsize) {