| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // The entry point for all Mac Chromium processes, including the outer app | |
| 6 // bundle (browser) and helper app (renderer, plugin, and friends). | |
| 7 | |
| 8 #include <dlfcn.h> | |
| 9 #include <errno.h> | |
| 10 #include <libgen.h> | |
| 11 #include <mach-o/dyld.h> | |
| 12 #include <stddef.h> | |
| 13 #include <stdint.h> | |
| 14 #include <stdio.h> | |
| 15 #include <stdlib.h> | |
| 16 #include <string.h> | |
| 17 #include <unistd.h> | |
| 18 | |
| 19 #include "chrome/common/chrome_version.h" | |
| 20 | |
| 21 typedef int (*ChromeMainPtr)(int, char**); | |
| 22 | |
| 23 __attribute__((visibility("default"))) int main(int argc, char* argv[]) { | |
| 24 #if defined(HELPER_EXECUTABLE) | |
| 25 const char* const rel_path = | |
| 26 "../../../" PRODUCT_FULLNAME_STRING | |
| 27 " Framework.framework/" PRODUCT_FULLNAME_STRING " Framework"; | |
| 28 #else | |
| 29 const char* const rel_path = | |
| 30 "../Versions/" CHROME_VERSION_STRING "/" PRODUCT_FULLNAME_STRING | |
| 31 " Framework.framework/" PRODUCT_FULLNAME_STRING " Framework"; | |
| 32 #endif // defined(HELPER_EXECUTABLE) | |
| 33 | |
| 34 uint32_t exec_path_size = 0; | |
| 35 int rv = _NSGetExecutablePath(NULL, &exec_path_size); | |
| 36 if (rv != -1) { | |
| 37 fprintf(stderr, "_NSGetExecutablePath: get length failed\n"); | |
| 38 abort(); | |
| 39 } | |
| 40 | |
| 41 char* exec_path = malloc(exec_path_size); | |
| 42 if (!exec_path) { | |
| 43 fprintf(stderr, "malloc %u: %s\n", exec_path_size, strerror(errno)); | |
| 44 abort(); | |
| 45 } | |
| 46 | |
| 47 rv = _NSGetExecutablePath(exec_path, &exec_path_size); | |
| 48 if (rv != 0) { | |
| 49 fprintf(stderr, "_NSGetExecutablePath: get path failed\n"); | |
| 50 abort(); | |
| 51 } | |
| 52 | |
| 53 // Slice off the last part of the main executable path, and append the | |
| 54 // version framework information. | |
| 55 const char* parent_dir = dirname(exec_path); | |
| 56 if (!parent_dir) { | |
| 57 fprintf(stderr, "dirname %s: %s\n", exec_path, strerror(errno)); | |
| 58 abort(); | |
| 59 } | |
| 60 free(exec_path); | |
| 61 | |
| 62 const size_t parent_path_len = strlen(parent_dir); | |
| 63 const size_t rel_path_len = strlen(rel_path); | |
| 64 // 2 accounts for a trailing NUL byte and the '/' in the middle of the paths. | |
| 65 const size_t framework_path_size = parent_path_len + rel_path_len + 2; | |
| 66 char* framework_path = malloc(framework_path_size); | |
| 67 if (!framework_path) { | |
| 68 fprintf(stderr, "malloc %zu: %s\n", framework_path_size, strerror(errno)); | |
| 69 abort(); | |
| 70 } | |
| 71 snprintf(framework_path, framework_path_size, "%s/%s", parent_dir, rel_path); | |
| 72 | |
| 73 void* library = dlopen(framework_path, RTLD_LAZY | RTLD_LOCAL | RTLD_FIRST); | |
| 74 if (!library) { | |
| 75 fprintf(stderr, "dlopen %s: %s\n", framework_path, dlerror()); | |
| 76 abort(); | |
| 77 } | |
| 78 free(framework_path); | |
| 79 | |
| 80 const ChromeMainPtr chrome_main = dlsym(library, "ChromeMain"); | |
| 81 if (!chrome_main) { | |
| 82 fprintf(stderr, "dlsym ChromeMain: %s\n", dlerror()); | |
| 83 abort(); | |
| 84 } | |
| 85 rv = chrome_main(argc, argv); | |
| 86 | |
| 87 // exit, don't return from main, to avoid the apparent removal of main from | |
| 88 // stack backtraces under tail call optimization. | |
| 89 exit(rv); | |
| 90 } | |
| OLD | NEW |