summaryrefslogtreecommitdiffstats
path: root/util/system/execpath.cpp
diff options
context:
space:
mode:
authorakhropov <[email protected]>2022-02-10 16:46:32 +0300
committerDaniil Cherednik <[email protected]>2022-02-10 16:46:32 +0300
commit298c6da79f1d8f35089a67f463f0b541bec36d9b (patch)
tree1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /util/system/execpath.cpp
parent00afc96e9c0298054b7386fa7fb9e3cc3d67b974 (diff)
Restoring authorship annotation for <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'util/system/execpath.cpp')
-rw-r--r--util/system/execpath.cpp114
1 files changed, 57 insertions, 57 deletions
diff --git a/util/system/execpath.cpp b/util/system/execpath.cpp
index 7cff2d59737..33198af58b0 100644
--- a/util/system/execpath.cpp
+++ b/util/system/execpath.cpp
@@ -9,9 +9,9 @@
#elif defined(_win_)
#include "winint.h"
#include <io.h>
-#elif defined(_linux_)
+#elif defined(_linux_)
#include <unistd.h>
-#elif defined(_freebsd_)
+#elif defined(_freebsd_)
#include <string.h>
#include <sys/types.h> // for u_int not defined in sysctl.h
#include <sys/sysctl.h>
@@ -28,7 +28,7 @@
#include <util/string/cast.h>
#include "filemap.h"
-#include "execpath.h"
+#include "execpath.h"
#include "fs.h"
#if defined(_freebsd_)
@@ -36,28 +36,28 @@ static inline bool GoodPath(const TString& path) {
return path.find('/') != TString::npos;
}
-static inline int FreeBSDSysCtl(int* mib, size_t mibSize, TTempBuf& res) {
- for (size_t i = 0; i < 2; ++i) {
- size_t cb = res.Size();
+static inline int FreeBSDSysCtl(int* mib, size_t mibSize, TTempBuf& res) {
+ for (size_t i = 0; i < 2; ++i) {
+ size_t cb = res.Size();
if (sysctl(mib, mibSize, res.Data(), &cb, nullptr, 0) == 0) {
- res.Proceed(cb);
- return 0;
- } else if (errno == ENOMEM) {
- res = TTempBuf(cb);
- } else {
- return errno;
- }
+ res.Proceed(cb);
+ return 0;
+ } else if (errno == ENOMEM) {
+ res = TTempBuf(cb);
+ } else {
+ return errno;
+ }
}
- return errno;
+ return errno;
}
static inline TString FreeBSDGetExecPath() {
int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
- TTempBuf buf;
+ TTempBuf buf;
int r = FreeBSDSysCtl(mib, Y_ARRAY_SIZE(mib), buf);
- if (r == 0) {
+ if (r == 0) {
return TString(buf.Data(), buf.Filled() - 1);
- } else if (r == ENOTSUP) { // older FreeBSD version
+ } else if (r == ENOTSUP) { // older FreeBSD version
/*
* BSD analogue for /proc/self is /proc/curproc.
* See:
@@ -65,7 +65,7 @@ static inline TString FreeBSDGetExecPath() {
*/
TString path("/proc/curproc/file");
return NFs::ReadLink(path);
- } else {
+ } else {
return TString();
}
}
@@ -74,56 +74,56 @@ static inline TString FreeBSDGetArgv0() {
int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ARGS, getpid()};
TTempBuf buf;
int r = FreeBSDSysCtl(mib, Y_ARRAY_SIZE(mib), buf);
- if (r == 0) {
+ if (r == 0) {
return TString(buf.Data());
- } else if (r == ENOTSUP) {
+ } else if (r == ENOTSUP) {
return TString();
- } else {
- ythrow yexception() << "FreeBSDGetArgv0() failed: " << LastSystemErrorText();
- }
+ } else {
+ ythrow yexception() << "FreeBSDGetArgv0() failed: " << LastSystemErrorText();
+ }
}
static inline bool FreeBSDGuessExecPath(const TString& guessPath, TString& execPath) {
if (NFs::Exists(guessPath)) {
- // now it should work for real
- execPath = FreeBSDGetExecPath();
- if (RealPath(execPath) == RealPath(guessPath)) {
- return true;
- }
- }
- return false;
-}
-
+ // now it should work for real
+ execPath = FreeBSDGetExecPath();
+ if (RealPath(execPath) == RealPath(guessPath)) {
+ return true;
+ }
+ }
+ return false;
+}
+
static inline bool FreeBSDGuessExecBasePath(const TString& guessBasePath, TString& execPath) {
return FreeBSDGuessExecPath(TString(guessBasePath) + "/" + getprogname(), execPath);
-}
-
+}
+
#endif
static TString GetExecPathImpl() {
#if defined(_solaris_)
return execname();
#elif defined(_darwin_)
- TTempBuf execNameBuf;
- for (size_t i = 0; i < 2; ++i) {
+ TTempBuf execNameBuf;
+ for (size_t i = 0; i < 2; ++i) {
std::remove_pointer_t<TFunctionArg<decltype(_NSGetExecutablePath), 1>> bufsize = execNameBuf.Size();
int r = _NSGetExecutablePath(execNameBuf.Data(), &bufsize);
if (r == 0) {
return execNameBuf.Data();
- } else if (r == -1) {
- execNameBuf = TTempBuf(bufsize);
+ } else if (r == -1) {
+ execNameBuf = TTempBuf(bufsize);
}
}
- ythrow yexception() << "GetExecPathImpl() failed";
+ ythrow yexception() << "GetExecPathImpl() failed";
#elif defined(_win_)
- TTempBuf execNameBuf;
+ TTempBuf execNameBuf;
for (;;) {
DWORD r = GetModuleFileName(nullptr, execNameBuf.Data(), execNameBuf.Size());
- if (r == execNameBuf.Size()) {
- execNameBuf = TTempBuf(execNameBuf.Size() * 2);
- } else if (r == 0) {
- ythrow yexception() << "GetExecPathImpl() failed: " << LastSystemErrorText();
- } else {
+ if (r == execNameBuf.Size()) {
+ execNameBuf = TTempBuf(execNameBuf.Size() * 2);
+ } else if (r == 0) {
+ ythrow yexception() << "GetExecPathImpl() failed: " << LastSystemErrorText();
+ } else {
return execNameBuf.Data();
}
}
@@ -133,23 +133,23 @@ static TString GetExecPathImpl() {
// TODO(yoda): check if the filename ends with " (deleted)"
#elif defined(_freebsd_)
TString execPath = FreeBSDGetExecPath();
- if (GoodPath(execPath)) {
- return execPath;
+ if (GoodPath(execPath)) {
+ return execPath;
+ }
+ if (FreeBSDGuessExecPath(FreeBSDGetArgv0(), execPath)) {
+ return execPath;
}
- if (FreeBSDGuessExecPath(FreeBSDGetArgv0(), execPath)) {
- return execPath;
+ if (FreeBSDGuessExecPath(getenv("_"), execPath)) {
+ return execPath;
}
- if (FreeBSDGuessExecPath(getenv("_"), execPath)) {
- return execPath;
+ if (FreeBSDGuessExecBasePath(getenv("PWD"), execPath)) {
+ return execPath;
}
- if (FreeBSDGuessExecBasePath(getenv("PWD"), execPath)) {
- return execPath;
- }
if (FreeBSDGuessExecBasePath(NFs::CurrentWorkingDirectory(), execPath)) {
- return execPath;
- }
+ return execPath;
+ }
- ythrow yexception() << "can not resolve exec path";
+ ythrow yexception() << "can not resolve exec path";
#else
#error dont know how to implement GetExecPath on this platform
#endif