Skip to content

Commit 0ba2a8b

Browse files
committed
feat(ocre): pass stdin/out/err in create
We want to be able to forward stdin/stdout/stderr to arbitrary file descriptors. We add to the create functions, so the user can pass the custom file descriptors for the container. Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
1 parent 370dc77 commit 0ba2a8b

11 files changed

Lines changed: 133 additions & 81 deletions

File tree

src/ocre/container.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <errno.h>
99
#include <stdlib.h>
1010
#include <stdint.h>
11+
#include <unistd.h>
1112
#include <string.h>
1213
#include <stdbool.h>
1314
#include <pthread.h>
@@ -145,12 +146,23 @@ static const char *ocre_find_best_matching_runtime(const char *image)
145146

146147
struct ocre_container *ocre_container_create(const char *img_path, const char *workdir, const char *runtime,
147148
const char *container_id, bool detached,
148-
const struct ocre_container_args *arguments)
149+
const struct ocre_container_args *arguments, int stdin_fd, int stdout_fd,
150+
int stderr_fd)
149151
{
150152
int rc;
151153
const char **capabilities = NULL;
152154
const char **mounts = NULL;
153155

156+
if (stdin_fd < 0) {
157+
stdin_fd = STDIN_FILENO;
158+
}
159+
if (stdout_fd < 0) {
160+
stdout_fd = STDOUT_FILENO;
161+
}
162+
if (stderr_fd < 0) {
163+
stderr_fd = STDERR_FILENO;
164+
}
165+
154166
if (!runtime) {
155167
runtime = ocre_find_best_matching_runtime(img_path);
156168
if (!runtime) {
@@ -266,9 +278,9 @@ struct ocre_container *ocre_container_create(const char *img_path, const char *w
266278
goto error_mutex;
267279
}
268280

269-
container->runtime_context =
270-
container->runtime->create(container_id, img_path, workdir, capabilities,
271-
(const char **)container->argv, (const char **)container->envp, mounts);
281+
container->runtime_context = container->runtime->create(
282+
container_id, img_path, workdir, capabilities, (const char **)container->argv,
283+
(const char **)container->envp, mounts, stdin_fd, stdout_fd, stderr_fd);
272284
if (!container->runtime_context) {
273285
LOG_ERR("Failed to create container");
274286
goto error_cond;

src/ocre/container.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ struct ocre_container;
1313

1414
struct ocre_container *ocre_container_create(const char *img_path, const char *workdir, const char *runtime,
1515
const char *container_id, bool detached,
16-
const struct ocre_container_args *arguments);
16+
const struct ocre_container_args *arguments, int stdin_fd, int stdout_fd,
17+
int stderr_fd);
1718
int ocre_container_destroy(struct ocre_container *container);

src/ocre/context.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ int ocre_context_destroy(struct ocre_context *context)
177177

178178
struct ocre_container *ocre_context_create_container(struct ocre_context *context, const char *image,
179179
const char *const runtime, const char *container_id, bool detached,
180-
const struct ocre_container_args *arguments)
180+
const struct ocre_container_args *arguments, int stdin_fd,
181+
int stdout_fd, int stderr_fd)
181182
{
182183
const char *computed_container_id = NULL;
183184
struct ocre_container *container = NULL;
@@ -285,7 +286,7 @@ struct ocre_container *ocre_context_create_container(struct ocre_context *contex
285286
/* Create the container */
286287

287288
container = ocre_container_create(image_path, container_workdir, runtime, computed_container_id, detached,
288-
arguments);
289+
arguments, stdin_fd, stdout_fd, stderr_fd);
289290
if (!container) {
290291
LOG_ERR("Failed to create container %s: errno=%d", computed_container_id, errno);
291292
goto error;

src/ocre/include/ocre/context.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,16 @@ struct ocre_container_args {
129129
* @param container_id The ID to assign to the container. Can be NULL, in which case a random ID will be generated
130130
* @param detached Whether the container should be detached (run in the background) or not
131131
* @param arguments The container arguments to pass to the container. Can be NULL
132+
* @param stdin_fd The file descriptor to use for stdin. Can be -1 to use the default (STDIN_FILENO)
133+
* @param stdout_fd The file descriptor to use for stdout. Can be -1 to use the default (STDOUT_FILENO)
134+
* @param stderr_fd The file descriptor to use for stderr. Can be -1 to use the default (STDERR_FILENO)
132135
*
133136
* @return A pointer to the newly created container, or NULL on failure
134137
*/
135138
struct ocre_container *ocre_context_create_container(struct ocre_context *context, const char *image,
136139
const char *const runtime, const char *container_id, bool detached,
137-
const struct ocre_container_args *arguments);
140+
const struct ocre_container_args *arguments, int stdin_fd,
141+
int stdout_fd, int stderr_fd);
138142

139143
/**
140144
* @brief Get a container by its ID

src/runtime/include/ocre/runtime/vtable.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,15 @@ struct ocre_runtime_vtable {
7272
* @param envp A NULL-terminated array of environment variables to be passed to the
7373
* container
7474
* @param mounts Mount points for the runtime instance
75+
* @param stdin_fd The file descriptor to use for stdin. Should be valid and open
76+
* @param stdout_fd The file descriptor to use for stdout. Should be valid and open
77+
* @param stderr_fd The file descriptor to use for stderr. Should be valid and open
7578
*
7679
* @return Pointer to the runtime context on success, NULL on failure
7780
*/
7881
void *(*create)(const char *container_id, const char *img_path, const char *workdir, const char **capabilities,
79-
const char **argv, const char **envp, const char **mounts);
82+
const char **argv, const char **envp, const char **mounts, int stdin_fd, int stdout_fd,
83+
int stderr_fd);
8084

8185
/**
8286
* @brief Destroy a runtime instance

src/runtime/wamr-wasip1/wamr.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,11 @@ static int runtime_deinit(void)
218218
}
219219

220220
static void *instance_create(const char *container_id, const char *img_path, const char *workdir,
221-
const char **capabilities, const char **argv, const char **envp, const char **mounts)
221+
const char **capabilities, const char **argv, const char **envp, const char **mounts,
222+
int stdin_fd, int stdout_fd, int stderr_fd)
222223
{
223224
struct wamr_context *context = NULL;
224-
// char **dir_map_list = NULL;
225225
char **new_dir_map_list = NULL;
226-
// size_t dir_map_list_len = 0;
227226

228227
if (!img_path) {
229228
LOG_ERR("Invalid arguments");
@@ -401,8 +400,9 @@ static void *instance_create(const char *container_id, const char *img_path, con
401400
context->dir_map_list[context->dir_map_list_len] = NULL;
402401
}
403402

404-
wasm_runtime_set_wasi_args(context->module, NULL, 0, (const char **)context->dir_map_list,
405-
context->dir_map_list_len, envp, envn, context->argv, argc + 1);
403+
wasm_runtime_set_wasi_args_ex(context->module, NULL, 0, (const char **)context->dir_map_list,
404+
context->dir_map_list_len, envp, envn, context->argv, argc + 1, stdin_fd,
405+
stdout_fd, stderr_fd);
406406

407407
return context;
408408

src/samples/demo/main.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ int main(int argc, char *argv[])
3636
}
3737

3838
struct ocre_container *hello_world =
39-
ocre_context_create_container(ocre, "hello-world.wasm", "wamr/wasip1", NULL, false, &args);
39+
ocre_context_create_container(ocre, "hello-world.wasm", "wamr/wasip1", NULL, false, &args, STDIN_FILENO,
40+
STDOUT_FILENO, STDERR_FILENO);
4041

4142
if (!hello_world) {
4243
fprintf(stderr, "Failed to create container\n");
@@ -55,8 +56,8 @@ int main(int argc, char *argv[])
5556
return 1;
5657
}
5758

58-
struct ocre_container *blinky =
59-
ocre_context_create_container(ocre, "blinky.wasm", "wamr/wasip1", NULL, true, &args);
59+
struct ocre_container *blinky = ocre_context_create_container(
60+
ocre, "blinky.wasm", "wamr/wasip1", NULL, true, &args, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
6061

6162
if (!blinky) {
6263
fprintf(stderr, "Failed to create container\n");
@@ -91,16 +92,16 @@ int main(int argc, char *argv[])
9192
return 1;
9293
}
9394

94-
struct ocre_container *subscriber =
95-
ocre_context_create_container(ocre, "subscriber.wasm", "wamr/wasip1", NULL, true, &args);
95+
struct ocre_container *subscriber = ocre_context_create_container(
96+
ocre, "subscriber.wasm", "wamr/wasip1", NULL, true, &args, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
9697

9798
if (!subscriber) {
9899
fprintf(stderr, "Failed to create container\n");
99100
return 1;
100101
}
101102

102-
struct ocre_container *publisher =
103-
ocre_context_create_container(ocre, "publisher.wasm", "wamr/wasip1", NULL, true, &args);
103+
struct ocre_container *publisher = ocre_context_create_container(
104+
ocre, "publisher.wasm", "wamr/wasip1", NULL, true, &args, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
104105

105106
if (!publisher) {
106107
fprintf(stderr, "Failed to create container\n");

src/samples/mini/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ int main(int argc, char *argv[])
9191

9292
free(sample_path);
9393

94-
struct ocre_container *container =
95-
ocre_context_create_container(ocre, "sample.wasm", "wamr/wasip1", NULL, false, NULL);
94+
struct ocre_container *container = ocre_context_create_container(
95+
ocre, "sample.wasm", "wamr/wasip1", NULL, false, NULL, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
9696

9797
if (!container) {
9898
fprintf(stderr, "Failed to create container\n");

src/shell/container/create.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ int cmd_container_create_run(struct ocre_context *ctx, const char *argv0, int ar
216216
};
217217

218218
struct ocre_container *container =
219-
ocre_context_create_container(ctx, argv[optind], runtime, container_id, detached, &arguments);
219+
ocre_context_create_container(ctx, argv[optind], runtime, container_id, detached, &arguments,
220+
STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
220221

221222
if (!container) {
222223
fprintf(stderr, "Failed to create container\n");

tests/system/container.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ void setUp(void)
2929
ocre_initialize(NULL);
3030
context = ocre_create_context(NULL);
3131

32-
hello_world = ocre_context_create_container(context, "hello-world.wasm", "wamr/wasip1", "hello", false, NULL);
33-
blinky = ocre_context_create_container(context, "blinky.wasm", "wamr/wasip1", NULL, true, &args);
32+
hello_world = ocre_context_create_container(context, "hello-world.wasm", "wamr/wasip1", "hello", false, NULL,
33+
STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
34+
blinky = ocre_context_create_container(context, "blinky.wasm", "wamr/wasip1", NULL, true, &args, STDIN_FILENO,
35+
STDOUT_FILENO, STDERR_FILENO);
3436
}
3537

3638
void tearDown(void)

0 commit comments

Comments
 (0)