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/calls/ntspawn.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2022-10-12 04:06:27 +00:00
|
|
|
|
#include "libc/intrin/kprintf.h"
|
|
|
|
|
#include "libc/intrin/pushpop.h"
|
|
|
|
|
#include "libc/intrin/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 {
|
2022-04-28 16:42:36 +00:00
|
|
|
|
union {
|
|
|
|
|
struct {
|
|
|
|
|
char16_t cmdline[ARG_MAX / 2];
|
|
|
|
|
char16_t envvars[ARG_MAX / 2];
|
2022-05-25 18:31:08 +00:00
|
|
|
|
char buf[ARG_MAX];
|
2022-04-28 16:42:36 +00:00
|
|
|
|
};
|
2022-05-25 18:31:08 +00:00
|
|
|
|
char __pad[ROUNDUP(ARG_MAX / 2 * 3 * sizeof(char16_t), FRAMESIZE)];
|
2022-04-28 16:42:36 +00:00
|
|
|
|
};
|
2021-01-25 21:08:05 +00:00
|
|
|
|
};
|
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;
|
|
|
|
|
struct SpawnBlock *block;
|
2022-04-28 16:42:36 +00:00
|
|
|
|
char16_t prog16[PATH_MAX];
|
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;
|
2022-04-28 16:42:36 +00:00
|
|
|
|
if ((handle = CreateFileMapping(-1, 0, pushpop(kNtPageReadwrite), 0,
|
|
|
|
|
sizeof(*block), 0)) &&
|
2022-03-23 15:09:01 +00:00
|
|
|
|
(block = MapViewOfFileEx(handle, kNtFileMapRead | kNtFileMapWrite, 0, 0,
|
2022-04-28 16:42:36 +00:00
|
|
|
|
sizeof(*block), 0)) &&
|
2022-04-08 03:30:04 +00:00
|
|
|
|
mkntcmdline(block->cmdline, prog, argv) != -1 &&
|
2022-05-25 18:31:08 +00:00
|
|
|
|
mkntenvblock(block->envvars, envp, extravar, block->buf) != -1 &&
|
2022-04-08 03:30:04 +00:00
|
|
|
|
CreateProcess(prog16, block->cmdline, opt_lpProcessAttributes,
|
|
|
|
|
opt_lpThreadAttributes, bInheritHandles,
|
2022-10-12 04:06:27 +00:00
|
|
|
|
dwCreationFlags | kNtCreateUnicodeEnvironment |
|
|
|
|
|
kNtInheritParentAffinity,
|
2022-04-08 03:30:04 +00:00
|
|
|
|
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
|
|
|
|
}
|