Permalink
Browse files

Get started on spawn sync

  • Loading branch information...
1 parent fe18438 commit cb683ea3f51c7b4ab8c7ab6af7e05a2d2c9af6c8 @felixge felixge committed with piscisaureus Sep 5, 2011
Showing with 114 additions and 0 deletions.
  1. +5 −0 TODO
  2. +23 −0 include/uv.h
  3. +22 −0 src/unix/process.c
  4. +3 −0 test/test-list.h
  5. +61 −0 test/test-spawn-sync.c
View
5 TODO
@@ -0,0 +1,5 @@
+* Define interface
+
+* uv_spawn_sync(loop, process, options)
+
+* Use pid_t for normal spawn process type (uv.h)
View
23 include/uv.h
@@ -57,6 +57,7 @@ typedef struct uv_idle_s uv_idle_t;
typedef struct uv_async_s uv_async_t;
typedef struct uv_getaddrinfo_s uv_getaddrinfo_t;
typedef struct uv_process_s uv_process_t;
+typedef struct uv_sync_process_s uv_sync_process_t;
typedef struct uv_counters_s uv_counters_t;
/* Request types */
typedef struct uv_req_s uv_req_t;
@@ -873,6 +874,28 @@ struct uv_process_s {
/* Initializes uv_process_t and starts the process. */
int uv_spawn(uv_loop_t*, uv_process_t*, uv_process_options_t options);
+/* uv_spawn_sync() options */
+typedef struct uv_sync_process_options_s {
+ const char* file; /* Path to program to execute. */
+ /*
+ * Command line arguments. args[0] should be the path to the program.
+ */
+ char** args;
+} uv_sync_process_options_t;
+
+/*
+ * uv_process_t is not a subclass of uv_handle_t
+ */
+struct uv_sync_process_s {
+ pid_t pid;
+ int exit_code;
+};
+
+/* Initializes uv_sync_process_t and executes the process. */
+int uv_spawn_sync(uv_loop_t*, uv_sync_process_t*,
+ uv_sync_process_options_t options);
+
+
/*
* Kills the process with the specified signal. The user must still
* call uv_close on the process.
View
22 src/unix/process.c
@@ -298,3 +298,25 @@ int uv_process_kill(uv_process_t* process, int signum) {
return 0;
}
}
+
+int uv_spawn_sync(uv_loop_t* loop, uv_sync_process_t* process,
+ uv_sync_process_options_t options) {
+
+ pid_t pid;
+
+ switch (pid = fork()) {
+ case -1:
+ perror("fork");
+ return -1;
+
+ case 0: /* Child */
+ execvp(options.file, options.args);
+ perror("execvp()");
+ _exit(127);
+ break;
+ }
+
+ process->pid = pid;
+
+ return 0;
+}
View
3 test/test-list.h
@@ -79,6 +79,7 @@ TEST_DECLARE (spawn_exit_code)
TEST_DECLARE (spawn_stdout)
TEST_DECLARE (spawn_stdin)
TEST_DECLARE (spawn_and_kill)
+TEST_DECLARE (spawn_sync_exit_code)
TEST_DECLARE (fs_file_noent)
TEST_DECLARE (fs_file_async)
TEST_DECLARE (fs_file_sync)
@@ -200,6 +201,8 @@ TASK_LIST_START
TEST_ENTRY (spawn_stdout)
TEST_ENTRY (spawn_stdin)
TEST_ENTRY (spawn_and_kill)
+
+ TEST_ENTRY (spawn_sync_exit_code)
#ifdef _WIN32
TEST_ENTRY (spawn_detect_pipe_name_collisions_on_windows)
TEST_ENTRY (argument_escaping)
View
61 test/test-spawn-sync.c
@@ -0,0 +1,61 @@
+/* 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.
+ */
+
+#include "uv.h"
+#include "task.h"
+#include <stdio.h>
+#include <stdlib.h>
+
+static char exepath[1024];
+static size_t exepath_size = 1024;
+static uv_sync_process_t process;
+static uv_sync_process_options_t options = { 0 };
+static char* args[3];
+
+static void init_process_options(char* test) {
+ /* Note spawn_helper1 defined in test/run-tests.c */
+ int r = uv_exepath(exepath, &exepath_size);
+ ASSERT(r == 0);
+ exepath[exepath_size] = '\0';
+ args[0] = exepath;
+ args[1] = test;
+ args[2] = NULL;
+
+ options.file = exepath;
+ options.args = args;
+}
+
+TEST_IMPL(spawn_sync_exit_code) {
+ int r;
+ uv_init();
+
+ init_process_options("spawn_helper1");
+
+ r = uv_spawn_sync(uv_default_loop(), &process, options);
+
+ ASSERT(process.pid >= 0);
+ ASSERT(r == 0);
+ ASSERT(process.exit_code == 1);
+
+ fprintf(stdout, "Fuck!\n");
+
+ return 0;
+}

0 comments on commit cb683ea

Please sign in to comment.