mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-07 06:53:33 +00:00
Add execve() test
This commit is contained in:
parent
38728cef79
commit
a259e43d84
7 changed files with 75 additions and 13 deletions
|
@ -14,10 +14,12 @@
|
|||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
|
||||
STATIC_YOINK("strerror");
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
sigset_t ss;
|
||||
if (argc < 3) {
|
||||
fputs("USAGE: EXEC.COM PROG ARGV₀ [ARGV₁...]\n", stderr);
|
||||
if (argc < 2) {
|
||||
fputs("USAGE: EXEC.COM PROG [ARGV₀...]\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -26,6 +28,6 @@ int main(int argc, char *argv[]) {
|
|||
sigaddset(&ss, SIGPWR);
|
||||
sigprocmask(SIG_BLOCK, &ss, 0);
|
||||
|
||||
execv(argv[1], argv + 1);
|
||||
execv(argv[1], argv + 2);
|
||||
return 127;
|
||||
}
|
||||
|
|
|
@ -60,7 +60,12 @@ textwindows noasan int mkntcmdline(char16_t cmdline[ARG_MAX], const char *prog,
|
|||
int slashes, n;
|
||||
bool needsquote;
|
||||
char16_t cbuf[2];
|
||||
char *ansiargv[2];
|
||||
size_t i, j, k, s;
|
||||
if (!argv[0]) {
|
||||
bzero(ansiargv, sizeof(ansiargv));
|
||||
argv = ansiargv;
|
||||
}
|
||||
for (arg = prog, k = i = 0; arg; arg = argv[++i]) {
|
||||
if (i) APPEND(u' ');
|
||||
if ((needsquote = NeedsQuotes(arg))) APPEND(u'"');
|
||||
|
|
|
@ -44,12 +44,12 @@ CreateProcess(const char16_t *opt_lpApplicationName, char16_t *lpCommandLine,
|
|||
opt_lpCurrentDirectory, lpStartupInfo,
|
||||
opt_out_lpProcessInformation);
|
||||
if (!ok) __winerr();
|
||||
NTTRACE(
|
||||
"CreateFile(%#hs, %#hs, %s, %s, %hhhd, %u, %p, %#hs, %p, %p) → %hhhd% m",
|
||||
opt_lpApplicationName, lpCommandLine,
|
||||
DescribeNtSecurityAttributes(opt_lpProcessAttributes),
|
||||
DescribeNtSecurityAttributes(opt_lpThreadAttributes), bInheritHandles,
|
||||
dwCreationFlags, opt_lpEnvironment, opt_lpCurrentDirectory, lpStartupInfo,
|
||||
opt_out_lpProcessInformation, ok);
|
||||
NTTRACE("CreateProcess(%#hs, %#!hs, %s, %s, %hhhd, %u, %p, %#hs, %p, %p) → "
|
||||
"%hhhd% m",
|
||||
opt_lpApplicationName, lpCommandLine,
|
||||
DescribeNtSecurityAttributes(opt_lpProcessAttributes),
|
||||
DescribeNtSecurityAttributes(opt_lpThreadAttributes), bInheritHandles,
|
||||
dwCreationFlags, opt_lpEnvironment, opt_lpCurrentDirectory,
|
||||
lpStartupInfo, opt_out_lpProcessInformation, ok);
|
||||
return ok;
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ bool32 CreateProcess(const char16_t *opt_lpApplicationName,
|
|||
const char16_t *opt_lpCurrentDirectory,
|
||||
const struct NtStartupInfo *lpStartupInfo,
|
||||
struct NtProcessInformation *opt_out_lpProcessInformation)
|
||||
paramsnonnull((2, 9));
|
||||
paramsnonnull((9));
|
||||
|
||||
uint32_t GetThreadId(int64_t hThread); /* cf. NT_TID */
|
||||
uint32_t GetProcessId(int64_t hProcess); /* cf. NT_PID */
|
||||
|
|
|
@ -251,7 +251,7 @@ __msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) {
|
|||
* @param hInstance call GetModuleHandle(NULL) from main if you need it
|
||||
*/
|
||||
__msabi textwindows int64_t WinMain(int64_t hInstance, int64_t hPrevInstance,
|
||||
const char *lpCmdLine, int nCmdShow) {
|
||||
const char *lpCmdLine, int64_t nCmdShow) {
|
||||
const char16_t *cmdline;
|
||||
extern char os asm("__hostos");
|
||||
extern uint64_t ts asm("kStartTsc");
|
||||
|
|
49
test/libc/calls/execve_test.c
Normal file
49
test/libc/calls/execve_test.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
/*-*- 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 2022 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/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
void SetUp(void) {
|
||||
if (getenv("_SUBPROCESS")) {
|
||||
if (!__argv[0]) {
|
||||
exit(0);
|
||||
} else {
|
||||
exit(7);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(execve, testWeirdAnsiC89emptyArgv) {
|
||||
char *prog;
|
||||
int pid, ws;
|
||||
if (IsWindows()) return;
|
||||
if (IsOpenbsd()) return;
|
||||
prog = GetProgramExecutableName();
|
||||
ASSERT_NE(-1, (pid = fork()));
|
||||
if (!pid) {
|
||||
execve(prog, (char *const[]){0}, (char *const[]){"_SUBPROCESS=1", 0});
|
||||
_Exit(127);
|
||||
}
|
||||
ASSERT_NE(-1, wait(&ws));
|
||||
EXPECT_TRUE(WIFEXITED(ws));
|
||||
EXPECT_EQ(0, WEXITSTATUS(ws));
|
||||
}
|
|
@ -25,7 +25,13 @@
|
|||
|
||||
char16_t cmdline[ARG_MAX];
|
||||
|
||||
TEST(mkntcmdline, emptyArgvList_isEmpty) {
|
||||
TEST(mkntcmdline, emptyArgvList_cantBeEmptyOnWindows) {
|
||||
char *argv[] = {NULL};
|
||||
EXPECT_NE(-1, mkntcmdline(cmdline, "foo", argv));
|
||||
EXPECT_STREQ(u"foo", cmdline);
|
||||
}
|
||||
|
||||
TEST(mkntcmdline, emptyArgvListWithProg_isEmpty) {
|
||||
char *argv[] = {NULL};
|
||||
EXPECT_NE(-1, mkntcmdline(cmdline, argv[0], argv));
|
||||
EXPECT_STREQ(u"", cmdline);
|
||||
|
|
Loading…
Reference in a new issue