2012-04-02 22:18:01 -05:00
|
|
|
|
|
|
|
#include "rust_kernel.h"
|
2011-03-11 06:30:18 -06:00
|
|
|
|
2012-02-07 20:55:02 -06:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <crt_externs.h>
|
|
|
|
#endif
|
|
|
|
|
2011-03-11 06:30:18 -06:00
|
|
|
#if defined(__WIN32__)
|
|
|
|
|
|
|
|
#include <process.h>
|
|
|
|
#include <io.h>
|
|
|
|
|
2012-01-23 20:37:26 -06:00
|
|
|
bool backslash_run_ends_in_quote(char const *c) {
|
|
|
|
while (*c == '\\') ++c;
|
|
|
|
return *c == '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
void append_first_char(char *&buf, char const *c) {
|
|
|
|
switch (*c) {
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
// Escape quotes.
|
|
|
|
*buf++ = '\\';
|
|
|
|
*buf++ = '"';
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
if (backslash_run_ends_in_quote(c)) {
|
|
|
|
// Double all backslashes that are in runs before quotes.
|
|
|
|
*buf++ = '\\';
|
|
|
|
*buf++ = '\\';
|
|
|
|
} else {
|
|
|
|
// Pass other backslashes through unescaped.
|
|
|
|
*buf++ = '\\';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*buf++ = *c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains_whitespace(char const *arg) {
|
|
|
|
while (*arg) {
|
|
|
|
switch (*arg++) {
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void append_arg(char *& buf, char const *arg, bool last) {
|
|
|
|
bool quote = contains_whitespace(arg);
|
|
|
|
if (quote)
|
|
|
|
*buf++ = '"';
|
|
|
|
while (*arg)
|
|
|
|
append_first_char(buf, arg++);
|
|
|
|
if (quote)
|
|
|
|
*buf++ = '"';
|
|
|
|
|
|
|
|
if (! last) {
|
|
|
|
*buf++ = ' ';
|
|
|
|
} else {
|
|
|
|
*buf++ = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-11 06:30:18 -06:00
|
|
|
extern "C" CDECL int
|
2012-02-07 20:55:02 -06:00
|
|
|
rust_run_program(const char* argv[],
|
|
|
|
void* envp,
|
|
|
|
const char* dir,
|
|
|
|
int in_fd, int out_fd, int err_fd) {
|
2011-03-11 06:30:18 -06:00
|
|
|
STARTUPINFO si;
|
|
|
|
ZeroMemory(&si, sizeof(STARTUPINFO));
|
|
|
|
si.cb = sizeof(STARTUPINFO);
|
|
|
|
si.dwFlags = STARTF_USESTDHANDLES;
|
2011-08-03 16:50:51 -05:00
|
|
|
|
|
|
|
HANDLE curproc = GetCurrentProcess();
|
|
|
|
HANDLE origStdin = (HANDLE)_get_osfhandle(in_fd ? in_fd : 0);
|
|
|
|
if (!DuplicateHandle(curproc, origStdin,
|
|
|
|
curproc, &si.hStdInput, 0, 1, DUPLICATE_SAME_ACCESS))
|
|
|
|
return -1;
|
|
|
|
HANDLE origStdout = (HANDLE)_get_osfhandle(out_fd ? out_fd : 1);
|
|
|
|
if (!DuplicateHandle(curproc, origStdout,
|
|
|
|
curproc, &si.hStdOutput, 0, 1, DUPLICATE_SAME_ACCESS))
|
|
|
|
return -1;
|
|
|
|
HANDLE origStderr = (HANDLE)_get_osfhandle(err_fd ? err_fd : 2);
|
|
|
|
if (!DuplicateHandle(curproc, origStderr,
|
|
|
|
curproc, &si.hStdError, 0, 1, DUPLICATE_SAME_ACCESS))
|
|
|
|
return -1;
|
2011-03-11 06:30:18 -06:00
|
|
|
|
|
|
|
size_t cmd_len = 0;
|
|
|
|
for (const char** arg = argv; *arg; arg++) {
|
|
|
|
cmd_len += strlen(*arg);
|
2012-01-23 20:37:26 -06:00
|
|
|
cmd_len += 3; // Two quotes plus trailing space or \0
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
2012-01-23 20:37:26 -06:00
|
|
|
cmd_len *= 2; // Potentially backslash-escape everything.
|
|
|
|
|
2011-03-11 06:30:18 -06:00
|
|
|
char* cmd = (char*)malloc(cmd_len);
|
|
|
|
char* pos = cmd;
|
|
|
|
for (const char** arg = argv; *arg; arg++) {
|
2012-01-23 20:37:26 -06:00
|
|
|
append_arg(pos, *arg, *(arg+1) == NULL);
|
2011-03-11 06:30:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
BOOL created = CreateProcess(NULL, cmd, NULL, NULL, TRUE,
|
2012-02-07 20:55:02 -06:00
|
|
|
0, envp, dir, &si, &pi);
|
2011-08-03 18:25:38 -05:00
|
|
|
|
2011-08-03 16:50:51 -05:00
|
|
|
CloseHandle(si.hStdInput);
|
|
|
|
CloseHandle(si.hStdOutput);
|
|
|
|
CloseHandle(si.hStdError);
|
2011-03-11 06:30:18 -06:00
|
|
|
free(cmd);
|
|
|
|
|
|
|
|
if (!created) return -1;
|
|
|
|
return (int)pi.hProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" CDECL int
|
2011-10-20 05:32:43 -05:00
|
|
|
rust_process_wait(int proc) {
|
2011-03-11 06:30:18 -06:00
|
|
|
DWORD status;
|
|
|
|
while (true) {
|
|
|
|
if (GetExitCodeProcess((HANDLE)proc, &status) &&
|
|
|
|
status != STILL_ACTIVE)
|
|
|
|
return (int)status;
|
|
|
|
WaitForSingleObject((HANDLE)proc, INFINITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
|
2012-02-09 00:08:24 -06:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
extern char **environ;
|
|
|
|
#endif
|
|
|
|
|
2011-03-11 06:30:18 -06:00
|
|
|
extern "C" CDECL int
|
2012-02-07 20:55:02 -06:00
|
|
|
rust_run_program(const char* argv[],
|
|
|
|
void* envp,
|
|
|
|
const char* dir,
|
|
|
|
int in_fd, int out_fd, int err_fd) {
|
2011-03-11 06:30:18 -06:00
|
|
|
int pid = fork();
|
|
|
|
if (pid != 0) return pid;
|
|
|
|
|
|
|
|
sigset_t sset;
|
|
|
|
sigemptyset(&sset);
|
|
|
|
sigprocmask(SIG_SETMASK, &sset, NULL);
|
|
|
|
|
|
|
|
if (in_fd) dup2(in_fd, 0);
|
|
|
|
if (out_fd) dup2(out_fd, 1);
|
|
|
|
if (err_fd) dup2(err_fd, 2);
|
|
|
|
/* Close all other fds. */
|
|
|
|
for (int fd = getdtablesize() - 1; fd >= 3; fd--) close(fd);
|
2012-02-07 22:28:57 -06:00
|
|
|
if (dir) {
|
|
|
|
int result = chdir(dir);
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2674): need error handling
|
2012-02-07 22:28:57 -06:00
|
|
|
assert(!result && "chdir failed");
|
|
|
|
}
|
2012-02-07 20:55:02 -06:00
|
|
|
|
|
|
|
if (envp) {
|
2012-03-28 22:28:35 -05:00
|
|
|
#ifdef __APPLE__
|
2012-02-07 20:55:02 -06:00
|
|
|
*_NSGetEnviron() = (char **)envp;
|
|
|
|
#else
|
2012-03-28 22:28:35 -05:00
|
|
|
environ = (char **)envp;
|
2012-02-07 20:55:02 -06:00
|
|
|
#endif
|
2012-03-28 22:28:35 -05:00
|
|
|
}
|
2012-02-07 20:55:02 -06:00
|
|
|
|
2012-03-28 22:28:35 -05:00
|
|
|
execvp(argv[0], (char * const *)argv);
|
2011-03-11 06:30:18 -06:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-03-25 02:09:20 -05:00
|
|
|
extern "C" CDECL int
|
2011-10-20 05:32:43 -05:00
|
|
|
rust_process_wait(int proc) {
|
2012-06-21 16:17:54 -05:00
|
|
|
// FIXME: stub; exists to placate linker. (#2692)
|
2011-03-25 02:09:20 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-11 06:30:18 -06:00
|
|
|
#else
|
|
|
|
#error "Platform not supported."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: C++
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-07-13 15:51:20 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2011-03-11 06:30:18 -06:00
|
|
|
// End:
|
|
|
|
//
|