#include <netinet/in.h>
#include <arpa/inet.h>
#include <limits.h> /* PATH_MAX */
+#include <sys/uio.h> /* writev */
#ifdef __sun
# include <sys/types.h>
void uv__req_init(uv_req_t*);
void uv__next(EV_P_ ev_idle* watcher, int revents);
-static int uv__stream_open(uv_stream_t*, int fd);
+static int uv__stream_open(uv_stream_t*, int fd, int flags);
static void uv__finish_close(uv_handle_t* handle);
static uv_err_t uv_err_new(uv_handle_t* handle, int sys_error);
UV_CLOSED = 0x00000002, /* close(2) finished. */
UV_READING = 0x00000004, /* uv_read_start() called. */
UV_SHUTTING = 0x00000008, /* uv_shutdown() called but not complete. */
- UV_SHUT = 0x00000010 /* Write side closed. */
+ UV_SHUT = 0x00000010, /* Write side closed. */
+ UV_READABLE = 0x00000020, /* The stream is readable */
+ UV_WRITABLE = 0x00000040 /* The stream is writable */
};
goto out;
}
- if (uv__stream_open((uv_stream_t*)tcp, fd)) {
+ if (uv__stream_open((uv_stream_t*)tcp, fd, UV_READABLE | UV_WRITABLE)) {
status = -2;
uv__close(fd);
goto out;
}
-static int uv__stream_open(uv_stream_t* stream, int fd) {
+static int uv__stream_open(uv_stream_t* stream, int fd, int flags) {
socklen_t yes;
assert(fd >= 0);
stream->fd = fd;
+ uv_flag_set((uv_handle_t*)stream, flags);
+
/* Reuse the port address if applicable. */
yes = 1;
if (stream->type == UV_TCP
goto out;
}
- if (uv__stream_open(streamClient, streamServer->accepted_fd)) {
+ if (uv__stream_open(streamClient, streamServer->accepted_fd,
+ UV_READABLE | UV_WRITABLE)) {
/* TODO handle error */
streamServer->accepted_fd = -1;
uv__close(streamServer->accepted_fd);
return -1;
}
- if (uv__stream_open((uv_stream_t*)tcp, fd)) {
+ if (uv__stream_open((uv_stream_t*)tcp, fd, UV_READABLE)) {
uv__close(fd);
return -1;
}
}
-int uv_shutdown(uv_shutdown_t* req, uv_stream_t* handle, uv_shutdown_cb cb) {
- uv_tcp_t* tcp = (uv_tcp_t*)handle;
- assert((handle->type == UV_TCP || handle->type == UV_NAMED_PIPE)
- && "uv_shutdown (unix) only supports uv_tcp_t right now");
- assert(tcp->fd >= 0);
+int uv_shutdown(uv_shutdown_t* req, uv_stream_t* stream, uv_shutdown_cb cb) {
+ assert((stream->type == UV_TCP || stream->type == UV_NAMED_PIPE) &&
+ "uv_shutdown (unix) only supports uv_handle_t right now");
+ assert(stream->fd >= 0);
+
+ if (!uv_flag_is_set((uv_handle_t*)stream, UV_WRITABLE) ||
+ uv_flag_is_set((uv_handle_t*)stream, UV_SHUT) ||
+ uv_flag_is_set((uv_handle_t*)stream, UV_CLOSED) ||
+ uv_flag_is_set((uv_handle_t*)stream, UV_CLOSING)) {
+ uv_err_new((uv_handle_t*)stream, EINVAL);
+ return -1;
+ }
/* Initialize request */
uv__req_init((uv_req_t*)req);
- req->handle = handle;
+ req->handle = stream;
req->cb = cb;
- if (uv_flag_is_set((uv_handle_t*)tcp, UV_SHUT) ||
- uv_flag_is_set((uv_handle_t*)tcp, UV_CLOSED) ||
- uv_flag_is_set((uv_handle_t*)tcp, UV_CLOSING)) {
- return -1;
- }
-
- tcp->shutdown_req = req;
+ stream->shutdown_req = req;
req->type = UV_SHUTDOWN;
- uv_flag_set((uv_handle_t*)tcp, UV_SHUTTING);
+ uv_flag_set((uv_handle_t*)stream, UV_SHUTTING);
- ev_io_start(EV_DEFAULT_UC_ &tcp->write_watcher);
+ ev_io_start(EV_DEFAULT_UC_ &stream->write_watcher);
return 0;
}
return -1;
}
- if (uv__stream_open(stream, sockfd)) {
+ if (uv__stream_open(stream, sockfd, UV_READABLE | UV_WRITABLE)) {
uv__close(sockfd);
return -2;
}
/* Success. */
handle->pipe_fname = pipe_fname; /* Is a strdup'ed copy. */
- handle->pipe_flock = pipe_flock;
handle->fd = sockfd;
status = 0;
assert(stdin_pipe[0] >= 0);
uv__close(stdin_pipe[0]);
uv__nonblock(stdin_pipe[1], 1);
- uv__stream_open((uv_stream_t*)options.stdin_stream, stdin_pipe[1]);
+ uv__stream_open((uv_stream_t*)options.stdin_stream, stdin_pipe[1],
+ UV_WRITABLE);
}
if (stdout_pipe[0] >= 0) {
assert(stdout_pipe[1] >= 0);
uv__close(stdout_pipe[1]);
uv__nonblock(stdout_pipe[0], 1);
- uv__stream_open((uv_stream_t*)options.stdout_stream, stdout_pipe[0]);
+ uv__stream_open((uv_stream_t*)options.stdout_stream, stdout_pipe[0],
+ UV_READABLE);
}
if (stderr_pipe[0] >= 0) {
assert(stderr_pipe[1] >= 0);
uv__close(stderr_pipe[1]);
uv__nonblock(stderr_pipe[0], 1);
- uv__stream_open((uv_stream_t*)options.stderr_stream, stderr_pipe[0]);
+ uv__stream_open((uv_stream_t*)options.stderr_stream, stderr_pipe[0],
+ UV_READABLE);
}
return 0;
}
-static const wchar_t DEFAULT_PATH[1] = L"";
-static const wchar_t DEFAULT_PATH_EXT[20] = L".COM;.EXE;.BAT;.CMD";
+static const wchar_t DEFAULT_PATH_EXT[10] = L".COM;.EXE";
static void uv_process_init(uv_process_t* handle) {
* 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* make_program_env(char** env_block) {
wchar_t* dst;
wchar_t* ptr;
char** env;
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';
int uv_spawn(uv_process_t* process, uv_process_options_t options) {
int err, i;
- const wchar_t* path = NULL;
- const wchar_t* path_ext = NULL;
+ wchar_t* path;
int size;
wchar_t* application_path, *application, *arguments, *env, *cwd;
STARTUPINFOW startup;
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;
+ env = options.env ? make_program_env(options.env) : NULL;
if (options.cwd) {
UTF8_TO_UTF16(options.cwd, cwd);
}
}
+ /* Get PATH env. variable. */
+ size = GetEnvironmentVariableW(L"PATH", NULL, 0) + 1;
+ path = (wchar_t*)malloc(size * sizeof(wchar_t));
+ if (!path) {
+ uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
+ }
+ GetEnvironmentVariableW(L"PATH", path, size * sizeof(wchar_t));
+ path[size - 1] = L'\0';
+
application_path = search_path(application,
cwd,
- path ? path : DEFAULT_PATH,
- path_ext ? path_ext : DEFAULT_PATH_EXT);
+ path,
+ DEFAULT_PATH_EXT);
if (!application_path) {
uv_set_error(UV_EINVAL, 0);
/* 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);
+ 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;
}
}
if (options.stdout_stream) {
- err = uv_create_stdio_pipe_pair(options.stdout_stream,
- &process->stdio_pipes[1].child_pipe, PIPE_ACCESS_INBOUND,
- GENERIC_WRITE);
+ err = uv_create_stdio_pipe_pair(options.stdout_stream, &process->stdio_pipes[1].child_pipe, PIPE_ACCESS_INBOUND, GENERIC_WRITE);
if (err) {
goto done;
}
}
if (options.stderr_stream) {
- err = uv_create_stdio_pipe_pair(options.stderr_stream,
- &process->stdio_pipes[2].child_pipe, PIPE_ACCESS_INBOUND,
- GENERIC_WRITE);
+ err = uv_create_stdio_pipe_pair(options.stderr_stream, &process->stdio_pipes[2].child_pipe, PIPE_ACCESS_INBOUND, GENERIC_WRITE);
if (err) {
goto done;
}
free(arguments);
free(cwd);
free(env);
+ free(path);
if (err) {
for (i = 0; i < COUNTOF(process->stdio_pipes); i++) {
/* The time in milliseconds after which a single test times out. */
#define TEST_TIMEOUT 5000
+static int maybe_run_test(int argc, char **argv);
+
int main(int argc, char **argv) {
int i;
- char buffer[32];
platform_init(argc, argv);
switch (argc) {
case 1: return run_tests(TEST_TIMEOUT, 0);
- case 2: {
- if (strcmp(argv[1], "spawn_helper1") == 0) {
- return 1;
- }
-
- if (strcmp(argv[1], "spawn_helper2") == 0) {
- printf("hello world\n");
- return 1;
- }
-
- if (strcmp(argv[1], "spawn_helper3") == 0) {
- fgets(buffer, sizeof(buffer) - 1, stdin);
- buffer[sizeof(buffer) - 1] = '\0';
- fputs(buffer, stdout);
- return 1;
- }
-
- if (strcmp(argv[1], "spawn_helper4") == 0) {
- // sleep
- uv_sleep(10000);
- return 100;
- }
-
- return run_test(argv[1], TEST_TIMEOUT, 0);
- }
+ case 2: return maybe_run_test(argc, argv);
case 3: return run_test_part(argv[1], argv[2]);
default:
LOGF("Too many arguments.\n");
return 1;
}
}
+
+
+static int maybe_run_test(int argc, char **argv) {
+ if (strcmp(argv[1], "spawn_helper1") == 0) {
+ return 1;
+ }
+
+ if (strcmp(argv[1], "spawn_helper2") == 0) {
+ printf("hello world\n");
+ return 1;
+ }
+
+ if (strcmp(argv[1], "spawn_helper3") == 0) {
+ char buffer[256];
+ fgets(buffer, sizeof(buffer) - 1, stdin);
+ buffer[sizeof(buffer) - 1] = '\0';
+ fputs(buffer, stdout);
+ return 1;
+ }
+
+ if (strcmp(argv[1], "spawn_helper4") == 0) {
+ uv_sleep(10000);
+ return 1;
+ }
+
+ return run_test(argv[1], TEST_TIMEOUT, 0);
+}