From af54df578d6b82f953e5438723670a5fbaf5e52f Mon Sep 17 00:00:00 2001
From: Bruno Haible <bruno@clisp.org>
Date: Fri, 30 Aug 2024 23:48:44 +0200
Subject: [PATCH] posix_openpt tests: Fix test failure on Haiku.

* tests/test-posix_openpt.c (main): On Haiku, create a child process and
call setsid().
---
 ChangeLog                 |  6 ++++++
 tests/test-posix_openpt.c | 23 +++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 85fae34ea2..83b15ee13e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-08-30  Bruno Haible  <bruno@clisp.org>
+
+	posix_openpt tests: Fix test failure on Haiku.
+	* tests/test-posix_openpt.c (main): On Haiku, create a child process and
+	call setsid().
+
 2024-08-30  Bruno Haible  <bruno@clisp.org>
 
 	sigsegv: Clarify license of the *.m4 files.
diff --git a/tests/test-posix_openpt.c b/tests/test-posix_openpt.c
index 95d69cd7ae..fdca5820d9 100644
--- a/tests/test-posix_openpt.c
+++ b/tests/test-posix_openpt.c
@@ -54,6 +54,29 @@ main (void)
   ASSERT (name);
   ASSERT (grantpt (master) == 0);
   ASSERT (unlockpt (master) == 0);
+
+#if defined __HAIKU__
+  /* On Haiku, the open() call below succeeds only if
+       - done in a child process, and
+       - if that child process has done a setsid() call.
+     So, do that.  */
+  pid_t child_pid = fork ();
+  ASSERT (child_pid >= 0);
+  if (child_pid > 0)
+    {
+      /* We are in the parent.  Wait for the child to terminate.  */
+      int child_status;
+      ASSERT (waitpid (child_pid, &child_status, 0) != -1);
+      ASSERT (WIFEXITED (child_status));
+      int child_exit_status = WEXITSTATUS (child_status);
+      ASSERT (child_exit_status == 0);
+
+      return test_exit_status;
+    }
+  /* We are in the child.  */
+  ASSERT (setsid () >= 0);
+#endif
+
   slave = open (name, O_RDWR);
   ASSERT (0 <= slave);
 
-- 
2.39.5