Bug 1910362 - Create new Nimbus helper r=aaronmt,ohorvath
[gecko.git] / xpcom / build / BinaryPath.h
blob1718caa3c66d17eb5b73e8123e6efd7f5cc0ed70
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_BinaryPath_h
8 #define mozilla_BinaryPath_h
10 #include "nsXPCOMPrivate.h" // for MAXPATHLEN
11 #ifdef XP_WIN
12 # include <windows.h>
13 #elif defined(XP_DARWIN)
14 # include <CoreFoundation/CoreFoundation.h>
15 #elif defined(XP_UNIX)
16 # include <unistd.h>
17 # include <stdlib.h>
18 # include <string.h>
19 #endif
20 #if defined(__FreeBSD__) || defined(__DragonFly__) || \
21 defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__)
22 # include <sys/sysctl.h>
23 #endif
24 #if defined(__OpenBSD__)
25 # include <sys/stat.h>
26 #endif
27 #include "mozilla/UniquePtr.h"
28 #include "mozilla/UniquePtrExtensions.h"
30 #ifdef MOZILLA_INTERNAL_API
31 # include "nsCOMPtr.h"
32 # include "nsIFile.h"
33 # include "nsString.h"
34 #endif
36 namespace mozilla {
38 class BinaryPath {
39 public:
40 #ifdef XP_WIN
41 static nsresult Get(char aResult[MAXPATHLEN]) {
42 wchar_t wide_path[MAXPATHLEN];
43 nsresult rv = GetW(wide_path);
44 if (NS_FAILED(rv)) {
45 return rv;
47 WideCharToMultiByte(CP_UTF8, 0, wide_path, -1, aResult, MAXPATHLEN, nullptr,
48 nullptr);
49 return NS_OK;
52 static nsresult GetLong(wchar_t aResult[MAXPATHLEN]) {
53 static bool cached = false;
54 static wchar_t exeLongPath[MAXPATHLEN] = L"";
56 if (!cached) {
57 nsresult rv = GetW(exeLongPath);
59 if (NS_FAILED(rv)) {
60 return rv;
63 if (!::GetLongPathNameW(exeLongPath, exeLongPath, MAXPATHLEN)) {
64 return NS_ERROR_FAILURE;
67 cached = true;
70 if (wcscpy_s(aResult, MAXPATHLEN, exeLongPath)) {
71 return NS_ERROR_FAILURE;
74 return NS_OK;
77 private:
78 static nsresult GetW(wchar_t aResult[MAXPATHLEN]) {
79 static bool cached = false;
80 static wchar_t moduleFileName[MAXPATHLEN] = L"";
82 if (!cached) {
83 if (!::GetModuleFileNameW(0, moduleFileName, MAXPATHLEN)) {
84 return NS_ERROR_FAILURE;
87 cached = true;
90 if (wcscpy_s(aResult, MAXPATHLEN, moduleFileName)) {
91 return NS_ERROR_FAILURE;
94 return NS_OK;
97 #elif defined(XP_DARWIN)
98 static nsresult Get(char aResult[MAXPATHLEN]) {
99 // Works even if we're not bundled.
100 CFBundleRef appBundle = CFBundleGetMainBundle();
101 if (!appBundle) {
102 return NS_ERROR_FAILURE;
105 CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
106 if (!executableURL) {
107 return NS_ERROR_FAILURE;
110 nsresult rv;
111 if (CFURLGetFileSystemRepresentation(executableURL, false, (UInt8*)aResult,
112 MAXPATHLEN)) {
113 // Sanitize path in case the app was launched from Terminal via
114 // './firefox' for example.
115 size_t readPos = 0;
116 size_t writePos = 0;
117 while (aResult[readPos] != '\0') {
118 if (aResult[readPos] == '.' && aResult[readPos + 1] == '/') {
119 readPos += 2;
120 } else {
121 aResult[writePos] = aResult[readPos];
122 readPos++;
123 writePos++;
126 aResult[writePos] = '\0';
127 rv = NS_OK;
128 } else {
129 rv = NS_ERROR_FAILURE;
132 CFRelease(executableURL);
133 return rv;
136 #elif defined(ANDROID)
137 static nsresult Get(char aResult[MAXPATHLEN]) {
138 // On Android, we use the MOZ_ANDROID_LIBDIR variable that is set by the
139 // Java bootstrap code.
140 const char* libDir = getenv("MOZ_ANDROID_LIBDIR");
141 if (!libDir) {
142 return NS_ERROR_FAILURE;
145 snprintf(aResult, MAXPATHLEN, "%s/%s", libDir, "dummy");
146 aResult[MAXPATHLEN - 1] = '\0';
147 return NS_OK;
150 #elif defined(XP_LINUX) || defined(XP_SOLARIS)
151 static nsresult Get(char aResult[MAXPATHLEN]) {
152 # if defined(XP_SOLARIS)
153 const char path[] = "/proc/self/path/a.out";
154 # else
155 const char path[] = "/proc/self/exe";
156 # endif
158 ssize_t len = readlink(path, aResult, MAXPATHLEN - 1);
159 if (len < 0) {
160 return NS_ERROR_FAILURE;
162 aResult[len] = '\0';
163 # if defined(XP_LINUX)
164 // Removing suffix " (deleted)" from the binary path
165 const char suffix[] = " (deleted)";
166 const ssize_t suffix_len = sizeof(suffix);
167 if (len >= suffix_len) {
168 char* result_end = &aResult[len - (suffix_len - 1)];
169 if (memcmp(result_end, suffix, suffix_len) == 0) {
170 *result_end = '\0';
173 # endif
174 return NS_OK;
177 #elif defined(__FreeBSD__) || defined(__DragonFly__) || \
178 defined(__FreeBSD_kernel__) || defined(__NetBSD__)
179 static nsresult Get(char aResult[MAXPATHLEN]) {
180 int mib[4];
181 mib[0] = CTL_KERN;
182 # ifdef __NetBSD__
183 mib[1] = KERN_PROC_ARGS;
184 mib[2] = -1;
185 mib[3] = KERN_PROC_PATHNAME;
186 # else
187 mib[1] = KERN_PROC;
188 mib[2] = KERN_PROC_PATHNAME;
189 mib[3] = -1;
190 # endif
192 size_t len = MAXPATHLEN;
193 if (sysctl(mib, 4, aResult, &len, nullptr, 0) < 0) {
194 return NS_ERROR_FAILURE;
197 return NS_OK;
200 #elif defined(__OpenBSD__)
201 static nsresult Get(char aResult[MAXPATHLEN]) {
202 static bool cached = false;
203 static char cachedPath[MAXPATHLEN];
204 nsresult r;
205 int mib[4];
206 if (cached) {
207 if (strlcpy(aResult, cachedPath, MAXPATHLEN) >= MAXPATHLEN) {
208 return NS_ERROR_FAILURE;
210 return NS_OK;
212 mib[0] = CTL_KERN;
213 mib[1] = KERN_PROC_ARGS;
214 mib[2] = getpid();
215 mib[3] = KERN_PROC_ARGV;
217 size_t len = 0;
218 if (sysctl(mib, 4, nullptr, &len, nullptr, 0) < 0) {
219 return NS_ERROR_FAILURE;
222 auto argv = MakeUnique<const char*[]>(len / sizeof(const char*));
223 if (sysctl(mib, 4, argv.get(), &len, nullptr, 0) < 0) {
224 return NS_ERROR_FAILURE;
227 r = GetFromArgv0(argv[0], aResult);
228 if (NS_SUCCEEDED(r)) {
229 if (strlcpy(cachedPath, aResult, MAXPATHLEN) >= MAXPATHLEN) {
230 return NS_ERROR_FAILURE;
232 cached = true;
234 return r;
237 static nsresult GetFromArgv0(const char* aArgv0, char aResult[MAXPATHLEN]) {
238 struct stat fileStat;
239 // 1) use realpath() on argv[0], which works unless we're loaded from the
240 // PATH. Only do so if argv[0] looks like a path (contains a /).
241 // 2) manually walk through the PATH and look for ourself
242 // 3) give up
243 if (strchr(aArgv0, '/') && realpath(aArgv0, aResult) &&
244 stat(aResult, &fileStat) == 0) {
245 return NS_OK;
248 const char* path = getenv("PATH");
249 if (!path) {
250 return NS_ERROR_FAILURE;
253 char* pathdup = strdup(path);
254 if (!pathdup) {
255 return NS_ERROR_OUT_OF_MEMORY;
258 bool found = false;
259 char* token = strtok(pathdup, ":");
260 while (token) {
261 char tmpPath[MAXPATHLEN];
262 sprintf(tmpPath, "%s/%s", token, aArgv0);
263 if (realpath(tmpPath, aResult) && stat(aResult, &fileStat) == 0) {
264 found = true;
265 break;
267 token = strtok(nullptr, ":");
269 free(pathdup);
270 if (found) {
271 return NS_OK;
273 return NS_ERROR_FAILURE;
276 #else
277 # error Oops, you need platform-specific code here
278 #endif
280 public:
281 static UniqueFreePtr<char> Get() {
282 char path[MAXPATHLEN];
283 if (NS_FAILED(Get(path))) {
284 return nullptr;
286 UniqueFreePtr<char> result;
287 result.reset(strdup(path));
288 return result;
291 #ifdef MOZILLA_INTERNAL_API
292 static nsresult GetFile(nsIFile** aResult) {
293 nsCOMPtr<nsIFile> lf;
294 # ifdef XP_WIN
295 wchar_t exePath[MAXPATHLEN];
296 nsresult rv = GetW(exePath);
297 # else
298 char exePath[MAXPATHLEN];
299 nsresult rv = Get(exePath);
300 # endif
301 if (NS_FAILED(rv)) {
302 return rv;
304 MOZ_TRY(NS_NewPathStringLocalFile(DependentPathString(exePath),
305 getter_AddRefs(lf)));
306 lf.forget(aResult);
307 return NS_OK;
309 #endif
312 } // namespace mozilla
314 #endif /* mozilla_BinaryPath_h */