Skip to content

Commit

Permalink
Overlook the access modes of STDIN, STDOUT and STDERR
Browse files Browse the repository at this point in the history
  • Loading branch information
lum1n0us committed Aug 8, 2024
1 parent 8a2e151 commit b1e2ec3
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,27 @@ fd_determine_type_rights(os_file_handle fd, __wasi_filetype_t *type,
__wasi_rights_t *rights_inheriting)
{
struct __wasi_filestat_t buf;
__wasi_errno_t error = os_fstat(fd, &buf);
__wasi_errno_t error;

if (os_is_stdin_handle(fd)) {
*rights_base = RIGHTS_STDIN;
*rights_inheriting = RIGHTS_STDIN;
return __WASI_ESUCCESS;
}

if (os_is_stdout_handle(fd)) {
*rights_base = RIGHTS_STDOUT;
*rights_inheriting = RIGHTS_STDOUT;
return __WASI_ESUCCESS;
}

if (os_is_stderr_handle(fd)) {
*rights_base = RIGHTS_STDERR;
*rights_inheriting = RIGHTS_STDERR;
return __WASI_ESUCCESS;
}

error = os_fstat(fd, &buf);
if (error != __WASI_ESUCCESS)
return error;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@
#define RIGHTS_CHARACTER_DEVICE_BASE RIGHTS_ALL
#define RIGHTS_CHARACTER_DEVICE_INHERITING RIGHTS_ALL


#define RIGHTS_STDIN \
(__WASI_RIGHT_FD_ADVISE | __WASI_RIGHT_FD_FILESTAT_GET | \
__WASI_RIGHT_FD_READ | __WASI_RIGHT_POLL_FD_READWRITE)

#define RIGHTS_STDOUT \
(__WASI_RIGHT_FD_ADVISE | __WASI_RIGHT_FD_DATASYNC | \
__WASI_RIGHT_FD_FILESTAT_GET | __WASI_RIGHT_FD_DATASYNC | \
__WASI_RIGHT_FD_WRITE | __WASI_RIGHT_POLL_FD_READWRITE)

#define RIGHTS_STDERR RIGHTS_STDOUT

// Only allow directory operations on directories. Directories can only
// yield file descriptors to other directories and files.
#define RIGHTS_DIRECTORY_BASE \
Expand Down
39 changes: 30 additions & 9 deletions core/shared/platform/common/posix/posix_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@
#define CONFIG_HAS_O_SYNC
#endif

#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif

#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif

#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif

// Converts a POSIX timespec to a WASI timestamp.
static __wasi_timestamp_t
convert_timespec(const struct timespec *ts)
Expand Down Expand Up @@ -858,30 +870,39 @@ os_isatty(os_file_handle handle)
#endif
}

bool
os_is_stdin_handle(os_file_handle fd)
{
return fd == STDIN_FILENO;
}

bool
os_is_stdout_handle(os_file_handle fd)
{
return fd == STDOUT_FILENO;
}

bool
os_is_stderr_handle(os_file_handle fd)
{
return fd == STDERR_FILENO;
}

os_file_handle
os_convert_stdin_handle(os_raw_file_handle raw_stdin)
{
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
return raw_stdin >= 0 ? raw_stdin : STDIN_FILENO;
}

os_file_handle
os_convert_stdout_handle(os_raw_file_handle raw_stdout)
{
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
return raw_stdout >= 0 ? raw_stdout : STDOUT_FILENO;
}

os_file_handle
os_convert_stderr_handle(os_raw_file_handle raw_stderr)
{
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
return raw_stderr >= 0 ? raw_stderr : STDERR_FILENO;
}

Expand Down
39 changes: 30 additions & 9 deletions core/shared/platform/esp-idf/espidf_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@
#define CONFIG_HAS_O_SYNC
#endif

#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif

#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif

#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif

// Converts a POSIX timespec to a WASI timestamp.
static __wasi_timestamp_t
convert_timespec(const struct timespec *ts)
Expand Down Expand Up @@ -858,30 +870,39 @@ os_isatty(os_file_handle handle)
#endif
}

bool
os_is_stdin_handle(os_file_handle fd)
{
return fd == STDIN_FILENO;
}

bool
os_is_stdout_handle(os_file_handle fd)
{
return fd == STDOUT_FILENO;
}

bool
os_is_stderr_handle(os_file_handle fd)
{
return fd == STDERR_FILENO;
}

os_file_handle
os_convert_stdin_handle(os_raw_file_handle raw_stdin)
{
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
return raw_stdin >= 0 ? raw_stdin : STDIN_FILENO;
}

os_file_handle
os_convert_stdout_handle(os_raw_file_handle raw_stdout)
{
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
return raw_stdout >= 0 ? raw_stdout : STDOUT_FILENO;
}

os_file_handle
os_convert_stderr_handle(os_raw_file_handle raw_stderr)
{
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
return raw_stderr >= 0 ? raw_stderr : STDERR_FILENO;
}

Expand Down
27 changes: 27 additions & 0 deletions core/shared/platform/include/platform_api_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,33 @@ os_convert_stdout_handle(os_raw_file_handle raw_stdout);
os_file_handle
os_convert_stderr_handle(os_raw_file_handle raw_stderr);

/**
*
* @param fd a file handle
*
* @return true if it is stdin
*/
bool
os_is_stdin_handle(os_file_handle fd);

/**
*
* @param fd a file handle
*
* @return true if it is stdout
*/
bool
os_is_stdout_handle(os_file_handle fd);

/**
*
* @param fd a file handle
*
* @return true if it is stderr
*/
bool
os_is_stderr_handle(os_file_handle fd);

/**
* Open a directory stream for the provided directory handle. The returned
* directory stream will be positioned at the first entry in the directory.
Expand Down
1 change: 1 addition & 0 deletions core/shared/platform/linux/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/resource.h>
#include <unistd.h>

#ifdef __cplusplus
extern "C" {
Expand Down
18 changes: 18 additions & 0 deletions core/shared/platform/windows/win_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,24 @@ create_stdio_handle(HANDLE raw_stdio_handle, DWORD stdio)
return stdio_handle;
}

bool
os_is_stdin_handle(os_file_handle fd)
{
return fd->raw.handle == GetStdHandle(STD_INPUT_HANDLE);
}

bool
os_is_stdout_handle(os_file_handle fd)
{
return fd->raw.handle == GetStdHandle(STD_OUTPUT_HANDLE);
}

bool
os_is_stderr_handle(os_file_handle fd)
{
return fd->raw.handle == GetStdHandle(STD_ERROR_HANDLE);
}

os_file_handle
os_convert_stdin_handle(os_raw_file_handle raw_stdin)
{
Expand Down

0 comments on commit b1e2ec3

Please sign in to comment.