| 92 | #endif |
| 93 | |
| 94 | static TString GetExecPathImpl() { |
| 95 | #if defined(_solaris_) |
| 96 | return execname(); |
| 97 | #elif defined(_darwin_) |
| 98 | TTempBuf execNameBuf; |
| 99 | for (size_t i = 0; i < 2; ++i) { |
| 100 | std::remove_pointer_t<TFunctionArg<decltype(_NSGetExecutablePath), 1>> bufsize = execNameBuf.Size(); |
| 101 | int r = _NSGetExecutablePath(execNameBuf.Data(), &bufsize); |
| 102 | if (r == 0) { |
| 103 | return execNameBuf.Data(); |
| 104 | } else if (r == -1) { |
| 105 | execNameBuf = TTempBuf(bufsize); |
| 106 | } |
| 107 | } |
| 108 | ythrow yexception() << "GetExecPathImpl() failed"; |
| 109 | #elif defined(_win_) |
| 110 | TTempBuf execNameBuf; |
| 111 | for (;;) { |
| 112 | DWORD r = GetModuleFileName(nullptr, execNameBuf.Data(), execNameBuf.Size()); |
| 113 | if (r == execNameBuf.Size()) { |
| 114 | execNameBuf = TTempBuf(execNameBuf.Size() * 2); |
| 115 | } else if (r == 0) { |
| 116 | ythrow yexception() << "GetExecPathImpl() failed: " << LastSystemErrorText(); |
| 117 | } else { |
| 118 | return execNameBuf.Data(); |
| 119 | } |
| 120 | } |
| 121 | #elif defined(_linux_) || defined(_cygwin_) |
| 122 | TString path("/proc/self/exe"); |
| 123 | return NFs::ReadLink(path); |
| 124 | // TODO(yoda): check if the filename ends with " (deleted)" |
| 125 | #elif defined(_freebsd_) |
| 126 | TString execPath = FreeBSDGetExecPath(); |
| 127 | if (GoodPath(execPath)) { |
| 128 | return execPath; |
| 129 | } |
| 130 | if (FreeBSDGuessExecPath(FreeBSDGetArgv0(), execPath)) { |
| 131 | return execPath; |
| 132 | } |
| 133 | if (FreeBSDGuessExecPath(getenv("_"), execPath)) { |
| 134 | return execPath; |
| 135 | } |
| 136 | if (FreeBSDGuessExecBasePath(getenv("PWD"), execPath)) { |
| 137 | return execPath; |
| 138 | } |
| 139 | if (FreeBSDGuessExecBasePath(NFs::CurrentWorkingDirectory(), execPath)) { |
| 140 | return execPath; |
| 141 | } |
| 142 | |
| 143 | ythrow yexception() << "can not resolve exec path"; |
| 144 | #else |
| 145 | #error dont know how to implement GetExecPath on this platform |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | static bool GetPersistentExecPathImpl(TString& to) { |
| 150 | #if defined(_solaris_) |
no test coverage detected