Skip to content

Commit

Permalink
Added spawning examples
Browse files Browse the repository at this point in the history
  • Loading branch information
john-sharratt committed Sep 22, 2022
1 parent 8e96199 commit 1605470
Show file tree
Hide file tree
Showing 8 changed files with 514 additions and 399 deletions.
6 changes: 5 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ SRC=$(wildcard *.c)

export PATH := $(LLD_PATH):$(PATH)

all: longjmp file-copy signal fork
all: longjmp file-copy signal fork vfork execve spawn
cp -f ../dist/wasix/longjmp.wasm ../../ate/wasmer-web/public/bin/example-longjmp.wasm
cp -f ../dist/wasix/fork.wasm ../../ate/wasmer-web/public/bin/example-fork.wasm
cp -f ../dist/wasix/vfork.wasm ../../ate/wasmer-web/public/bin/example-vfork.wasm
cp -f ../dist/wasix/signal.wasm ../../ate/wasmer-web/public/bin/example-signal.wasm
cp -f ../dist/wasix/execve.wasm ../../ate/wasmer-web/public/bin/example-execve.wasm
cp -f ../dist/wasix/spawn.wasm ../../ate/wasmer-web/public/bin/example-spawn.wasm

%: %.c
mkdir -p ../dist/host
Expand Down
37 changes: 37 additions & 0 deletions examples/execve.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

int main(void) {
printf("Main program started\n");

int pid = vfork();
if (pid == -1) {
printf("failed to fork - %d", errno);
return 1;
}

if (pid == 0) {
printf("execve: echo hi-from-child\n");
char* argv[] = { "echo", "hi-from-child", NULL };
char* envp[] = { NULL };
if (execve("echo", argv, envp) == -1) {
perror("Could not execve");
}
} else {
int status = 0;
waitpid(pid, &status, 0);
printf("Child(%d) exited with %d\n", pid, status);

printf("execve: echo hi-from-parent\n");
char* argv[] = { "echo", "hi-from-parent", NULL };
char* envp[] = { NULL };
if (execve("echo", argv, envp) == -1) {
perror("Could not execve");
}
}

return 1;
}
31 changes: 31 additions & 0 deletions examples/spawn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>

extern char **environ;

int main(int argc, char* argv[])
{
pid_t pid;
char *child_argv[] = {"echo", "hi", NULL};
int status;
status = posix_spawn(&pid, "echo", NULL, NULL, child_argv, environ);
if (status == 0) {
printf("Child pid: %i\n", pid);
do {
if (waitpid(pid, &status, 0) != -1) {
printf("Child status %d\n", WEXITSTATUS(status));
} else {
perror("waitpid");
exit(1);
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
} else {
printf("posix_spawn: %s\n", strerror(status));
}
return 0;
}
24 changes: 24 additions & 0 deletions examples/vfork.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

int main() {
int pid = vfork();
if (pid == -1) {
printf("failed to fork - %d", errno);
return 1;
}

if (pid == 0)
_exit(10);
else {
printf("Parent waiting on Child(%d)\n", pid);

int status = 0;
waitpid(pid, &status, 0);
printf("Child(%d) exited with %d\n", pid, status);
}
return 0;
}
32 changes: 16 additions & 16 deletions libc-bottom-half/headers/public/wasi/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ _Static_assert(_Alignof(int32_t) == 4, "non-wasi data layout");
_Static_assert(_Alignof(uint32_t) == 4, "non-wasi data layout");
_Static_assert(_Alignof(int64_t) == 8, "non-wasi data layout");
_Static_assert(_Alignof(uint64_t) == 8, "non-wasi data layout");
_Static_assert(_Alignof(intptr_t) == 8, "non-wasi data layout");
_Static_assert(_Alignof(uintptr_t) == 8, "non-wasi data layout");
_Static_assert(_Alignof(void*) == 8, "non-wasi data layout");
typedef int64_t __wasi_int_t;
typedef uint64_t __wasi_uint_t;
_Static_assert(_Alignof(__wasi_int_t) == 8, "non-wasi data layout");
_Static_assert(_Alignof(__wasi_uint_t) == 8, "non-wasi data layout");
_Static_assert(_Alignof(intptr_t) == 4, "non-wasi data layout");
_Static_assert(_Alignof(uintptr_t) == 4, "non-wasi data layout");
_Static_assert(_Alignof(void*) == 4, "non-wasi data layout");
typedef int32_t __wasi_int_t;
typedef uint32_t __wasi_uint_t;
_Static_assert(_Alignof(__wasi_int_t) == 4, "non-wasi data layout");
_Static_assert(_Alignof(__wasi_uint_t) == 4, "non-wasi data layout");
#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -43,8 +43,8 @@ extern "C" {
*/
typedef __wasi_uint_t __wasi_pointersize_t;

_Static_assert(sizeof(__wasi_pointersize_t) == 8, "witx calculated size");
_Static_assert(_Alignof(__wasi_pointersize_t) == 8, "witx calculated align");
_Static_assert(sizeof(__wasi_pointersize_t) == 4, "witx calculated size");
_Static_assert(_Alignof(__wasi_pointersize_t) == 4, "witx calculated align");

/**
* Represents a number of items
Expand Down Expand Up @@ -1025,10 +1025,10 @@ typedef struct __wasi_iovec_t {

} __wasi_iovec_t;

_Static_assert(sizeof(__wasi_iovec_t) == 16, "witx calculated size");
_Static_assert(_Alignof(__wasi_iovec_t) == 8, "witx calculated align");
_Static_assert(sizeof(__wasi_iovec_t) == 8, "witx calculated size");
_Static_assert(_Alignof(__wasi_iovec_t) == 4, "witx calculated align");
_Static_assert(offsetof(__wasi_iovec_t, buf) == 0, "witx calculated offset");
_Static_assert(offsetof(__wasi_iovec_t, buf_len) == 8, "witx calculated offset");
_Static_assert(offsetof(__wasi_iovec_t, buf_len) == 4, "witx calculated offset");

/**
* A region of memory for scatter/gather writes.
Expand All @@ -1046,10 +1046,10 @@ typedef struct __wasi_ciovec_t {

} __wasi_ciovec_t;

_Static_assert(sizeof(__wasi_ciovec_t) == 16, "witx calculated size");
_Static_assert(_Alignof(__wasi_ciovec_t) == 8, "witx calculated align");
_Static_assert(sizeof(__wasi_ciovec_t) == 8, "witx calculated size");
_Static_assert(_Alignof(__wasi_ciovec_t) == 4, "witx calculated align");
_Static_assert(offsetof(__wasi_ciovec_t, buf) == 0, "witx calculated offset");
_Static_assert(offsetof(__wasi_ciovec_t, buf_len) == 8, "witx calculated offset");
_Static_assert(offsetof(__wasi_ciovec_t, buf_len) == 4, "witx calculated offset");

/**
* Relative offset within a file.
Expand Down Expand Up @@ -3593,7 +3593,7 @@ _Static_assert(sizeof(__wasi_prestat_t) == 8, "witx calculated size");
_Static_assert(_Alignof(__wasi_prestat_t) == 4, "witx calculated align");

/**
* @defgroup wasix_64v1
* @defgroup wasix_32v1
* @{
*/

Expand Down
Loading

0 comments on commit 1605470

Please sign in to comment.