Skip to content

Commit

Permalink
Added the vfork functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
john-sharratt committed Sep 20, 2022
1 parent 91c3c79 commit 8e96199
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ __year_to_secs
_environ
_exit
_flushlbf
_fork_internal
_initialize
_pthread_cleanup_pop
_pthread_cleanup_push
Expand Down
1 change: 1 addition & 0 deletions expected/wasm64-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ __year_to_secs
_environ
_exit
_flushlbf
_fork_internal
_initialize
_pthread_cleanup_pop
_pthread_cleanup_push
Expand Down
6 changes: 6 additions & 0 deletions libc-bottom-half/headers/public/wasi/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -4552,6 +4552,12 @@ __wasi_errno_t __wasi_proc_raise(
* number then its the current process and the $pid represents the child.
*/
__wasi_errno_t __wasi_proc_fork(
/**
* Indicates if the memory will be copied into the new process
* (if it is not copied this then becomes similar to `vfork` in
* that the current process pauses until `proc_exec` is called)
*/
__wasi_bool_t copy_memory,
__wasi_pid_t *retptr0
) __attribute__((__warn_unused_result__));
/**
Expand Down
5 changes: 3 additions & 2 deletions libc-bottom-half/sources/__wasilibc_real.c
Original file line number Diff line number Diff line change
Expand Up @@ -967,15 +967,16 @@ __wasi_errno_t __wasi_proc_raise(
return (uint16_t) ret;
}

int32_t __imported_wasix_64v1_proc_fork(int64_t arg0) __attribute__((
int32_t __imported_wasix_64v1_proc_fork(int32_t arg0, int64_t arg1) __attribute__((
__import_module__("wasix_64v1"),
__import_name__("proc_fork")
));

__wasi_errno_t __wasi_proc_fork(
__wasi_bool_t copy_memory,
__wasi_pid_t *retptr0
){
int32_t ret = __imported_wasix_64v1_proc_fork((intptr_t) retptr0);
int32_t ret = __imported_wasix_64v1_proc_fork((int32_t) copy_memory, (intptr_t) retptr0);
return (uint16_t) ret;
}

Expand Down
3 changes: 2 additions & 1 deletion libc-top-half/musl/include/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ int pause(void);
#endif

pid_t fork(void);
pid_t _Fork(void);
pid_t _fork_internal(int copy_mem);
pid_t _Fork(int copy_mem);
int execve(const char *, char *const [], char *const []);
int execv(const char *, char *const []);
int execle(const char *, const char *, ...);
Expand Down
4 changes: 2 additions & 2 deletions libc-top-half/musl/src/process/_Fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
static void dummy(int x) { }
weak_alias(dummy, __aio_atfork);

pid_t _Fork(void)
pid_t _Fork(int copy_mem)
{
pid_t ret;
sigset_t set;
Expand All @@ -28,7 +28,7 @@ pid_t _Fork(void)
#endif
#else
__wasi_pid_t pid = -1;
int err = __wasi_proc_fork(&pid);
int err = __wasi_proc_fork(copy_mem, &pid);
if (err != 0) {
ret = -err;
} else {
Expand Down
7 changes: 6 additions & 1 deletion libc-top-half/musl/src/process/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ weak_alias(dummy_0, __tl_unlock);
#endif

pid_t fork(void)
{
return _fork_internal(1);
}

pid_t _fork_internal(int copy_mem)
{
sigset_t set;
__fork_handler(-1);
Expand All @@ -68,7 +73,7 @@ pid_t fork(void)
__tl_lock();
}
pthread_t self=__pthread_self(), next=self->next;
pid_t ret = _Fork();
pid_t ret = _Fork(copy_mem);
int errno_save = errno;
if (need_locks) {
if (!ret) {
Expand Down
2 changes: 1 addition & 1 deletion libc-top-half/musl/src/process/vfork.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ pid_t vfork(void)
return syscall(SYS_clone, SIGCHLD, 0);
#endif
#else
return fork();
return _fork_internal(0);
#endif
}
2 changes: 1 addition & 1 deletion tools/wasix-headers/WASI

0 comments on commit 8e96199

Please sign in to comment.