mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-08 12:18:31 +00:00
Apply clang-format update to repo (#1154)
Commit bc6c183
introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
This commit is contained in:
parent
342d0c81e5
commit
6e6fc38935
863 changed files with 9201 additions and 4627 deletions
|
@ -130,7 +130,8 @@ static void PutEnv(char **p, char *kv) {
|
|||
struct Env e;
|
||||
e = __getenv(p, kv);
|
||||
p[e.i] = kv;
|
||||
if (!e.s) p[e.i + 1] = 0;
|
||||
if (!e.s)
|
||||
p[e.i + 1] = 0;
|
||||
}
|
||||
|
||||
static void UnsetEnv(char **p, const char *k) {
|
||||
|
@ -330,7 +331,8 @@ static int CatDump(const char *path, int fd, bool dontclose) {
|
|||
}
|
||||
return 1;
|
||||
}
|
||||
if (!rc) break;
|
||||
if (!rc)
|
||||
break;
|
||||
rc = write(1, buf, rc);
|
||||
if (rc == -1) {
|
||||
perror("write");
|
||||
|
@ -392,18 +394,21 @@ static int Read(void) {
|
|||
unsigned char c;
|
||||
int i, j, rc = 1;
|
||||
for (i = 1; i < n; ++i) {
|
||||
if (args[i][0] != '-') break;
|
||||
if (args[i][0] != '-')
|
||||
break;
|
||||
if (args[i][1] == 'p' && !args[i][2] && i + 1 < n) {
|
||||
Write(1, args[++i]);
|
||||
}
|
||||
}
|
||||
if (i >= n) return 1;
|
||||
if (i >= n)
|
||||
return 1;
|
||||
for (j = 0; args[i][j]; ++j) {
|
||||
Append(args[i][j]);
|
||||
}
|
||||
Append('=');
|
||||
while (read(0, &c, 1) > 0) {
|
||||
if (c == '\n') break;
|
||||
if (c == '\n')
|
||||
break;
|
||||
Append(c);
|
||||
rc = 0;
|
||||
}
|
||||
|
@ -469,7 +474,8 @@ static int Kill(void) {
|
|||
int sig, rc = 0, i = 1;
|
||||
if (i < n && args[i][0] == '-') {
|
||||
sig = GetSignalByName(args[i++] + 1);
|
||||
if (!sig) return -1; // fallback to system kill command
|
||||
if (!sig)
|
||||
return -1; // fallback to system kill command
|
||||
} else {
|
||||
sig = SIGTERM;
|
||||
}
|
||||
|
@ -510,20 +516,30 @@ static int Usleep(void) {
|
|||
static int Test(void) {
|
||||
int w, m = n;
|
||||
struct stat st;
|
||||
if (m && READ16LE(args[m - 1]) == READ16LE("]")) --m;
|
||||
if (m && READ16LE(args[m - 1]) == READ16LE("]"))
|
||||
--m;
|
||||
if (m == 4) {
|
||||
w = READ32LE(args[2]) & 0x00ffffff;
|
||||
if ((w & 65535) == READ16LE("=")) return !!strcmp(args[1], args[3]);
|
||||
if (w == READ24("==")) return !!strcmp(args[1], args[3]);
|
||||
if (w == READ24("!=")) return !strcmp(args[1], args[3]);
|
||||
if ((w & 65535) == READ16LE("="))
|
||||
return !!strcmp(args[1], args[3]);
|
||||
if (w == READ24("=="))
|
||||
return !!strcmp(args[1], args[3]);
|
||||
if (w == READ24("!="))
|
||||
return !strcmp(args[1], args[3]);
|
||||
} else if (m == 3) {
|
||||
w = READ32LE(args[1]) & 0x00ffffff;
|
||||
if (w == READ24("-n")) return !(strlen(args[2]) > 0);
|
||||
if (w == READ24("-z")) return !(strlen(args[2]) == 0);
|
||||
if (w == READ24("-e")) return !!stat(args[2], &st);
|
||||
if (w == READ24("-f")) return !(!stat(args[2], &st) && S_ISREG(st.st_mode));
|
||||
if (w == READ24("-d")) return !(!stat(args[2], &st) && S_ISDIR(st.st_mode));
|
||||
if (w == READ24("-h")) return !(!stat(args[2], &st) && S_ISLNK(st.st_mode));
|
||||
if (w == READ24("-n"))
|
||||
return !(strlen(args[2]) > 0);
|
||||
if (w == READ24("-z"))
|
||||
return !(strlen(args[2]) == 0);
|
||||
if (w == READ24("-e"))
|
||||
return !!stat(args[2], &st);
|
||||
if (w == READ24("-f"))
|
||||
return !(!stat(args[2], &st) && S_ISREG(st.st_mode));
|
||||
if (w == READ24("-d"))
|
||||
return !(!stat(args[2], &st) && S_ISDIR(st.st_mode));
|
||||
if (w == READ24("-h"))
|
||||
return !(!stat(args[2], &st) && S_ISLNK(st.st_mode));
|
||||
}
|
||||
return -1; // fall back to system test command
|
||||
}
|
||||
|
@ -558,7 +574,8 @@ static int Rm(void) {
|
|||
if ((!force && (lstat(args[i], &st) ||
|
||||
(!S_ISLNK(st.st_mode) && access(args[i], W_OK)))) ||
|
||||
unlink(args[i])) {
|
||||
if (force && errno == ENOENT) continue;
|
||||
if (force && errno == ENOENT)
|
||||
continue;
|
||||
perror(args[i]);
|
||||
return 1;
|
||||
}
|
||||
|
@ -675,31 +692,56 @@ static wontreturn void Exec(void) {
|
|||
}
|
||||
|
||||
static int TryBuiltin(bool wantexec) {
|
||||
if (!n) return exitstatus;
|
||||
if (!strcmp(args[0], "exit")) Exit();
|
||||
if (!strcmp(args[0], "exec")) Exec();
|
||||
if (!strcmp(args[0], "cd")) return Cd();
|
||||
if (!strcmp(args[0], "rm")) return Rm();
|
||||
if (!strcmp(args[0], "[")) return Test();
|
||||
if (!strcmp(args[0], "cat")) return Cat();
|
||||
if (!strcmp(args[0], "env")) return Env();
|
||||
if (!strcmp(args[0], "pwd")) return Pwd();
|
||||
if (!strcmp(args[0], "wait")) return Wait();
|
||||
if (!strcmp(args[0], "echo")) return Echo();
|
||||
if (!strcmp(args[0], "read")) return Read();
|
||||
if (!strcmp(args[0], "true")) return True();
|
||||
if (!strcmp(args[0], "test")) return Test();
|
||||
if (!strcmp(args[0], "kill")) return Kill();
|
||||
if (!strcmp(args[0], "pause")) return Pause();
|
||||
if (!strcmp(args[0], "flock")) return Flock();
|
||||
if (!strcmp(args[0], "chmod")) return Chmod();
|
||||
if (!strcmp(args[0], "touch")) return Touch();
|
||||
if (!strcmp(args[0], "rmdir")) return Rmdir();
|
||||
if (!strcmp(args[0], "mkdir")) return Mkdir();
|
||||
if (!strcmp(args[0], "false")) return False();
|
||||
if (!strcmp(args[0], "mktemp")) return Mktemp();
|
||||
if (!strcmp(args[0], "usleep")) return Usleep();
|
||||
if (!strcmp(args[0], "toupper")) return Toupper();
|
||||
if (!n)
|
||||
return exitstatus;
|
||||
if (!strcmp(args[0], "exit"))
|
||||
Exit();
|
||||
if (!strcmp(args[0], "exec"))
|
||||
Exec();
|
||||
if (!strcmp(args[0], "cd"))
|
||||
return Cd();
|
||||
if (!strcmp(args[0], "rm"))
|
||||
return Rm();
|
||||
if (!strcmp(args[0], "["))
|
||||
return Test();
|
||||
if (!strcmp(args[0], "cat"))
|
||||
return Cat();
|
||||
if (!strcmp(args[0], "env"))
|
||||
return Env();
|
||||
if (!strcmp(args[0], "pwd"))
|
||||
return Pwd();
|
||||
if (!strcmp(args[0], "wait"))
|
||||
return Wait();
|
||||
if (!strcmp(args[0], "echo"))
|
||||
return Echo();
|
||||
if (!strcmp(args[0], "read"))
|
||||
return Read();
|
||||
if (!strcmp(args[0], "true"))
|
||||
return True();
|
||||
if (!strcmp(args[0], "test"))
|
||||
return Test();
|
||||
if (!strcmp(args[0], "kill"))
|
||||
return Kill();
|
||||
if (!strcmp(args[0], "pause"))
|
||||
return Pause();
|
||||
if (!strcmp(args[0], "flock"))
|
||||
return Flock();
|
||||
if (!strcmp(args[0], "chmod"))
|
||||
return Chmod();
|
||||
if (!strcmp(args[0], "touch"))
|
||||
return Touch();
|
||||
if (!strcmp(args[0], "rmdir"))
|
||||
return Rmdir();
|
||||
if (!strcmp(args[0], "mkdir"))
|
||||
return Mkdir();
|
||||
if (!strcmp(args[0], "false"))
|
||||
return False();
|
||||
if (!strcmp(args[0], "mktemp"))
|
||||
return Mktemp();
|
||||
if (!strcmp(args[0], "usleep"))
|
||||
return Usleep();
|
||||
if (!strcmp(args[0], "toupper"))
|
||||
return Toupper();
|
||||
if (_weaken(_tr) && !strcmp(args[0], "tr")) {
|
||||
return Fake(_weaken(_tr), wantexec);
|
||||
}
|
||||
|
@ -736,13 +778,17 @@ static void Pipe(void) {
|
|||
if (!pid) {
|
||||
unassert(dup2(pfds[1], 1) == 1);
|
||||
// we can't rely on cloexec because builtins
|
||||
if (pfds[0] != 1) unassert(!close(pfds[0]));
|
||||
if (pfds[1] != 1) unassert(!close(pfds[1]));
|
||||
if (pfds[0] != 1)
|
||||
unassert(!close(pfds[0]));
|
||||
if (pfds[1] != 1)
|
||||
unassert(!close(pfds[1]));
|
||||
_Exit(ShellExec());
|
||||
}
|
||||
unassert(dup2(pfds[0], 0) == 0);
|
||||
if (pfds[0] != 0) unassert(!close(pfds[0]));
|
||||
if (pfds[1] != 0) unassert(!close(pfds[1]));
|
||||
if (pfds[0] != 0)
|
||||
unassert(!close(pfds[0]));
|
||||
if (pfds[1] != 0)
|
||||
unassert(!close(pfds[1]));
|
||||
n = 0;
|
||||
}
|
||||
|
||||
|
@ -837,7 +883,8 @@ static char *Tokenize(void) {
|
|||
switch (t) {
|
||||
|
||||
case STATE_WHITESPACE:
|
||||
if (!*p) return 0;
|
||||
if (!*p)
|
||||
return 0;
|
||||
if (*p == ' ' || *p == '\t' || *p == '\n' ||
|
||||
(p[0] == '\\' && p[1] == '\n')) {
|
||||
continue;
|
||||
|
@ -859,10 +906,12 @@ static char *Tokenize(void) {
|
|||
t = STATE_VAR;
|
||||
var[(vari = 0)] = 0;
|
||||
} else if (*p == '\\') {
|
||||
if (!p[1]) UnsupportedSyntax(*p);
|
||||
if (!p[1])
|
||||
UnsupportedSyntax(*p);
|
||||
Append(*++p);
|
||||
} else if (*p == '=') {
|
||||
if (!n && q > r) assign = r;
|
||||
if (!n && q > r)
|
||||
assign = r;
|
||||
Append(*p);
|
||||
} else if (*p == '|') {
|
||||
if (q > r) {
|
||||
|
@ -914,11 +963,13 @@ static char *Tokenize(void) {
|
|||
case STATE_VAR:
|
||||
// XXX: we need to find a simple elegant way to break up
|
||||
// unquoted variable expansions into multiple args.
|
||||
if (CopyVar()) t = STATE_CMD, --p;
|
||||
if (CopyVar())
|
||||
t = STATE_CMD, --p;
|
||||
break;
|
||||
|
||||
case STATE_SINGLE:
|
||||
if (!*p) goto UnterminatedString;
|
||||
if (!*p)
|
||||
goto UnterminatedString;
|
||||
if (*p == '\'') {
|
||||
t = STATE_CMD;
|
||||
} else {
|
||||
|
@ -931,12 +982,15 @@ static char *Tokenize(void) {
|
|||
_Exit(6);
|
||||
|
||||
case STATE_QUOTED_VAR:
|
||||
if (!*p) goto UnterminatedString;
|
||||
if (CopyVar()) t = STATE_QUOTED, --p;
|
||||
if (!*p)
|
||||
goto UnterminatedString;
|
||||
if (CopyVar())
|
||||
t = STATE_QUOTED, --p;
|
||||
break;
|
||||
|
||||
case STATE_QUOTED:
|
||||
if (!*p) goto UnterminatedString;
|
||||
if (!*p)
|
||||
goto UnterminatedString;
|
||||
if (*p == '"') {
|
||||
t = STATE_CMD;
|
||||
} else if (p[0] == '$') {
|
||||
|
@ -1044,7 +1098,8 @@ int _cocmd(int argc, char **argv, char **envp) {
|
|||
n = 0;
|
||||
r = q = argbuf;
|
||||
while ((arg = Tokenize())) {
|
||||
if (arg == TOMBSTONE) continue;
|
||||
if (arg == TOMBSTONE)
|
||||
continue;
|
||||
if (n + 1 < ARRAYLEN(args)) {
|
||||
if (isdigit(arg[0]) && arg[1] == '>' && arg[2] == '&' &&
|
||||
isdigit(arg[3])) {
|
||||
|
|
|
@ -42,13 +42,20 @@ struct StringBuilder {
|
|||
|
||||
// returns true if fd can't be inherited by anything
|
||||
textwindows bool __is_cloexec(const struct Fd *f) {
|
||||
if (f->kind == kFdEmpty) return true;
|
||||
if (f->kind == kFdReserved) return true;
|
||||
if (f->kind == kFdZip) return true;
|
||||
if (f->kind == kFdEpoll) return true;
|
||||
if (f->flags & O_CLOEXEC) return true;
|
||||
if (f->handle == -1) return true;
|
||||
if (!f->handle) return true;
|
||||
if (f->kind == kFdEmpty)
|
||||
return true;
|
||||
if (f->kind == kFdReserved)
|
||||
return true;
|
||||
if (f->kind == kFdZip)
|
||||
return true;
|
||||
if (f->kind == kFdEpoll)
|
||||
return true;
|
||||
if (f->flags & O_CLOEXEC)
|
||||
return true;
|
||||
if (f->handle == -1)
|
||||
return true;
|
||||
if (!f->handle)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -81,14 +88,16 @@ textwindows char *__describe_fds(const struct Fd *fds, size_t fdslen,
|
|||
uint32_t handlecount = 0;
|
||||
|
||||
// setup memory for environment variable
|
||||
if (!(sb.p = strdup(FDS_VAR))) return 0;
|
||||
if (!(sb.p = strdup(FDS_VAR)))
|
||||
return 0;
|
||||
sb.i = sizeof(FDS_VAR) - 1;
|
||||
sb.n = sizeof(FDS_VAR);
|
||||
|
||||
// setup memory for explicitly inherited handle list
|
||||
for (int fd = 0; fd < fdslen; ++fd) {
|
||||
const struct Fd *f = fds + fd;
|
||||
if (__is_cloexec(f)) continue;
|
||||
if (__is_cloexec(f))
|
||||
continue;
|
||||
++handlecount;
|
||||
}
|
||||
if (!(handles = calloc(handlecount, sizeof(*handles)))) {
|
||||
|
@ -101,7 +110,8 @@ textwindows char *__describe_fds(const struct Fd *fds, size_t fdslen,
|
|||
// serialize file descriptors
|
||||
for (int fd = 0; fd < fdslen; ++fd) {
|
||||
const struct Fd *f = fds + fd;
|
||||
if (__is_cloexec(f)) continue;
|
||||
if (__is_cloexec(f))
|
||||
continue;
|
||||
|
||||
// make inheritable version of handle exist in creator process
|
||||
if (!DuplicateHandle(GetCurrentProcess(), f->handle, hCreatorProcess,
|
||||
|
|
|
@ -40,19 +40,22 @@ int execl(const char *exe, const char *arg, ... /*, NULL*/) {
|
|||
va_list va, vb;
|
||||
va_copy(vb, va);
|
||||
va_start(va, arg);
|
||||
for (i = 0; va_arg(va, const char *); ++i) donothing;
|
||||
for (i = 0; va_arg(va, const char *); ++i)
|
||||
donothing;
|
||||
va_end(va);
|
||||
#pragma GCC push_options
|
||||
#pragma GCC diagnostic ignored "-Walloca-larger-than="
|
||||
int nbytes = (i + 2) * sizeof(char *);
|
||||
if (__get_safe_size(nbytes, 4096) < nbytes) return enomem();
|
||||
if (__get_safe_size(nbytes, 4096) < nbytes)
|
||||
return enomem();
|
||||
char **argv = alloca(nbytes);
|
||||
CheckLargeStackAllocation(argv, nbytes);
|
||||
#pragma GCC pop_options
|
||||
va_start(vb, arg);
|
||||
argv[0] = (char *)arg;
|
||||
for (i = 1;; ++i) {
|
||||
if (!(argv[i] = va_arg(vb, char *))) break;
|
||||
if (!(argv[i] = va_arg(vb, char *)))
|
||||
break;
|
||||
}
|
||||
va_end(vb);
|
||||
return execv(exe, argv);
|
||||
|
|
|
@ -43,20 +43,23 @@ int execle(const char *exe, const char *arg,
|
|||
char **argv, **envp;
|
||||
va_copy(vb, va);
|
||||
va_start(va, arg);
|
||||
for (i = 0; va_arg(va, const char *); ++i) donothing;
|
||||
for (i = 0; va_arg(va, const char *); ++i)
|
||||
donothing;
|
||||
envp = va_arg(va, char **);
|
||||
va_end(va);
|
||||
#pragma GCC push_options
|
||||
#pragma GCC diagnostic ignored "-Walloca-larger-than="
|
||||
int nbytes = (i + 2) * sizeof(char *);
|
||||
if (__get_safe_size(nbytes, 4096) < nbytes) return enomem();
|
||||
if (__get_safe_size(nbytes, 4096) < nbytes)
|
||||
return enomem();
|
||||
argv = alloca(nbytes);
|
||||
CheckLargeStackAllocation(argv, nbytes);
|
||||
#pragma GCC pop_options
|
||||
va_start(vb, arg);
|
||||
argv[0] = (char *)arg;
|
||||
for (i = 1;; ++i) {
|
||||
if (!(argv[i] = va_arg(vb, char *))) break;
|
||||
if (!(argv[i] = va_arg(vb, char *)))
|
||||
break;
|
||||
}
|
||||
va_end(vb);
|
||||
return execve(exe, argv, envp);
|
||||
|
|
|
@ -47,19 +47,22 @@ int execlp(const char *prog, const char *arg, ... /*, NULL*/) {
|
|||
// turn varargs into array
|
||||
va_copy(vb, va);
|
||||
va_start(va, arg);
|
||||
for (i = 0; va_arg(va, const char *); ++i) (void)0;
|
||||
for (i = 0; va_arg(va, const char *); ++i)
|
||||
(void)0;
|
||||
va_end(va);
|
||||
#pragma GCC push_options
|
||||
#pragma GCC diagnostic ignored "-Walloca-larger-than="
|
||||
int nbytes = (i + 2) * sizeof(char *);
|
||||
if (__get_safe_size(nbytes, 4096) < nbytes) return enomem();
|
||||
if (__get_safe_size(nbytes, 4096) < nbytes)
|
||||
return enomem();
|
||||
argv = alloca(nbytes);
|
||||
CheckLargeStackAllocation(argv, nbytes);
|
||||
#pragma GCC pop_options
|
||||
va_start(vb, arg);
|
||||
argv[0] = (char *)arg;
|
||||
for (i = 1;; ++i) {
|
||||
if (!(argv[i] = va_arg(vb, char *))) break;
|
||||
if (!(argv[i] = va_arg(vb, char *)))
|
||||
break;
|
||||
}
|
||||
va_end(vb);
|
||||
|
||||
|
|
|
@ -109,12 +109,14 @@ int sys_execve(const char *prog, char *const argv[], char *const envp[]) {
|
|||
|
||||
// allocate memory
|
||||
int argc;
|
||||
for (argc = 0; argv[argc];) ++argc;
|
||||
for (argc = 0; argv[argc];)
|
||||
++argc;
|
||||
#pragma GCC push_options
|
||||
#pragma GCC diagnostic ignored "-Walloca-larger-than="
|
||||
int nbytes = (argc + 4) * sizeof(char *);
|
||||
int ntotal = nbytes + PATH_MAX;
|
||||
if (__get_safe_size(ntotal, 4096) < ntotal) return enomem();
|
||||
if (__get_safe_size(ntotal, 4096) < ntotal)
|
||||
return enomem();
|
||||
char **shargs = alloca(nbytes);
|
||||
CheckLargeStackAllocation(shargs, nbytes);
|
||||
#pragma GCC pop_options
|
||||
|
|
|
@ -64,11 +64,13 @@ int execvpe(const char *prog, char *const argv[], char *const *envp) {
|
|||
// change argv[0] to resolved path if it's ambiguous
|
||||
// otherwise the program won't have much luck finding itself
|
||||
if (argv[0] && *prog != '/' && *exe == '/' && !strcmp(prog, argv[0])) {
|
||||
for (i = 0; argv[i++];) (void)0;
|
||||
for (i = 0; argv[i++];)
|
||||
(void)0;
|
||||
#pragma GCC push_options
|
||||
#pragma GCC diagnostic ignored "-Walloca-larger-than="
|
||||
int nbytes = i * sizeof(*argv);
|
||||
if (__get_safe_size(nbytes, 4096) < nbytes) return enomem();
|
||||
if (__get_safe_size(nbytes, 4096) < nbytes)
|
||||
return enomem();
|
||||
argv2 = alloca(nbytes);
|
||||
CheckLargeStackAllocation(argv2, nbytes);
|
||||
#pragma GCC pop_options
|
||||
|
|
|
@ -232,7 +232,8 @@ int fexecve(int fd, char *const argv[], char *const envp[]) {
|
|||
break;
|
||||
}
|
||||
size_t numenvs;
|
||||
for (numenvs = 0; envp[numenvs];) ++numenvs;
|
||||
for (numenvs = 0; envp[numenvs];)
|
||||
++numenvs;
|
||||
// const size_t desenvs = min(500, max(numenvs + 1, 2));
|
||||
static _Thread_local char *envs[500];
|
||||
memcpy(envs, envp, numenvs * sizeof(char *));
|
||||
|
|
|
@ -69,7 +69,8 @@ static textwindows wontreturn void AbortFork(const char *func) {
|
|||
|
||||
static textwindows char16_t *ParseInt(char16_t *p, int64_t *x) {
|
||||
*x = 0;
|
||||
while (*p == ' ') p++;
|
||||
while (*p == ' ')
|
||||
p++;
|
||||
while ('0' <= *p && *p <= '9') {
|
||||
*x *= 10;
|
||||
*x += *p++ - '0';
|
||||
|
@ -97,7 +98,8 @@ static dontinline textwindows bool ForkIo2(int64_t h, void *buf, size_t n,
|
|||
struct NtOverlapped *),
|
||||
const char *sf, bool ischild) {
|
||||
ssize_t rc = ForkIo(h, buf, n, fn);
|
||||
if (ischild) __tls_enabled_set(false); // prevent tls crash in kprintf
|
||||
if (ischild)
|
||||
__tls_enabled_set(false); // prevent tls crash in kprintf
|
||||
NTTRACE("%s(%ld, %p, %'zu) → %'zd% m", sf, h, buf, n, rc);
|
||||
return rc != -1;
|
||||
}
|
||||
|
@ -106,7 +108,8 @@ static dontinline textwindows bool WriteAll(int64_t h, void *buf, size_t n) {
|
|||
bool ok;
|
||||
ok = ForkIo2(h, buf, n, (void *)WriteFile, "WriteFile", false);
|
||||
#ifndef NDEBUG
|
||||
if (ok) ok = ForkIo2(h, &n, sizeof(n), (void *)WriteFile, "WriteFile", false);
|
||||
if (ok)
|
||||
ok = ForkIo2(h, &n, sizeof(n), (void *)WriteFile, "WriteFile", false);
|
||||
#endif
|
||||
#if SYSDEBUG
|
||||
if (!ok) {
|
||||
|
@ -195,7 +198,8 @@ textwindows void WinMainForked(void) {
|
|||
// check to see if the process was actually forked
|
||||
// this variable should have the pipe handle numba
|
||||
varlen = GetEnvironmentVariable(u"_FORK", fvar, ARRAYLEN(fvar));
|
||||
if (!varlen || varlen >= ARRAYLEN(fvar)) return;
|
||||
if (!varlen || varlen >= ARRAYLEN(fvar))
|
||||
return;
|
||||
NTTRACE("WinMainForked()");
|
||||
SetEnvironmentVariable(u"_FORK", NULL);
|
||||
#if SYSDEBUG
|
||||
|
@ -302,7 +306,8 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
|
|||
tib = __get_tls();
|
||||
ftrace_enabled(-1);
|
||||
strace_enabled(-1);
|
||||
if (!(proc = __proc_new())) return -1;
|
||||
if (!(proc = __proc_new()))
|
||||
return -1;
|
||||
if (!setjmp(jb)) {
|
||||
reader = CreateNamedPipe(__create_pipe_name(pipename), kNtPipeAccessInbound,
|
||||
kNtPipeTypeByte | kNtPipeReadmodeByte, 1, PIPE_BUF,
|
||||
|
@ -323,14 +328,16 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
|
|||
// forked process since the flag was removed by __intercept_flag
|
||||
if (strace_enabled(0) > 0) {
|
||||
int n;
|
||||
for (n = 0; args[n];) ++n;
|
||||
for (n = 0; args[n];)
|
||||
++n;
|
||||
#pragma GCC push_options
|
||||
#pragma GCC diagnostic ignored "-Walloca-larger-than="
|
||||
int nbytes = (n + 2) * sizeof(char *);
|
||||
char **args2 = alloca(nbytes);
|
||||
CheckLargeStackAllocation(args2, nbytes);
|
||||
#pragma GCC pop_options
|
||||
for (i = 0; i < n; ++i) args2[i] = args[i];
|
||||
for (i = 0; i < n; ++i)
|
||||
args2[i] = args[i];
|
||||
args2[i++] = "--strace";
|
||||
args2[i] = 0;
|
||||
args = args2;
|
||||
|
@ -360,10 +367,13 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
|
|||
ok = WriteAll(writer, p, _mmi.p[i].size);
|
||||
}
|
||||
}
|
||||
if (ok) ok = WriteAll(writer, __data_start, __data_end - __data_start);
|
||||
if (ok) ok = WriteAll(writer, __bss_start, __bss_end - __bss_start);
|
||||
if (ok)
|
||||
ok = WriteAll(writer, __data_start, __data_end - __data_start);
|
||||
if (ok)
|
||||
ok = WriteAll(writer, __bss_start, __bss_end - __bss_start);
|
||||
if (ok) {
|
||||
if (!CloseHandle(writer)) ok = false;
|
||||
if (!CloseHandle(writer))
|
||||
ok = false;
|
||||
writer = -1;
|
||||
}
|
||||
if (ok) {
|
||||
|
@ -377,8 +387,10 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (reader != -1) CloseHandle(reader);
|
||||
if (writer != -1) CloseHandle(writer);
|
||||
if (reader != -1)
|
||||
CloseHandle(reader);
|
||||
if (writer != -1)
|
||||
CloseHandle(writer);
|
||||
if (rc == -1 && errno != ENOMEM) {
|
||||
eagain(); // posix fork() only specifies two errors
|
||||
}
|
||||
|
|
|
@ -45,7 +45,8 @@ int _fork(uint32_t dwCreationFlags) {
|
|||
int ax, dx, tid, parent;
|
||||
parent = __pid;
|
||||
BLOCK_SIGNALS;
|
||||
if (IsWindows()) __proc_lock();
|
||||
if (IsWindows())
|
||||
__proc_lock();
|
||||
if (__threaded && _weaken(_pthread_onfork_prepare)) {
|
||||
_weaken(_pthread_onfork_prepare)();
|
||||
}
|
||||
|
@ -107,7 +108,8 @@ int _fork(uint32_t dwCreationFlags) {
|
|||
if (__threaded && _weaken(_pthread_onfork_parent)) {
|
||||
_weaken(_pthread_onfork_parent)();
|
||||
}
|
||||
if (IsWindows()) __proc_unlock();
|
||||
if (IsWindows())
|
||||
__proc_unlock();
|
||||
STRACE("fork() → %d% m", ax);
|
||||
}
|
||||
ALLOW_SIGNALS;
|
||||
|
|
|
@ -84,8 +84,10 @@ textwindows int sys_kill_nt(int pid, int sig) {
|
|||
// perform actual kill
|
||||
// process will report WIFSIGNALED with WTERMSIG(sig)
|
||||
bool32 ok = TerminateProcess(handle, sig);
|
||||
if (closeme) CloseHandle(closeme);
|
||||
if (ok) return 0;
|
||||
if (closeme)
|
||||
CloseHandle(closeme);
|
||||
if (ok)
|
||||
return 0;
|
||||
|
||||
// handle error
|
||||
OnError:
|
||||
|
|
|
@ -24,7 +24,9 @@
|
|||
* Sends signal to process group.
|
||||
*/
|
||||
int killpg(int pgrp, int sig) {
|
||||
if (!(0 < sig && sig < NSIG)) return einval();
|
||||
if (pgrp == 1 || pgrp < 0) return esrch();
|
||||
if (!(0 < sig && sig < NSIG))
|
||||
return einval();
|
||||
if (pgrp == 1 || pgrp < 0)
|
||||
return esrch();
|
||||
return kill(IsWindows() ? pgrp : -pgrp, sig);
|
||||
}
|
||||
|
|
|
@ -39,10 +39,12 @@ int nice(int delta) {
|
|||
p = delta;
|
||||
} else {
|
||||
delta = clamp(delta);
|
||||
if ((p = getpriority(PRIO_PROCESS, 0)) == -1) return -1;
|
||||
if ((p = getpriority(PRIO_PROCESS, 0)) == -1)
|
||||
return -1;
|
||||
p += delta;
|
||||
}
|
||||
p = clamp(p);
|
||||
if (setpriority(PRIO_PROCESS, 0, p) == -1) return -1;
|
||||
if (setpriority(PRIO_PROCESS, 0, p) == -1)
|
||||
return -1;
|
||||
return p;
|
||||
}
|
||||
|
|
|
@ -110,17 +110,21 @@ struct SpawnFds {
|
|||
static atomic_bool has_vfork; // i.e. not qemu/wsl/xnu/openbsd
|
||||
|
||||
static textwindows int64_t spawnfds_handle(struct SpawnFds *fds, int fd) {
|
||||
if (__is_cloexec(fds->p + fd)) return -1;
|
||||
if (__is_cloexec(fds->p + fd))
|
||||
return -1;
|
||||
return fds->p[fd].handle;
|
||||
}
|
||||
|
||||
static textwindows errno_t spawnfds_ensure(struct SpawnFds *fds, int fd) {
|
||||
int n2;
|
||||
struct Fd *p2;
|
||||
if (fd < 0) return EBADF;
|
||||
if (fd < fds->n) return 0;
|
||||
if (fd < 0)
|
||||
return EBADF;
|
||||
if (fd < fds->n)
|
||||
return 0;
|
||||
n2 = fd + 1;
|
||||
if (!(p2 = realloc(fds->p, n2 * sizeof(*fds->p)))) return ENOMEM;
|
||||
if (!(p2 = realloc(fds->p, n2 * sizeof(*fds->p))))
|
||||
return ENOMEM;
|
||||
bzero(p2 + fds->n, (n2 - fds->n) * sizeof(*fds->p));
|
||||
fds->p = p2;
|
||||
fds->n = n2;
|
||||
|
@ -141,7 +145,8 @@ static textwindows void spawnfds_destroy(struct SpawnFds *fds) {
|
|||
static textwindows int spawnfds_closelater(struct SpawnFds *fds,
|
||||
int64_t handle) {
|
||||
struct Closer *closer;
|
||||
if (!(closer = malloc(sizeof(struct Closer)))) return ENOMEM;
|
||||
if (!(closer = malloc(sizeof(struct Closer))))
|
||||
return ENOMEM;
|
||||
closer->handle = handle;
|
||||
dll_init(&closer->elem);
|
||||
dll_make_last(&fds->closers, &closer->elem);
|
||||
|
@ -170,7 +175,8 @@ static textwindows errno_t spawnfds_dup2(struct SpawnFds *fds, int fildes,
|
|||
} else {
|
||||
return EBADF;
|
||||
}
|
||||
if ((err = spawnfds_ensure(fds, newfildes))) return err;
|
||||
if ((err = spawnfds_ensure(fds, newfildes)))
|
||||
return err;
|
||||
struct Fd *neu = fds->p + newfildes;
|
||||
memcpy(neu, old, sizeof(struct Fd));
|
||||
neu->flags &= ~O_CLOEXEC;
|
||||
|
@ -189,7 +195,8 @@ static textwindows errno_t spawnfds_open(struct SpawnFds *fds, int64_t dirhand,
|
|||
errno_t err;
|
||||
char16_t path16[PATH_MAX];
|
||||
uint32_t perm, share, disp, attr;
|
||||
if ((err = spawnfds_ensure(fds, fildes))) return err;
|
||||
if ((err = spawnfds_ensure(fds, fildes)))
|
||||
return err;
|
||||
if (__mkntpathath(dirhand, path, 0, path16) != -1 &&
|
||||
GetNtOpenFlags(oflag, mode, &perm, &share, &disp, &attr) != -1 &&
|
||||
(h = CreateFile(path16, perm, share, &kNtIsInheritable, disp, attr, 0))) {
|
||||
|
@ -277,8 +284,10 @@ static textwindows errno_t posix_spawn_nt_impl(
|
|||
|
||||
// fork file descriptor table
|
||||
for (int fd = g_fds.n; fd--;) {
|
||||
if (__is_cloexec(g_fds.p + fd)) continue;
|
||||
if ((err = spawnfds_ensure(&fds, fd))) goto ReturnErr;
|
||||
if (__is_cloexec(g_fds.p + fd))
|
||||
continue;
|
||||
if ((err = spawnfds_ensure(&fds, fd)))
|
||||
goto ReturnErr;
|
||||
fds.p[fd] = g_fds.p[fd];
|
||||
}
|
||||
|
||||
|
@ -369,7 +378,8 @@ static textwindows errno_t posix_spawn_nt_impl(
|
|||
// launch process
|
||||
int rc = -1;
|
||||
struct NtProcessInformation procinfo;
|
||||
if (!envp) envp = environ;
|
||||
if (!envp)
|
||||
envp = environ;
|
||||
if ((fdspec = __describe_fds(fds.p, fds.n, &startinfo, hCreatorProcess,
|
||||
&lpExplicitHandles, &dwExplicitHandleCount))) {
|
||||
rc = ntspawn(dirhand, path, argv, envp, (char *[]){fdspec, maskvar, 0},
|
||||
|
@ -385,7 +395,8 @@ static textwindows errno_t posix_spawn_nt_impl(
|
|||
CloseHandle(procinfo.hThread);
|
||||
proc->pid = procinfo.dwProcessId;
|
||||
proc->handle = procinfo.hProcess;
|
||||
if (pid) *pid = proc->pid;
|
||||
if (pid)
|
||||
*pid = proc->pid;
|
||||
__proc_lock();
|
||||
__proc_add(proc);
|
||||
__proc_unlock();
|
||||
|
@ -395,8 +406,10 @@ static textwindows errno_t posix_spawn_nt_impl(
|
|||
}
|
||||
|
||||
static const char *DescribePid(char buf[12], int err, int *pid) {
|
||||
if (err) return "n/a";
|
||||
if (!pid) return "NULL";
|
||||
if (err)
|
||||
return "n/a";
|
||||
if (!pid)
|
||||
return "NULL";
|
||||
FormatInt32(buf, *pid);
|
||||
return buf;
|
||||
}
|
||||
|
@ -496,7 +509,8 @@ errno_t posix_spawn(int *pid, const char *path,
|
|||
bool lost_cloexec = 0;
|
||||
struct sigaction dfl = {0};
|
||||
short flags = attrp && *attrp ? (*attrp)->flags : 0;
|
||||
if (use_pipe) close(pfds[0]);
|
||||
if (use_pipe)
|
||||
close(pfds[0]);
|
||||
for (int sig = 1; sig < _NSIG; sig++) {
|
||||
if (__sighandrvas[sig] != (long)SIG_DFL &&
|
||||
(__sighandrvas[sig] != (long)SIG_IGN ||
|
||||
|
@ -606,7 +620,8 @@ errno_t posix_spawn(int *pid, const char *path,
|
|||
childmask = oldmask;
|
||||
}
|
||||
sigprocmask(SIG_SETMASK, &childmask, 0);
|
||||
if (!envp) envp = environ;
|
||||
if (!envp)
|
||||
envp = environ;
|
||||
execve(path, argv, envp);
|
||||
ChildFailed:
|
||||
res = errno;
|
||||
|
@ -631,7 +646,8 @@ errno_t posix_spawn(int *pid, const char *path,
|
|||
read(pfds[0], &res, sizeof(res));
|
||||
}
|
||||
if (!res) {
|
||||
if (pid) *pid = child;
|
||||
if (pid)
|
||||
*pid = child;
|
||||
} else {
|
||||
wait4(child, 0, 0, 0);
|
||||
}
|
||||
|
|
|
@ -24,9 +24,11 @@
|
|||
int __posix_spawn_add_file_action(posix_spawn_file_actions_t *l,
|
||||
struct _posix_faction a) {
|
||||
struct _posix_faction *ap;
|
||||
if (!(ap = malloc(sizeof(*ap)))) return ENOMEM;
|
||||
if (!(ap = malloc(sizeof(*ap))))
|
||||
return ENOMEM;
|
||||
*ap = a;
|
||||
while (*l) l = &(*l)->next;
|
||||
while (*l)
|
||||
l = &(*l)->next;
|
||||
*l = ap;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -32,7 +32,8 @@
|
|||
int posix_spawn_file_actions_addchdir_np(
|
||||
posix_spawn_file_actions_t *file_actions, const char *path) {
|
||||
char *path2;
|
||||
if (!(path2 = strdup(path))) return ENOMEM;
|
||||
if (!(path2 = strdup(path)))
|
||||
return ENOMEM;
|
||||
return __posix_spawn_add_file_action(file_actions,
|
||||
(struct _posix_faction){
|
||||
.action = _POSIX_SPAWN_CHDIR,
|
||||
|
|
|
@ -31,7 +31,8 @@
|
|||
*/
|
||||
int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t *file_actions,
|
||||
int fildes) {
|
||||
if (fildes < 0) return EBADF;
|
||||
if (fildes < 0)
|
||||
return EBADF;
|
||||
return __posix_spawn_add_file_action(file_actions,
|
||||
(struct _posix_faction){
|
||||
.action = _POSIX_SPAWN_CLOSE,
|
||||
|
|
|
@ -31,7 +31,8 @@
|
|||
*/
|
||||
int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t *file_actions,
|
||||
int fildes, int newfildes) {
|
||||
if (fildes < 0 || newfildes < 0) return EBADF;
|
||||
if (fildes < 0 || newfildes < 0)
|
||||
return EBADF;
|
||||
return __posix_spawn_add_file_action(file_actions,
|
||||
(struct _posix_faction){
|
||||
.action = _POSIX_SPAWN_DUP2,
|
||||
|
|
|
@ -30,7 +30,8 @@
|
|||
*/
|
||||
int posix_spawn_file_actions_addfchdir_np(
|
||||
posix_spawn_file_actions_t *file_actions, int fildes) {
|
||||
if (fildes < 0) return EBADF;
|
||||
if (fildes < 0)
|
||||
return EBADF;
|
||||
return __posix_spawn_add_file_action(file_actions,
|
||||
(struct _posix_faction){
|
||||
.action = _POSIX_SPAWN_FCHDIR,
|
||||
|
|
|
@ -36,8 +36,10 @@ int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t *file_actions,
|
|||
int fildes, const char *path, int oflag,
|
||||
unsigned mode) {
|
||||
char *path2;
|
||||
if (fildes < 0) return EBADF;
|
||||
if (!(path2 = strdup(path))) return ENOMEM;
|
||||
if (fildes < 0)
|
||||
return EBADF;
|
||||
if (!(path2 = strdup(path)))
|
||||
return ENOMEM;
|
||||
return __posix_spawn_add_file_action(file_actions,
|
||||
(struct _posix_faction){
|
||||
.action = _POSIX_SPAWN_OPEN,
|
||||
|
|
|
@ -33,6 +33,7 @@ int posix_spawnp(int *pid, const char *path,
|
|||
const posix_spawnattr_t *attrp, char *const argv[],
|
||||
char *const envp[]) {
|
||||
char pathbuf[PATH_MAX];
|
||||
if (!(path = commandv(path, pathbuf, sizeof(pathbuf)))) return errno;
|
||||
if (!(path = commandv(path, pathbuf, sizeof(pathbuf))))
|
||||
return errno;
|
||||
return posix_spawn(pid, path, file_actions, attrp, argv, envp);
|
||||
}
|
||||
|
|
|
@ -84,7 +84,8 @@ textwindows int __proc_harvest(struct Proc *pr, bool iswait4) {
|
|||
uint32_t status;
|
||||
struct rusage ru;
|
||||
GetExitCodeProcess(pr->handle, &status);
|
||||
if (status == kNtStillActive) return 0;
|
||||
if (status == kNtStillActive)
|
||||
return 0;
|
||||
__proc_stats(pr->handle, &ru);
|
||||
rusage_add(&pr->ru, &ru);
|
||||
rusage_add(&__proc.ruchlds, &ru);
|
||||
|
@ -176,8 +177,10 @@ static textwindows dontinstrument uint32_t __proc_worker(void *arg) {
|
|||
|
||||
// release our waiter status
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (handles[j] == __proc.onbirth) continue;
|
||||
if (j == i) continue;
|
||||
if (handles[j] == __proc.onbirth)
|
||||
continue;
|
||||
if (j == i)
|
||||
continue;
|
||||
if (!--objects[j]->waiters && objects[j]->status == PROC_UNDEAD) {
|
||||
__proc_free(objects[j]);
|
||||
}
|
||||
|
|
|
@ -53,7 +53,8 @@ textwindows int sys_setpriority_nt(int which, unsigned pid, int nice) {
|
|||
tier = kNtIdlePriorityClass;
|
||||
}
|
||||
|
||||
if (SetPriorityClass(handle, tier)) return 0;
|
||||
if (SetPriorityClass(handle, tier))
|
||||
return 0;
|
||||
STRACE("SetPriorityClass() failed with %d", GetLastError());
|
||||
switch (GetLastError()) {
|
||||
case kNtErrorInvalidHandle:
|
||||
|
|
|
@ -64,7 +64,8 @@
|
|||
int system(const char *cmdline) {
|
||||
int pid, wstatus;
|
||||
sigset_t chldmask, savemask;
|
||||
if (!cmdline) return 1;
|
||||
if (!cmdline)
|
||||
return 1;
|
||||
sigemptyset(&chldmask);
|
||||
sigaddset(&chldmask, SIGINT);
|
||||
sigaddset(&chldmask, SIGQUIT);
|
||||
|
|
|
@ -38,10 +38,12 @@ static dontinline long times2(struct tms *out_times, struct rusage *ru) {
|
|||
struct timeval tv;
|
||||
struct NtFileTime CreationTime, ExitTime, KernelTime, UserTime;
|
||||
if (!IsWindows()) {
|
||||
if (getrusage(RUSAGE_SELF, ru) == -1) return -1;
|
||||
if (getrusage(RUSAGE_SELF, ru) == -1)
|
||||
return -1;
|
||||
out_times->tms_utime = ConvertMicros(ru->ru_utime);
|
||||
out_times->tms_stime = ConvertMicros(ru->ru_stime);
|
||||
if (getrusage(RUSAGE_CHILDREN, ru) == -1) return -1;
|
||||
if (getrusage(RUSAGE_CHILDREN, ru) == -1)
|
||||
return -1;
|
||||
out_times->tms_cutime = ConvertMicros(ru->ru_utime);
|
||||
out_times->tms_cstime = ConvertMicros(ru->ru_stime);
|
||||
} else {
|
||||
|
@ -54,7 +56,8 @@ static dontinline long times2(struct tms *out_times, struct rusage *ru) {
|
|||
out_times->tms_cutime = 0;
|
||||
out_times->tms_cstime = 0;
|
||||
}
|
||||
if (gettimeofday(&tv, NULL) == -1) return -1;
|
||||
if (gettimeofday(&tv, NULL) == -1)
|
||||
return -1;
|
||||
return ConvertMicros(tv);
|
||||
}
|
||||
|
||||
|
|
|
@ -116,7 +116,8 @@ static textwindows int __proc_wait(int pid, int *wstatus, int options,
|
|||
for (struct Dll *e = dll_first(__proc.list); e;
|
||||
e = dll_next(__proc.list, e)) {
|
||||
pr = PROC_CONTAINER(e);
|
||||
if (pid == pr->pid) break;
|
||||
if (pid == pr->pid)
|
||||
break;
|
||||
}
|
||||
if (pr) {
|
||||
// by making the waiter count non-zero, the proc daemon stops
|
||||
|
@ -211,12 +212,15 @@ static textwindows int __proc_wait(int pid, int *wstatus, int options,
|
|||
textwindows int sys_wait4_nt(int pid, int *opt_out_wstatus, int options,
|
||||
struct rusage *opt_out_rusage) {
|
||||
// no support for WCONTINUED and WUNTRACED yet
|
||||
if (options & ~WNOHANG) return einval();
|
||||
if (options & ~WNOHANG)
|
||||
return einval();
|
||||
// XXX: NT doesn't really have process groups. For instance the
|
||||
// CreateProcess() flag for starting a process group actually
|
||||
// just does an "ignore ctrl-c" internally.
|
||||
if (pid == 0) pid = -1;
|
||||
if (pid < -1) pid = -pid;
|
||||
if (pid == 0)
|
||||
pid = -1;
|
||||
if (pid < -1)
|
||||
pid = -pid;
|
||||
sigset_t m = __sig_block();
|
||||
int rc = __proc_wait(pid, opt_out_wstatus, options, opt_out_rusage,
|
||||
m | 1ull << (SIGCHLD - 1));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue