From 70cbc8dc33fcda173b352d6c44c362393a7a8f5c Mon Sep 17 00:00:00 2001
From: Sergei Reznikov <diver@gelios.net>
Date: Sun, 14 Mar 2021 18:05:04 -0400
Subject: Haiku fixes


diff --git a/auto/cc/gcc b/auto/cc/gcc
index a5c5c18..ed07ec3 100644
--- a/auto/cc/gcc
+++ b/auto/cc/gcc
@@ -140,7 +140,7 @@ fi
 # -W requires at least -O
 CFLAGS="$CFLAGS ${NGX_GCC_OPT:--O} -W"
 
-CFLAGS="$CFLAGS -Wall -Wpointer-arith"
+CFLAGS="$CFLAGS -Wall -Wpointer-arith -Wno-unused-but-set-variable"
 #CFLAGS="$CFLAGS -Wconversion"
 #CFLAGS="$CFLAGS -Winline"
 #CFLAGS="$CFLAGS -Wmissing-prototypes"
diff --git a/auto/install b/auto/install
index c764fdd..0604d7e 100644
--- a/auto/install
+++ b/auto/install
@@ -154,8 +154,8 @@ install:	build $NGX_INSTALL_PERL_MODULES
 	test -d '\$(DESTDIR)`dirname "$NGX_HTTP_LOG_PATH"`' \\
 		|| mkdir -p '\$(DESTDIR)`dirname "$NGX_HTTP_LOG_PATH"`'
 
-	test -d '\$(DESTDIR)$NGX_PREFIX/html' \\
-		|| cp -R $NGX_HTML '\$(DESTDIR)$NGX_PREFIX'
+	test -d '\$(DESTDIR)$NGX_DOC_PREFIX/html' \\
+		|| cp -R $NGX_HTML '\$(DESTDIR)$NGX_DOC_PREFIX'
 END
 
 
diff --git a/auto/options b/auto/options
index 80be906..491600b 100644
--- a/auto/options
+++ b/auto/options
@@ -13,6 +13,7 @@ NGX_CONF_PATH=
 NGX_ERROR_LOG_PATH=
 NGX_PID_PATH=
 NGX_LOCK_PATH=
+NGX_DOC_PATH=
 NGX_USER=
 NGX_GROUP=
 NGX_BUILD=
@@ -190,6 +191,7 @@ do
         --sbin-path=*)                   NGX_SBIN_PATH="$value"     ;;
         --modules-path=*)                NGX_MODULES_PATH="$value"  ;;
         --conf-path=*)                   NGX_CONF_PATH="$value"     ;;
+        --doc-prefix=*)                  NGX_DOC_PREFIX="$value"    ;;
         --error-log-path=*)              NGX_ERROR_LOG_PATH="$value";;
         --pid-path=*)                    NGX_PID_PATH="$value"      ;;
         --lock-path=*)                   NGX_LOCK_PATH="$value"     ;;
@@ -420,6 +422,7 @@ cat << END
   --sbin-path=PATH                   set nginx binary pathname
   --modules-path=PATH                set modules path
   --conf-path=PATH                   set nginx.conf pathname
+  --doc-prefix=PATH                  set documentation
   --error-log-path=PATH              set error log pathname
   --pid-path=PATH                    set nginx.pid pathname
   --lock-path=PATH                   set nginx.lock pathname
@@ -603,6 +606,7 @@ NGX_SBIN_PATH=${NGX_SBIN_PATH:-sbin/nginx}
 NGX_MODULES_PATH=${NGX_MODULES_PATH:-modules}
 NGX_CONF_PATH=${NGX_CONF_PATH:-conf/nginx.conf}
 NGX_CONF_PREFIX=`dirname $NGX_CONF_PATH`
+NGX_DOC_PREFIX${NGX_DOC_PREFIX:-$NGX_PREFIX}
 NGX_PID_PATH=${NGX_PID_PATH:-logs/nginx.pid}
 NGX_LOCK_PATH=${NGX_LOCK_PATH:-logs/nginx.lock}
 
diff --git a/auto/os/conf b/auto/os/conf
index 7c6cb69..d770822 100644
--- a/auto/os/conf
+++ b/auto/os/conf
@@ -77,6 +77,24 @@ case "$NGX_PLATFORM" in
         CC_AUX_FLAGS="$CC_AUX_FLAGS -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64"
     ;;
 
+    Haiku*)
+        # Haiku
+        have=NGX_HAIKU . auto/have_headers
+        CORE_INCS="$UNIX_INCS"
+        CORE_DEPS="$UNIX_DEPS $POSIX_DEPS"
+        CORE_SRCS="$UNIX_SRCS $HAIKU_SRCS"
+        CORE_LIBS="$CORE_LIBS -lnetwork"
+        NGX_PREFIX="${NGX_PREFIX:-`finddir B_SYSTEM_NONPACKAGED_DIRECTORY`}"
+        NGX_SBIN_PATH="${NGX_SBIN_PATH:-$NGX_PREFIX/bin/nginx}"
+        # XXX: move those to auto/options?
+        #NGX_CONF_PREFIX="`finddir B_SYSTEM_SETTINGS_DIRECTORY`/nginx"
+        #NGX_CONF_PATH="$NGX_CONF_PREFIX/nginx.conf"
+        #NGX_PID_PATH="`finddir B_COMMON_VAR_DIRECTORY`/nginx.pid"
+        #NGX_LOCK_PATH="`finddir B_COMMON_VAR_DIRECTORY`/nginx.lock"
+        #NGX_ERROR_LOG_PATH="`finddir B_SYSTEM_LOG_DIRECTORY`/error.log"
+        #NGX_HTTP_LOG_PATH="`finddir B_SYSTEM_LOG_DIRECTORY`/access.log"
+    ;;
+
     *)
         CORE_INCS="$UNIX_INCS"
         CORE_DEPS="$UNIX_DEPS $POSIX_DEPS"
@@ -88,7 +106,7 @@ esac
 
 case "$NGX_MACHINE" in
 
-    i386 | i686 | i86pc)
+    i386 | i686 | i86pc | BePC)
         have=NGX_HAVE_NONALIGNED . auto/have
         NGX_MACH_CACHE_LINE=32
     ;;
diff --git a/auto/sources b/auto/sources
index 3dad111..e4eb087 100644
--- a/auto/sources
+++ b/auto/sources
@@ -208,6 +208,7 @@ DARWIN_DEPS="src/os/unix/ngx_darwin_config.h src/os/unix/ngx_darwin.h"
 DARWIN_SRCS=src/os/unix/ngx_darwin_init.c
 DARWIN_SENDFILE_SRCS=src/os/unix/ngx_darwin_sendfile_chain.c
 
+HAIKU_SRCS=src/os/unix/ngx_haiku_errno.c
 
 WIN32_INCS="$CORE_INCS $EVENT_INCS src/os/win32"
 
diff --git a/auto/summary b/auto/summary
index 9aa776e..c02bdf0 100644
--- a/auto/summary
+++ b/auto/summary
@@ -49,6 +49,7 @@ cat << END
   nginx modules path: "$NGX_MODULES_PATH"
   nginx configuration prefix: "$NGX_CONF_PREFIX"
   nginx configuration file: "$NGX_CONF_PATH"
+  nginx documentation prefix: "$NGX_DOC_PREFIX"
   nginx pid file: "$NGX_PID_PATH"
 END
 
diff --git a/auto/unix b/auto/unix
index 9c402e7..2e7e72c 100644
--- a/auto/unix
+++ b/auto/unix
@@ -278,6 +278,18 @@ if [ $ngx_found = no ]; then
 fi
 
 
+ngx_feature="setpriority()"
+ngx_feature_name="NGX_HAVE_SETPRIORITY"
+ngx_feature_run=no
+ngx_feature_incs="#include <unistd.h>
+                  #include <sys/types.h>
+                  #include <sys/resource.h>"
+ngx_feature_path=
+ngx_feature_libs=
+ngx_feature_test="setpriority(PRIO_PROCESS, getpid(), 0)"
+. auto/feature
+
+
 ngx_feature="sched_yield()"
 ngx_feature_name="NGX_HAVE_SCHED_YIELD"
 ngx_feature_run=no
diff --git a/src/os/unix/ngx_errno.c b/src/os/unix/ngx_errno.c
index ca23b2d..daebbf3 100644
--- a/src/os/unix/ngx_errno.c
+++ b/src/os/unix/ngx_errno.c
@@ -9,6 +9,7 @@
 #include <ngx_core.h>
 
 
+#if !(NGX_HAIKU)
 static ngx_str_t   ngx_unknown_error = ngx_string("Unknown error");
 
 
@@ -208,3 +209,5 @@ failed:
 }
 
 #endif
+
+#endif
diff --git a/src/os/unix/ngx_haiku_errno.c b/src/os/unix/ngx_haiku_errno.c
new file mode 100644
index 0000000..05d793c
--- /dev/null
+++ b/src/os/unix/ngx_haiku_errno.c
@@ -0,0 +1,158 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) Nginx, Inc.
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+
+
+/*
+ * The strerror() messages are copied because:
+ *
+ * 1) strerror() and strerror_r() functions are not Async-Signal-Safe,
+ *    therefore, they cannot be used in signal handlers;
+ *
+ * 2) a direct sys_errlist[] array may be used instead of these functions,
+ *    but Linux linker warns about its usage:
+ *
+ * warning: `sys_errlist' is deprecated; use `strerror' or `strerror_r' instead
+ * warning: `sys_nerr' is deprecated; use `strerror' or `strerror_r' instead
+ *
+ *    causing false bug reports.
+ *
+ * We avoid using an indexed array because:
+ *
+ * 1) on Haiku at least, errors are declared as negative numbers.
+ *
+ * 2) even though there is a posix_error_mapper static library
+ *    on Haiku that works around this, the converted errors are
+ *    around 0x7fff0000 anyway.
+ *
+ * 3) the POSIX error codes are not all defined as B_POSIX_ERROR_BASE + n
+ *    on Haiku anyway.
+ */
+
+
+typedef struct {
+    int              error;
+    ngx_str_t        str;
+} ngx_errmap_t;
+
+static ngx_errmap_t   ngx_sys_errmap[] = {
+/*
+ * This list should be kept in sync with src/os/unix/ngx_errno.h and
+ * is generated by:
+
+awk 'BEGIN {d=0}
+/define NGX_/ {print "{ " $2 ", ngx_null_string },"}
+/#if .*_HAVE_/ {d=1;print $0}
+/#endif/ && d {d=0;print}
+' src/os/unix/ngx_errno.h | uniq
+
+ */
+{ NGX_EPERM, ngx_null_string },
+{ NGX_ENOENT, ngx_null_string },
+{ NGX_ENOPATH, ngx_null_string },
+{ NGX_ESRCH, ngx_null_string },
+{ NGX_EINTR, ngx_null_string },
+{ NGX_ECHILD, ngx_null_string },
+{ NGX_ENOMEM, ngx_null_string },
+{ NGX_EACCES, ngx_null_string },
+{ NGX_EBUSY, ngx_null_string },
+{ NGX_EEXIST, ngx_null_string },
+{ NGX_EXDEV, ngx_null_string },
+{ NGX_ENOTDIR, ngx_null_string },
+{ NGX_EISDIR, ngx_null_string },
+{ NGX_EINVAL, ngx_null_string },
+{ NGX_ENFILE, ngx_null_string },
+{ NGX_EMFILE, ngx_null_string },
+{ NGX_ENOSPC, ngx_null_string },
+{ NGX_EPIPE, ngx_null_string },
+{ NGX_EINPROGRESS, ngx_null_string },
+{ NGX_ENOPROTOOPT, ngx_null_string },
+{ NGX_EOPNOTSUPP, ngx_null_string },
+{ NGX_EADDRINUSE, ngx_null_string },
+{ NGX_ECONNABORTED, ngx_null_string },
+{ NGX_ECONNRESET, ngx_null_string },
+{ NGX_ENOTCONN, ngx_null_string },
+{ NGX_ETIMEDOUT, ngx_null_string },
+{ NGX_ECONNREFUSED, ngx_null_string },
+{ NGX_ENAMETOOLONG, ngx_null_string },
+{ NGX_ENETDOWN, ngx_null_string },
+{ NGX_ENETUNREACH, ngx_null_string },
+{ NGX_EHOSTDOWN, ngx_null_string },
+{ NGX_EHOSTUNREACH, ngx_null_string },
+{ NGX_ENOSYS, ngx_null_string },
+{ NGX_ECANCELED, ngx_null_string },
+{ NGX_EILSEQ, ngx_null_string },
+{ NGX_ENOMOREFILES, ngx_null_string },
+{ NGX_ELOOP, ngx_null_string },
+{ NGX_EBADF, ngx_null_string },
+#if (NGX_HAVE_OPENAT)
+{ NGX_EMLINK, ngx_null_string },
+#endif
+{ NGX_EAGAIN, ngx_null_string },
+{ 0, ngx_null_string }
+};
+static ngx_str_t      ngx_unknown_error = ngx_string("Unknown error");
+
+
+u_char *
+ngx_strerror(ngx_err_t err, u_char *errstr, size_t size)
+{
+    ngx_str_t  *msg = &ngx_unknown_error;
+    int i;
+
+    for (i = 0; ngx_sys_errmap[i].error != 0; i++) {
+        if (ngx_sys_errmap[i].error == err) {
+            msg = &ngx_sys_errmap[i].str;
+            break;
+        }
+    }
+
+    size = ngx_min(size, msg->len);
+
+    return ngx_cpymem(errstr, msg->data, size);
+}
+
+
+ngx_int_t
+ngx_strerror_init(void)
+{
+    char       *msg;
+    u_char     *p;
+    size_t      len;
+    ngx_err_t   err;
+    int i;
+
+    /*
+     * ngx_strerror() is not ready to work at this stage, therefore,
+     * malloc() is used and possible errors are logged using strerror().
+     */
+
+    for (i = 0; ngx_sys_errmap[i].error != 0; i++) {
+        msg = strerror(ngx_sys_errmap[i].error);
+        len = ngx_strlen(msg);
+
+        p = malloc(len);
+        if (p == NULL) {
+            goto failed;
+        }
+
+        ngx_memcpy(p, msg, len);
+        ngx_sys_errmap[i].str.len = len;
+        ngx_sys_errmap[i].str.data = p;
+    }
+
+    return NGX_OK;
+
+failed:
+
+    err = errno;
+    ngx_log_stderr(0, "malloc(%uz) failed (%d: %s)", len, err, strerror(err));
+
+    return NGX_ERROR;
+}
diff --git a/src/os/unix/ngx_process.c b/src/os/unix/ngx_process.c
index 1568023..e7037f7 100644
--- a/src/os/unix/ngx_process.c
+++ b/src/os/unix/ngx_process.c
@@ -71,7 +71,9 @@ ngx_signal_t  signals[] = {
 
     { SIGINT, "SIGINT", "", ngx_signal_handler },
 
+#ifdef SIGIO
     { SIGIO, "SIGIO", "", ngx_signal_handler },
+#endif
 
     { SIGCHLD, "SIGCHLD", "", ngx_signal_handler },
 
@@ -143,19 +145,23 @@ ngx_spawn_process(ngx_cycle_t *cycle, ngx_spawn_proc_pt proc, void *data,
         }
 
         on = 1;
+#ifdef FIOASYNC
         if (ioctl(ngx_processes[s].channel[0], FIOASYNC, &on) == -1) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
                           "ioctl(FIOASYNC) failed while spawning \"%s\"", name);
             ngx_close_channel(ngx_processes[s].channel, cycle->log);
             return NGX_INVALID_PID;
         }
+#endif
 
+#ifdef F_SETOWN
         if (fcntl(ngx_processes[s].channel[0], F_SETOWN, ngx_pid) == -1) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
                           "fcntl(F_SETOWN) failed while spawning \"%s\"", name);
             ngx_close_channel(ngx_processes[s].channel, cycle->log);
             return NGX_INVALID_PID;
         }
+#endif
 
         if (fcntl(ngx_processes[s].channel[0], F_SETFD, FD_CLOEXEC) == -1) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
@@ -394,9 +400,11 @@ ngx_signal_handler(int signo, siginfo_t *siginfo, void *ucontext)
             ngx_sigalrm = 1;
             break;
 
+#ifdef SIGIO
         case SIGIO:
             ngx_sigio = 1;
             break;
+#endif
 
         case SIGCHLD:
             ngx_reap = 1;
@@ -433,7 +441,9 @@ ngx_signal_handler(int signo, siginfo_t *siginfo, void *ucontext)
 
         case ngx_signal_value(NGX_RECONFIGURE_SIGNAL):
         case ngx_signal_value(NGX_CHANGEBIN_SIGNAL):
+#ifdef SIGIO
         case SIGIO:
+#endif
             action = ", ignoring";
             break;
         }
diff --git a/src/os/unix/ngx_process_cycle.c b/src/os/unix/ngx_process_cycle.c
index f87e009..3b65196 100644
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -87,7 +87,9 @@ ngx_master_process_cycle(ngx_cycle_t *cycle)
     sigemptyset(&set);
     sigaddset(&set, SIGCHLD);
     sigaddset(&set, SIGALRM);
+#ifdef SIGIO
     sigaddset(&set, SIGIO);
+#endif
     sigaddset(&set, SIGINT);
     sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL));
     sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL));
@@ -786,12 +788,14 @@ ngx_worker_process_init(ngx_cycle_t *cycle, ngx_int_t worker)
 
     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
+#if (NGX_HAVE_SETPRIORITY)
     if (worker >= 0 && ccf->priority != 0) {
         if (setpriority(PRIO_PROCESS, 0, ccf->priority) == -1) {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
                           "setpriority(%d) failed", ccf->priority);
         }
     }
+#endif
 
     if (ccf->rlimit_nofile != NGX_CONF_UNSET) {
         rlmt.rlim_cur = (rlim_t) ccf->rlimit_nofile;
-- 
2.30.0
