Skip to content
Permalink
Browse files

context: Added bxf_context_current()

  • Loading branch information...
Snaipe committed Aug 7, 2016
1 parent f7d88a0 commit a226eeb5e8d30ec189ee742eadf0f976e18b0de3
Showing with 22 additions and 1 deletion.
  1. +2 −0 include/boxfort.h
  2. +9 −0 sample/context.c
  3. +11 −1 src/context.c
@@ -123,6 +123,8 @@ int bxf_context_addfile(bxf_context ctx, const char *name, FILE *file);

int bxf_context_getfile(bxf_context ctx, const char *name, FILE **file);

bxf_context bxf_context_current(void);

/* Sandbox API */

typedef unsigned long long bxf_pid;
@@ -17,7 +17,13 @@ static int *my_int;

static int child(void)
{
bxf_context ctx = bxf_context_current();

long *my_long = NULL;
bxf_context_getobject(ctx, "long_id", (void **)&my_long);

printf("my_int = %d\n", *my_int);
printf("my_long = %ld\n", *my_long);
return 0;
}

@@ -42,6 +48,9 @@ EXPORT int main(void)
my_int = bxf_arena_ptr(arena, intp);
*my_int = 42;

long my_long = 24;
_assert(!bxf_context_addobject(ctx, "long_id", &my_long, sizeof (my_long)));

/* We run the child function with the created context */
_assert(!bxf_run(child, .inherit.context = ctx));

@@ -136,7 +136,7 @@ static int find_obj(void *ptr, size_t size, void *user)

struct bxfi_ctx_object *obj = ptr;
if (!strcmp(obj->data, ctx->name)) {
ctx->result = ptr;
ctx->result = &obj->data[obj->namesz];
return 1;
}
return 0;
@@ -308,6 +308,8 @@ static int inherit_elt(void *ptr, size_t size, void *user)
return 0;
}

static struct bxf_context current_ctx;

int bxfi_context_inherit(struct bxfi_ctx_arena *ctx)
{
#ifdef BXF_ARENA_REOPEN_SHM
@@ -332,5 +334,13 @@ int bxfi_context_inherit(struct bxfi_ctx_arena *ctx)
if (rc < 0)
return rc;

current_ctx.arena = arena;
return bxf_arena_iter(arena, inherit_elt, NULL);
}

bxf_context bxf_context_current(void)
{
if (!current_ctx.arena)
return NULL;
return &current_ctx;
}

0 comments on commit a226eeb

Please sign in to comment.
You can’t perform that action at this time.