Skip to content

Commit

Permalink
Merge branch 'cloexec' into 'main'
Browse files Browse the repository at this point in the history
Support for `CLOEXEC` with extended `fcntl(...)` interface

See merge request repositories/wasi_ext_lib!13
  • Loading branch information
k-juszczyk committed Aug 24, 2023
2 parents 548a2f6 + 0a848f3 commit 19125e8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
21 changes: 21 additions & 0 deletions c_lib/wasi_ext_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,27 @@ int wasi_ext_fcntl(int fd, enum FcntlCommand cmd, void *arg) {
// like F_DUPFD, return allocated fd
return min_fd;
}
case F_GETFD: {
__wasi_fdstat_t stat;
err = __wasi_fd_fdstat_get(fd, &stat);

if (__WASI_ERRNO_SUCCESS != err) {
return -err;
}

__wasi_fdflags_t flags = stat.fs_flags & WASI_EXT_FDFLAG_MASK;

return (int)flags;
}
case F_SETFD: {
__wasi_fdflags_t flags = *((__wasi_fdflags_t *)arg);
// set control bit to enable extended flags processing
flags |= WASI_EXT_FDFLAG_CTRL_BIT;

err = __wasi_fd_fdstat_set_flags(fd, flags);

return -err;
}
}

return -EINVAL;
Expand Down
9 changes: 8 additions & 1 deletion c_lib/wasi_ext_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

#define _MAX_FD_NUM 1024

#include <wasi/api.h>

#include <stdlib.h>

// Ioctl magic numbers
Expand All @@ -43,7 +45,12 @@ const unsigned int TIOCSRAW = _IOW(1, 1, 4);
const unsigned int TIOCSECHO = _IOW(1, 2, 4);

// Fnctl commands
enum FcntlCommand { F_MVFD };
enum FcntlCommand { F_MVFD, F_GETFD, F_SETFD };

// Extended fs_fdflags
const __wasi_fdflags_t WASI_EXT_FDFLAG_CTRL_BIT = 0x0020;
const __wasi_fdflags_t WASI_EXT_FDFLAG_MASK = 0xffc0;
const __wasi_fdflags_t WASI_EXT_FDFLAG_CLOEXEC = 0x0040;

const int STDIN = 0;
const int STDOUT = 1;
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ use wasi_ext_lib_generated::{
Redirect_Data, Redirect_Data_Path, STDIN, STDOUT,
};

pub use wasi_ext_lib_generated::{
WASI_EXT_FDFLAG_CLOEXEC, WASI_EXT_FDFLAG_CTRL_BIT, WASI_EXT_FDFLAG_MASK,
};

#[cfg(feature = "hterm")]
pub use wasi_ext_lib_generated::{
WasiEvents, TIOCGWINSZ, TIOCSECHO, TIOCSRAW, WASI_EVENTS_MASK_SIZE, WASI_EVENTS_NUM,
Expand Down Expand Up @@ -114,6 +118,8 @@ impl From<&Redirect> for wasi_ext_lib_generated::Redirect {
pub enum FcntlCommand {
// like F_DUPFD but it move fd insted of duplicating
F_MVFD { min_fd_num: wasi::Fd },
F_GETFD,
F_SETFD { flags: wasi::Fdflags },
}

pub fn chdir<P: AsRef<Path>>(path: P) -> Result<(), ExitCode> {
Expand Down Expand Up @@ -329,6 +335,22 @@ pub fn fcntl(fd: wasi::Fd, cmd: FcntlCommand) -> Result<i32, ExitCode> {
(&mut min_fd as *mut u32) as *mut c_void,
)
},
FcntlCommand::F_GETFD => unsafe {
let null_ptr = ptr::null_mut::<c_void>();
wasi_ext_lib_generated::wasi_ext_fcntl(
fd as c_int,
wasi_ext_lib_generated::FcntlCommand_F_GETFD,
null_ptr,
)
},
FcntlCommand::F_SETFD { flags } => unsafe {
let mut flags = flags;
wasi_ext_lib_generated::wasi_ext_fcntl(
fd as c_int,
wasi_ext_lib_generated::FcntlCommand_F_SETFD,
(&mut flags as *mut wasi::Fdflags) as *mut c_void,
)
},
};

if result < 0 {
Expand Down

0 comments on commit 19125e8

Please sign in to comment.