]> Savannah Git Hosting - gnulib.git/commitdiff
get_ppid_of, get_progname_of: Fix runtime error on Mac OS X < 10.5.
authorBruno Haible <bruno@clisp.org>
Tue, 21 Dec 2021 20:38:30 +0000 (21:38 +0100)
committerBruno Haible <bruno@clisp.org>
Tue, 21 Dec 2021 20:45:05 +0000 (21:45 +0100)
Reported by Evan Miller <emmiller@gmail.com>
<https://lists.gnu.org/archive/html/bug-gnulib/2021-12/msg00081.html>.

* lib/get_ppid_of.c (proc_pidinfo): Declare with WEAK_IMPORT_ATTRIBUTE.
(get_ppid_of): Test whether the symbol declared weak evaluates to
non-NULL before invoking it.
* lib/get_progname_of (get_progname_of): Declare with
WEAK_IMPORT_ATTRIBUTE.
(get_ppid_of): Test whether the symbol declared weak evaluates to
non-NULL before invoking it.

ChangeLog
lib/get_ppid_of.c
lib/get_progname_of.c

index e4055c12ffc344602838278b30967b5f2903f22a..9c5b479082651b52dc4a590a07028c68963e1e2b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2021-12-21  Bruno Haible  <bruno@clisp.org>
+
+       get_ppid_of, get_progname_of: Fix runtime error on Mac OS X < 10.5.
+       Reported by Evan Miller <emmiller@gmail.com>
+       <https://lists.gnu.org/archive/html/bug-gnulib/2021-12/msg00081.html>.
+       * lib/get_ppid_of.c (proc_pidinfo): Declare with WEAK_IMPORT_ATTRIBUTE.
+       (get_ppid_of): Test whether the symbol declared weak evaluates to
+       non-NULL before invoking it.
+       * lib/get_progname_of (get_progname_of): Declare with
+       WEAK_IMPORT_ATTRIBUTE.
+       (get_ppid_of): Test whether the symbol declared weak evaluates to
+       non-NULL before invoking it.
+
 2021-12-21  Paul Eggert  <eggert@cs.ucla.edu>
 
        Move AM_V_GEN to recipe start
index dd4e38129d0ec5172fb5ecf039a61643f7427818..636dfa3448fead73749600825eea56c01abb152d 100644 (file)
 # include <AvailabilityMacros.h>
 # if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
 #  include <libproc.h>
+#  if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
+/* Mac OS X versions < 10.5 don't have this function.  Therefore declare it as
+   weak, in order to avoid a runtime error when the binaries are run on these
+   older versions.  */
+extern int proc_pidinfo (int, int, uint64_t, void *, int) WEAK_IMPORT_ATTRIBUTE;
+#  endif
 # endif
 #endif
 
@@ -236,11 +242,16 @@ get_ppid_of (pid_t pid)
 
   /* Mac OS X >= 10.7 has PROC_PIDT_SHORTBSDINFO.  */
 #  if defined PROC_PIDT_SHORTBSDINFO
-  struct proc_bsdshortinfo info;
+#   if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
+  if (proc_pidinfo != NULL) /* at runtime Mac OS X >= 10.5 ? */
+#   endif
+    {
+      struct proc_bsdshortinfo info;
 
-  if (proc_pidinfo (pid, PROC_PIDT_SHORTBSDINFO, 0, &info, sizeof (info))
-      == sizeof (info))
-    return info.pbsi_ppid;
+      if (proc_pidinfo (pid, PROC_PIDT_SHORTBSDINFO, 0, &info, sizeof (info))
+          == sizeof (info))
+        return info.pbsi_ppid;
+    }
 #  endif
 
 #  if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
@@ -249,10 +260,15 @@ get_ppid_of (pid_t pid)
      32-bit and 64-bit environments, and the kernel of Mac OS X 10.5 knows
      only about the 32-bit 'struct proc_bsdinfo'.  Fortunately all the info
      we need is in the first part, which is the same in 32-bit and 64-bit.  */
-  struct proc_bsdinfo info;
+#   if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
+  if (proc_pidinfo != NULL) /* at runtime Mac OS X >= 10.5 ? */
+#   endif
+    {
+      struct proc_bsdinfo info;
 
-  if (proc_pidinfo (pid, PROC_PIDTBSDINFO, 0, &info, 128) == 128)
-    return info.pbi_ppid;
+      if (proc_pidinfo (pid, PROC_PIDTBSDINFO, 0, &info, 128) == 128)
+        return info.pbi_ppid;
+    }
 #  endif
 
 # endif
index bdda3c60d8e28d805fa52751f22e2df8b1233300..a5b99c038bfe06a1e9a80218e53fce7cddaecc0f 100644 (file)
 # include <AvailabilityMacros.h>
 # if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
 #  include <libproc.h>
+#  if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
+/* Mac OS X versions < 10.5 don't have this function.  Therefore declare it as
+   weak, in order to avoid a runtime error when the binaries are run on these
+   older versions.  */
+extern int proc_pidinfo (int, int, uint64_t, void *, int) WEAK_IMPORT_ATTRIBUTE;
+#  endif
 # endif
 #endif
 
@@ -276,11 +282,16 @@ get_progname_of (pid_t pid)
 
   /* Mac OS X >= 10.7 has PROC_PIDT_SHORTBSDINFO.  */
 #  if defined PROC_PIDT_SHORTBSDINFO
-  struct proc_bsdshortinfo info;
+#   if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
+  if (proc_pidinfo != NULL) /* at runtime Mac OS X >= 10.5 ? */
+#   endif
+    {
+      struct proc_bsdshortinfo info;
 
-  if (proc_pidinfo (pid, PROC_PIDT_SHORTBSDINFO, 0, &info, sizeof (info))
-      == sizeof (info))
-    return strdup (info.pbsi_comm);
+      if (proc_pidinfo (pid, PROC_PIDT_SHORTBSDINFO, 0, &info, sizeof (info))
+          == sizeof (info))
+        return strdup (info.pbsi_comm);
+    }
 #  endif
 
 #  if MAC_OS_X_VERSION_MIN_REQUIRED < 1070
@@ -289,10 +300,15 @@ get_progname_of (pid_t pid)
      32-bit and 64-bit environments, and the kernel of Mac OS X 10.5 knows
      only about the 32-bit 'struct proc_bsdinfo'.  Fortunately all the info
      we need is in the first part, which is the same in 32-bit and 64-bit.  */
-  struct proc_bsdinfo info;
+#   if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
+  if (proc_pidinfo != NULL) /* at runtime Mac OS X >= 10.5 ? */
+#   endif
+    {
+      struct proc_bsdinfo info;
 
-  if (proc_pidinfo (pid, PROC_PIDTBSDINFO, 0, &info, 128) == 128)
-    return strdup (info.pbi_comm);
+      if (proc_pidinfo (pid, PROC_PIDTBSDINFO, 0, &info, 128) == 128)
+        return strdup (info.pbi_comm);
+    }
 #  endif
 
 # endif