aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/execpath.cpp
blob: 3877c912e729732f335f8a766c7e5ee994af7278 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "platform.h"

#include <stdlib.h>

#if defined(_solaris_)
    #include <stdlib.h>
#elif defined(_darwin_)
    #include <mach-o/dyld.h>
#elif defined(_win_)
    #include "winint.h"
    #include <io.h>
#elif defined(_linux_)
    #include <unistd.h>
#elif defined(_freebsd_)
    #include <string.h>
    #include <sys/types.h> // for u_int not defined in sysctl.h
    #include <sys/sysctl.h>
    #include <unistd.h>
#endif

#include <util/folder/dirut.h>
#include <util/generic/singleton.h>
#include <util/generic/function.h>
#include <util/generic/yexception.h>
#include <util/memory/tempbuf.h>
#include <util/stream/file.h>
#include <util/stream/pipe.h>
#include <util/string/cast.h>

#include "filemap.h"
#include "execpath.h"
#include "fs.h"

#if defined(_freebsd_)
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();
        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;
        }
    }
    return errno;
}

static inline TString FreeBSDGetExecPath() { 
    int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
    TTempBuf buf;
    int r = FreeBSDSysCtl(mib, Y_ARRAY_SIZE(mib), buf);
    if (r == 0) {
        return TString(buf.Data(), buf.Filled() - 1); 
    } else if (r == ENOTSUP) { // older FreeBSD version
        /*
         * BSD analogue for /proc/self is /proc/curproc.
         * See:
         * https://www.freebsd.org/cgi/man.cgi?query=procfs&sektion=5&format=html
         */
        TString path("/proc/curproc/file");
        return NFs::ReadLink(path);
    } else {
        return TString(); 
    }
}

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) {
        return TString(buf.Data()); 
    } else if (r == ENOTSUP) {
        return TString(); 
    } 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;
}

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) {
        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);
        }
    }
    ythrow yexception() << "GetExecPathImpl() failed";
#elif defined(_win_)
    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 {
            return execNameBuf.Data();
        }
    }
#elif defined(_linux_) || defined(_cygwin_)
    TString path("/proc/self/exe");
    return NFs::ReadLink(path);
// TODO(yoda): check if the filename ends with " (deleted)"
#elif defined(_freebsd_)
    TString execPath = FreeBSDGetExecPath(); 
    if (GoodPath(execPath)) {
        return execPath;
    }
    if (FreeBSDGuessExecPath(FreeBSDGetArgv0(), execPath)) {
        return execPath;
    }
    if (FreeBSDGuessExecPath(getenv("_"), execPath)) {
        return execPath;
    }
    if (FreeBSDGuessExecBasePath(getenv("PWD"), execPath)) {
        return execPath;
    }
    if (FreeBSDGuessExecBasePath(NFs::CurrentWorkingDirectory(), execPath)) {
        return execPath;
    }

    ythrow yexception() << "can not resolve exec path";
#else
    #error dont know how to implement GetExecPath on this platform
#endif
}

static bool GetPersistentExecPathImpl(TString& to) {
#if defined(_solaris_)
    to = TString("/proc/self/object/a.out");
    return true;
#elif defined(_linux_) || defined(_cygwin_)
    to = TString("/proc/self/exe");
    return true;
#elif defined(_freebsd_)
    to = TString("/proc/curproc/file");
    return true;
#else // defined(_win_) || defined(_darwin_)  or unknown
    Y_UNUSED(to);
    return false;
#endif
}

namespace {
    struct TExecPathsHolder {
        inline TExecPathsHolder() {
            ExecPath = GetExecPathImpl();

            if (!GetPersistentExecPathImpl(PersistentExecPath)) {
                PersistentExecPath = ExecPath;
            }
        }

        static inline auto Instance() {
            return SingletonWithPriority<TExecPathsHolder, 1>();
        }

        TString ExecPath; 
        TString PersistentExecPath;
    };
}

const TString& GetExecPath() { 
    return TExecPathsHolder::Instance()->ExecPath;
}

const TString& GetPersistentExecPath() {
    return TExecPathsHolder::Instance()->PersistentExecPath;
}