@@ -0,0 +1,382 @@
From c99db7565d0208b88f81891e63fd64924f40dfe2 Mon Sep 17 00:00:00 2001
From: Leorize <leorize+oss@disroot.org>
Date: Sat, 25 May 2019 15:15:03 -0400
Subject: [PATCH] build, core, unix, test: add support for Haiku

This commit add support for Haiku, an open-source operating system
inspired by BeOS.
---
Makefile.am | 15 ++++
configure.ac | 4 +
include/uv/unix.h | 3 +-
src/unix/bsd-ifaddrs.c | 7 +-
src/unix/core.c | 4 +-
src/unix/haiku.c | 176 ++++++++++++++++++++++++++++++++++++++++
src/unix/process.c | 5 ++
test/test-fs-copyfile.c | 3 +-
test/test-fs.c | 6 +-
9 files changed, 217 insertions(+), 6 deletions(-)
create mode 100644 src/unix/haiku.c

diff --git a/Makefile.am b/Makefile.am
index 07224f3..d143d08 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -320,6 +320,10 @@ test_run_tests_CFLAGS += -D_ALL_SOURCE \
-D_LINUX_SOURCE_COMPAT
endif

+if HAIKU
+test_run_tests_CFLAGS += -D_BSD_SOURCE
+endif
+
if LINUX
test_run_tests_CFLAGS += -D_GNU_SOURCE
endif
@@ -409,6 +413,17 @@ libuv_la_SOURCES += src/unix/bsd-ifaddrs.c \
test_run_tests_LDFLAGS += -lutil
endif

+if HAIKU
+uvinclude_HEADERS += include/uv/posix.h
+libuv_la_CFLAGS += -D_BSD_SOURCE
+libuv_la_SOURCES += src/unix/bsd-ifaddrs.c \
+ src/unix/haiku.c \
+ src/unix/no-fsevents.c \
+ src/unix/no-proctitle.c \
+ src/unix/posix-hrtime.c \
+ src/unix/posix-poll.c
+endif
+
if HURD
uvinclude_HEADERS += include/uv/posix.h
libuv_la_SOURCES += src/unix/no-fsevents.c \
diff --git a/configure.ac b/configure.ac
index ad99f9a..5ab75b8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -56,6 +56,7 @@ AM_CONDITIONAL([CYGWIN], [AS_CASE([$host_os],[cygwin*], [true], [false])
AM_CONDITIONAL([DARWIN], [AS_CASE([$host_os],[darwin*], [true], [false])])
AM_CONDITIONAL([DRAGONFLY],[AS_CASE([$host_os],[dragonfly*], [true], [false])])
AM_CONDITIONAL([FREEBSD], [AS_CASE([$host_os],[*freebsd*], [true], [false])])
+AM_CONDITIONAL([HAIKU], [AS_CASE([$host_os],[haiku], [true], [false])])
AM_CONDITIONAL([HURD], [AS_CASE([$host_os],[gnu*], [true], [false])])
AM_CONDITIONAL([LINUX], [AS_CASE([$host_os],[linux*], [true], [false])])
AM_CONDITIONAL([MSYS], [AS_CASE([$host_os],[msys*], [true], [false])])
@@ -71,6 +72,9 @@ AS_CASE([$host_os], [netbsd*], [AC_CHECK_LIB([kvm], [kvm_open])])
AS_CASE([$host_os], [kfreebsd*], [
LIBS="$LIBS -lfreebsd-glue"
])
+AS_CASE([$host_os], [haiku], [
+ LIBS="$LIBS -lnetwork"
+])
AC_CHECK_HEADERS([sys/ahafs_evProds.h])
AC_CONFIG_FILES([Makefile libuv.pc])
AC_CONFIG_LINKS([test/fixtures/empty_file:test/fixtures/empty_file])
diff --git a/include/uv/unix.h b/include/uv/unix.h
index f73b7b0..c8d36e7 100644
--- a/include/uv/unix.h
+++ b/include/uv/unix.h
@@ -64,7 +64,8 @@
#elif defined(__PASE__) || \
defined(__CYGWIN__) || \
defined(__MSYS__) || \
- defined(__GNU__)
+ defined(__GNU__) || \
+ defined(__HAIKU__)
# include "uv/posix.h"
#endif

diff --git a/src/unix/bsd-ifaddrs.c b/src/unix/bsd-ifaddrs.c
index a4c6bf9..0d7bbe6 100644
--- a/src/unix/bsd-ifaddrs.c
+++ b/src/unix/bsd-ifaddrs.c
@@ -31,6 +31,10 @@
#include <net/if_dl.h>
#endif

+#if defined(__HAIKU__)
+#define IFF_RUNNING IFF_LINK
+#endif
+
static int uv__ifaddr_exclude(struct ifaddrs *ent, int exclude_type) {
if (!((ent->ifa_flags & IFF_UP) && (ent->ifa_flags & IFF_RUNNING)))
return 1;
@@ -45,7 +49,8 @@ static int uv__ifaddr_exclude(struct ifaddrs *ent, int exclude_type) {
if (exclude_type == UV__EXCLUDE_IFPHYS)
return (ent->ifa_addr->sa_family != AF_LINK);
#endif
-#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__)
+#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__DragonFly__) || \
+ defined(__HAIKU__)
/*
* On BSD getifaddrs returns information related to the raw underlying
* devices. We're not interested in this information.
diff --git a/src/unix/core.c b/src/unix/core.c
index 3bada90..1b3de43 100644
--- a/src/unix/core.c
+++ b/src/unix/core.c
@@ -579,7 +579,7 @@ int uv__nonblock_ioctl(int fd, int set) {
}


-#if !defined(__CYGWIN__) && !defined(__MSYS__)
+#if !defined(__CYGWIN__) && !defined(__MSYS__) && !defined(__HAIKU__)
int uv__cloexec_ioctl(int fd, int set) {
int r;

@@ -947,7 +947,7 @@ int uv_getrusage(uv_rusage_t* rusage) {
rusage->ru_stime.tv_sec = usage.ru_stime.tv_sec;
rusage->ru_stime.tv_usec = usage.ru_stime.tv_usec;

-#if !defined(__MVS__)
+#if !defined(__MVS__) && !defined(__HAIKU__)
rusage->ru_maxrss = usage.ru_maxrss;
rusage->ru_ixrss = usage.ru_ixrss;
rusage->ru_idrss = usage.ru_idrss;
diff --git a/src/unix/haiku.c b/src/unix/haiku.c
new file mode 100644
index 0000000..70ccdcc
--- /dev/null
+++ b/src/unix/haiku.c
@@ -0,0 +1,176 @@
+/* Copyright libuv project contributors. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include "uv.h"
+#include "internal.h"
+
+#include <FindDirectory.h> /* find_path() */
+#include <OS.h>
+
+
+void uv_loadavg(double avg[3]) {
+ avg[0] = 0;
+ avg[1] = 0;
+ avg[2] = 0;
+}
+
+
+uint64_t uv_get_constrained_memory(void) {
+ return 0;
+}
+
+
+int uv_exepath(char* buffer, size_t* size) {
+ char abspath[B_PATH_NAME_LENGTH];
+ status_t status;
+ ssize_t abspath_len;
+
+ if (buffer == NULL || size == NULL || *size == 0)
+ return UV_EINVAL;
+
+ status = find_path(B_APP_IMAGE_SYMBOL, B_FIND_PATH_IMAGE_PATH, NULL, abspath,
+ sizeof(abspath));
+ if (status != B_OK)
+ return UV__ERR(status);
+
+ abspath_len = uv__strscpy(buffer, abspath, *size);
+ *size -= 1;
+ if (abspath_len >= 0 && *size > (size_t)abspath_len)
+ *size = (size_t)abspath_len;
+
+ return 0;
+}
+
+
+uint64_t uv_get_free_memory(void) {
+ status_t status;
+ system_info sinfo;
+
+ status = get_system_info(&sinfo);
+ if (status != B_OK)
+ return 0;
+
+ return (sinfo.max_pages - sinfo.used_pages) * B_PAGE_SIZE;
+}
+
+
+uint64_t uv_get_total_memory(void) {
+ status_t status;
+ system_info sinfo;
+
+ status = get_system_info(&sinfo);
+ if (status != B_OK)
+ return 0;
+
+ return sinfo.max_pages * B_PAGE_SIZE;
+}
+
+
+int uv_resident_set_memory(size_t* rss) {
+ area_info area;
+ ssize_t cookie;
+ status_t status;
+ thread_info thread;
+
+ status = get_thread_info(find_thread(NULL), &thread);
+ if (status != B_OK)
+ return UV__ERR(status);
+
+ cookie = 0;
+ *rss = 0;
+ while (get_next_area_info(thread.team, &cookie, &area) == B_OK)
+ *rss += area.ram_size;
+
+ return 0;
+}
+
+
+int uv_uptime(double* uptime) {
+ /* system_time() returns time since booting in microseconds */
+ *uptime = (double)system_time() / 1000000;
+ return 0;
+}
+
+
+int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
+ cpu_topology_node_info* topology_infos;
+ int i;
+ status_t status;
+ system_info system;
+ uint32_t topology_count;
+ uint64_t cpuspeed;
+ uv_cpu_info_t* cpu_info;
+
+ if (cpu_infos == NULL || count == NULL)
+ return UV_EINVAL;
+
+ status = get_cpu_topology_info(NULL, &topology_count);
+ if (status != B_OK)
+ return UV__ERR(status);
+
+ topology_infos = uv__malloc(topology_count * sizeof(*topology_infos));
+ if (topology_infos == NULL)
+ return UV_ENOMEM;
+
+ status = get_cpu_topology_info(topology_infos, &topology_count);
+ if (status != B_OK) {
+ uv__free(topology_infos);
+ return UV__ERR(status);
+ }
+
+ cpuspeed = 0;
+ for (i = 0; i < (int)topology_count; i++) {
+ if (topology_infos[i].type == B_TOPOLOGY_CORE) {
+ cpuspeed = topology_infos[i].data.core.default_frequency;
+ break;
+ }
+ }
+
+ uv__free(topology_infos);
+
+ status = get_system_info(&system);
+ if (status != B_OK)
+ return UV__ERR(status);
+
+ *cpu_infos = uv__calloc(system.cpu_count, sizeof(**cpu_infos));
+ if (*cpu_infos == NULL)
+ return UV_ENOMEM;
+
+ /* CPU time and model are not exposed by Haiku. */
+ cpu_info = *cpu_infos;
+ for (i = 0; i < (int)system.cpu_count; i++) {
+ cpu_info->model = uv__strdup("unknown");
+ cpu_info->speed = (int)(cpuspeed / 1000000);
+ cpu_info++;
+ }
+ *count = system.cpu_count;
+
+ return 0;
+}
+
+void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
+ int i;
+
+ for (i = 0; i < count; i++)
+ uv__free(cpu_infos[i].model);
+
+ uv__free(cpu_infos);
+}
diff --git a/src/unix/process.c b/src/unix/process.c
index b284308..e29bf15 100644
--- a/src/unix/process.c
+++ b/src/unix/process.c
@@ -385,6 +385,11 @@ static void uv__process_child_init(const uv_process_options_t* options,
if (n == SIGKILL || n == SIGSTOP)
continue; /* Can't be changed. */

+#if defined(__HAIKU__)
+ if (n == SIGKILLTHR)
+ continue; /* Can't be changed. */
+#endif
+
if (SIG_ERR != signal(n, SIG_DFL))
continue;

diff --git a/test/test-fs-copyfile.c b/test/test-fs-copyfile.c
index cd8a2ea..918c086 100644
--- a/test/test-fs-copyfile.c
+++ b/test/test-fs-copyfile.c
@@ -24,7 +24,8 @@

#if defined(__unix__) || defined(__POSIX__) || \
defined(__APPLE__) || defined(__sun) || \
- defined(_AIX) || defined(__MVS__)
+ defined(_AIX) || defined(__MVS__) || \
+ defined(__HAIKU__)
#include <unistd.h> /* unlink, etc. */
#else
# include <direct.h>
diff --git a/test/test-fs.c b/test/test-fs.c
index c3153c7..6cc2257 100644
--- a/test/test-fs.c
+++ b/test/test-fs.c
@@ -31,7 +31,8 @@
/* FIXME we shouldn't need to branch in this file */
#if defined(__unix__) || defined(__POSIX__) || \
defined(__APPLE__) || defined(__sun) || \
- defined(_AIX) || defined(__MVS__)
+ defined(_AIX) || defined(__MVS__) || \
+ defined(__HAIKU__)
#include <unistd.h> /* unlink, rmdir, etc. */
#else
# include <winioctl.h>
@@ -1653,6 +1654,8 @@ TEST_IMPL(fs_chown) {
uv_run(loop, UV_RUN_DEFAULT);
ASSERT(fchown_cb_count == 1);

+#ifndef __HAIKU__
+ /* Haiku doesn't support hardlink */
/* sync link */
r = uv_fs_link(NULL, &req, "test_file", "test_file_link", NULL);
ASSERT(r == 0);
@@ -1670,6 +1673,7 @@ TEST_IMPL(fs_chown) {
ASSERT(r == 0);
uv_run(loop, UV_RUN_DEFAULT);
ASSERT(lchown_cb_count == 1);
+#endif

/* Close file */
r = uv_fs_close(NULL, &req, file, NULL);
--
2.21.0