From 5c53f32b401baffb4c6dc896ca07beff2add2a42 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Fri, 9 Jul 2021 05:32:00 +0430 Subject: [PATCH 7/7] build: Add platform-specific stubs and implementations --- CMakeLists.txt | 2 + src/unix/serenity-core.c | 137 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 src/unix/serenity-core.c diff --git a/CMakeLists.txt b/CMakeLists.txt index f30ec26..6f0bf0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -365,9 +365,11 @@ if(CMAKE_SYSTEM_NAME STREQUAL "SerenityOS") src/unix/posix-poll.c src/unix/no-fsevents.c src/unix/no-proctitle.c + src/unix/serenity-core.c ) list(APPEND uv_libraries dl + pthread ) endif() diff --git a/src/unix/serenity-core.c b/src/unix/serenity-core.c new file mode 100644 index 0000000..821cf37 --- /dev/null +++ b/src/unix/serenity-core.c @@ -0,0 +1,137 @@ +#include "uv.h" +#include "internal.h" + +#include +#include + +#include +#include + +static int uv__ifaddr_exclude(void *ent, int exclude_type) { + return 1; +} + +int uv_interface_addresses(uv_interface_address_t** addresses, int* count) { + *count = 0; + *addresses = NULL; + return UV_ENOSYS; +} + + +void uv_free_interface_addresses(uv_interface_address_t* addresses, + int count) { + int i; + + for (i = 0; i < count; i++) { + uv__free(addresses[i].name); + } + + uv__free(addresses); +} + +static int uv__slurp(const char* filename, char* buf, size_t len) { + ssize_t n; + int fd; + + assert(len > 0); + + fd = uv__open_cloexec(filename, O_RDONLY); + if (fd < 0) + return fd; + + do + n = read(fd, buf, len - 1); + while (n == -1 && errno == EINTR); + + if (uv__close_nocheckstdio(fd)) + abort(); + + if (n < 0) + return UV__ERR(errno); + + buf[n] = '\0'; + + return 0; +} + + +static uint64_t uv__read_proc_memstat(const char* what) { + uint64_t rc; + char* p; + char buf[4096]; /* Large enough to hold all of /proc/memstat. */ + + if (uv__slurp("/proc/memstat", buf, sizeof(buf))) + return 0; + + p = strstr(buf, what); + + if (p == NULL) + return 0; + + p += strlen(what); + + rc = 0; + sscanf(p, "%" PRIu64, &rc); + + return rc; +} + +uint64_t uv_get_free_memory(void) { + return uv__read_proc_memstat("user_physical_available\":") * PAGE_SIZE; +} + + +uint64_t uv_get_total_memory(void) { + return (uv__read_proc_memstat("user_physical_allocated\":") + uv__read_proc_memstat("user_physical_available\":")) * PAGE_SIZE; +} + +void uv_loadavg(double avg[3]) { + avg[0] = 0.0f; + avg[1] = 0.0f; + avg[2] = 0.0f; +} + +int uv_uptime(double* uptime) { + char buf[128]; + struct timespec now; + int r; + + /* Try /proc/uptime first, then fallback to clock_gettime(). */ + + if (0 == uv__slurp("/proc/uptime", buf, sizeof(buf))) + if (1 == sscanf(buf, "%lf", uptime)) + return 0; + + r = clock_gettime(CLOCK_MONOTONIC, &now); + if (r) + return UV__ERR(errno); + + *uptime = now.tv_sec; + return 0; +} + +uint64_t uv_get_constrained_memory(void) { + return 0; +} + +int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) { + *cpu_infos = NULL; + *count = 0; + return 0; +} + +int uv_exepath(char* buffer, size_t* size) { + if (buffer == NULL || size == NULL || *size == 0) + return UV_EINVAL; + + int rc = readlink("/proc/self/exe", buffer, *size); + if (rc < 0) + return UV__ERR(errno); + *size = rc; + return 0; +} + +int uv_resident_set_memory(size_t* rss) { + *rss = 0; + return 0; +} -- 2.32.0