cosmopolitan/libc/calls/hefty/execve-nt.c

69 lines
3.4 KiB
C
Raw Normal View History

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
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/hefty/internal.h"
#include "libc/calls/hefty/ntspawn.h"
#include "libc/calls/internal.h"
2020-12-01 11:43:40 +00:00
#include "libc/nt/accounting.h"
2020-06-15 14:18:57 +00:00
#include "libc/nt/enum/startf.h"
2020-12-01 11:43:40 +00:00
#include "libc/nt/enum/status.h"
2020-06-15 14:18:57 +00:00
#include "libc/nt/runtime.h"
2020-12-01 11:43:40 +00:00
#include "libc/nt/struct/processinformation.h"
2020-06-15 14:18:57 +00:00
#include "libc/nt/struct/startupinfo.h"
2020-12-01 11:43:40 +00:00
#include "libc/nt/synchronization.h"
2020-06-15 14:18:57 +00:00
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/sock.h"
textwindows int execve$nt(const char *program, char *const argv[],
char *const envp[]) {
2020-12-01 11:43:40 +00:00
int i;
uint32_t dwExitCode;
2020-06-15 14:18:57 +00:00
struct NtStartupInfo startinfo;
2020-12-01 11:43:40 +00:00
struct NtProcessInformation procinfo;
2020-06-15 14:18:57 +00:00
memset(&startinfo, 0, sizeof(startinfo));
startinfo.cb = sizeof(struct NtStartupInfo);
startinfo.dwFlags = kNtStartfUsestdhandles;
2020-12-01 11:43:40 +00:00
startinfo.hStdInput = g_fds.p[0].handle;
startinfo.hStdOutput = g_fds.p[1].handle;
startinfo.hStdError = g_fds.p[2].handle;
for (i = 2; i < g_fds.n; ++i) {
if (g_fds.p[i].kind != kFdEmpty && (g_fds.p[i].flags & O_CLOEXEC)) {
close(i);
}
}
2020-06-15 14:18:57 +00:00
if (ntspawn(program, argv, envp, NULL, NULL, true, 0, NULL, &startinfo,
2020-12-01 11:43:40 +00:00
&procinfo) == -1) {
return -1;
}
CloseHandle(procinfo.hThread);
for (i = 0; i < g_fds.n; ++i) {
if (g_fds.p[i].kind != kFdEmpty) {
close(i);
}
2020-06-15 14:18:57 +00:00
}
2020-12-01 11:43:40 +00:00
do {
WaitForSingleObject(procinfo.hProcess, -1);
dwExitCode = kNtStillActive;
GetExitCodeProcess(procinfo.hProcess, &dwExitCode);
} while (dwExitCode == kNtStillActive);
ExitProcess(dwExitCode);
2020-06-15 14:18:57 +00:00
}