LIBCx is the kLIBC extension library which provides some enhanced
features to OS/2 kLIBC.

It provides its own wait(), waitid() and waitpid() to support its
spawn2().

Use them if possible.

* lib/sys_wait.in.h (rpl_wait, rpl_waitid, rpl_waitpid): New
declaration.
* lib/wait.c: New file.
* lib/waitid.c: New file.
* lib/waitpid.c (rpl_waitpid) [kLIBC]: New function.
* m4/sys_wait_h.m4 (gl_SYS_WAIT_H): Check for declarations of wait() and
waitid() as well.
* m4/wait.m4: New file.
* m4/waitid.m4: New file.
* m4/waitpid.m4 (gl_FUNC_WAITPID) [os2*]: Set REPLACE_WAITPID to 0.
* modules/sys_wait (Makefile.am): Replace GNULIB_WAIT, GNULIB_WAITID,
REPLACE_WAIT, REPLACE_WAITID and REPLACE_WAITPID.
* modules/wait: New file.
* modules/waitid: New file.
* modules/waitpid: New file.
---
 lib/sys_wait.in.h | 54 +++++++++++++++++++++++++++++++++++++++++++++--
 lib/wait.c        | 46 ++++++++++++++++++++++++++++++++++++++++
 lib/waitid.c      | 46 ++++++++++++++++++++++++++++++++++++++++
 lib/waitpid.c     | 28 ++++++++++++++++++++++++
 m4/sys_wait_h.m4  |  7 +++++-
 m4/wait.m4        | 14 ++++++++++++
 m4/waitid.m4      | 14 ++++++++++++
 m4/waitpid.m4     |  3 ++-
 modules/sys_wait  |  5 +++++
 modules/wait      | 28 ++++++++++++++++++++++++
 modules/waitid    | 28 ++++++++++++++++++++++++
 modules/waitpid   |  3 ++-
 12 files changed, 271 insertions(+), 5 deletions(-)
 create mode 100644 lib/wait.c
 create mode 100644 lib/waitid.c
 create mode 100644 m4/wait.m4
 create mode 100644 m4/waitid.m4
 create mode 100644 modules/wait
 create mode 100644 modules/waitid

diff --git a/lib/sys_wait.in.h b/lib/sys_wait.in.h
index 0d87631c0d..453153cb71 100644
--- a/lib/sys_wait.in.h
+++ b/lib/sys_wait.in.h
@@ -115,13 +115,63 @@
 
 /* Declarations of functions.  */
 
+#if @GNULIB_WAIT@
+# if @REPLACE_WAIT@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef wait
+#   define wait rpl_wait
+#  endif
+_GL_FUNCDECL_RPL (wait, pid_t, (int *statusp));
+_GL_CXXALIAS_RPL (wait, pid_t, (int *statusp));
+# endif
+_GL_CXXALIASWARN (wait);
+#elif defined GNULIB_POSIXCHECK
+# undef wait
+# if HAVE_RAW_DECL_WAIT
+_GL_WARN_ON_USE (wait, "wait is unportable - "
+                 "use gnulib module sys_wait for portability");
+# endif
+#endif
+
+
+#if @GNULIB_WAITID@
+# if @REPLACE_WAITID@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef waitid
+#   define waitid rpl_waitid
+#  endif
+_GL_FUNCDECL_RPL (waitid, int,
+                  (idtype_t idtype, id_t id, siginfo_t *infop, int options),
+                  _GL_ARG_NONNULL ((3)));
+_GL_FUNCDECL_RPL (waitid, int,
+                  (idtype_t idtype, id_t id, siginfo_t *infop, int options));
+# endif
+_GL_CXXALIASWARN (waitid);
+#elif defined GNULIB_POSIXCHECK
+# undef waitid
+# if HAVE_RAW_DECL_WAITID
+_GL_WARN_ON_USE (waitid, "waitid is unportable - "
+                 "use gnulib module sys_wait for portability");
+# endif
+#endif
+
+
 #if @GNULIB_WAITPID@
-# if defined _WIN32 && ! defined __CYGWIN__
+# if @REPLACE_WAITPID@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef waitpid
+#   define waitpid rpl_waitpid
+#  endif
+_GL_FUNCDECL_RPL (waitpid, pid_t, (pid_t pid, int *statusp, int options));
+_GL_CXXALIAS_RPL (waitpid, pid_t, (pid_t pid, int *statusp, int options));
+# else
+#  if defined _WIN32 && ! defined __CYGWIN__
 _GL_FUNCDECL_SYS (waitpid, pid_t, (pid_t pid, int *statusp, int options));
-# endif
+#  endif
 /* Need to cast, because on Cygwin, the second parameter is
                                                 __wait_status_ptr_t statusp.  
*/
 _GL_CXXALIAS_SYS_CAST (waitpid, pid_t, (pid_t pid, int *statusp, int options));
+# endif
 _GL_CXXALIASWARN (waitpid);
 #elif defined GNULIB_POSIXCHECK
 # undef waitpid
diff --git a/lib/wait.c b/lib/wait.c
new file mode 100644
index 0000000000..d8111b2f9e
--- /dev/null
+++ b/lib/wait.c
@@ -0,0 +1,46 @@
+/* Wait for process state change.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <sys/wait.h>
+
+/* Repalcements for OS/2 kLIBC.  */
+
+#include <stddef.h>     /* for NULL */
+#include <dlfcn.h>
+
+static pid_t (*wait_pfn) (int *) = NULL;
+
+#undef wait
+
+pid_t
+rpl_wait (int *statusp)
+{
+  if (wait_pfn == NULL)
+    {
+      void *libcx_handle;
+
+      libcx_handle = dlopen ("libcx0", RTLD_LAZY);
+      if (libcx_handle != NULL)
+        wait_pfn = dlsym (libcx_handle, "_wait");
+      if (wait_pfn == NULL)
+        wait_pfn = wait;
+    }
+
+  return wait_pfn (statusp);
+}
diff --git a/lib/waitid.c b/lib/waitid.c
new file mode 100644
index 0000000000..487d24aa93
--- /dev/null
+++ b/lib/waitid.c
@@ -0,0 +1,46 @@
+/* Wait for process state change.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <sys/wait.h>
+
+/* Repalcements for OS/2 kLIBC.  */
+
+#include <stddef.h>     /* for NULL */
+#include <dlfcn.h>
+
+static int (*waitid_pfn) (idtype_t, id_t, siginfo_t *, int) = NULL;
+
+#undef waitid
+
+pid_t
+rpl_waitid (idtype_t idtype, id_t id, siginfo_t *infop, int options)
+{
+  if (waitid_pfn == NULL)
+    {
+      void *libcx_handle;
+
+      libcx_handle = dlopen ("libcx0", RTLD_LAZY);
+      if (libcx_handle != NULL)
+        waitid_pfn = dlsym (libcx_handle, "_waitid");
+      if (waitid_pfn == NULL)
+        waitid_pfn = waitid;
+    }
+
+  return waitid_pfn (idtype, id, infop, options);
+}
diff --git a/lib/waitpid.c b/lib/waitpid.c
index e1d36be5c5..09540cd63e 100644
--- a/lib/waitpid.c
+++ b/lib/waitpid.c
@@ -19,6 +19,7 @@
 /* Specification.  */
 #include <sys/wait.h>
 
+#if defined _WIN32 && ! defined __CYGWIN__
 /* Implementation for native Windows systems.  */
 
 #include <process.h> /* for _cwait, WAIT_CHILD */
@@ -28,3 +29,30 @@ waitpid (pid_t pid, int *statusp, int options)
 {
   return _cwait (statusp, pid, WAIT_CHILD);
 }
+#elif defined __KLIBC__
+/* Repalcements for OS/2 kLIBC.  */
+
+#include <stddef.h>     /* for NULL */
+#include <dlfcn.h>
+
+static pid_t (*waitpid_pfn) (pid_t, int *, int) = NULL;
+
+#undef waitpid
+
+pid_t
+rpl_waitpid (pid_t pid, int *statusp, int options)
+{
+  if (waitpid_pfn == NULL)
+    {
+      void *libcx_handle;
+
+      libcx_handle = dlopen ("libcx0", RTLD_LAZY);
+      if (libcx_handle != NULL)
+        waitpid_pfn = dlsym (libcx_handle, "_waitpid");
+      if (waitpid_pfn == NULL)
+        waitpid_pfn = waitpid;
+    }
+
+  return waitpid_pfn (pid, statusp, options);
+}
+#endif
diff --git a/m4/sys_wait_h.m4 b/m4/sys_wait_h.m4
index 95f8495d12..ee50c697ea 100644
--- a/m4/sys_wait_h.m4
+++ b/m4/sys_wait_h.m4
@@ -18,7 +18,7 @@ AC_DEFUN_ONCE([gl_SYS_WAIT_H],
   dnl Check for declarations of anything we want to poison if the
   dnl corresponding gnulib module is not in use.
   gl_WARN_ON_USE_PREPARE([[#include <sys/wait.h>]],
-    [waitpid])
+    [wait waitid waitpid])
 ])
 
 # gl_SYS_WAIT_MODULE_INDICATOR([modulename])
@@ -40,6 +40,8 @@ AC_DEFUN([gl_SYS_WAIT_MODULE_INDICATOR],
 AC_DEFUN([gl_SYS_WAIT_H_REQUIRE_DEFAULTS],
 [
   m4_defun(GL_MODULE_INDICATOR_PREFIX[_SYS_WAIT_H_MODULE_INDICATOR_DEFAULTS], [
+    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_WAIT])
+    gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_WAITID])
     gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_WAITPID])
   ])
   m4_require(GL_MODULE_INDICATOR_PREFIX[_SYS_WAIT_H_MODULE_INDICATOR_DEFAULTS])
@@ -49,4 +51,7 @@ AC_DEFUN([gl_SYS_WAIT_H_REQUIRE_DEFAULTS],
 AC_DEFUN([gl_SYS_WAIT_H_DEFAULTS],
 [
   dnl Assume proper GNU behavior unless another module says otherwise.
+  REPLACE_WAIT=0;       AC_SUBST([REPLACE_WAIT])
+  REPLACE_WAITID=0;     AC_SUBST([REPLACE_WAITID])
+  REPLACE_WAITPID=0;    AC_SUBST([REPLACE_WAITPID])
 ])
diff --git a/m4/wait.m4 b/m4/wait.m4
new file mode 100644
index 0000000000..6bf59fa329
--- /dev/null
+++ b/m4/wait.m4
@@ -0,0 +1,14 @@
+# wait.m4
+# serial 1
+dnl Copyright (C) 2024 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_WAIT],
+[
+  AC_REQUIRE([AC_CANONICAL_HOST])
+  case $host_os in
+    os2*) REPLACE_WAIT=1 ;;
+  esac
+])
diff --git a/m4/waitid.m4 b/m4/waitid.m4
new file mode 100644
index 0000000000..9d034b01c0
--- /dev/null
+++ b/m4/waitid.m4
@@ -0,0 +1,14 @@
+# waitid.m4
+# serial 1
+dnl Copyright (C) 2024 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_WAITID],
+[
+  AC_REQUIRE([AC_CANONICAL_HOST])
+  case $host_os in
+    os2*) REPLACE_WAITID=1 ;;
+  esac
+])
diff --git a/m4/waitpid.m4 b/m4/waitpid.m4
index f0069d4591..78b598647c 100644
--- a/m4/waitpid.m4
+++ b/m4/waitpid.m4
@@ -1,5 +1,5 @@
 # waitpid.m4
-# serial 3
+# serial 4
 dnl Copyright (C) 2010-2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -11,5 +11,6 @@ AC_DEFUN([gl_FUNC_WAITPID],
   HAVE_WAITPID=1
   case $host_os in
     mingw* | windows*) HAVE_WAITPID=0 ;;
+    os2*) REPLACE_WAITPID=1 ;;
   esac
 ])
diff --git a/modules/sys_wait b/modules/sys_wait
index 455cf961fb..0c5f8ae930 100644
--- a/modules/sys_wait
+++ b/modules/sys_wait
@@ -30,7 +30,12 @@ sys/wait.h: sys_wait.in.h $(top_builddir)/config.status 
$(CXXDEFS_H) $(WARN_ON_U
              -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \
              -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \
              -e 's|@''NEXT_SYS_WAIT_H''@|$(NEXT_SYS_WAIT_H)|g' \
+             -e 's/@''GNULIB_WAIT''@/$(GNULIB_WAIT)/g' \
+             -e 's/@''GNULIB_WAITID''@/$(GNULIB_WAITID)/g' \
              -e 's/@''GNULIB_WAITPID''@/$(GNULIB_WAITPID)/g' \
+             -e 's|@''REPLACE_WAIT''@|$(REPLACE_WAIT)|g' \
+             -e 's|@''REPLACE_WAITID''@|$(REPLACE_WAITID)|g' \
+             -e 's|@''REPLACE_WAITPID''@|$(REPLACE_WAITPID)|g' \
              -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
              -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \
              $(srcdir)/sys_wait.in.h > $@-t
diff --git a/modules/wait b/modules/wait
new file mode 100644
index 0000000000..3e861131a8
--- /dev/null
+++ b/modules/wait
@@ -0,0 +1,28 @@
+Description:
+wait() function: wait for process state change
+
+Files:
+lib/wait.c
+m4/wait.m4
+
+Depends-on:
+sys_wait
+
+configure.ac:
+gl_FUNC_WAIT
+gl_CONDITIONAL([GL_COND_OBJ_WAIT], [test $REPLACE_WAIT = 1])
+gl_SYS_WAIT_MODULE_INDICATOR([wait])
+
+Makefile.am:
+if GL_COND_OBJ_WAIT
+lib_SOURCES += wait.c
+endif
+
+Include:
+<sys/wait.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all
diff --git a/modules/waitid b/modules/waitid
new file mode 100644
index 0000000000..9082440ab6
--- /dev/null
+++ b/modules/waitid
@@ -0,0 +1,28 @@
+Description:
+waitid() function: wait for process state change
+
+Files:
+lib/waitid.c
+m4/waitid.m4
+
+Depends-on:
+sys_wait
+
+configure.ac:
+gl_FUNC_WAITID
+gl_CONDITIONAL([GL_COND_OBJ_WAITID], [test $REPLACE_WAITID = 1])
+gl_SYS_WAIT_MODULE_INDICATOR([waitid])
+
+Makefile.am:
+if GL_COND_OBJ_WAITID
+lib_SOURCES += waitid.c
+endif
+
+Include:
+<sys/wait.h>
+
+License:
+LGPLv2+
+
+Maintainer:
+all
diff --git a/modules/waitpid b/modules/waitpid
index 52a7a9bda8..3f82c256de 100644
--- a/modules/waitpid
+++ b/modules/waitpid
@@ -10,7 +10,8 @@ sys_wait
 
 configure.ac:
 gl_FUNC_WAITPID
-gl_CONDITIONAL([GL_COND_OBJ_WAITPID], [test $HAVE_WAITPID = 0])
+gl_CONDITIONAL([GL_COND_OBJ_WAITPID],
+               [test $HAVE_WAITPID = 0 || test $REPLACE_WAITPID = 1])
 gl_SYS_WAIT_MODULE_INDICATOR([waitpid])
 
 Makefile.am:
-- 
2.42.0


Reply via email to