Skip to content

Commit

Permalink
[libc] Enable spawn lib in riscv
Browse files Browse the repository at this point in the history
In this patch we add support for the spawn lib in riscv.

Only small changes were required, the biggest one was to use of dup3
instead of dup2, if the latter is not available. This follows our
implementation of dup2.

Differential Revision: https://reviews.llvm.org/D146145
  • Loading branch information
mikhailramalho committed Mar 16, 2023
1 parent 63ed8ab commit fe99de3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libc/config/linux/riscv64/entrypoints.txt
Expand Up @@ -431,6 +431,14 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigfillset
libc.src.signal.signal

# spawn.h entrypoints
libc.src.spawn.posix_spawn
libc.src.spawn.posix_spawn_file_actions_addclose
libc.src.spawn.posix_spawn_file_actions_adddup2
libc.src.spawn.posix_spawn_file_actions_addopen
libc.src.spawn.posix_spawn_file_actions_destroy
libc.src.spawn.posix_spawn_file_actions_init

# threads.h entrypoints
libc.src.threads.call_once
libc.src.threads.cnd_broadcast
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv64/headers.txt
Expand Up @@ -9,6 +9,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.pthread
libc.include.sched
libc.include.signal
libc.include.spawn
libc.include.stdio
libc.include.stdlib
libc.include.string
Expand Down
8 changes: 8 additions & 0 deletions libc/src/spawn/linux/posix_spawn.cpp
Expand Up @@ -14,6 +14,7 @@
#include "src/spawn/file_actions.h"

#include <fcntl.h>
#include <signal.h> // For SIGCHLD
#include <spawn.h>
#include <sys/syscall.h> // For syscall numbers.

Expand Down Expand Up @@ -50,8 +51,15 @@ cpp::optional<int> open(const char *path, int oflags, mode_t mode) {

void close(int fd) { __llvm_libc::syscall_impl(SYS_close, fd); }

// We use dup3 if dup2 is not available, similar to our implementation of dup2
bool dup2(int fd, int newfd) {
#ifdef SYS_dup2
long ret = __llvm_libc::syscall_impl(SYS_dup2, fd, newfd);
#elif defined(SYS_dup3)
long ret = __llvm_libc::syscall_impl(SYS_dup3, fd, newfd, 0);
#else
#error "SYS_dup2 and SYS_dup3 not available for the target."
#endif
return ret < 0 ? false : true;
}

Expand Down

0 comments on commit fe99de3

Please sign in to comment.