Skip to content

Commit

Permalink
support passing stdio descriptors
Browse files Browse the repository at this point in the history
This commit requires the stdio file descriptors to be explicitly
defined. This allows embedders to run multiple WASI applications
in the same process (assuming they also monkey patch the
uvwasi_proc_exit() functionality to not force the process to
exit).
  • Loading branch information
cjihrig committed Mar 14, 2020
1 parent 95f2869 commit e115a62
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ int main(void) {
uvwasi_errno_t err;

/* Setup the initialization options. */
init_options.stdin = 0;
init_options.stdout = 1;
init_options.stderr = 2;
init_options.fd_table_size = 3;
init_options.argc = 3;
init_options.argv = calloc(3, sizeof(char*));
Expand All @@ -38,6 +41,7 @@ int main(void) {
init_options.preopens = calloc(1, sizeof(uvwasi_preopen_t));
init_options.preopens[0].mapped_path = "/var";
init_options.preopens[0].real_path = ".";
init_options.allocator = NULL;

/* Initialize the sandbox. */
err = uvwasi_init(&uvwasi, &init_options);
Expand Down Expand Up @@ -146,6 +150,10 @@ typedef struct uvwasi_options_s {
size_t argc;
char** argv;
char** envp;
uvwasi_fd_t stdin;
uvwasi_fd_t stdout;
uvwasi_fd_t stderr;
const uvwasi_mem_t* allocator;
} uvwasi_options_t;
```

Expand Down
3 changes: 3 additions & 0 deletions include/uvwasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ typedef struct uvwasi_options_s {
size_t argc;
char** argv;
char** envp;
uvwasi_fd_t stdin;
uvwasi_fd_t stdout;
uvwasi_fd_t stderr;
const uvwasi_mem_t* allocator;
} uvwasi_options_t;

Expand Down
8 changes: 4 additions & 4 deletions src/fd_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

static uvwasi_errno_t uvwasi__insert_stdio(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
const uv_file fd,
const uvwasi_fd_t fd,
const uvwasi_fd_t expected,
const char* name) {
struct uvwasi_fd_wrap_t* wrap;
Expand Down Expand Up @@ -190,15 +190,15 @@ uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi,
}

/* Create the stdio FDs. */
err = uvwasi__insert_stdio(uvwasi, table, 0, 0, "<stdin>");
err = uvwasi__insert_stdio(uvwasi, table, options->stdin, 0, "<stdin>");
if (err != UVWASI_ESUCCESS)
goto error_exit;

err = uvwasi__insert_stdio(uvwasi, table, 1, 1, "<stdout>");
err = uvwasi__insert_stdio(uvwasi, table, options->stdout, 1, "<stdout>");
if (err != UVWASI_ESUCCESS)
goto error_exit;

err = uvwasi__insert_stdio(uvwasi, table, 2, 2, "<stderr>");
err = uvwasi__insert_stdio(uvwasi, table, options->stderr, 2, "<stderr>");
if (err != UVWASI_ESUCCESS)
goto error_exit;

Expand Down
3 changes: 3 additions & 0 deletions test/test-args-get.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ int main(void) {
char** args_get_argv;
char* buf;

init_options.stdin = 0;
init_options.stdout = 1;
init_options.stderr = 2;
init_options.fd_table_size = 3;
init_options.argc = 3;
init_options.argv = calloc(3, sizeof(char*));
Expand Down
3 changes: 3 additions & 0 deletions test/test-basic-file-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ int main(void) {
uv_fs_req_cleanup(&req);
assert(r == 0 || r == UV_EEXIST);

init_options.stdin = 0;
init_options.stdout = 1;
init_options.stderr = 2;
init_options.fd_table_size = 3;
init_options.argc = 0;
init_options.argv = NULL;
Expand Down
3 changes: 3 additions & 0 deletions test/test-ebadf-input-validation.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ int main(void) {

test_void = (void*) &test_fdstat;

init_options.stdin = 0;
init_options.stdout = 1;
init_options.stderr = 2;
init_options.fd_table_size = 3;
init_options.argc = 0;
init_options.argv = NULL;
Expand Down
3 changes: 3 additions & 0 deletions test/test-environ-get.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ int main(void) {
char* buf;
size_t i;

init_options.stdin = 0;
init_options.stdout = 1;
init_options.stderr = 2;
init_options.fd_table_size = 3;
init_options.argc = 0;
init_options.argv = NULL;
Expand Down
3 changes: 3 additions & 0 deletions test/test-fd-prestat-dir-name.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ int main(void) {
uv_fs_req_cleanup(&req);
assert(r == 0 || r == UV_EEXIST);

init_options.stdin = 0;
init_options.stdout = 1;
init_options.stderr = 2;
init_options.fd_table_size = 3;
init_options.argc = 0;
init_options.argv = NULL;
Expand Down
3 changes: 3 additions & 0 deletions test/test-multiple-wasi-destroys.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ int main(void) {
uvwasi_options_t init_options;
uvwasi_errno_t err;

init_options.stdin = 0;
init_options.stdout = 1;
init_options.stderr = 2;
init_options.fd_table_size = 3;
init_options.argc = 0;
init_options.argv = NULL;
Expand Down
3 changes: 3 additions & 0 deletions test/test-path-create-remove-directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ int main(void) {
uv_fs_req_cleanup(&req);
assert(r == 0 || r == UV_ENOENT);

init_options.stdin = 0;
init_options.stdout = 1;
init_options.stderr = 2;
init_options.fd_table_size = 3;
init_options.argc = 0;
init_options.argv = NULL;
Expand Down
3 changes: 3 additions & 0 deletions test/test-random-get.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ int main(void) {
int success;
int i;

init_options.stdin = 0;
init_options.stdout = 1;
init_options.stderr = 2;
init_options.fd_table_size = 3;
init_options.argc = 0;
init_options.argv = NULL;
Expand Down

0 comments on commit e115a62

Please sign in to comment.