// Stdio // ===== #ifdef _WIN32 #include #else #include #include extern char** environ; #endif typedef struct { char** argv; u32 argc; int in; int out; u32 pid; } ChildOpen; static void child_free(ChildOpen* c) { for (u32 i = 0; i < c->argc; i += 1) { free(c->argv[i]); } free(c->argv); free(c); } #ifdef _WIN32 static wchar_t* child_wide(const char* s) { int len = MultiByteToWideChar(CP_UTF8, 0, s, -1, NULL, 0); wchar_t* wide = io_mem(malloc((u64)len * sizeof(wchar_t))); MultiByteToWideChar(CP_UTF8, 0, s, -1, wide, len); return wide; } // The argv joined as CommandLineToArgvW splits it. static wchar_t* child_line(ChildOpen* c) { u64 size = 1; for (u32 i = 0; i < c->argc; i += 1) { size += 2 * strlen(c->argv[i]) + 3; } char* line = io_mem(malloc(size)); char* at = line; for (u32 i = 0; i < c->argc; i += 1) { const char* a = c->argv[i]; bool q = *a == '\0' || strpbrk(a, " \t\n\v\"") != NULL; at += sprintf(at, i == 0 ? "%s" : " %s", q ? "\"" : ""); // backslashes double before a quote, which then takes one more for (;; a += 1) { u64 n = strspn(a, "\\"); a += n; n = !q ? n : *a == '\0' ? 2 * n : *a == '"' ? 2 * n + 1 : n; memset(at, '\\', n); at += n; if (*a == '\0') { break; } *at++ = *a; } at += sprintf(at, "%s", q ? "\"" : ""); } wchar_t* wide = child_wide(line); free(line); return wide; } static void child_shut(HANDLE h) { if (h != NULL) { CloseHandle(h); } } // CreateProcessW, the child inheriting in, out and err alone. static int child_spawn(ChildOpen* c, HANDLE in, HANDLE out, HANDLE err) { HANDLE theirs[3] = {in, out, err}; SetHandleInformation(in, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); SetHandleInformation(out, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); SIZE_T size = 0; InitializeProcThreadAttributeList(NULL, 1, 0, &size); LPPROC_THREAD_ATTRIBUTE_LIST list = io_mem(malloc(size)); InitializeProcThreadAttributeList(list, 1, 0, &size); UpdateProcThreadAttribute(list, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, theirs, sizeof theirs, NULL, NULL); STARTUPINFOEXW si = { { .cb = sizeof si, .dwFlags = STARTF_USESTDHANDLES, .hStdInput = in, .hStdOutput = out, .hStdError = err }, list }; PROCESS_INFORMATION pi = {0}; // CreateProcessW finds no program whose path has a "/". for (char* at = c->argv[0]; *at != '\0'; at += 1) { *at = *at == '/' ? '\\' : *at; } wchar_t* line = child_line(c); bool made = CreateProcessW(NULL, line, NULL, NULL, TRUE, EXTENDED_STARTUPINFO_PRESENT, NULL, NULL, &si.StartupInfo, &pi); DWORD why = made ? 0 : GetLastError(); DeleteProcThreadAttributeList(list); free(list); free(line); if (made) { c->pid = (u32)pi.dwProcessId; CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return 0; } return why == ERROR_FILE_NOT_FOUND || why == ERROR_PATH_NOT_FOUND ? ENOENT : why == ERROR_ACCESS_DENIED ? EACCES : EIO; } static void child_call(IoWork* w) { ChildOpen* c = (ChildOpen*)w->data; HANDLE in[2] = {NULL, NULL}; HANDLE out[2] = {NULL, NULL}; HANDLE err = NULL; bool piped = CreatePipe(&in[0], &in[1], NULL, 65536) && CreatePipe(&out[0], &out[1], NULL, 65536) && DuplicateHandle(GetCurrentProcess(), GetStdHandle(STD_ERROR_HANDLE), GetCurrentProcess(), &err, 0, TRUE, DUPLICATE_SAME_ACCESS); w->code = piped ? child_spawn(c, in[0], out[1], err) : EMFILE; child_shut(in[0]); child_shut(out[1]); child_shut(err); if (w->code != 0) { child_shut(in[1]); child_shut(out[0]); return; } c->in = _open_osfhandle((intptr_t)in[1], _O_WRONLY | _O_BINARY); c->out = _open_osfhandle((intptr_t)out[0], _O_RDONLY | _O_BINARY); } #else static int child_pipe(int fds[2]) { #ifdef __APPLE__ if (pipe(fds) != 0) { return -1; } fcntl(fds[0], F_SETFD, FD_CLOEXEC); fcntl(fds[1], F_SETFD, FD_CLOEXEC); return 0; #else return pipe2(fds, O_CLOEXEC); #endif } static void child_shut(int fd) { if (fd >= 0) { close(fd); } } // Waits for the child, so that it does not stay a zombie. static void* child_reap(void* pid) { waitpid((pid_t)(intptr_t)pid, NULL, 0); return NULL; } static int child_spawn(ChildOpen* c, int in, int out, pid_t* pid) { posix_spawn_file_actions_t actions; int code = posix_spawn_file_actions_init(&actions); if (code != 0) { return code; } code = posix_spawn_file_actions_adddup2(&actions, in, 0); code = code != 0 ? code : posix_spawn_file_actions_adddup2(&actions, out, 1); code = code != 0 ? code : posix_spawnp(pid, c->argv[0], &actions, NULL, c->argv, environ); posix_spawn_file_actions_destroy(&actions); return code; } static void child_call(IoWork* w) { ChildOpen* c = (ChildOpen*)w->data; int in[2] = {-1, -1}; int out[2] = {-1, -1}; pid_t pid = -1; bool piped = child_pipe(in) == 0 && child_pipe(out) == 0; w->code = piped ? child_spawn(c, in[0], out[1], &pid) : errno; child_shut(in[0]); child_shut(out[1]); if (w->code != 0) { child_shut(in[1]); child_shut(out[0]); return; } pthread_t reaper; if (pthread_create(&reaper, NULL, child_reap, (void*)(intptr_t)pid) == 0) { pthread_detach(reaper); } c->in = in[1]; c->out = out[0]; c->pid = (u32)pid; } #endif static Term child_pack(Env e, IoWork* w) { ChildOpen* c = (ChildOpen*)w->data; Term result = w->code != 0 ? io_fail(e, w->code, NULL) : io_done(e, io_tup(e, (Term)c->pid, io_tup(e, io_hand(c->in), io_hand(c->out)))); child_free(c); return result; } Term child_open_run(Env e, Term* f, IoWork* w) { ChildOpen* c = io_mem(calloc(1, sizeof(ChildOpen))); u64 len = 0; u32 capacity = 8; c->argv = io_mem(calloc(capacity, sizeof(char*))); c->argv[c->argc++] = io_cstr(e, f[0], &len); w->code = len == 0 || io_nul(c->argv[0], len) ? EINVAL : 0; Term xs = f[1]; while (term_aux(xs) == CID(Con)) { Term pair[2]; spare_free(e, cls_fit(2), ctr_take(e, xs, 2, pair)); char* arg = io_cstr(e, pair[0], &len); if (io_nul(arg, len)) { w->code = EINVAL; } if (c->argc + 1 >= capacity) { capacity *= 2; c->argv = io_mem(realloc(c->argv, capacity * sizeof(char*))); } c->argv[c->argc++] = arg; xs = pair[1]; } c->argv[c->argc] = NULL; w->data = (char*)c; return w->code != 0 ? child_pack(e, w) : io_work(w, child_call, child_pack); } static void __attribute__((constructor)) child_open_use(void) { io_eff(CID(open), child_open_run); }