diff mbox series

libbacktrace: Support FreeBSD/DragonFlyBSD/NetBSD without /proc

Message ID 20180301220732.60106-1-bdrewery@FreeBSD.org
State New
Headers show
Series libbacktrace: Support FreeBSD/DragonFlyBSD/NetBSD without /proc | expand

Commit Message

Bryan Drewery March 1, 2018, 10:07 p.m. UTC
From: Bryan Drewery <bryan@shatow.net>

FreeBSD has had this patch against Rust's bundled libbacktrace for a
while which allows not having /proc mounted to get the process name.
I am open to refactoring this if there's a better place to handle
some of this, such as configure.ac or a bsd-compat.c file.

Authored by:	Jan Beich <jbeich@FreeBSD.org>
---
 libbacktrace/fileline.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

Comments

Jeff Law June 11, 2018, 9:10 p.m. UTC | #1
On 03/01/2018 03:07 PM, Bryan Drewery wrote:
> From: Bryan Drewery <bryan@shatow.net>
> 
> FreeBSD has had this patch against Rust's bundled libbacktrace for a
> while which allows not having /proc mounted to get the process name.
> I am open to refactoring this if there's a better place to handle
> some of this, such as configure.ac or a bsd-compat.c file.
> 
> Authored by:	Jan Beich <jbeich@FreeBSD.org>
> ---
>  libbacktrace/fileline.c | 31 +++++++++++++++++++++++++++++++
>  1 file changed, 31 insertions(+)
> 
> diff --git libbacktrace/fileline.c libbacktrace/fileline.c
> index e5673068379..6d56f89cfa9 100644
> --- libbacktrace/fileline.c
> +++ libbacktrace/fileline.c
> @@ -39,9 +39,40 @@ POSSIBILITY OF SUCH DAMAGE.  */
>  #include <stdlib.h>
>  #include <unistd.h>
>  
> +#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)
> +#include <sys/sysctl.h>
> +#include <limits.h>
> +#endif
I would have expected these to be tested for by autoconf and
conditionally included based on a suitable HAVE_  test.


> +
>  #include "backtrace.h"
>  #include "internal.h"
>  
> +#if !defined(HAVE_GETEXECNAME) && defined(KERN_PROC_PATHNAME)
> +/* Return pathname of executable or 0 on failure. */
> +#define HAVE_GETEXECNAME
> +static char execname[PATH_MAX + 1];
> +static const char *
> +getexecname(void)
> +{
> +  size_t path_len = sizeof(execname);
> +  int mib[] = {
> +    CTL_KERN,
> +#if defined(__NetBSD__)
> +    KERN_PROC_ARGS,
> +    -1,
> +    KERN_PROC_PATHNAME,
> +#else
> +    KERN_PROC,
> +    KERN_PROC_PATHNAME,
> +    -1,
> +#endif
> +  };
> +  u_int miblen = sizeof(mib) / sizeof(mib[0]);
> +  int rc = sysctl(mib, miblen, execname, &path_len, NULL, 0);
> +  return rc ? NULL : execname;
> +}
> +#endif /* !HAVE_GETEXECNAME && KERN_PROC_PATHNAME */
So what system are you expecting to be handled in the #else clause?

Is there a cleaner (less #if crap) way to do this?

Ian has the final call here.


jeff
diff mbox series

Patch

diff --git libbacktrace/fileline.c libbacktrace/fileline.c
index e5673068379..6d56f89cfa9 100644
--- libbacktrace/fileline.c
+++ libbacktrace/fileline.c
@@ -39,9 +39,40 @@  POSSIBILITY OF SUCH DAMAGE.  */
 #include <stdlib.h>
 #include <unistd.h>
 
+#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)
+#include <sys/sysctl.h>
+#include <limits.h>
+#endif
+
 #include "backtrace.h"
 #include "internal.h"
 
+#if !defined(HAVE_GETEXECNAME) && defined(KERN_PROC_PATHNAME)
+/* Return pathname of executable or 0 on failure. */
+#define HAVE_GETEXECNAME
+static char execname[PATH_MAX + 1];
+static const char *
+getexecname(void)
+{
+  size_t path_len = sizeof(execname);
+  int mib[] = {
+    CTL_KERN,
+#if defined(__NetBSD__)
+    KERN_PROC_ARGS,
+    -1,
+    KERN_PROC_PATHNAME,
+#else
+    KERN_PROC,
+    KERN_PROC_PATHNAME,
+    -1,
+#endif
+  };
+  u_int miblen = sizeof(mib) / sizeof(mib[0]);
+  int rc = sysctl(mib, miblen, execname, &path_len, NULL, 0);
+  return rc ? NULL : execname;
+}
+#endif /* !HAVE_GETEXECNAME && KERN_PROC_PATHNAME */
+
 #ifndef HAVE_GETEXECNAME
 #define getexecname() NULL
 #endif