OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 <mach-o/dyld.h> | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 #include <string.h> | |
13 #include <unistd.h> | |
14 | |
15 #include "chrome/common/chrome_version.h" | |
16 | |
17 typedef int (*ChromeMainPtr)(int, char**); | |
18 | |
19 __attribute__((visibility("default"))) int main(int argc, char* argv[]) { | |
20 #if defined(HELPER_EXECUTABLE) | |
21 const char* const rel_path = | |
22 "../../../" PRODUCT_FULLNAME_STRING | |
23 " Framework.framework/" PRODUCT_FULLNAME_STRING " Framework"; | |
24 #else | |
25 const char* const rel_path = | |
26 "../Versions/" CHROME_VERSION_STRING "/" PRODUCT_FULLNAME_STRING | |
27 " Framework.framework/" PRODUCT_FULLNAME_STRING " Framework"; | |
28 #endif // defined(HELPER_EXECUTABLE) | |
29 | |
30 uint32_t exec_path_size = 0; | |
31 int rc = _NSGetExecutablePath(NULL, &exec_path_size); | |
32 if (rc != -1) { | |
33 fprintf(stderr, | |
34 "Unexpected return condition from _NSGetExecutablePath().\n"); | |
35 abort(); | |
36 } | |
37 | |
38 char* exec_path = malloc(exec_path_size); | |
Mark Mentovai
2015/10/22 23:15:11
fprintf(stderr, "malloc %zu: %s\n", framework_path
Greg K
2015/10/23 19:12:49
Done.
| |
39 if (!exec_path) { | |
40 fprintf(stderr, "Failed to allocate memory.\n"); | |
41 abort(); | |
42 } | |
43 | |
44 rc = _NSGetExecutablePath(exec_path, &exec_path_size); | |
45 if (rc != 0) { | |
46 fprintf(stderr, | |
47 "_NSGetExecutablePath() call failed, " | |
48 "cannot calculate framework path.\n"); | |
49 abort(); | |
50 } | |
51 | |
52 // Slice off the last part of the main executable path, and append the | |
53 // version framework information. | |
54 char* last_slash = strrchr(exec_path, '/'); | |
Mark Mentovai
2015/10/22 23:15:11
Hmm, this will be messed-up with valid but stupid
Greg K
2015/10/23 19:12:48
Done.
| |
55 if (last_slash) { | |
56 *(last_slash + 1) = '\0'; | |
57 } else { | |
58 fprintf(stderr, "Unexpected executable path.\n"); | |
59 abort(); | |
60 } | |
61 | |
62 size_t exec_path_len = strlen(exec_path); | |
63 size_t rel_path_len = strlen(rel_path); | |
64 size_t framework_path_size = exec_path_len + rel_path_len + 1; | |
65 char* framework_path = malloc(framework_path_size); | |
66 if (!framework_path) { | |
67 fprintf(stderr, "Failed to allocate memory.\n"); | |
68 abort(); | |
69 } | |
70 | |
71 memcpy(framework_path, exec_path, exec_path_len); | |
72 memcpy(framework_path + exec_path_len, rel_path, rel_path_len); | |
73 framework_path[exec_path_len + rel_path_len] = '\0'; | |
Mark Mentovai
2015/10/22 23:15:12
snprintf(framework_path, framework_path_size, "%s/
Greg K
2015/10/23 19:12:49
Done.
| |
74 | |
75 void* library = dlopen(framework_path, RTLD_LAZY); | |
Mark Mentovai
2015/10/22 23:15:11
| RTLD_LOCAL | RTLD_FIRST
Greg K
2015/10/23 19:12:48
Done.
| |
76 if (!library) { | |
77 fprintf(stderr, "Unable to dlopen() the framework.\n"); | |
Mark Mentovai
2015/10/22 23:15:11
Say the framework name dlerror(). Something like
Greg K
2015/10/23 19:12:48
Done.
| |
78 abort(); | |
79 } | |
Mark Mentovai
2015/10/22 23:15:11
free() framework_path now.
Greg K
2015/10/23 19:12:48
Done.
| |
80 | |
81 ChromeMainPtr ChromeMain = dlsym(library, "ChromeMain"); | |
Mark Mentovai
2015/10/22 23:15:11
It’s still a variable, call it chrome_main
Greg K
2015/10/23 19:12:48
Done.
| |
82 if (!ChromeMain) { | |
83 fprintf(stderr, "Could not resolve the ChromeMain symbol.\n"); | |
Mark Mentovai
2015/10/22 23:15:11
Similarly, fprintf(stderr, "dlopen ChromeMain: %s\
Greg K
2015/10/23 19:12:49
Done.
| |
84 abort(); | |
85 } | |
86 int rv = ChromeMain(argc, argv); | |
87 | |
88 // Free all resources here. | |
Mark Mentovai
2015/10/22 23:15:11
Technically the library handle is a freeable resou
Greg K
2015/10/23 19:12:48
Done.
| |
89 free(exec_path); | |
90 free(framework_path); | |
91 | |
92 // exit, don't return from main, to avoid the apparent removal of main from | |
93 // stack backtraces under tail call optimization. | |
94 exit(rv); | |
95 } | |
OLD | NEW |