Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Get started on spawn sync
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge authored and piscisaureus committed Oct 1, 2011
1 parent fe18438 commit cb683ea
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
5 changes: 5 additions & 0 deletions TODO
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
* Define interface

* uv_spawn_sync(loop, process, options)

* Use pid_t for normal spawn process type (uv.h)
23 changes: 23 additions & 0 deletions include/uv.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typedef struct uv_idle_s uv_idle_t;
typedef struct uv_async_s uv_async_t; typedef struct uv_async_s uv_async_t;
typedef struct uv_getaddrinfo_s uv_getaddrinfo_t; typedef struct uv_getaddrinfo_s uv_getaddrinfo_t;
typedef struct uv_process_s uv_process_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; typedef struct uv_counters_s uv_counters_t;
/* Request types */ /* Request types */
typedef struct uv_req_s uv_req_t; typedef struct uv_req_s uv_req_t;
Expand Down Expand Up @@ -873,6 +874,28 @@ struct uv_process_s {
/* Initializes uv_process_t and starts the process. */ /* Initializes uv_process_t and starts the process. */
int uv_spawn(uv_loop_t*, uv_process_t*, uv_process_options_t options); 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 * Kills the process with the specified signal. The user must still
* call uv_close on the process. * call uv_close on the process.
Expand Down
22 changes: 22 additions & 0 deletions src/unix/process.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -298,3 +298,25 @@ int uv_process_kill(uv_process_t* process, int signum) {
return 0; 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;
}
3 changes: 3 additions & 0 deletions test/test-list.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ TEST_DECLARE (spawn_exit_code)
TEST_DECLARE (spawn_stdout) TEST_DECLARE (spawn_stdout)
TEST_DECLARE (spawn_stdin) TEST_DECLARE (spawn_stdin)
TEST_DECLARE (spawn_and_kill) TEST_DECLARE (spawn_and_kill)
TEST_DECLARE (spawn_sync_exit_code)
TEST_DECLARE (fs_file_noent) TEST_DECLARE (fs_file_noent)
TEST_DECLARE (fs_file_async) TEST_DECLARE (fs_file_async)
TEST_DECLARE (fs_file_sync) TEST_DECLARE (fs_file_sync)
Expand Down Expand Up @@ -200,6 +201,8 @@ TASK_LIST_START
TEST_ENTRY (spawn_stdout) TEST_ENTRY (spawn_stdout)
TEST_ENTRY (spawn_stdin) TEST_ENTRY (spawn_stdin)
TEST_ENTRY (spawn_and_kill) TEST_ENTRY (spawn_and_kill)

TEST_ENTRY (spawn_sync_exit_code)
#ifdef _WIN32 #ifdef _WIN32
TEST_ENTRY (spawn_detect_pipe_name_collisions_on_windows) TEST_ENTRY (spawn_detect_pipe_name_collisions_on_windows)
TEST_ENTRY (argument_escaping) TEST_ENTRY (argument_escaping)
Expand Down
61 changes: 61 additions & 0 deletions test/test-spawn-sync.c
Original file line number Original file line Diff line number Diff line change
@@ -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.