From baa6a925c2fe297555880345224a60e3c55ee421 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sun, 31 Jul 2011 18:13:05 -0700 Subject: [PATCH] Upgrade libuv to e9bee51 --- deps/uv/config-mingw.mk | 2 +- deps/uv/include/uv-win.h | 26 +- deps/uv/include/uv.h | 6 +- deps/uv/msvs/libuv-test.vcxproj | 5 +- deps/uv/src/win/handle.c | 10 + deps/uv/src/win/internal.h | 11 + deps/uv/src/win/pipe.c | 79 +++- deps/uv/src/win/process.c | 762 +++++++++++++++++++++++++++++++- deps/uv/src/win/req.c | 8 + deps/uv/src/win/util.c | 2 +- deps/uv/test/benchmark-list.h | 3 + deps/uv/test/benchmark-sizes.c | 1 + deps/uv/test/benchmark-spawn.c | 154 +++++++ deps/uv/test/run-benchmarks.c | 9 +- deps/uv/test/run-tests.c | 13 + deps/uv/test/test-list.h | 4 + deps/uv/test/test-spawn.c | 81 +++- 17 files changed, 1158 insertions(+), 18 deletions(-) create mode 100644 deps/uv/test/benchmark-spawn.c diff --git a/deps/uv/config-mingw.mk b/deps/uv/config-mingw.mk index abd244e3ae..242bba9435 100644 --- a/deps/uv/config-mingw.mk +++ b/deps/uv/config-mingw.mk @@ -33,7 +33,7 @@ WIN_OBJS=$(WIN_SRCS:.c=.o) RUNNER_CFLAGS=$(CFLAGS) -D_GNU_SOURCE # Need _GNU_SOURCE for strdup? RUNNER_LINKFLAGS=$(LINKFLAGS) -RUNNER_LIBS=-lws2_32 +RUNNER_LIBS=-lws2_32 -lrpcrt4 -lole32 RUNNER_SRC=test/runner-win.c uv.a: $(WIN_OBJS) src/uv-common.o src/uv-eio.o src/eio/eio.o $(CARES_OBJS) diff --git a/deps/uv/include/uv-win.h b/deps/uv/include/uv-win.h index b58c11b44a..ec2429a60d 100644 --- a/deps/uv/include/uv-win.h +++ b/deps/uv/include/uv-win.h @@ -42,6 +42,14 @@ typedef struct uv_buf_t { char* base; } uv_buf_t; +#define UV_REQ_TYPE_PRIVATE \ + /* TODO: remove the req suffix */ \ + UV_ARES_EVENT_REQ, \ + UV_ARES_CLEANUP_REQ, \ + UV_GETADDRINFO_REQ, \ + UV_PROCESS_EXIT, \ + UV_PROCESS_CLOSE + #define UV_REQ_PRIVATE_FIELDS \ union { \ /* Used by I/O operations */ \ @@ -159,6 +167,20 @@ typedef struct uv_buf_t { int retcode; #define UV_PROCESS_PRIVATE_FIELDS \ - -int uv_utf16_to_utf8(wchar_t* utf16Buffer, size_t utf16Size, char* utf8Buffer, size_t utf8Size); + struct uv_process_exit_s { \ + UV_REQ_FIELDS \ + } exit_req; \ + struct uv_process_close_s { \ + UV_REQ_FIELDS \ + } close_req; \ + struct uv_process_stdio_s { \ + uv_pipe_t* server_pipe; \ + HANDLE child_pipe; \ + } stdio_pipes[3]; \ + int exit_signal; \ + HANDLE wait_handle; \ + HANDLE process_handle; \ + HANDLE close_handle; + +int uv_utf16_to_utf8(const wchar_t* utf16Buffer, size_t utf16Size, char* utf8Buffer, size_t utf8Size); int uv_utf8_to_utf16(const char* utf8Buffer, wchar_t* utf16Buffer, size_t utf16Size); diff --git a/deps/uv/include/uv.h b/deps/uv/include/uv.h index 46b03e1124..bb3e37c45d 100644 --- a/deps/uv/include/uv.h +++ b/deps/uv/include/uv.h @@ -159,10 +159,7 @@ typedef enum { UV_WRITE, UV_SHUTDOWN, UV_WAKEUP, - /* TODO: remove the req suffix */ - UV_ARES_EVENT_REQ, - UV_ARES_CLEANUP_REQ, - UV_GETADDRINFO_REQ + UV_REQ_TYPE_PRIVATE } uv_req_type; @@ -620,6 +617,7 @@ uv_counters_t* uv_counters(); /* Don't export the private CPP symbols. */ +#undef UV_REQ_TYPE_PRIVATE #undef UV_REQ_PRIVATE_FIELDS #undef UV_STREAM_PRIVATE_FIELDS #undef UV_TCP_PRIVATE_FIELDS diff --git a/deps/uv/msvs/libuv-test.vcxproj b/deps/uv/msvs/libuv-test.vcxproj index cb64f194e3..b91de56ae6 100644 --- a/deps/uv/msvs/libuv-test.vcxproj +++ b/deps/uv/msvs/libuv-test.vcxproj @@ -84,7 +84,7 @@ MachineX86 true Console - kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;ws2_32.lib;%(AdditionalDependencies) + kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;ws2_32.lib;RpcRT4.Lib;%(AdditionalDependencies) @@ -162,6 +162,7 @@ + @@ -183,4 +184,4 @@ - + \ No newline at end of file diff --git a/deps/uv/src/win/handle.c b/deps/uv/src/win/handle.c index cc6d2f88b5..f75eeb8cfa 100644 --- a/deps/uv/src/win/handle.c +++ b/deps/uv/src/win/handle.c @@ -43,6 +43,7 @@ int uv_is_active(uv_handle_t* handle) { static void uv_close_error(uv_handle_t* handle, uv_err_t e) { uv_tcp_t* tcp; uv_pipe_t* pipe; + uv_process_t* process; if (handle->flags & UV_HANDLE_CLOSING) { return; @@ -103,6 +104,11 @@ static void uv_close_error(uv_handle_t* handle, uv_err_t e) { } return; + case UV_PROCESS: + process = (uv_process_t*)handle; + uv_process_close(process); + return; + default: /* Not supported */ abort(); @@ -158,6 +164,10 @@ void uv_process_endgames() { uv_async_endgame((uv_async_t*)handle); break; + case UV_PROCESS: + uv_process_endgame((uv_process_t*)handle); + break; + default: assert(0); break; diff --git a/deps/uv/src/win/internal.h b/deps/uv/src/win/internal.h index efb05f85fe..e40ef3bea5 100644 --- a/deps/uv/src/win/internal.h +++ b/deps/uv/src/win/internal.h @@ -163,8 +163,10 @@ void uv_process_tcp_connect_req(uv_tcp_t* handle, uv_connect_t* req); * Pipes */ int uv_pipe_init_with_handle(uv_pipe_t* handle, HANDLE pipeHandle); +int uv_stdio_pipe_server(uv_pipe_t* handle, DWORD access, char* name, size_t nameSize); void close_pipe(uv_pipe_t* handle, int* status, uv_err_t* err); void uv_pipe_endgame(uv_pipe_t* handle); +int uv_unique_pipe_name(char* name, size_t size); int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb); int uv_pipe_accept(uv_pipe_t* server, uv_pipe_t* client); @@ -196,6 +198,15 @@ void uv_async_endgame(uv_async_t* handle); void uv_process_async_wakeup_req(uv_async_t* handle, uv_req_t* req); +/* + * Spawn + */ +void uv_process_proc_exit(uv_process_t* handle); +void uv_process_proc_close(uv_process_t* handle); +void uv_process_close(uv_process_t* handle); +void uv_process_endgame(uv_process_t* handle); + + /* * C-ares integration */ diff --git a/deps/uv/src/win/pipe.c b/deps/uv/src/win/pipe.c index ee627c8879..b6763249d5 100644 --- a/deps/uv/src/win/pipe.c +++ b/deps/uv/src/win/pipe.c @@ -21,6 +21,7 @@ #include #include +#include #include "uv.h" #include "../uv-common.h" @@ -31,6 +32,32 @@ static char uv_zero_[] = ""; +int uv_unique_pipe_name(char* name, size_t size) { + unsigned char* guid_str = NULL; + GUID guid; + int err; + + if (CoCreateGuid(&guid) != S_OK) { + err = -1; + goto done; + } + + if (UuidToStringA(&guid, &guid_str) != ERROR_SUCCESS) { + err = -1; + goto done; + } + + _snprintf(name, size, "\\\\.\\pipe\\uv\\%s", guid_str); + err = 0; + +done: + if (guid_str) { + RpcStringFreeA(&guid_str); + } + return err; +} + + int uv_pipe_init(uv_pipe_t* handle) { uv_stream_init((uv_stream_t*)handle); @@ -63,6 +90,53 @@ int uv_pipe_init_with_handle(uv_pipe_t* handle, HANDLE pipeHandle) { } +int uv_stdio_pipe_server(uv_pipe_t* handle, DWORD access, char* name, size_t nameSize) { + HANDLE pipeHandle; + int err; + + err = uv_unique_pipe_name(name, nameSize); + if (err) { + goto done; + } + + pipeHandle = CreateNamedPipeA(name, + access | FILE_FLAG_OVERLAPPED | FILE_FLAG_FIRST_PIPE_INSTANCE, + PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, + 1, + 65536, + 65536, + 0, + NULL); + + if (pipeHandle == INVALID_HANDLE_VALUE) { + uv_set_sys_error(GetLastError()); + err = -1; + goto done; + } + + if (CreateIoCompletionPort(pipeHandle, + LOOP->iocp, + (ULONG_PTR)handle, + 0) == NULL) { + uv_set_sys_error(GetLastError()); + err = -1; + goto done; + } + + uv_connection_init((uv_stream_t*)handle); + handle->handle = pipeHandle; + handle->flags |= UV_HANDLE_GIVEN_OS_HANDLE; + err = 0; + +done: + if (err && pipeHandle != INVALID_HANDLE_VALUE) { + CloseHandle(pipeHandle); + } + + return err; +} + + static int uv_set_pipe_handle(uv_pipe_t* handle, HANDLE pipeHandle) { DWORD mode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT; @@ -593,7 +667,7 @@ int uv_pipe_write(uv_write_t* req, uv_pipe_t* handle, uv_buf_t bufs[], int bufcn NULL, &req->overlapped); - if (!result && GetLastError() != WSA_IO_PENDING) { + if (!result && GetLastError() != ERROR_IO_PENDING) { uv_set_sys_error(GetLastError()); return -1; } @@ -648,6 +722,8 @@ void uv_process_pipe_read_req(uv_pipe_t* handle, uv_req_t* req) { break; } + /* TODO: do we need to check avail > 0? */ + buf = handle->alloc_cb((uv_stream_t*)handle, avail); assert(buf.len > 0); @@ -718,7 +794,6 @@ void uv_process_pipe_accept_req(uv_pipe_t* handle, uv_req_t* raw_req) { if (req->error.code == UV_OK) { assert(req->pipeHandle != INVALID_HANDLE_VALUE); - req->next_pending = handle->pending_accepts; handle->pending_accepts = req; diff --git a/deps/uv/src/win/process.c b/deps/uv/src/win/process.c index 7e978565f3..81bb430e0a 100644 --- a/deps/uv/src/win/process.c +++ b/deps/uv/src/win/process.c @@ -19,18 +19,772 @@ * IN THE SOFTWARE. */ -#include - #include "uv.h" #include "../uv-common.h" #include "internal.h" +#include +#include +#include +#include + +#define UTF8_TO_UTF16(s, t) \ + size = uv_utf8_to_utf16(s, NULL, 0) * sizeof(wchar_t); \ + t = (wchar_t*)malloc(size); \ + if (!t) { \ + uv_fatal_error(ERROR_OUTOFMEMORY, "malloc"); \ + } \ + if (!uv_utf8_to_utf16(s, t, size / sizeof(wchar_t))) { \ + uv_set_sys_error(GetLastError()); \ + err = -1; \ + goto done; \ + } + + +static const wchar_t DEFAULT_PATH[1] = L""; +static const wchar_t DEFAULT_PATH_EXT[20] = L".COM;.EXE;.BAT;.CMD"; + + +static void uv_process_init(uv_process_t* handle) { + handle->type = UV_PROCESS; + handle->flags = 0; + handle->error = uv_ok_; + handle->exit_cb = NULL; + handle->pid = 0; + handle->exit_signal = 0; + handle->wait_handle = INVALID_HANDLE_VALUE; + handle->process_handle = INVALID_HANDLE_VALUE; + handle->close_handle = INVALID_HANDLE_VALUE; + handle->stdio_pipes[0].server_pipe = NULL; + handle->stdio_pipes[0].child_pipe = INVALID_HANDLE_VALUE; + handle->stdio_pipes[1].server_pipe = NULL; + handle->stdio_pipes[1].child_pipe = INVALID_HANDLE_VALUE; + handle->stdio_pipes[2].server_pipe = NULL; + handle->stdio_pipes[2].child_pipe = INVALID_HANDLE_VALUE; + + uv_req_init((uv_req_t*)&handle->exit_req); + handle->exit_req.type = UV_PROCESS_EXIT; + handle->exit_req.data = handle; + uv_req_init((uv_req_t*)&handle->close_req); + handle->close_req.type = UV_PROCESS_CLOSE; + handle->close_req.data = handle; + + uv_counters()->handle_init++; + uv_counters()->process_init++; + + uv_ref(); +} + + +/* + * Path search functions + */ + +/* + * Helper function for search_path + */ +static wchar_t* search_path_join_test(const wchar_t* dir, + int dir_len, + const wchar_t* name, + int name_len, + const wchar_t* ext, + int ext_len, + const wchar_t* cwd, + int cwd_len) { + wchar_t *result, *result_pos; + DWORD attrs; + + if (dir_len >= 1 && (dir[0] == L'/' || dir[0] == L'\\')) { + /* It's a full path without drive letter, use cwd's drive letter only */ + cwd_len = 2; + } else if (dir_len >= 2 && dir[1] == L':' && + (dir_len < 3 || (dir[2] != L'/' && dir[2] != L'\\'))) { + /* It's a relative path with drive letter (ext.g. D:../some/file) + * Replace drive letter in dir by full cwd if it points to the same drive, + * otherwise use the dir only. + */ + if (cwd_len < 2 || _wcsnicmp(cwd, dir, 2) != 0) { + cwd_len = 0; + } else { + dir += 2; + dir_len -= 2; + } + } else if (dir_len > 2 && dir[1] == L':') { + /* It's an absolute path with drive letter + * Don't use the cwd at all + */ + cwd_len = 0; + } + + /* Allocate buffer for output */ + result = result_pos = + (wchar_t*)malloc(sizeof(wchar_t) * (cwd_len + 1 + dir_len + 1 + name_len + 1 + ext_len + 1)); + + /* Copy cwd */ + wcsncpy(result_pos, cwd, cwd_len); + result_pos += cwd_len; + + /* Add a path separator if cwd didn't end with one */ + if (cwd_len && wcsrchr(L"\\/:", result_pos[-1]) == NULL) { + result_pos[0] = L'\\'; + result_pos++; + } + + /* Copy dir */ + wcsncpy(result_pos, dir, dir_len); + result_pos += dir_len; + + /* Add a separator if the dir didn't end with one */ + if (dir_len && wcsrchr(L"\\/:", result_pos[-1]) == NULL) { + result_pos[0] = L'\\'; + result_pos++; + } + + /* Copy filename */ + wcsncpy(result_pos, name, name_len); + result_pos += name_len; + + /* Copy extension */ + if (ext_len) { + result_pos[0] = L'.'; + result_pos++; + wcsncpy(result_pos, ext, ext_len); + result_pos += ext_len; + } + + /* Null terminator */ + result_pos[0] = L'\0'; + + attrs = GetFileAttributesW(result); + + if (attrs != INVALID_FILE_ATTRIBUTES && + !(attrs & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT))) { + return result; + } + + free(result); + return NULL; +} + + +/* + * Helper function for search_path + */ +static wchar_t* path_search_walk_ext(const wchar_t *dir, + int dir_len, + const wchar_t *name, + int name_len, + wchar_t *cwd, + int cwd_len, + const wchar_t *path_ext, + int name_has_ext) { + wchar_t* result = NULL; + + const wchar_t *ext_start, + *ext_end = path_ext; + + /* If the name itself has a nonemtpy extension, try this extension first */ + if (name_has_ext) { + result = search_path_join_test(dir, dir_len, + name, name_len, + L"", 0, + cwd, cwd_len); + } + + /* Add path_ext extensions and try to find a name that matches */ + while (result == NULL) { + if (*ext_end == L'\0') { + break; + } + + /* Skip the separator that ext_end now points to */ + if (ext_end != path_ext) { + ext_end++; + } + + /* Find the next dot in path_ext */ + ext_start = wcschr(ext_end, L'.'); + if (ext_start == NULL) { + break; + } + + /* Skip the dot */ + ext_start++; + + /* Slice until we found a ; or alternatively a \0 */ + ext_end = wcschr(ext_start, L';'); + if (ext_end == NULL) { + ext_end = wcschr(ext_start, '\0'); + } + + result = search_path_join_test(dir, dir_len, + name, name_len, + ext_start, (ext_end - ext_start), + cwd, cwd_len); + } + + return result; +} + + +/* + * search_path searches the system path for an executable filename - + * the windows API doesn't provide this as a standalone function nor as an + * option to CreateProcess. + * + * It tries to return an absolute filename. + * + * Furthermore, it tries to follow the semantics that cmd.exe uses as closely + * as possible: + * + * - Do not search the path if the filename already contains a path (either + * relative or absolute). + * (but do use path_ext) + * + * - If there's really only a filename, check the current directory for file, + * then search all path directories. + * + * - If filename specifies has *any* extension, search for the file with the + * specified extension first. + * (not necessary an executable one or one that appears in path_ext; + * *but* no extension or just a dot is *not* allowed) + * + * - If the literal filename is not found in a directory, try *appending* + * (not replacing) extensions from path_ext in the specified order. + * (an extension consisting of just a dot *may* appear in path_ext; + * unlike what happens if the specified filename ends with a dot, + * if path_ext specifies a single dot cmd.exe *does* look for an + * extension-less file) + * + * - The path variable may contain relative paths; relative paths are relative + * to the cwd. + * + * - Directories in path may or may not end with a trailing backslash. + * + * - Extensions path_ext portions must always start with a dot. + * + * - CMD does not trim leading/trailing whitespace from path/pathex entries + * nor from the environment variables as a whole. + * + * - When cmd.exe cannot read a directory, it wil just skip it and go on + * searching. However, unlike posix-y systems, it will happily try to run a + * file that is not readable/executable; if the spawn fails it will not + * continue searching. + * + * TODO: correctly interpret UNC paths + * TODO: check with cmd what should happen when a pathext entry does not start + * with a dot + */ +static wchar_t* search_path(const wchar_t *file, + wchar_t *cwd, + const wchar_t *path, + const wchar_t *path_ext) { + int file_has_dir; + wchar_t* result = NULL; + wchar_t *file_name_start; + wchar_t *dot; + int name_has_ext; + + int file_len = wcslen(file); + int cwd_len = wcslen(cwd); + + /* If the caller supplies an empty filename, + * we're not gonna return c:\windows\.exe -- GFY! + */ + if (file_len == 0 + || (file_len == 1 && file[0] == L'.')) { + return NULL; + } + + /* Find the start of the filename so we can split the directory from the name */ + for (file_name_start = (wchar_t*)file + file_len; + file_name_start > file + && file_name_start[-1] != L'\\' + && file_name_start[-1] != L'/' + && file_name_start[-1] != L':'; + file_name_start--); + + file_has_dir = file_name_start != file; + + /* Check if the filename includes an extension */ + dot = wcschr(file_name_start, L'.'); + name_has_ext = (dot != NULL && dot[1] != L'\0'); + + if (file_has_dir) { + /* The file has a path inside, don't use path (but do use path_ex) */ + result = path_search_walk_ext( + file, file_name_start - file, + file_name_start, file_len - (file_name_start - file), + cwd, cwd_len, + path_ext, name_has_ext); + + } else { + const wchar_t *dir_start, + *dir_end = path; + + /* The file is really only a name; look in cwd first, then scan path */ + result = path_search_walk_ext(L"", 0, + file, file_len, + cwd, cwd_len, + path_ext, name_has_ext); + + while (result == NULL) { + if (*dir_end == L'\0') { + break; + } + + /* Skip the separator that dir_end now points to */ + if (dir_end != path) { + dir_end++; + } + + /* Next slice starts just after where the previous one ended */ + dir_start = dir_end; + + /* Slice until the next ; or \0 is found */ + dir_end = wcschr(dir_start, L';'); + if (dir_end == NULL) { + dir_end = wcschr(dir_start, L'\0'); + } + + /* If the slice is zero-length, don't bother */ + if (dir_end - dir_start == 0) { + continue; + } + + result = path_search_walk_ext(dir_start, dir_end - dir_start, + file, file_len, + cwd, cwd_len, + path_ext, name_has_ext); + } + } + + return result; +} + + +static wchar_t* make_program_args(char** args) { + wchar_t* dst; + wchar_t* ptr; + char** arg; + size_t size = 0; + size_t len; + int arg_count = 0; + + /* Count the required size. */ + for (arg = args; *arg; arg++) { + size += (uv_utf8_to_utf16(*arg, NULL, 0) * sizeof(wchar_t)); + arg_count++; + } + + /* Arguments are separated with a space. */ + if (arg_count > 0) { + size += arg_count - 1; + } + + dst = (wchar_t*)malloc(size); + if (!dst) { + uv_fatal_error(ERROR_OUTOFMEMORY, "malloc"); + } + + ptr = dst; + for (arg = args; *arg; arg++, ptr += len) { + len = uv_utf8_to_utf16(*arg, ptr, (size_t)(size - (ptr - dst))); + if (!len) { + free(dst); + return NULL; + } + + if (*(arg + 1)) { + /* Replace with a space if there are more args. */ + *((ptr + len) - 1) = L' '; + } + } + + return dst; +} + +/* + * The way windows takes environment variables is different than what C does; + * Windows wants a contiguous block of null-terminated strings, terminated + * with an additional null. + * Get a pointer to the pathext and path environment variables as well, + * because search_path needs it. These are just pointers into env_win. + */ +wchar_t* make_program_env(char** env_block, const wchar_t **path, const wchar_t **path_ext) { + wchar_t* dst; + wchar_t* ptr; + char** env; + int env_len = 1 * sizeof(wchar_t); /* room for closing null */ + int len; + + for (env = env_block; *env; env++) { + env_len += (uv_utf8_to_utf16(*env, NULL, 0) * sizeof(wchar_t)); + } + + dst = (wchar_t*)malloc(env_len); + if (!dst) { + uv_fatal_error(ERROR_OUTOFMEMORY, "malloc"); + } + + ptr = dst; + + for (env = env_block; *env; env++, ptr += len) { + len = uv_utf8_to_utf16(*env, ptr, (size_t)(env_len - (ptr - dst))); + if (!len) { + free(dst); + return NULL; + } + + /* Try to get a pointer to PATH and PATHEXT */ + if (_wcsnicmp(L"PATH=", ptr, 5) == 0) { + *path = ptr + 5; + } + if (_wcsnicmp(L"PATHEXT=", ptr, 8) == 0) { + *path_ext = ptr + 8; + } + } + + *ptr = L'\0'; + return dst; +} + + +/* + * Called on Windows thread-pool thread to indicate that + * a child process has exited. + */ +static void CALLBACK exit_wait_callback(void* data, BOOLEAN didTimeout) { + uv_process_t* process = (uv_process_t*)data; + + assert(didTimeout == FALSE); + assert(process); + + memset(&process->exit_req.overlapped, 0, sizeof(process->exit_req.overlapped)); + + /* Post completed */ + if (!PostQueuedCompletionStatus(LOOP->iocp, + 0, + 0, + &process->exit_req.overlapped)) { + uv_fatal_error(GetLastError(), "PostQueuedCompletionStatus"); + } +} + + +/* + * Called on Windows thread-pool thread to indicate that + * UnregisterWaitEx has completed. + */ +static void CALLBACK close_wait_callback(void* data, BOOLEAN didTimeout) { + uv_process_t* process = (uv_process_t*)data; + + assert(didTimeout == FALSE); + assert(process); + + memset(&process->close_req.overlapped, 0, sizeof(process->close_req.overlapped)); + + /* Post completed */ + if (!PostQueuedCompletionStatus(LOOP->iocp, + 0, + 0, + &process->close_req.overlapped)) { + uv_fatal_error(GetLastError(), "PostQueuedCompletionStatus"); + } +} + + +/* Called on main thread after a child process has exited. */ +void uv_process_proc_exit(uv_process_t* handle) { + int i; + DWORD exit_code; + + /* Close stdio handles. */ + for (i = 0; i < COUNTOF(handle->stdio_pipes); i++) { + if (handle->stdio_pipes[i].child_pipe != INVALID_HANDLE_VALUE) { + CloseHandle(handle->stdio_pipes[i].child_pipe); + handle->stdio_pipes[i].child_pipe = INVALID_HANDLE_VALUE; + } + } + + /* Unregister from process notification. */ + if (handle->wait_handle != INVALID_HANDLE_VALUE) { + UnregisterWait(handle->wait_handle); + handle->wait_handle = INVALID_HANDLE_VALUE; + } + + /* Clean-up the process handle. */ + if (handle->process_handle != INVALID_HANDLE_VALUE) { + /* Get the exit code. */ + if (!GetExitCodeProcess(handle->process_handle, &exit_code)) { + exit_code = 1; + } + + CloseHandle(handle->process_handle); + handle->process_handle = INVALID_HANDLE_VALUE; + } + + /* Fire the exit callback. */ + if (handle->exit_cb) { + handle->exit_cb(handle, exit_code, handle->exit_signal); + } +} + + +/* Called on main thread after UnregisterWaitEx finishes. */ +void uv_process_proc_close(uv_process_t* handle) { + uv_want_endgame((uv_handle_t*)handle); +} + + +void uv_process_endgame(uv_process_t* handle) { + if (handle->flags & UV_HANDLE_CLOSING) { + assert(!(handle->flags & UV_HANDLE_CLOSED)); + handle->flags |= UV_HANDLE_CLOSED; + + if (handle->close_cb) { + handle->close_cb((uv_handle_t*)handle); + } + + uv_unref(); + } +} + + +void uv_process_close(uv_process_t* handle) { + if (handle->wait_handle != INVALID_HANDLE_VALUE) { + handle->close_handle = CreateEvent(NULL, FALSE, FALSE, NULL); + UnregisterWaitEx(handle->wait_handle, handle->close_handle); + handle->wait_handle = NULL; + + RegisterWaitForSingleObject(&handle->wait_handle, handle->close_handle, + close_wait_callback, (void*)handle, INFINITE, + WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE); + } else { + uv_want_endgame((uv_handle_t*)handle); + } +} + + +static int uv_create_stdio_pipe_pair(uv_pipe_t* server_pipe, HANDLE* child_pipe, DWORD server_access, DWORD child_access) { + int err; + SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE }; + char pipe_name[64]; + DWORD mode = PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT; + + if (server_pipe->type != UV_NAMED_PIPE) { + uv_set_error(UV_EINVAL, 0); + err = -1; + goto done; + } + + /* Create server pipe handle. */ + err = uv_stdio_pipe_server(server_pipe, server_access, pipe_name, sizeof(pipe_name)); + if (err) { + goto done; + } + + /* Create child pipe handle. */ + *child_pipe = CreateFileA(pipe_name, + child_access, + 0, + &sa, + OPEN_EXISTING, + 0, + NULL); + + if (*child_pipe == INVALID_HANDLE_VALUE) { + uv_set_sys_error(GetLastError()); + err = -1; + goto done; + } + + if (!SetNamedPipeHandleState(*child_pipe, &mode, NULL, NULL)) { + uv_set_sys_error(GetLastError()); + err = -1; + goto done; + } + + /* Do a blocking ConnectNamedPipe. This should not block because + * we have both ends of the pipe created. + */ + if (!ConnectNamedPipe(server_pipe->handle, NULL)) { + if (GetLastError() != ERROR_PIPE_CONNECTED) { + uv_set_sys_error(GetLastError()); + err = -1; + goto done; + } + } + + err = 0; + +done: + if (err) { + if (server_pipe->handle != INVALID_HANDLE_VALUE) { + close_pipe(server_pipe, NULL, NULL); + } + + if (*child_pipe != INVALID_HANDLE_VALUE) { + CloseHandle(*child_pipe); + *child_pipe = INVALID_HANDLE_VALUE; + } + } + + return err; +} + int uv_spawn(uv_process_t* process, uv_process_options_t options) { - assert(0 && "implement me"); + int err, i; + const wchar_t* path = NULL; + const wchar_t* path_ext = NULL; + int size; + wchar_t* application_path, *application, *arguments, *env, *cwd; + STARTUPINFOW startup; + PROCESS_INFORMATION info; + + uv_process_init(process); + + process->exit_cb = options.exit_cb; + UTF8_TO_UTF16(options.file, application); + arguments = options.args ? make_program_args(options.args) : NULL; + env = options.env ? make_program_env(options.env, &path, &path_ext) : NULL; + + if (options.cwd) { + UTF8_TO_UTF16(options.cwd, cwd); + } else { + size = GetCurrentDirectoryW(0, NULL) * sizeof(wchar_t); + if (size) { + cwd = (wchar_t*)malloc(size); + if (!cwd) { + uv_fatal_error(ERROR_OUTOFMEMORY, "malloc"); + } + GetCurrentDirectoryW(size, cwd); + } else { + uv_set_sys_error(GetLastError()); + err = -1; + goto done; + } + } + + application_path = search_path(application, + cwd, + path ? path : DEFAULT_PATH, + path_ext ? path_ext : DEFAULT_PATH_EXT); + + if (!application_path) { + uv_set_error(UV_EINVAL, 0); + err = -1; + goto done; + } + + /* Create stdio pipes. */ + if (options.stdin_stream) { + err = uv_create_stdio_pipe_pair(options.stdin_stream, &process->stdio_pipes[0].child_pipe, PIPE_ACCESS_OUTBOUND, GENERIC_READ | FILE_WRITE_ATTRIBUTES); + if (err) { + goto done; + } + + process->stdio_pipes[0].server_pipe = options.stdin_stream; + } + + if (options.stdout_stream) { + err = uv_create_stdio_pipe_pair(options.stdout_stream, &process->stdio_pipes[1].child_pipe, PIPE_ACCESS_INBOUND, GENERIC_WRITE); + if (err) { + goto done; + } + + process->stdio_pipes[1].server_pipe = options.stdout_stream; + } + + if (options.stderr_stream) { + err = uv_create_stdio_pipe_pair(options.stderr_stream, &process->stdio_pipes[2].child_pipe, PIPE_ACCESS_INBOUND, GENERIC_WRITE); + if (err) { + goto done; + } + + process->stdio_pipes[2].server_pipe = options.stderr_stream; + } + + startup.cb = sizeof(startup); + startup.lpReserved = NULL; + startup.lpDesktop = NULL; + startup.lpTitle = NULL; + startup.dwFlags = STARTF_USESTDHANDLES; + startup.cbReserved2 = 0; + startup.lpReserved2 = NULL; + startup.hStdInput = process->stdio_pipes[0].child_pipe; + startup.hStdOutput = process->stdio_pipes[1].child_pipe; + startup.hStdError = process->stdio_pipes[2].child_pipe; + + if (!CreateProcessW(application_path, + arguments, + NULL, + NULL, + 1, + CREATE_UNICODE_ENVIRONMENT, + env, + cwd, + &startup, + &info)) { + uv_set_sys_error(GetLastError()); + err = -1; + goto done; + } + + process->process_handle = info.hProcess; + process->pid = info.dwProcessId; + + /* Setup notifications for when the child process exits. */ + if (!RegisterWaitForSingleObject(&process->wait_handle, process->process_handle, + exit_wait_callback, (void*)process, INFINITE, + WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE)) { + uv_set_sys_error(GetLastError()); + err = -1; + goto done; + } + + CloseHandle(info.hThread); + err = 0; + +done: + free(application_path); + free(application); + free(arguments); + free(cwd); + free(env); + + if (err) { + for (i = 0; i < COUNTOF(process->stdio_pipes); i++) { + if (process->stdio_pipes[i].child_pipe != INVALID_HANDLE_VALUE) { + CloseHandle(process->stdio_pipes[i].child_pipe); + process->stdio_pipes[i].child_pipe = INVALID_HANDLE_VALUE; + } + } + + if (process->wait_handle != INVALID_HANDLE_VALUE) { + UnregisterWait(process->wait_handle); + process->wait_handle = INVALID_HANDLE_VALUE; + } + + if (process->process_handle != INVALID_HANDLE_VALUE) { + CloseHandle(process->process_handle); + process->process_handle = INVALID_HANDLE_VALUE; + } + } + + return err; } int uv_process_kill(uv_process_t* process, int signum) { - assert(0 && "implement me"); + process->exit_signal = signum; + + /* On windows killed processes normally return 1 */ + if (process->process_handle != INVALID_HANDLE_VALUE && + TerminateProcess(process->process_handle, 1)) { + return 0; + } + + return -1; } diff --git a/deps/uv/src/win/req.c b/deps/uv/src/win/req.c index 627d7ec058..630818865b 100644 --- a/deps/uv/src/win/req.c +++ b/deps/uv/src/win/req.c @@ -126,6 +126,14 @@ void uv_process_reqs() { uv_process_getaddrinfo_req((uv_getaddrinfo_t*) req->data, req); break; + case UV_PROCESS_EXIT: + uv_process_proc_exit((uv_process_t*) req->data); + break; + + case UV_PROCESS_CLOSE: + uv_process_proc_close((uv_process_t*) req->data); + break; + default: assert(0); } diff --git a/deps/uv/src/win/util.c b/deps/uv/src/win/util.c index 3299e7a11d..65679b101c 100644 --- a/deps/uv/src/win/util.c +++ b/deps/uv/src/win/util.c @@ -27,7 +27,7 @@ #include "internal.h" -int uv_utf16_to_utf8(wchar_t* utf16Buffer, size_t utf16Size, char* utf8Buffer, size_t utf8Size) { +int uv_utf16_to_utf8(const wchar_t* utf16Buffer, size_t utf16Size, char* utf8Buffer, size_t utf8Size) { return WideCharToMultiByte(CP_UTF8, 0, utf16Buffer, utf16Size, utf8Buffer, utf8Size, NULL, NULL); } diff --git a/deps/uv/test/benchmark-list.h b/deps/uv/test/benchmark-list.h index 5e79878021..bc654b5afe 100644 --- a/deps/uv/test/benchmark-list.h +++ b/deps/uv/test/benchmark-list.h @@ -27,6 +27,7 @@ BENCHMARK_DECLARE (pipe_pump100_client) BENCHMARK_DECLARE (pipe_pump1_client) BENCHMARK_DECLARE (gethostbyname) BENCHMARK_DECLARE (getaddrinfo) +BENCHMARK_DECLARE (spawn) HELPER_DECLARE (tcp_pump_server) HELPER_DECLARE (pipe_pump_server) HELPER_DECLARE (tcp4_echo_server) @@ -55,4 +56,6 @@ TASK_LIST_START BENCHMARK_HELPER (gethostbyname, dns_server) BENCHMARK_ENTRY (getaddrinfo) + + BENCHMARK_ENTRY (spawn) TASK_LIST_END diff --git a/deps/uv/test/benchmark-sizes.c b/deps/uv/test/benchmark-sizes.c index ab60e96fcc..3967f301f9 100644 --- a/deps/uv/test/benchmark-sizes.c +++ b/deps/uv/test/benchmark-sizes.c @@ -34,5 +34,6 @@ BENCHMARK_IMPL(sizes) { LOGF("uv_idle_t: %u bytes\n", (unsigned int) sizeof(uv_idle_t)); LOGF("uv_async_t: %u bytes\n", (unsigned int) sizeof(uv_async_t)); LOGF("uv_timer_t: %u bytes\n", (unsigned int) sizeof(uv_timer_t)); + LOGF("uv_process_t: %u bytes\n", (unsigned int) sizeof(uv_process_t)); return 0; } diff --git a/deps/uv/test/benchmark-spawn.c b/deps/uv/test/benchmark-spawn.c new file mode 100644 index 0000000000..21e43a77ef --- /dev/null +++ b/deps/uv/test/benchmark-spawn.c @@ -0,0 +1,154 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +/* This benchmark spawns itself 1000 times. */ + +#include "task.h" +#include "uv.h" + +static int N = 1000; +static int done; + +static uv_process_t process; +static uv_process_options_t options = { 0 }; +static char exepath[1024]; +static size_t exepath_size = 1024; +static char* args[3]; +static uv_pipe_t out; + +#define OUTPUT_SIZE 1024 +static char output[OUTPUT_SIZE]; +static int output_used; + +static int process_open; +static int pipe_open; + + +static void spawn(); + + +void maybe_spawn() { + if (process_open == 0 && pipe_open == 0) { + done++; + if (done < N) { + spawn(); + } + } +} + + +static void process_close_cb(uv_handle_t* handle) { + ASSERT(process_open == 1); + process_open = 0; + maybe_spawn(); +} + + +static void exit_cb(uv_process_t* process, int exit_status, int term_signal) { + ASSERT(exit_status == 42); + ASSERT(term_signal == 0); + uv_close((uv_handle_t*)process, process_close_cb); +} + + +uv_buf_t on_alloc(uv_stream_t* tcp, size_t suggested_size) { + uv_buf_t buf; + buf.base = output + output_used; + buf.len = OUTPUT_SIZE - output_used; + return buf; +} + + +void pipe_close_cb(uv_handle_t* pipe) { + ASSERT(pipe_open == 1); + pipe_open = 0; + maybe_spawn(); +} + + +void on_read(uv_stream_t* pipe, ssize_t nread, uv_buf_t buf) { + uv_err_t err = uv_last_error(); + + if (nread > 0) { + ASSERT(pipe_open == 1); + output_used += nread; + } else if (nread < 0) { + if (err.code == UV_EOF) { + uv_close((uv_handle_t*)pipe, pipe_close_cb); + } + } +} + + +static void spawn() { + int r; + + ASSERT(process_open == 0); + ASSERT(pipe_open == 0); + + args[0] = exepath; + args[1] = "spawn_helper"; + args[2] = NULL; + options.file = exepath; + options.args = args; + options.exit_cb = exit_cb; + + uv_pipe_init(&out); + options.stdout_stream = &out; + + r = uv_spawn(&process, options); + ASSERT(r == 0); + + process_open = 1; + pipe_open = 1; + output_used = 0; + + r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read); + ASSERT(r == 0); +} + + +BENCHMARK_IMPL(spawn) { + int r; + static int64_t start_time, end_time; + + uv_init(); + + r = uv_exepath(exepath, &exepath_size); + ASSERT(r == 0); + exepath[exepath_size] = '\0'; + + uv_update_time(); + start_time = uv_now(); + + spawn(); + + r = uv_run(); + ASSERT(r == 0); + + uv_update_time(); + end_time = uv_now(); + + LOGF("spawn: %.0f spawns/s\n", + (double) N / (double) (end_time - start_time) * 1000.0); + + return 0; +} diff --git a/deps/uv/test/run-benchmarks.c b/deps/uv/test/run-benchmarks.c index bdef4a4be4..d04c78d5e7 100644 --- a/deps/uv/test/run-benchmarks.c +++ b/deps/uv/test/run-benchmarks.c @@ -38,7 +38,14 @@ int main(int argc, char **argv) { switch (argc) { case 1: return run_tests(BENCHMARK_TIMEOUT, 1); - case 2: return run_test(argv[1], BENCHMARK_TIMEOUT, 1); + case 2: { + if (strcmp(argv[1], "spawn_helper") == 0) { + printf("hello world\n"); + return 42; + } + + return run_test(argv[1], BENCHMARK_TIMEOUT, 1); + } case 3: return run_test_part(argv[1], argv[2]); default: LOGF("Too many arguments.\n"); diff --git a/deps/uv/test/run-tests.c b/deps/uv/test/run-tests.c index c4a70dc93e..a22f62066b 100644 --- a/deps/uv/test/run-tests.c +++ b/deps/uv/test/run-tests.c @@ -35,6 +35,7 @@ int main(int argc, char **argv) { int i; + char buffer[32]; platform_init(argc, argv); @@ -50,6 +51,18 @@ int main(int argc, char **argv) { return 1; } + if (strcmp(argv[1], "spawn_helper3") == 0) { + gets(buffer); + printf(buffer); + return 1; + } + + if (strcmp(argv[1], "spawn_helper4") == 0) { + // sleep + uv_sleep(10000); + return 100; + } + return run_test(argv[1], TEST_TIMEOUT, 0); } case 3: return run_test_part(argv[1], argv[2]); diff --git a/deps/uv/test/test-list.h b/deps/uv/test/test-list.h index 99d59e556f..c4a4e43634 100644 --- a/deps/uv/test/test-list.h +++ b/deps/uv/test/test-list.h @@ -65,6 +65,8 @@ TEST_DECLARE (fail_always) TEST_DECLARE (pass_always) TEST_DECLARE (spawn_exit_code) TEST_DECLARE (spawn_stdout) +TEST_DECLARE (spawn_stdin) +TEST_DECLARE (spawn_and_kill) HELPER_DECLARE (tcp4_echo_server) HELPER_DECLARE (tcp6_echo_server) HELPER_DECLARE (pipe_echo_server) @@ -144,6 +146,8 @@ TASK_LIST_START TEST_ENTRY (spawn_exit_code) TEST_ENTRY (spawn_stdout) + TEST_ENTRY (spawn_stdin) + TEST_ENTRY (spawn_and_kill) #if 0 /* These are for testing the test runner. */ diff --git a/deps/uv/test/test-spawn.c b/deps/uv/test/test-spawn.c index 1ad274ef78..813bf2e92d 100644 --- a/deps/uv/test/test-spawn.c +++ b/deps/uv/test/test-spawn.c @@ -27,6 +27,7 @@ static int close_cb_called; static int exit_cb_called; static uv_process_t process; +static uv_timer_t timer; static uv_process_options_t options = { 0 }; static char exepath[1024]; static size_t exepath_size = 1024; @@ -73,6 +74,12 @@ void on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) { } +void write_cb(uv_write_t* req, int status) { + ASSERT(status == 0); + uv_close((uv_handle_t*)req->handle, close_cb); +} + + static void init_process_options(char* test) { /* Note spawn_helper1 defined in test/run-tests.c */ int r = uv_exepath(exepath, &exepath_size); @@ -87,6 +94,12 @@ static void init_process_options(char* test) { } +static void timer_cb(uv_timer_t* handle, int status) { + uv_process_kill(&process, 0); + uv_close((uv_handle_t*)handle, close_cb); +} + + TEST_IMPL(spawn_exit_code) { int r; @@ -129,7 +142,73 @@ TEST_IMPL(spawn_stdout) { ASSERT(exit_cb_called == 1); ASSERT(close_cb_called == 2); /* Once for process once for the pipe. */ - ASSERT(strcmp("hello world\n", output) == 0); + printf("output is: %s", output); + ASSERT(strcmp("hello world\n", output) == 0 || strcmp("hello world\r\n", output) == 0); + + return 0; +} + + +TEST_IMPL(spawn_stdin) { +int r; + uv_pipe_t out; + uv_pipe_t in; + uv_write_t write_req; + uv_buf_t buf; + char buffer[] = "hello-from-spawn_stdin"; + + uv_init(); + + init_process_options("spawn_helper3"); + + uv_pipe_init(&out); + uv_pipe_init(&in); + options.stdout_stream = &out; + options.stdin_stream = ∈ + + r = uv_spawn(&process, options); + ASSERT(r == 0); + + buf.base = buffer; + buf.len = sizeof(buffer); + r = uv_write(&write_req, (uv_stream_t*)&in, &buf, 1, write_cb); + ASSERT(r == 0); + + r = uv_read_start((uv_stream_t*) &out, on_alloc, on_read); + ASSERT(r == 0); + + r = uv_run(); + ASSERT(r == 0); + + ASSERT(exit_cb_called == 1); + ASSERT(close_cb_called == 3); /* Once for process twice for the pipe. */ + ASSERT(strcmp(buffer, output) == 0); + + return 0; +} + + +TEST_IMPL(spawn_and_kill) { + int r; + + uv_init(); + + init_process_options("spawn_helper4"); + + r = uv_spawn(&process, options); + ASSERT(r == 0); + + r = uv_timer_init(&timer); + ASSERT(r == 0); + + r = uv_timer_start(&timer, timer_cb, 1000, 0); + ASSERT(r == 0); + + r = uv_run(); + ASSERT(r == 0); + + ASSERT(exit_cb_called == 1); + ASSERT(close_cb_called == 2); /* Once for process and once for timer. */ return 0; }