From 5732cf50e177132c3644c31dda0834c8dc04d6f4 Mon Sep 17 00:00:00 2001 From: Yelso Honnr <126364456+yelsohonnr@users.noreply.github.com> Date: Tue, 27 Jun 2023 11:58:29 -0700 Subject: [PATCH] Add OpenBSD Support (#11255) Summary: I made some changes to add OpenBSD support. Second time doing something like this, so I apologize in advance if I'm doing something wrong (had some minor hiccups with how github worked). Fixes https://github.com/facebook/rocksdb/issues/11220 Pull Request resolved: https://github.com/facebook/rocksdb/pull/11255 Reviewed By: akankshamahajan15 Differential Revision: D46361706 Pulled By: ajkr fbshipit-source-id: 90922fa30197fe6d6f3c0e3ecca2dbb92c337277 --- port/stack_trace.cc | 39 +++++++++++++++++++++++++++------------ util/filelock_test.cc | 3 +++ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/port/stack_trace.cc b/port/stack_trace.cc index 9e9d8b8b5..1ccf9d804 100644 --- a/port/stack_trace.cc +++ b/port/stack_trace.cc @@ -31,6 +31,10 @@ void* SaveStack(int* /*num_frames*/, int /*first_frames_to_skip*/) { #include #include +#ifdef OS_OPENBSD +#include +#include +#endif // OS_OPENBSD #ifdef OS_FREEBSD #include #endif // OS_FREEBSD @@ -51,28 +55,39 @@ namespace port { namespace { -#if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_GNU_KFREEBSD) +#if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_OPENBSD) || defined(OS_GNU_KFREEBSD) const char* GetExecutableName() { static char name[1024]; -#if !defined(OS_FREEBSD) - char link[1024]; - snprintf(link, sizeof(link), "/proc/%d/exe", getpid()); - auto read = readlink(link, name, sizeof(name) - 1); - if (-1 == read) { +#if defined(OS_FREEBSD) + int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; + size_t namesz = sizeof(name); + + auto ret = sysctl(mib, 4, name, &namesz, nullptr, 0); + if (-1 == ret) { return nullptr; } else { - name[read] = 0; return name; } -#else - int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; +#elif defined(OS_OPENBSD) + int mib[4] = {CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV}; size_t namesz = sizeof(name); + char* bin[namesz]; - auto ret = sysctl(mib, 4, name, &namesz, nullptr, 0); + auto ret = sysctl(mib, 4, bin, &namesz, nullptr, 0); if (-1 == ret) { return nullptr; } else { + return bin[0]; + } +#else + char link[1024]; + snprintf(link, sizeof(link), "/proc/%d/exe", getpid()); + auto read = readlink(link, name, sizeof(name) - 1); + if (-1 == read) { + return nullptr; + } else { + name[read] = 0; return name; } #endif @@ -271,7 +286,7 @@ void PrintStack(int first_frames_to_skip) { const int kMaxFrames = 100; void* frames[kMaxFrames]; - auto num_frames = backtrace(frames, kMaxFrames); + int num_frames = (int) backtrace(frames, kMaxFrames); PrintStack(&frames[first_frames_to_skip], num_frames - first_frames_to_skip); } @@ -284,7 +299,7 @@ void* SaveStack(int* num_frames, int first_frames_to_skip) { const int kMaxFrames = 100; void* frames[kMaxFrames]; - auto count = backtrace(frames, kMaxFrames); + int count = (int) backtrace(frames, kMaxFrames); *num_frames = count - first_frames_to_skip; void* callstack = malloc(sizeof(void*) * *num_frames); memcpy(callstack, &frames[first_frames_to_skip], sizeof(void*) * *num_frames); diff --git a/util/filelock_test.cc b/util/filelock_test.cc index 69947a732..82021aec9 100644 --- a/util/filelock_test.cc +++ b/util/filelock_test.cc @@ -11,6 +11,9 @@ #include #include #endif +#ifdef __OpenBSD__ +#include +#endif #include #include "test_util/testharness.h"