Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make stdio configurable #102

Merged
merged 4 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.in = 0;
init_options.out = 1;
init_options.err = 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 in;
uvwasi_fd_t out;
uvwasi_fd_t err;
const uvwasi_mem_t* allocator;
} uvwasi_options_t;
```

Expand Down
4 changes: 2 additions & 2 deletions include/fd_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "wasi_types.h"

struct uvwasi_s;
struct uvwasi_options_s;

struct uvwasi_fd_wrap_t {
uvwasi_fd_t id;
Expand All @@ -27,8 +28,7 @@ struct uvwasi_fd_table_t {
};

uvwasi_errno_t uvwasi_fd_table_init(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table,
uint32_t init_size);
struct uvwasi_options_s* options);
void uvwasi_fd_table_free(struct uvwasi_s* uvwasi,
struct uvwasi_fd_table_t* table);
uvwasi_errno_t uvwasi_fd_table_insert(struct uvwasi_s* uvwasi,
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 in;
uvwasi_fd_t out;
uvwasi_fd_t err;
const uvwasi_mem_t* allocator;
} uvwasi_options_t;

Expand Down
96 changes: 57 additions & 39 deletions src/fd_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,46 @@
#include "uvwasi_alloc.h"


static uvwasi_errno_t uvwasi__insert_stdio(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
const uvwasi_fd_t fd,
const uvwasi_fd_t expected,
const char* name) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_filetype_t type;
uvwasi_rights_t base;
uvwasi_rights_t inheriting;
uvwasi_errno_t err;

err = uvwasi__get_filetype_by_fd(fd, &type);
if (err != UVWASI_ESUCCESS)
return err;

err = uvwasi__get_rights(fd, UV_FS_O_RDWR, type, &base, &inheriting);
if (err != UVWASI_ESUCCESS)
return err;

err = uvwasi_fd_table_insert(uvwasi,
table,
fd,
name,
name,
type,
base,
inheriting,
0,
&wrap);
if (err != UVWASI_ESUCCESS)
return err;

if (wrap->id != expected)
err = UVWASI_EBADF;

uv_mutex_unlock(&wrap->mutex);
return err;
}


uvwasi_errno_t uvwasi_fd_table_insert(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
uv_file fd,
Expand Down Expand Up @@ -117,25 +157,21 @@ uvwasi_errno_t uvwasi_fd_table_insert(uvwasi_t* uvwasi,


uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi,
struct uvwasi_fd_table_t* table,
uint32_t init_size) {
struct uvwasi_fd_wrap_t* wrap;
uvwasi_filetype_t type;
uvwasi_rights_t base;
uvwasi_rights_t inheriting;
uvwasi_options_t* options) {
struct uvwasi_fd_table_t* table;
uvwasi_errno_t err;
int i;
int r;

/* Require an initial size of at least three to store the stdio FDs. */
if (table == NULL || init_size < 3)
if (uvwasi == NULL || options == NULL || options->fd_table_size < 3)
return UVWASI_EINVAL;

table = &uvwasi->fds;
table->fds = NULL;
table->used = 0;
table->size = init_size;
table->size = options->fd_table_size;
table->fds = uvwasi__calloc(uvwasi,
init_size,
options->fd_table_size,
sizeof(struct uvwasi_fd_wrap_t*));

if (table->fds == NULL)
Expand All @@ -154,35 +190,17 @@ uvwasi_errno_t uvwasi_fd_table_init(uvwasi_t* uvwasi,
}

/* Create the stdio FDs. */
for (i = 0; i < 3; ++i) {
err = uvwasi__get_filetype_by_fd(i, &type);
if (err != UVWASI_ESUCCESS)
goto error_exit;

err = uvwasi__get_rights(i, UV_FS_O_RDWR, type, &base, &inheriting);
if (err != UVWASI_ESUCCESS)
goto error_exit;

err = uvwasi_fd_table_insert(uvwasi,
table,
i,
"",
"",
type,
base,
inheriting,
0,
&wrap);
if (err != UVWASI_ESUCCESS)
goto error_exit;

r = (int) wrap->id != i || wrap->id != (uvwasi_fd_t) wrap->fd;
uv_mutex_unlock(&wrap->mutex);
if (r) {
err = UVWASI_EBADF;
goto error_exit;
}
}
err = uvwasi__insert_stdio(uvwasi, table, options->in, 0, "<stdin>");
if (err != UVWASI_ESUCCESS)
goto error_exit;

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

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

return UVWASI_ESUCCESS;
error_exit:
Expand Down
2 changes: 1 addition & 1 deletion src/uvwasi.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ uvwasi_errno_t uvwasi_init(uvwasi_t* uvwasi, uvwasi_options_t* options) {
}
}

err = uvwasi_fd_table_init(uvwasi, &uvwasi->fds, options->fd_table_size);
err = uvwasi_fd_table_init(uvwasi, options);
if (err != UVWASI_ESUCCESS)
goto 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.in = 0;
init_options.out = 1;
init_options.err = 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.in = 0;
init_options.out = 1;
init_options.err = 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.in = 0;
init_options.out = 1;
init_options.err = 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.in = 0;
init_options.out = 1;
init_options.err = 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.in = 0;
init_options.out = 1;
init_options.err = 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.in = 0;
init_options.out = 1;
init_options.err = 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.in = 0;
init_options.out = 1;
init_options.err = 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.in = 0;
init_options.out = 1;
init_options.err = 2;
init_options.fd_table_size = 3;
init_options.argc = 0;
init_options.argv = NULL;
Expand Down