#include <config.h>
#include <roken.h>
#include <strsafe.h>
#ifndef _WIN32
#error This implementation is Windows specific.
#endif
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
wait_for_process_timed(pid_t pid, time_t (*func)(void *),
void *ptr, time_t timeout)
{
HANDLE hProcess;
DWORD wrv = 0;
DWORD dtimeout;
int rv = 0;
hProcess = OpenProcess(SYNCHRONIZE, FALSE, pid);
if (hProcess == NULL) {
return SE_E_WAITPIDFAILED;
}
dtimeout = (DWORD) ((timeout == 0)? INFINITE: timeout * 1000);
do {
wrv = WaitForSingleObject(hProcess, dtimeout);
if (wrv == WAIT_OBJECT_0) {
DWORD prv = 0;
GetExitCodeProcess(hProcess, &prv);
rv = (int) prv;
break;
} else if (wrv == WAIT_TIMEOUT) {
if (func == NULL)
continue;
timeout = (*func)(ptr);
if (timeout == (time_t)-1) {
if (TerminateProcess(hProcess, 128 + 9)) {
dtimeout = INFINITE;
continue;
}
rv = SE_E_UNSPECIFIED;
break;
} else if (timeout == (time_t) -2) {
rv = SE_E_EXECTIMEOUT;
break;
} else {
dtimeout = (DWORD) ((timeout == 0)? INFINITE: timeout * 1000);
continue;
}
} else {
rv = SE_E_UNSPECIFIED;
break;
}
} while(TRUE);
CloseHandle(hProcess);
return rv;
}
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
wait_for_process(pid_t pid)
{
return wait_for_process_timed(pid, NULL, NULL, 0);
}
static char *
collect_commandline(const char * fn, va_list * ap)
{
size_t len = 0;
size_t alloc_len = 0;
const char * s;
char * cmd = NULL;
for (s = fn; s; s = (char *) va_arg(*ap, char *)) {
size_t cmp_len;
int need_quote = FALSE;
if (FAILED(StringCchLength(s, MAX_PATH, &cmp_len))) {
if (cmd)
free(cmd);
return NULL;
}
if (cmp_len == 0)
continue;
if (strchr(s, ' ') &&
s[0] != '"' &&
s[cmp_len - 1] != '"') {
need_quote = TRUE;
cmp_len += 2 * sizeof(char);
}
if (s != fn)
cmp_len += 1 * sizeof(char);
if (alloc_len < len + cmp_len + 1) {
char * nc;
alloc_len += ((len + cmp_len - alloc_len) / MAX_PATH + 1) * MAX_PATH;
nc = (char *) realloc(cmd, alloc_len * sizeof(char));
if (nc == NULL) {
if (cmd)
free(cmd);
return NULL;
}
}
if (cmd == NULL)
return NULL;
if (s != fn)
cmd[len++] = ' ';
if (need_quote) {
StringCchPrintf(cmd + len, alloc_len - len, "\"%s\"", s);
} else {
StringCchCopy(cmd + len, alloc_len - len, s);
}
len += cmp_len;
}
return cmd;
}
ROKEN_LIB_FUNCTION pid_t ROKEN_LIB_CALL
pipe_execv(FILE **stdin_fd, FILE **stdout_fd, FILE **stderr_fd,
const char *file, ...)
{
HANDLE hOut_r = NULL;
HANDLE hOut_w = NULL;
HANDLE hIn_r = NULL;
HANDLE hIn_w = NULL;
HANDLE hErr_r = NULL;
HANDLE hErr_w = NULL;
SECURITY_ATTRIBUTES sa;
STARTUPINFO si;
PROCESS_INFORMATION pi;
char * commandline = NULL;
pid_t rv = (pid_t) -1;
{
va_list ap;
va_start(ap, file);
commandline = collect_commandline(file, &ap);
if (commandline == NULL)
return rv;
}
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
ZeroMemory(&sa, sizeof(sa));
pi.hProcess = NULL;
pi.hThread = NULL;
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
if ((stdout_fd && !CreatePipe(&hOut_r, &hOut_w, &sa, 0 )) ||
(stdin_fd && !CreatePipe(&hIn_r, &hIn_w, &sa, 0)) ||
(stderr_fd && !CreatePipe(&hErr_r, &hErr_w, &sa, 0)) ||
(!stdout_fd && (hOut_w = CreateFile("CON", GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
&sa, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE) ||
(!stdin_fd && (hIn_r = CreateFile("CON",GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,
&sa, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE) ||
(!stderr_fd && (hErr_w = CreateFile("CON", GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
&sa, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE))
goto _exit;
if (hOut_r)
SetHandleInformation(hOut_r, HANDLE_FLAG_INHERIT, FALSE);
if (hIn_w)
SetHandleInformation(hIn_w, HANDLE_FLAG_INHERIT, FALSE);
if (hErr_r)
SetHandleInformation(hErr_r, HANDLE_FLAG_INHERIT, FALSE);
si.cb = sizeof(si);
si.lpReserved = NULL;
si.lpDesktop = NULL;
si.lpTitle = NULL;
si.dwFlags = STARTF_USESTDHANDLES;
si.hStdInput = hIn_r;
si.hStdOutput = hOut_w;
si.hStdError = hErr_w;
if (!CreateProcess(file, commandline, NULL, NULL,
TRUE,
CREATE_NO_WINDOW,
NULL,
NULL,
&si,
&pi)) {
rv = (pid_t) (GetLastError() == ERROR_FILE_NOT_FOUND)? 127 : -1;
goto _exit;
}
if (stdin_fd) {
*stdin_fd = _fdopen(_open_osfhandle((intptr_t) hIn_w, 0), "wb");
if (*stdin_fd)
hIn_w = NULL;
}
if (stdout_fd) {
*stdout_fd = _fdopen(_open_osfhandle((intptr_t) hOut_r, _O_RDONLY), "rb");
if (*stdout_fd)
hOut_r = NULL;
}
if (stderr_fd) {
*stderr_fd = _fdopen(_open_osfhandle((intptr_t) hErr_r, _O_RDONLY), "rb");
if (*stderr_fd)
hErr_r = NULL;
}
rv = (pid_t) pi.dwProcessId;
_exit:
if (pi.hProcess) CloseHandle(pi.hProcess);
if (pi.hThread) CloseHandle(pi.hThread);
if (hIn_r) CloseHandle(hIn_r);
if (hIn_w) CloseHandle(hIn_w);
if (hOut_r) CloseHandle(hOut_r);
if (hOut_w) CloseHandle(hOut_w);
if (hErr_r) CloseHandle(hErr_r);
if (hErr_w) CloseHandle(hErr_w);
return rv;
}
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execvp_timed(const char *file, char *const args[],
time_t (*func)(void *), void *ptr, time_t timeout)
{
intptr_t hp;
int rv;
hp = spawnvp(_P_NOWAIT, file, args);
if (hp == -1)
return (errno == ENOENT)? 127: 126;
else if (hp == 0)
return 0;
rv = wait_for_process_timed(GetProcessId((HANDLE) hp), func, ptr, timeout);
CloseHandle((HANDLE) hp);
return rv;
}
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execvp(const char *file, char *const args[])
{
return simple_execvp_timed(file, args, NULL, NULL, 0);
}
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execve_timed(const char *file, char *const args[], char *const envp[],
time_t (*func)(void *), void *ptr, time_t timeout)
{
intptr_t hp;
int rv;
hp = spawnve(_P_NOWAIT, file, args, envp);
if (hp == -1)
return (errno == ENOENT)? 127: 126;
else if (hp == 0)
return 0;
rv = wait_for_process_timed(GetProcessId((HANDLE) hp), func, ptr, timeout);
CloseHandle((HANDLE) hp);
return rv;
}
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execve(const char *file, char *const args[], char *const envp[])
{
return simple_execve_timed(file, args, envp, NULL, NULL, 0);
}
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execlp(const char *file, ...)
{
va_list ap;
char **argv;
int ret;
va_start(ap, file);
argv = vstrcollect(&ap);
va_end(ap);
if(argv == NULL)
return SE_E_UNSPECIFIED;
ret = simple_execvp(file, argv);
free(argv);
return ret;
}
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execle(const char *file, ... )
{
va_list ap;
char **argv;
char *const* envp;
int ret;
va_start(ap, file);
argv = vstrcollect(&ap);
envp = va_arg(ap, char **);
va_end(ap);
if(argv == NULL)
return SE_E_UNSPECIFIED;
ret = simple_execve(file, argv, envp);
free(argv);
return ret;
}