mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-13 06:29:11 +00:00
Make more fixes and improvements
- Remove PAGESIZE constant - Fix realloc() documentation - Fix ttyname_r() error reporting - Make forking more reliable on Windows - Make execvp() a few microseconds faster - Make system() a few microseconds faster - Tighten up the socket-related magic numbers - Loosen restrictions on mmap() offset alignment - Improve GetProgramExecutableName() with getenv("_") - Use mkstemp() as basis for mktemp(), tmpfile(), tmpfd() - Fix flakes in pthread_cancel_test, unix_test, fork_test - Fix recently introduced futex stack overflow regression - Let sockets be passed as stdio to subprocesses on Windows - Improve security of bind() on Windows w/ SO_EXCLUSIVEADDRUSE
This commit is contained in:
parent
140a8a52e5
commit
18bb5888e1
311 changed files with 1239 additions and 2622 deletions
|
@ -285,7 +285,9 @@ DIR *opendir(const char *name) {
|
|||
res->zip.offset = GetZipCdirOffset(zip->cdir);
|
||||
res->zip.records = GetZipCdirRecords(zip->cdir);
|
||||
res->zip.prefix = malloc(zipname.len + 2);
|
||||
memcpy(res->zip.prefix, zipname.path, zipname.len);
|
||||
if (zipname.len) {
|
||||
memcpy(res->zip.prefix, zipname.path, zipname.len);
|
||||
}
|
||||
if (zipname.len && res->zip.prefix[zipname.len - 1] != '/') {
|
||||
res->zip.prefix[zipname.len++] = '/';
|
||||
}
|
||||
|
@ -364,7 +366,9 @@ static struct dirent *readdir_impl(DIR *dir) {
|
|||
ent->d_off = dir->zip.offset;
|
||||
ent->d_reclen = MIN(n, 255);
|
||||
ent->d_type = S_ISDIR(mode) ? DT_DIR : DT_REG;
|
||||
memcpy(ent->d_name, s, ent->d_reclen);
|
||||
if (ent->d_reclen) {
|
||||
memcpy(ent->d_name, s, ent->d_reclen);
|
||||
}
|
||||
ent->d_name[ent->d_reclen] = 0;
|
||||
} else {
|
||||
lastent = (struct dirent *)dir->buf;
|
||||
|
@ -377,7 +381,9 @@ static struct dirent *readdir_impl(DIR *dir) {
|
|||
ent->d_off = -1;
|
||||
ent->d_reclen = n;
|
||||
ent->d_type = DT_DIR;
|
||||
memcpy(ent->d_name, s, ent->d_reclen);
|
||||
if (ent->d_reclen) {
|
||||
memcpy(ent->d_name, s, ent->d_reclen);
|
||||
}
|
||||
ent->d_name[ent->d_reclen] = 0;
|
||||
}
|
||||
}
|
||||
|
@ -474,7 +480,9 @@ errno_t readdir_r(DIR *dir, struct dirent *output, struct dirent **result) {
|
|||
return err;
|
||||
}
|
||||
if (entry) {
|
||||
memcpy(output, entry, entry->d_reclen);
|
||||
if (entry->d_reclen) {
|
||||
memcpy(output, entry, entry->d_reclen);
|
||||
}
|
||||
} else {
|
||||
output = 0;
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ char *fgets_unlocked(char *s, int size, FILE *f) {
|
|||
if ((t = memchr(b, '\n', n))) {
|
||||
n = t + 1 - b;
|
||||
}
|
||||
memcpy(p, b, n);
|
||||
if (n) memcpy(p, b, n);
|
||||
f->beg += n;
|
||||
size -= n - 1;
|
||||
p += n;
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
|
@ -39,7 +40,8 @@ int mkostempsmi(char *tpl, int slen, unsigned flags, uint64_t *rando, int mode,
|
|||
if (len < wildlen || slen > len - wildlen) return einval();
|
||||
char *ss = tpl + len - wildlen - slen;
|
||||
npassert(memcmp(ss, WILDCARD, wildlen) == 0);
|
||||
flags = (flags & ~(flags & O_ACCMODE)) | O_RDWR | O_CREAT | O_EXCL;
|
||||
flags = (flags & ~(flags & O_ACCMODE)) | O_RDWR | O_CREAT | O_EXCL |
|
||||
(IsWindows() ? 0x00410000 : 0);
|
||||
unsigned attempts = ATTEMPTS;
|
||||
do {
|
||||
char *p = ss;
|
||||
|
@ -74,8 +76,7 @@ static uint64_t g_mkostemps_reseed;
|
|||
* or -1 w/ errno
|
||||
* @see kTmpPath
|
||||
*/
|
||||
dontdiscard int mkostempsm(char *template, int suffixlen, unsigned flags,
|
||||
int mode) {
|
||||
int mkostempsm(char *template, int suffixlen, unsigned flags, int mode) {
|
||||
int fd;
|
||||
if (g_mkostemps_reseed++ % RESEED == 0) g_mkostemps_rand = _rand64();
|
||||
fd = mkostempsmi(template, suffixlen, flags, &g_mkostemps_rand, mode, open);
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
/*-*- 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 2020 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/intrin/bits.h"
|
||||
#include "libc/stdio/rand.h"
|
||||
#include "libc/stdio/temp.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
/**
|
||||
* Generates temporary filename.
|
||||
*
|
||||
* char tmp[14] = "/tmp/hiXXXXXX";
|
||||
* puts(mkstemp(tmp));
|
||||
*
|
||||
* @param template is mutated to replace last six X's with rng
|
||||
* @raise EINVAL if `template` didn't end with `XXXXXX`
|
||||
* @return pointer to mutated `template`, or 0 w/ errno
|
||||
* @see mkstemp()
|
||||
*/
|
||||
char *mktemp(char *template) {
|
||||
int i, n;
|
||||
uint64_t w;
|
||||
if ((n = strlen(template)) < 6 ||
|
||||
READ16LE(template + n - 2) != READ16LE("XX") ||
|
||||
READ32LE(template + n - 6) != READ32LE("XXXX")) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
w = _rand64();
|
||||
for (i = 0; i < 6; ++i) {
|
||||
template[n - 6 + i] = "0123456789abcdefghijklmnopqrstuvwxyz"[w % 36];
|
||||
w /= 36;
|
||||
}
|
||||
return template;
|
||||
}
|
|
@ -87,13 +87,13 @@ errno_t posix_spawn(int *pid, const char *path,
|
|||
posix_spawnattr_getflags(attrp, &flags);
|
||||
if (flags & POSIX_SPAWN_SETSID) {
|
||||
if (setsid()) {
|
||||
STRACE("posix_spawn fail #%d", 1);
|
||||
STRACE("posix_spawn fail #%d% m", 1);
|
||||
_Exit(127);
|
||||
}
|
||||
}
|
||||
if (flags & POSIX_SPAWN_SETPGROUP) {
|
||||
if (setpgid(0, (*attrp)->pgroup)) {
|
||||
STRACE("posix_spawn fail #%d", 1);
|
||||
STRACE("posix_spawn fail #%d% m", 2);
|
||||
_Exit(127);
|
||||
}
|
||||
}
|
||||
|
@ -103,14 +103,14 @@ errno_t posix_spawn(int *pid, const char *path,
|
|||
}
|
||||
if ((flags & POSIX_SPAWN_RESETIDS) &&
|
||||
(setgid(getgid()) || setuid(getuid()))) {
|
||||
STRACE("posix_spawn fail #%d", 2);
|
||||
STRACE("posix_spawn fail #%d% m", 3);
|
||||
_Exit(127);
|
||||
}
|
||||
if (flags & POSIX_SPAWN_SETSIGDEF) {
|
||||
for (s = 1; s < 32; s++) {
|
||||
if (sigismember(&(*attrp)->sigdefault, s)) {
|
||||
if (sigaction(s, &dfl, 0) == -1) {
|
||||
STRACE("posix_spawn fail #%d", 3);
|
||||
STRACE("posix_spawn fail #%d% m", 4);
|
||||
_Exit(127);
|
||||
}
|
||||
}
|
||||
|
@ -119,7 +119,7 @@ errno_t posix_spawn(int *pid, const char *path,
|
|||
}
|
||||
if (file_actions) {
|
||||
if (RunFileActions(*file_actions) == -1) {
|
||||
STRACE("posix_spawn fail #%d", 4);
|
||||
STRACE("posix_spawn fail #%d% m", 5);
|
||||
_Exit(127);
|
||||
}
|
||||
}
|
||||
|
@ -128,21 +128,21 @@ errno_t posix_spawn(int *pid, const char *path,
|
|||
posix_spawnattr_getschedpolicy(attrp, &policy);
|
||||
posix_spawnattr_getschedparam(attrp, ¶m);
|
||||
if (sched_setscheduler(0, policy, ¶m) == -1) {
|
||||
STRACE("posix_spawn fail #%d", 5);
|
||||
STRACE("posix_spawn fail #%d% m", 6);
|
||||
_Exit(127);
|
||||
}
|
||||
}
|
||||
if (flags & POSIX_SPAWN_SETSCHEDPARAM) {
|
||||
posix_spawnattr_getschedparam(attrp, ¶m);
|
||||
if (sched_setparam(0, ¶m) == -1) {
|
||||
STRACE("posix_spawn fail #%d", 6);
|
||||
STRACE("posix_spawn fail #%d% m", 7);
|
||||
_Exit(127);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!envp) envp = environ;
|
||||
execve(path, argv, envp);
|
||||
STRACE("posix_spawn fail #%d", 7);
|
||||
STRACE("posix_spawn fail #%d% m", 8);
|
||||
_Exit(127);
|
||||
} else if (child != -1) {
|
||||
if (pid) *pid = child;
|
||||
|
|
|
@ -46,34 +46,34 @@
|
|||
int system(const char *cmdline) {
|
||||
int pid, wstatus;
|
||||
sigset_t chldmask, savemask;
|
||||
struct sigaction ignore, saveint, savequit;
|
||||
if (!cmdline) return 1;
|
||||
BLOCK_CANCELLATIONS;
|
||||
ignore.sa_flags = 0;
|
||||
ignore.sa_handler = SIG_IGN;
|
||||
sigemptyset(&ignore.sa_mask);
|
||||
sigaction(SIGINT, &ignore, &saveint);
|
||||
sigaction(SIGQUIT, &ignore, &savequit);
|
||||
sigemptyset(&chldmask);
|
||||
sigaddset(&chldmask, SIGINT);
|
||||
sigaddset(&chldmask, SIGQUIT);
|
||||
sigaddset(&chldmask, SIGCHLD);
|
||||
sigprocmask(SIG_BLOCK, &chldmask, &savemask);
|
||||
if (!(pid = fork())) {
|
||||
sigaction(SIGINT, &saveint, 0);
|
||||
sigaction(SIGQUIT, &savequit, 0);
|
||||
sigprocmask(SIG_SETMASK, &savemask, 0);
|
||||
_Exit(_cocmd(3, (char *[]){"system", "-c", cmdline, 0}, environ));
|
||||
} else if (pid != -1) {
|
||||
} else if (pid == -1) {
|
||||
wstatus = -1;
|
||||
} else {
|
||||
struct sigaction ignore, saveint, savequit;
|
||||
ignore.sa_flags = 0;
|
||||
ignore.sa_handler = SIG_IGN;
|
||||
sigemptyset(&ignore.sa_mask);
|
||||
sigaction(SIGINT, &ignore, &saveint);
|
||||
sigaction(SIGQUIT, &ignore, &savequit);
|
||||
while (wait4(pid, &wstatus, 0, 0) == -1) {
|
||||
if (errno != EINTR) {
|
||||
wstatus = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
wstatus = -1;
|
||||
sigaction(SIGINT, &saveint, 0);
|
||||
sigaction(SIGQUIT, &savequit, 0);
|
||||
}
|
||||
sigaction(SIGINT, &saveint, 0);
|
||||
sigaction(SIGQUIT, &savequit, 0);
|
||||
sigprocmask(SIG_SETMASK, &savemask, 0);
|
||||
ALLOW_CANCELLATIONS;
|
||||
return wstatus;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue