From: Bruno Haible Date: Tue, 21 Dec 2021 20:38:30 +0000 (+0100) Subject: get_ppid_of, get_progname_of: Fix runtime error on Mac OS X < 10.5. X-Git-Tag: v1.0~2501 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=119622a83d47b01b5a9fb2af4542cdb45f4eb83b;p=gnulib.git get_ppid_of, get_progname_of: Fix runtime error on Mac OS X < 10.5. Reported by Evan Miller . * 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. --- diff --git a/ChangeLog b/ChangeLog index e4055c12ff..9c5b479082 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2021-12-21 Bruno Haible + + get_ppid_of, get_progname_of: Fix runtime error on Mac OS X < 10.5. + Reported by Evan Miller + . + * 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 Move AM_V_GEN to recipe start diff --git a/lib/get_ppid_of.c b/lib/get_ppid_of.c index dd4e38129d..636dfa3448 100644 --- a/lib/get_ppid_of.c +++ b/lib/get_ppid_of.c @@ -39,6 +39,12 @@ # include # if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 # include +# 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 diff --git a/lib/get_progname_of.c b/lib/get_progname_of.c index bdda3c60d8..a5b99c038b 100644 --- a/lib/get_progname_of.c +++ b/lib/get_progname_of.c @@ -47,6 +47,12 @@ # include # if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 # include +# 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