From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Ali Mohammad Pur <ali.mpfard@gmail.com>
Date: Fri, 9 Jul 2021 05:32:00 +0430
Subject: [PATCH] build: Add platform-specific stubs and implementations

---
 CMakeLists.txt           |   2 +
 src/unix/minerva-core.c | 116 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 118 insertions(+)
 create mode 100644 src/unix/minerva-core.c

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c8425957cbd4d4ba28f0c0135b8c49f00c5dd507..4684ee906aa7d477b44ca4ed7cd43cf34ab5bb34 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -441,9 +441,11 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Minerva")
     src/unix/posix-poll.c
     src/unix/no-fsevents.c
     src/unix/no-proctitle.c
+    src/unix/minerva-core.c
   )
   list(APPEND uv_libraries
     dl
+    pthread
   )
 endif()
 
diff --git a/src/unix/minerva-core.c b/src/unix/minerva-core.c
new file mode 100644
index 0000000000000000000000000000000000000000..48fa813c57c5c420bee46ea78aa674c7be4aee73
--- /dev/null
+++ b/src/unix/minerva-core.c
@@ -0,0 +1,116 @@
+#include "uv.h"
+#include "internal.h"
+
+#include <errno.h>
+#include <stddef.h>
+
+#include <net/if.h>
+#include <unistd.h>
+
+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 uint64_t uv__read_proc_memstat(const char* what) {
+  uint64_t rc;
+  char* p;
+  char buf[4096];  /* Large enough to hold all of /sys/kernel/memstat. */
+
+  if (uv__slurp("/sys/kernel/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("physical_available\":") * PAGE_SIZE;
+}
+
+
+uint64_t uv_get_total_memory(void) {
+  return (uv__read_proc_memstat("physical_allocated\":") + uv__read_proc_memstat("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 /sys/kernel/uptime first, then fallback to clock_gettime(). */
+  
+  if (0 == uv__slurp("/sys/kernel/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;
+}
+
+uint64_t uv_get_available_memory(void) {
+  return uv_get_free_memory();
+}
