librb: rb_path_to_self(): use sysctl(2) interface on freebsd/dragonfly

This commit is contained in:
William Pitcock 2016-04-30 01:18:42 -05:00
parent e7c4ecd5b1
commit d71787ab69

View file

@ -40,6 +40,11 @@
#include <crt_externs.h> #include <crt_externs.h>
#endif #endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
#if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN) #if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN)
#include <spawn.h> #include <spawn.h>
@ -159,6 +164,7 @@ const char *
rb_path_to_self(void) rb_path_to_self(void)
{ {
static char path_buf[4096]; static char path_buf[4096];
size_t path_len = sizeof(path_buf);
#if defined(HAVE_GETEXECNAME) #if defined(HAVE_GETEXECNAME)
char *s = getexecname(); char *s = getexecname();
if (s == NULL) if (s == NULL)
@ -166,7 +172,12 @@ rb_path_to_self(void)
realpath(s, path_buf); realpath(s, path_buf);
return path_buf; return path_buf;
#elif defined(__linux__) #elif defined(__linux__)
if (readlink("/proc/self/exe", path_buf, sizeof path_buf) != -1) if (readlink("/proc/self/exe", path_buf, path_len) != -1)
return path_buf;
return NULL;
#elif defined(__FreeBSD__) || defined(__DragonFly__)
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
if (sysctl(mib, 4, path_buf, &path_len, NULL, 0) == 0)
return path_buf; return path_buf;
return NULL; return NULL;
#elif defined(HAVE_DLINFO) #elif defined(HAVE_DLINFO)