1 | n/a | /* |
---|
2 | n/a | * This wrapper program executes a python executable hidden inside an |
---|
3 | n/a | * application bundle inside the Python framework. This is needed to run |
---|
4 | n/a | * GUI code: some GUI API's don't work unless the program is inside an |
---|
5 | n/a | * application bundle. |
---|
6 | n/a | * |
---|
7 | n/a | * This program uses posix_spawn rather than plain execv because we need |
---|
8 | n/a | * slightly more control over how the "real" interpreter is executed. |
---|
9 | n/a | * |
---|
10 | n/a | * On OSX 10.4 (and earlier) this falls back to using exec because the |
---|
11 | n/a | * posix_spawnv functions aren't available there. |
---|
12 | n/a | */ |
---|
13 | n/a | |
---|
14 | n/a | #pragma weak_import posix_spawnattr_init |
---|
15 | n/a | #pragma weak_import posix_spawnattr_setbinpref_np |
---|
16 | n/a | #pragma weak_import posix_spawnattr_setflags |
---|
17 | n/a | #pragma weak_import posix_spawn |
---|
18 | n/a | |
---|
19 | n/a | #include <Python.h> |
---|
20 | n/a | #include <unistd.h> |
---|
21 | n/a | #ifdef HAVE_SPAWN_H |
---|
22 | n/a | #include <spawn.h> |
---|
23 | n/a | #endif |
---|
24 | n/a | #include <stdio.h> |
---|
25 | n/a | #include <string.h> |
---|
26 | n/a | #include <errno.h> |
---|
27 | n/a | #include <err.h> |
---|
28 | n/a | #include <dlfcn.h> |
---|
29 | n/a | #include <stdlib.h> |
---|
30 | n/a | #include <Python.h> |
---|
31 | n/a | #include <mach-o/dyld.h> |
---|
32 | n/a | |
---|
33 | n/a | |
---|
34 | n/a | extern char** environ; |
---|
35 | n/a | |
---|
36 | n/a | /* |
---|
37 | n/a | * Locate the python framework by looking for the |
---|
38 | n/a | * library that contains Py_Initialize. |
---|
39 | n/a | * |
---|
40 | n/a | * In a regular framework the structure is: |
---|
41 | n/a | * |
---|
42 | n/a | * Python.framework/Versions/2.7 |
---|
43 | n/a | * /Python |
---|
44 | n/a | * /Resources/Python.app/Contents/MacOS/Python |
---|
45 | n/a | * |
---|
46 | n/a | * In a virtualenv style structure the expected |
---|
47 | n/a | * structure is: |
---|
48 | n/a | * |
---|
49 | n/a | * ROOT |
---|
50 | n/a | * /bin/pythonw |
---|
51 | n/a | * /.Python <- the dylib |
---|
52 | n/a | * /.Resources/Python.app/Contents/MacOS/Python |
---|
53 | n/a | * |
---|
54 | n/a | * NOTE: virtualenv's are not an officially supported |
---|
55 | n/a | * feature, support for that structure is provided as |
---|
56 | n/a | * a convenience. |
---|
57 | n/a | */ |
---|
58 | n/a | static char* get_python_path(void) |
---|
59 | n/a | { |
---|
60 | n/a | size_t len; |
---|
61 | n/a | Dl_info info; |
---|
62 | n/a | char* end; |
---|
63 | n/a | char* g_path; |
---|
64 | n/a | |
---|
65 | n/a | if (dladdr(Py_Initialize, &info) == 0) { |
---|
66 | n/a | return NULL; |
---|
67 | n/a | } |
---|
68 | n/a | |
---|
69 | n/a | len = strlen(info.dli_fname); |
---|
70 | n/a | |
---|
71 | n/a | g_path = malloc(len+60); |
---|
72 | n/a | if (g_path == NULL) { |
---|
73 | n/a | return NULL; |
---|
74 | n/a | } |
---|
75 | n/a | |
---|
76 | n/a | strcpy(g_path, info.dli_fname); |
---|
77 | n/a | end = g_path + len - 1; |
---|
78 | n/a | while (end != g_path && *end != '/') { |
---|
79 | n/a | end --; |
---|
80 | n/a | } |
---|
81 | n/a | end++; |
---|
82 | n/a | if (*end == '.') { |
---|
83 | n/a | end++; |
---|
84 | n/a | } |
---|
85 | n/a | strcpy(end, "Resources/Python.app/Contents/MacOS/" PYTHONFRAMEWORK); |
---|
86 | n/a | |
---|
87 | n/a | return g_path; |
---|
88 | n/a | } |
---|
89 | n/a | |
---|
90 | n/a | #ifdef HAVE_SPAWN_H |
---|
91 | n/a | static void |
---|
92 | n/a | setup_spawnattr(posix_spawnattr_t* spawnattr) |
---|
93 | n/a | { |
---|
94 | n/a | size_t ocount; |
---|
95 | n/a | size_t count; |
---|
96 | n/a | cpu_type_t cpu_types[1]; |
---|
97 | n/a | short flags = 0; |
---|
98 | n/a | #ifdef __LP64__ |
---|
99 | n/a | int ch; |
---|
100 | n/a | #endif |
---|
101 | n/a | |
---|
102 | n/a | if ((errno = posix_spawnattr_init(spawnattr)) != 0) { |
---|
103 | n/a | err(2, "posix_spawnattr_int"); |
---|
104 | n/a | /* NOTREACHTED */ |
---|
105 | n/a | } |
---|
106 | n/a | |
---|
107 | n/a | count = 1; |
---|
108 | n/a | |
---|
109 | n/a | /* Run the real python executable using the same architecture as this |
---|
110 | n/a | * executable, this allows users to control the architecture using |
---|
111 | n/a | * "arch -ppc python" |
---|
112 | n/a | */ |
---|
113 | n/a | |
---|
114 | n/a | #if defined(__ppc64__) |
---|
115 | n/a | cpu_types[0] = CPU_TYPE_POWERPC64; |
---|
116 | n/a | |
---|
117 | n/a | #elif defined(__x86_64__) |
---|
118 | n/a | cpu_types[0] = CPU_TYPE_X86_64; |
---|
119 | n/a | |
---|
120 | n/a | #elif defined(__ppc__) |
---|
121 | n/a | cpu_types[0] = CPU_TYPE_POWERPC; |
---|
122 | n/a | #elif defined(__i386__) |
---|
123 | n/a | cpu_types[0] = CPU_TYPE_X86; |
---|
124 | n/a | #else |
---|
125 | n/a | # error "Unknown CPU" |
---|
126 | n/a | #endif |
---|
127 | n/a | |
---|
128 | n/a | if (posix_spawnattr_setbinpref_np(spawnattr, count, |
---|
129 | n/a | cpu_types, &ocount) == -1) { |
---|
130 | n/a | err(1, "posix_spawnattr_setbinpref"); |
---|
131 | n/a | /* NOTREACHTED */ |
---|
132 | n/a | } |
---|
133 | n/a | if (count != ocount) { |
---|
134 | n/a | fprintf(stderr, "posix_spawnattr_setbinpref failed to copy\n"); |
---|
135 | n/a | exit(1); |
---|
136 | n/a | /* NOTREACHTED */ |
---|
137 | n/a | } |
---|
138 | n/a | |
---|
139 | n/a | |
---|
140 | n/a | /* |
---|
141 | n/a | * Set flag that causes posix_spawn to behave like execv |
---|
142 | n/a | */ |
---|
143 | n/a | flags |= POSIX_SPAWN_SETEXEC; |
---|
144 | n/a | if ((errno = posix_spawnattr_setflags(spawnattr, flags)) != 0) { |
---|
145 | n/a | err(1, "posix_spawnattr_setflags"); |
---|
146 | n/a | /* NOTREACHTED */ |
---|
147 | n/a | } |
---|
148 | n/a | } |
---|
149 | n/a | #endif |
---|
150 | n/a | |
---|
151 | n/a | int |
---|
152 | n/a | main(int argc, char **argv) { |
---|
153 | n/a | char* exec_path = get_python_path(); |
---|
154 | n/a | static char path[PATH_MAX * 2]; |
---|
155 | n/a | static char real_path[PATH_MAX * 2]; |
---|
156 | n/a | int status; |
---|
157 | n/a | uint32_t size = PATH_MAX * 2; |
---|
158 | n/a | |
---|
159 | n/a | /* Set the original executable path in the environment. */ |
---|
160 | n/a | status = _NSGetExecutablePath(path, &size); |
---|
161 | n/a | if (status == 0) { |
---|
162 | n/a | /* |
---|
163 | n/a | * Note: don't call 'realpath', that will |
---|
164 | n/a | * erase symlink information, and that |
---|
165 | n/a | * breaks "pyvenv --symlink" |
---|
166 | n/a | * |
---|
167 | n/a | * It is nice to have the directory name |
---|
168 | n/a | * as a cleaned up absolute path though, |
---|
169 | n/a | * therefore call realpath on dirname(path) |
---|
170 | n/a | */ |
---|
171 | n/a | char* slash = strrchr(path, '/'); |
---|
172 | n/a | if (slash) { |
---|
173 | n/a | char replaced; |
---|
174 | n/a | replaced = slash[1]; |
---|
175 | n/a | slash[1] = 0; |
---|
176 | n/a | if (realpath(path, real_path) == NULL) { |
---|
177 | n/a | err(1, "realpath: %s", path); |
---|
178 | n/a | } |
---|
179 | n/a | slash[1] = replaced; |
---|
180 | n/a | if (strlcat(real_path, slash, sizeof(real_path)) > sizeof(real_path)) { |
---|
181 | n/a | errno = EINVAL; |
---|
182 | n/a | err(1, "realpath: %s", path); |
---|
183 | n/a | } |
---|
184 | n/a | |
---|
185 | n/a | } else { |
---|
186 | n/a | if (realpath(".", real_path) == NULL) { |
---|
187 | n/a | err(1, "realpath: %s", path); |
---|
188 | n/a | } |
---|
189 | n/a | if (strlcat(real_path, "/", sizeof(real_path)) > sizeof(real_path)) { |
---|
190 | n/a | errno = EINVAL; |
---|
191 | n/a | err(1, "realpath: %s", path); |
---|
192 | n/a | } |
---|
193 | n/a | if (strlcat(real_path, path, sizeof(real_path)) > sizeof(real_path)) { |
---|
194 | n/a | errno = EINVAL; |
---|
195 | n/a | err(1, "realpath: %s", path); |
---|
196 | n/a | } |
---|
197 | n/a | } |
---|
198 | n/a | |
---|
199 | n/a | setenv("__PYVENV_LAUNCHER__", real_path, 1); |
---|
200 | n/a | } |
---|
201 | n/a | |
---|
202 | n/a | /* |
---|
203 | n/a | * Let argv[0] refer to the new interpreter. This is needed to |
---|
204 | n/a | * get the effect we want on OSX 10.5 or earlier. That is, without |
---|
205 | n/a | * changing argv[0] the real interpreter won't have access to |
---|
206 | n/a | * the Window Server. |
---|
207 | n/a | */ |
---|
208 | n/a | argv[0] = exec_path; |
---|
209 | n/a | |
---|
210 | n/a | #ifdef HAVE_SPAWN_H |
---|
211 | n/a | /* We're weak-linking to posix-spawnv to ensure that |
---|
212 | n/a | * an executable build on 10.5 can work on 10.4. |
---|
213 | n/a | */ |
---|
214 | n/a | if (posix_spawn != NULL) { |
---|
215 | n/a | posix_spawnattr_t spawnattr = NULL; |
---|
216 | n/a | |
---|
217 | n/a | setup_spawnattr(&spawnattr); |
---|
218 | n/a | posix_spawn(NULL, exec_path, NULL, |
---|
219 | n/a | &spawnattr, argv, environ); |
---|
220 | n/a | err(1, "posix_spawn: %s", exec_path); |
---|
221 | n/a | } |
---|
222 | n/a | #endif |
---|
223 | n/a | execve(exec_path, argv, environ); |
---|
224 | n/a | err(1, "execve: %s", argv[0]); |
---|
225 | n/a | /* NOTREACHED */ |
---|
226 | n/a | } |
---|