Polish recent changes and make improvements

- Simulate SIGPIPE on Windows NT
- Fix commandv() regression on Windows NT
- Fix sigprocmask() strace bug on OpenBSD
- Add many more system calls to --strace logging
- Make errno state more pristine in redbean strace
This commit is contained in:
Justine Tunney 2022-03-19 03:37:00 -07:00
parent 10a766ebd0
commit 39688a73e4
69 changed files with 460 additions and 1976 deletions

View file

@ -21,6 +21,7 @@
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/calls/struct/dirent.h"
#include "libc/dce.h"
#include "libc/macros.internal.h"
@ -234,7 +235,6 @@ DIR *opendir(const char *name) {
struct Zipos *zip;
struct ZiposUri zipname;
if (weaken(__zipos_get) && weaken(__zipos_parseuri)(name, &zipname) != -1) {
ZTRACE("__zipos_opendir(%`'s)", name);
if (weaken(__zipos_stat)(&zipname, &st) != -1) {
if (S_ISDIR(st.st_mode)) {
zip = weaken(__zipos_get)();
@ -250,25 +250,25 @@ DIR *opendir(const char *name) {
}
res->zip.prefix[zipname.len] = '\0';
res->zip.prefixlen = zipname.len;
return res;
} else {
errno = ENOTDIR;
return 0;
res = 0;
}
} else {
return 0;
res = 0;
}
} else if (!IsWindows()) {
res = NULL;
res = 0;
if ((fd = open(name, O_RDONLY | O_DIRECTORY | O_CLOEXEC)) != -1) {
if (!(res = fdopendir(fd))) {
close(fd);
}
}
return res;
} else {
return opendir_nt(name);
res = opendir_nt(name);
}
STRACE("opendir(%#s) → %p% m", name, res);
return res;
}
/**
@ -284,10 +284,11 @@ DIR *fdopendir(int fd) {
if (!IsWindows()) {
if (!(dir = calloc(1, sizeof(*dir)))) return NULL;
dir->fd = fd;
return dir;
} else {
return fdopendir_nt(fd);
dir = fdopendir_nt(fd);
}
STRACE("fdopendir(%d) → %p% m", fd, dir);
return dir;
}
/**
@ -403,6 +404,7 @@ int closedir(DIR *dir) {
} else {
rc = 0;
}
STRACE("closedir(%p) → %d% m", dir, rc);
return rc;
}
@ -410,6 +412,7 @@ int closedir(DIR *dir) {
* Returns offset into directory data.
*/
long telldir(DIR *dir) {
STRACE("telldir(%p) → %d", dir, dir->tell);
return dir->tell;
}
@ -417,9 +420,16 @@ long telldir(DIR *dir) {
* Returns file descriptor associated with DIR object.
*/
int dirfd(DIR *dir) {
if (dir->iszip) return eopnotsupp();
if (IsWindows()) return eopnotsupp();
return dir->fd;
int rc;
if (dir->iszip) {
rc = eopnotsupp();
} else if (IsWindows()) {
rc = eopnotsupp();
} else {
rc = dir->fd;
}
STRACE("dirfd(%p) → %d% m", dir, rc);
return rc;
}
/**
@ -443,4 +453,5 @@ void rewinddir(DIR *dir) {
dir->isdone = true;
}
}
STRACE("rewinddir(%p)", dir);
}

View file

@ -56,9 +56,9 @@ textstartup int __fflush_register(FILE *f) {
struct StdioFlush *sf;
sf = &__fflush;
if (!sf->handles.p) {
sf->handles.p = &sf->handles_initmem[0];
sf->handles.p = sf->handles_initmem;
pushmov(&sf->handles.n, ARRAYLEN(sf->handles_initmem));
__cxa_atexit(fflush, NULL, NULL);
__cxa_atexit(fflush, 0, 0);
}
for (i = sf->handles.i; i; --i) {
if (!sf->handles.p[i - 1]) {
@ -76,7 +76,7 @@ void __fflush_unregister(FILE *f) {
sf = pushpop(sf);
for (i = sf->handles.i; i; --i) {
if (sf->handles.p[i - 1] == f) {
pushmov(&sf->handles.p[i - 1], NULL);
pushmov(&sf->handles.p[i - 1], 0);
return;
}
}

View file

@ -46,7 +46,7 @@ FILE *popen(const char *cmdline, const char *mode) {
if (pipe(pipefds) == -1) return NULL;
fcntl(pipefds[dir], F_SETFD, FD_CLOEXEC);
if ((f = fdopen(pipefds[dir], mode))) {
switch ((pid = vfork())) {
switch ((pid = fork())) {
case 0:
dup2(pipefds[!dir], !dir);
systemexec(cmdline);

View file

@ -45,7 +45,7 @@ int posix_spawn(int *pid, const char *path,
if (!(*pid = vfork())) {
if (attrp) {
if (attrp->posix_attr_flags & POSIX_SPAWN_SETPGROUP) {
if (setpgid(0, attrp->posix_attr_pgroup)) _exit(127);
if (setpgid(0, attrp->posix_attr_pgroup)) _Exit(127);
}
if (attrp->posix_attr_flags & POSIX_SPAWN_SETSIGMASK) {
sigprocmask(SIG_SETMASK, &attrp->posix_attr_sigmask, NULL);
@ -60,7 +60,7 @@ int posix_spawn(int *pid, const char *path,
sigfillset(&allsigs);
for (s = 0; sigismember(&allsigs, s); s++) {
if (sigismember(&attrp->posix_attr_sigdefault, s)) {
if (sigaction(s, &dfl, NULL) == -1) _exit(127);
if (sigaction(s, &dfl, NULL) == -1) _Exit(127);
}
}
}
@ -68,28 +68,28 @@ int posix_spawn(int *pid, const char *path,
if (file_actions) {
for (p = *file_actions; *p; p = strchr(p, ')') + 1) {
if (!strncmp(p, "close(", 6)) {
if (sscanf(p + 6, "%d)", &fd) != 1) _exit(127);
if (close(fd) == -1) _exit(127);
if (sscanf(p + 6, "%d)", &fd) != 1) _Exit(127);
if (close(fd) == -1) _Exit(127);
} else if (!strncmp(p, "dup2(", 5)) {
if (sscanf(p + 5, "%d,%d)", &fd, &newfd) != 2) _exit(127);
if (dup2(fd, newfd) == -1) _exit(127);
if (sscanf(p + 5, "%d,%d)", &fd, &newfd) != 2) _Exit(127);
if (dup2(fd, newfd) == -1) _Exit(127);
} else if (!strncmp(p, "open(", 5)) {
if (sscanf(p + 5, "%d,", &fd) != 1) _exit(127);
if (sscanf(p + 5, "%d,", &fd) != 1) _Exit(127);
p = strchr(p, ',') + 1;
q = strchr(p, '*');
if (!q || q - p + 1 > PATH_MAX) _exit(127);
if (!q || q - p + 1 > PATH_MAX) _Exit(127);
strncpy(opath, p, q - p);
opath[q - p] = '\0';
if (sscanf(q + 1, "%o,%o)", &oflag, &mode) != 2) _exit(127);
if (close(fd) == -1 && errno != EBADF) _exit(127);
if (sscanf(q + 1, "%o,%o)", &oflag, &mode) != 2) _Exit(127);
if (close(fd) == -1 && errno != EBADF) _Exit(127);
tempfd = open(opath, oflag, mode);
if (tempfd == -1) _exit(127);
if (tempfd == -1) _Exit(127);
if (tempfd != fd) {
if (dup2(tempfd, fd) == -1) _exit(127);
if (close(tempfd) == -1) _exit(127);
if (dup2(tempfd, fd) == -1) _Exit(127);
if (close(tempfd) == -1) _Exit(127);
}
} else {
_exit(127);
_Exit(127);
}
}
}
@ -97,17 +97,17 @@ int posix_spawn(int *pid, const char *path,
if (attrp->posix_attr_flags & POSIX_SPAWN_SETSCHEDULER) {
if (sched_setscheduler(0, attrp->posix_attr_schedpolicy,
&attrp->posix_attr_schedparam) == -1) {
_exit(127);
_Exit(127);
}
}
if (attrp->posix_attr_flags & POSIX_SPAWN_SETSCHEDPARAM) {
if (sched_setparam(0, &attrp->posix_attr_schedparam) == -1) {
_exit(127);
_Exit(127);
}
}
}
execve(path, argv, envp);
_exit(127);
_Exit(127);
} else {
if (*pid == -1) return errno;
return 0;