2020-06-15 14:18:57 +00:00
|
|
|
|
/*-*- 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│
|
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2021-01-25 21:08:05 +00:00
|
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
2020-06-15 14:18:57 +00:00
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-01-25 21:08:05 +00:00
|
|
|
|
#include "libc/bits/pushpop.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
#include "libc/calls/internal.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
|
#include "libc/calls/ntspawn.h"
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
|
#include "libc/calls/strace.internal.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
|
#include "libc/macros.internal.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
|
#include "libc/nt/enum/filemapflags.h"
|
|
|
|
|
#include "libc/nt/enum/pageflags.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
|
#include "libc/nt/enum/processcreationflags.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
|
#include "libc/nt/memory.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
#include "libc/nt/process.h"
|
|
|
|
|
#include "libc/nt/runtime.h"
|
2021-01-25 21:08:05 +00:00
|
|
|
|
#include "libc/nt/struct/processinformation.h"
|
|
|
|
|
#include "libc/nt/struct/securityattributes.h"
|
|
|
|
|
#include "libc/nt/struct/startupinfo.h"
|
|
|
|
|
|
|
|
|
|
struct SpawnBlock {
|
|
|
|
|
char16_t cmdline[ARG_MAX];
|
|
|
|
|
char16_t envvars[ARG_MAX];
|
|
|
|
|
};
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Spawns process on Windows NT.
|
|
|
|
|
*
|
|
|
|
|
* This function delegates to CreateProcess() with UTF-8 → UTF-16
|
|
|
|
|
* translation and argv escaping. Please note this will NOT escape
|
|
|
|
|
* command interpreter syntax.
|
|
|
|
|
*
|
2021-01-25 21:08:05 +00:00
|
|
|
|
* @param prog won't be PATH searched
|
|
|
|
|
* @param argv specifies prog arguments
|
2020-06-15 14:18:57 +00:00
|
|
|
|
* @param envp[𝟶,m-2] specifies "foo=bar" environment variables, which
|
|
|
|
|
* don't need to be passed in sorted order; however, this function
|
|
|
|
|
* goes faster the closer they are to sorted
|
|
|
|
|
* @param envp[m-1] is NULL
|
2021-02-03 06:17:53 +00:00
|
|
|
|
* @param extravar is added to envp to avoid setenv() in caller
|
2020-06-15 14:18:57 +00:00
|
|
|
|
* @param bInheritHandles means handles already marked inheritable will
|
|
|
|
|
* be inherited; which, assuming the System V wrapper functions are
|
|
|
|
|
* being used, should mean (1) all files and sockets that weren't
|
|
|
|
|
* opened with O_CLOEXEC; and (2) all memory mappings
|
|
|
|
|
* @param opt_out_lpProcessInformation can be used to return process and
|
|
|
|
|
* thread IDs to parent, as well as open handles that need close()
|
|
|
|
|
* @return 0 on success, or -1 w/ errno
|
|
|
|
|
* @see spawnve() which abstracts this function
|
|
|
|
|
*/
|
|
|
|
|
textwindows int ntspawn(
|
2021-01-30 06:00:10 +00:00
|
|
|
|
const char *prog, char *const argv[], char *const envp[],
|
2021-02-03 06:17:53 +00:00
|
|
|
|
const char *extravar, struct NtSecurityAttributes *opt_lpProcessAttributes,
|
2020-06-15 14:18:57 +00:00
|
|
|
|
struct NtSecurityAttributes *opt_lpThreadAttributes, bool32 bInheritHandles,
|
|
|
|
|
uint32_t dwCreationFlags, const char16_t *opt_lpCurrentDirectory,
|
2021-01-25 21:08:05 +00:00
|
|
|
|
const struct NtStartupInfo *lpStartupInfo,
|
2020-06-15 14:18:57 +00:00
|
|
|
|
struct NtProcessInformation *opt_out_lpProcessInformation) {
|
|
|
|
|
int rc;
|
2021-01-25 21:08:05 +00:00
|
|
|
|
int64_t handle;
|
|
|
|
|
size_t blocksize;
|
|
|
|
|
struct SpawnBlock *block;
|
2022-04-24 16:59:22 +00:00
|
|
|
|
char16_t prog16[PATH_MAX + 1];
|
2021-01-25 21:08:05 +00:00
|
|
|
|
rc = -1;
|
|
|
|
|
block = NULL;
|
2021-09-28 05:58:51 +00:00
|
|
|
|
if (__mkntpath(prog, prog16) == -1) return -1;
|
2021-01-25 21:08:05 +00:00
|
|
|
|
blocksize = ROUNDUP(sizeof(*block), FRAMESIZE);
|
2022-03-23 15:09:01 +00:00
|
|
|
|
if ((handle = CreateFileMapping(
|
2021-01-25 21:08:05 +00:00
|
|
|
|
-1,
|
|
|
|
|
&(struct NtSecurityAttributes){sizeof(struct NtSecurityAttributes),
|
|
|
|
|
NULL, false},
|
2022-03-23 15:09:01 +00:00
|
|
|
|
pushpop(kNtPageReadwrite), 0, blocksize, NULL)) &&
|
|
|
|
|
(block = MapViewOfFileEx(handle, kNtFileMapRead | kNtFileMapWrite, 0, 0,
|
2022-04-08 03:30:04 +00:00
|
|
|
|
blocksize, NULL)) &&
|
|
|
|
|
mkntcmdline(block->cmdline, prog, argv) != -1 &&
|
|
|
|
|
mkntenvblock(block->envvars, envp, extravar) != -1 &&
|
|
|
|
|
CreateProcess(prog16, block->cmdline, opt_lpProcessAttributes,
|
|
|
|
|
opt_lpThreadAttributes, bInheritHandles,
|
|
|
|
|
dwCreationFlags | kNtCreateUnicodeEnvironment,
|
|
|
|
|
block->envvars, opt_lpCurrentDirectory, lpStartupInfo,
|
|
|
|
|
opt_out_lpProcessInformation)) {
|
|
|
|
|
rc = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
}
|
2021-01-25 21:08:05 +00:00
|
|
|
|
if (block) UnmapViewOfFile(block);
|
|
|
|
|
if (handle) CloseHandle(handle);
|
2022-04-20 16:56:53 +00:00
|
|
|
|
return __fix_enotdir(rc, prog16);
|
2020-06-15 14:18:57 +00:00
|
|
|
|
}
|