Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ if(CMAKE_SYSTEM_NAME STREQUAL "AIX")
src/unix/aix-common.c)
endif()

if (CMAKE_SYSTEM_NAME STREQUAL "WASI")
list(APPEND uv_defines _GNU_SOURCE)
list(APPEND uv_sources
src/unix/random-getrandom.c
src/unix/posix-hrtime.c
src/unix/posix-poll.c
src/unix/no-fsevents.c
src/unix/no-proctitle.c)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Android")
list(APPEND uv_defines _GNU_SOURCE)
list(APPEND uv_libraries dl)
Expand Down
3 changes: 3 additions & 0 deletions include/uv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1808,12 +1808,15 @@ UV_EXTERN int uv_random(uv_loop_t* loop,
# define UV_IF_NAMESIZE (16 + 1)
#endif

// TODO: missing wasix entrypoint
#ifndef __wasi__
UV_EXTERN int uv_if_indextoname(unsigned int ifindex,
char* buffer,
size_t* size);
UV_EXTERN int uv_if_indextoiid(unsigned int ifindex,
char* buffer,
size_t* size);
#endif

UV_EXTERN int uv_exepath(char* buffer, size_t* size);

Expand Down
3 changes: 2 additions & 1 deletion include/uv/unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
defined(__MSYS__) || \
defined(__HAIKU__) || \
defined(__QNX__) || \
defined(__GNU__)
defined(__GNU__) || \
defined(__wasi__)
# include "uv/posix.h"
#endif

Expand Down
38 changes: 33 additions & 5 deletions src/unix/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,24 @@ static int uv__loop_alive(const uv_loop_t* loop) {


static int uv__backend_timeout(const uv_loop_t* loop) {
int timeout;

if (loop->stop_flag == 0 &&
/* uv__loop_alive(loop) && */
(uv__has_active_handles(loop) || uv__has_active_reqs(loop)) &&
uv__queue_empty(&loop->pending_queue) &&
uv__queue_empty(&loop->idle_handles) &&
(loop->flags & UV_LOOP_REAP_CHILDREN) == 0 &&
loop->closing_handles == NULL)
return uv__next_timeout(loop);
loop->closing_handles == NULL) {
timeout = uv__next_timeout(loop);
#if defined(__wasi__)
if (!uv__queue_empty(&loop->process_handles) &&
(timeout == -1 || timeout > 10)) {
timeout = 10;
}
#endif
return timeout;
}
return 0;
}

Expand Down Expand Up @@ -459,6 +469,11 @@ int uv_run(uv_loop_t* loop, uv_run_mode mode) {

uv__io_poll(loop, timeout);

#if defined(__wasi__)
if (!uv__queue_empty(&loop->process_handles))
uv__wait_children(loop);
#endif

/* Process immediate callbacks (e.g. write_cb) a small fixed number of
* times to avoid loop starvation.*/
for (r = 0; r < 8 && !uv__queue_empty(&loop->pending_queue); r++)
Expand Down Expand Up @@ -722,7 +737,8 @@ ssize_t uv__recvmsg(int fd, struct msghdr* msg, int flags) {
defined(__FreeBSD__) || \
defined(__NetBSD__) || \
defined(__OpenBSD__) || \
defined(__linux__)
defined(__linux__) || \
defined(__wasi__)
ssize_t rc;
rc = recvmsg(fd, msg, flags | MSG_CMSG_CLOEXEC);
if (rc == -1)
Expand Down Expand Up @@ -1611,6 +1627,7 @@ int uv_cpumask_size(void) {
}

int uv_os_getpriority(uv_pid_t pid, int* priority) {
#ifndef __wasi__
int r;

if (priority == NULL)
Expand All @@ -1624,16 +1641,22 @@ int uv_os_getpriority(uv_pid_t pid, int* priority) {

*priority = r;
return 0;
#else
*priority = 0;
return 0;
#endif
}


int uv_os_setpriority(uv_pid_t pid, int priority) {
#ifndef __wasi__
if (priority < UV_PRIORITY_HIGHEST || priority > UV_PRIORITY_LOW)
return UV_EINVAL;

if (setpriority(PRIO_PROCESS, (int) pid, priority) != 0)
return UV__ERR(errno);

#endif

return 0;
}

Expand All @@ -1644,6 +1667,7 @@ int uv_os_setpriority(uv_pid_t pid, int priority) {
* So the output parameter priority is actually the nice value.
*/
int uv_thread_getpriority(uv_thread_t tid, int* priority) {
#ifndef __wasi__
int r;
int policy;
struct sched_param param;
Expand Down Expand Up @@ -1671,6 +1695,10 @@ int uv_thread_getpriority(uv_thread_t tid, int* priority) {

*priority = param.sched_priority;
return 0;
#else
*priority = 0; /* WASIX does not support thread priority. */
return 0;
#endif
}

#ifdef __linux__
Expand All @@ -1695,7 +1723,7 @@ static int set_nice_for_calling_thread(int priority) {
* If the function fails, the return value is non-zero.
*/
int uv_thread_setpriority(uv_thread_t tid, int priority) {
#if !defined(__GNU__)
#if !defined(__GNU__) && !defined(__wasi__)
int r;
int min;
int max;
Expand Down
12 changes: 11 additions & 1 deletion src/unix/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
defined(__HAIKU__) || \
defined(__QNX__)
# include <sys/statvfs.h>
#else
#elif !defined(__wasi__)
# include <sys/statfs.h>
#endif

Expand Down Expand Up @@ -677,6 +677,7 @@ static int uv__fs_closedir(uv_fs_t* req) {
return 0;
}

#ifndef __wasi__
static int uv__fs_statfs(uv_fs_t* req) {
uv_statfs_t* stat_fs;
#if defined(__sun) || \
Expand Down Expand Up @@ -719,6 +720,7 @@ static int uv__fs_statfs(uv_fs_t* req) {
req->ptr = stat_fs;
return 0;
}
#endif

static ssize_t uv__fs_pathmax_size(const char* path) {
ssize_t pathmax;
Expand Down Expand Up @@ -1327,13 +1329,15 @@ static ssize_t uv__fs_copyfile(uv_fs_t* req) {
goto out;
}

#ifndef __wasi__
/*
* Change the ownership and permissions of the destination file to match the
* source file.
* `cp -p` does not care about errors here, so we don't either. Reuse the
* `result` variable to silence a -Wunused-result warning.
*/
result = fchown(dstfd, src_statsbuf.st_uid, src_statsbuf.st_gid);
#endif

if (fchmod(dstfd, src_statsbuf.st_mode) == -1) {
err = UV__ERR(errno);
Expand Down Expand Up @@ -1696,12 +1700,16 @@ static void uv__fs_work(struct uv__work* w) {
switch (req->fs_type) {
X(ACCESS, access(req->path, req->flags));
X(CHMOD, chmod(req->path, req->mode));
#ifndef __wasi__
X(CHOWN, chown(req->path, req->uid, req->gid));
#endif
X(CLOSE, uv__fs_close(req->file));
X(COPYFILE, uv__fs_copyfile(req));
X(FCHMOD, fchmod(req->file, req->mode));
#ifndef __wasi__
X(FCHOWN, fchown(req->file, req->uid, req->gid));
X(LCHOWN, lchown(req->path, req->uid, req->gid));
#endif
X(FDATASYNC, uv__fs_fdatasync(req));
X(FSTAT, uv__fs_fstat(req->file, &req->statbuf));
X(FSYNC, uv__fs_fsync(req));
Expand All @@ -1725,7 +1733,9 @@ static void uv__fs_work(struct uv__work* w) {
X(RMDIR, rmdir(req->path));
X(SENDFILE, uv__fs_sendfile(req));
X(STAT, uv__fs_stat(req->path, &req->statbuf));
#ifndef __wasi__
X(STATFS, uv__fs_statfs(req));
#endif
X(SYMLINK, symlink(req->path, req->new_path));
X(UNLINK, unlink(req->path));
X(UTIME, uv__fs_utime(req));
Expand Down
2 changes: 2 additions & 0 deletions src/unix/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,12 @@ struct uv__statx {
* Libuv uses uv__nonblock_fcntl() directly sometimes so ensure that it
* commutes with uv__nonblock().
*/
#if !defined(__wasi__)
#if defined(__linux__) && O_NDELAY != O_NONBLOCK
#undef uv__nonblock
#define uv__nonblock uv__nonblock_fcntl
#endif
#endif

/* core */
int uv__cloexec(int fd, int set);
Expand Down
31 changes: 31 additions & 0 deletions src/unix/no-proctitle.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,26 @@

#include <errno.h>
#include <stddef.h>
#include <string.h>

static char* uv__process_argv0;

char** uv_setup_args(int argc, char** argv) {
if (uv__process_argv0 != NULL) {
uv__free(uv__process_argv0);
uv__process_argv0 = NULL;
}

if (argc > 0 && argv != NULL && argv[0] != NULL) {
uv__process_argv0 = uv__strdup(argv[0]);
}

return argv;
}

void uv__process_title_cleanup(void) {
uv__free(uv__process_argv0);
uv__process_argv0 = NULL;
}

int uv_set_process_title(const char* title) {
Expand All @@ -43,3 +57,20 @@ int uv_get_process_title(char* buffer, size_t size) {
buffer[0] = '\0';
return 0;
}

int uv_exepath(char* buffer, size_t* size) {
ssize_t copied;

if (buffer == NULL || size == NULL || *size == 0)
return UV_EINVAL;

if (uv__process_argv0 == NULL)
return UV_ENOENT;

copied = uv__strscpy(buffer, uv__process_argv0, *size);
*size -= 1;
if (copied >= 0 && *size > (size_t) copied)
*size = (size_t) copied;

return copied < 0 ? UV_ENOBUFS : 0;
}
Loading