Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libexecinfo use sysctl to find the pathname argv[0] if a...



details:   https://anonhg.NetBSD.org/src/rev/ed5d899f06da
branches:  trunk
changeset: 789640:ed5d899f06da
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Aug 29 14:58:56 2013 +0000

description:
use sysctl to find the pathname argv[0] if available (from FreeBSD via
Ed Maste)

diffstat:

 lib/libexecinfo/backtrace.c |  26 +++++++++++++++++++++++---
 1 files changed, 23 insertions(+), 3 deletions(-)

diffs (57 lines):

diff -r 5d2333e1ca36 -r ed5d899f06da lib/libexecinfo/backtrace.c
--- a/lib/libexecinfo/backtrace.c       Thu Aug 29 14:25:40 2013 +0000
+++ b/lib/libexecinfo/backtrace.c       Thu Aug 29 14:58:56 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: backtrace.c,v 1.2 2012/07/09 03:11:59 christos Exp $   */
+/*     $NetBSD: backtrace.c,v 1.3 2013/08/29 14:58:56 christos Exp $   */
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: backtrace.c,v 1.2 2012/07/09 03:11:59 christos Exp $");
+__RCSID("$NetBSD: backtrace.c,v 1.3 2013/08/29 14:58:56 christos Exp $");
 
 #include <sys/param.h>
 #include <assert.h>
@@ -50,9 +50,29 @@
 #ifdef __linux__
 #define SELF   "/proc/self/exe"
 #else
+#include <sys/sysctl.h>
 #define SELF   "/proc/curproc/file"
 #endif
 
+static int
+open_self(int flags)
+{
+       const char *pathname = SELF;
+#ifdef KERN_PROC_PATHNAME
+       static const int name[] = {
+               CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1,
+       };
+       char path[MAXPATHLEN];
+       size_t len;
+
+       len = sizeof(path);
+       if (sysctl(name, __arraycount(name), path, &len, NULL, 0) != -1)
+               pathname = path;
+#endif
+       return open(pathname, flags);
+}
+
+
 static int __printflike(4, 5)
 rasprintf(char **buf, size_t *bufsiz, size_t offs, const char *fmt, ...)
 {
@@ -162,7 +182,7 @@
        symtab_t *st;
        int fd;
 
-       if ((fd = open(SELF, O_RDONLY)) != -1)
+       if ((fd = open_self(O_RDONLY)) != -1)
                st = symtab_create(fd, -1, STT_FUNC);
        else
                st = NULL;



Home | Main Index | Thread Index | Old Index