blob: 78813e818e7dace8f744113f7a587e64b0454dfd [file] [log] [blame]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001/*
2 * This wrapper program executes a python executable hidden inside an
3 * application bundle inside the Python framework. This is needed to run
4 * GUI code: some GUI API's don't work unless the program is inside an
5 * application bundle.
Ronald Oussoren6f6c5622009-12-24 14:03:19 +00006 *
7 * This program uses posix_spawn rather than plain execv because we need
8 * slightly more control over how the "real" interpreter is executed.
Ronald Oussoren755740f2010-02-07 19:56:39 +00009 *
10 * On OSX 10.4 (and earlier) this falls back to using exec because the
11 * posix_spawnv functions aren't available there.
Thomas Wouters477c8d52006-05-27 19:21:47 +000012 */
Ronald Oussoren755740f2010-02-07 19:56:39 +000013
14#pragma weak_import posix_spawnattr_init
15#pragma weak_import posix_spawnattr_setbinpref_np
16#pragma weak_import posix_spawnattr_setflags
17#pragma weak_import posix_spawn
18
19#include <Python.h>
Thomas Wouters477c8d52006-05-27 19:21:47 +000020#include <unistd.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +000021#ifdef HAVE_SPAWN_H
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000022#include <spawn.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +000023#endif
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000024#include <stdio.h>
25#include <string.h>
26#include <errno.h>
Thomas Wouters477c8d52006-05-27 19:21:47 +000027#include <err.h>
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000028#include <dlfcn.h>
29#include <stdlib.h>
30#include <Python.h>
Vinay Sajip90db6612012-07-17 17:33:46 +010031#include <mach-o/dyld.h>
Thomas Wouters477c8d52006-05-27 19:21:47 +000032
Thomas Wouters477c8d52006-05-27 19:21:47 +000033
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000034extern char** environ;
35
36/*
37 * Locate the python framework by looking for the
38 * library that contains Py_Initialize.
39 *
40 * In a regular framework the structure is:
41 *
42 * Python.framework/Versions/2.7
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 * /Python
44 * /Resources/Python.app/Contents/MacOS/Python
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000045 *
46 * In a virtualenv style structure the expected
47 * structure is:
48 *
49 * ROOT
50 * /bin/pythonw
51 * /.Python <- the dylib
52 * /.Resources/Python.app/Contents/MacOS/Python
53 *
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 * NOTE: virtualenv's are not an officially supported
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000055 * feature, support for that structure is provided as
56 * a convenience.
57 */
58static char* get_python_path(void)
59{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 size_t len;
61 Dl_info info;
62 char* end;
63 char* g_path;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (dladdr(Py_Initialize, &info) == 0) {
66 return NULL;
67 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 len = strlen(info.dli_fname);
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 g_path = malloc(len+60);
72 if (g_path == NULL) {
73 return NULL;
74 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 strcpy(g_path, info.dli_fname);
77 end = g_path + len - 1;
78 while (end != g_path && *end != '/') {
79 end --;
80 }
81 end++;
82 if (*end == '.') {
83 end++;
84 }
85 strcpy(end, "Resources/Python.app/Contents/MacOS/" PYTHONFRAMEWORK);
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 return g_path;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000088}
89
Ronald Oussoren755740f2010-02-07 19:56:39 +000090#ifdef HAVE_SPAWN_H
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000091static void
92setup_spawnattr(posix_spawnattr_t* spawnattr)
93{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 size_t ocount;
95 size_t count;
96 cpu_type_t cpu_types[1];
97 short flags = 0;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +000098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 if ((errno = posix_spawnattr_init(spawnattr)) != 0) {
100 err(2, "posix_spawnattr_int");
101 /* NOTREACHTED */
102 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 count = 1;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000105
Ezio Melotti4969f702011-03-15 05:59:46 +0200106 /* Run the real python executable using the same architecture as this
107 * executable, this allows users to control the architecture using
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 * "arch -ppc python"
109 */
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000110
111#if defined(__ppc64__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 cpu_types[0] = CPU_TYPE_POWERPC64;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000113
114#elif defined(__x86_64__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 cpu_types[0] = CPU_TYPE_X86_64;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000116
117#elif defined(__ppc__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 cpu_types[0] = CPU_TYPE_POWERPC;
Ronald Oussoren41761932020-11-08 10:05:27 +0100119
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000120#elif defined(__i386__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 cpu_types[0] = CPU_TYPE_X86;
Ronald Oussoren41761932020-11-08 10:05:27 +0100122
123#elif defined(__arm64__)
124 cpu_types[0] = CPU_TYPE_ARM64;
125
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000126#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127# error "Unknown CPU"
Ronald Oussoren41761932020-11-08 10:05:27 +0100128
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000129#endif
130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 if (posix_spawnattr_setbinpref_np(spawnattr, count,
132 cpu_types, &ocount) == -1) {
133 err(1, "posix_spawnattr_setbinpref");
134 /* NOTREACHTED */
135 }
136 if (count != ocount) {
137 fprintf(stderr, "posix_spawnattr_setbinpref failed to copy\n");
138 exit(1);
139 /* NOTREACHTED */
140 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000141
142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 /*
144 * Set flag that causes posix_spawn to behave like execv
145 */
146 flags |= POSIX_SPAWN_SETEXEC;
147 if ((errno = posix_spawnattr_setflags(spawnattr, flags)) != 0) {
148 err(1, "posix_spawnattr_setflags");
149 /* NOTREACHTED */
150 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000151}
Ronald Oussoren755740f2010-02-07 19:56:39 +0000152#endif
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154int
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000155main(int argc, char **argv) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 char* exec_path = get_python_path();
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100157 static char path[PATH_MAX * 2];
158 static char real_path[PATH_MAX * 2];
159 int status;
160 uint32_t size = PATH_MAX * 2;
161
162 /* Set the original executable path in the environment. */
163 status = _NSGetExecutablePath(path, &size);
164 if (status == 0) {
Vinay Sajip90db6612012-07-17 17:33:46 +0100165 /*
166 * Note: don't call 'realpath', that will
167 * erase symlink information, and that
168 * breaks "pyvenv --symlink"
169 *
170 * It is nice to have the directory name
171 * as a cleaned up absolute path though,
172 * therefore call realpath on dirname(path)
173 */
174 char* slash = strrchr(path, '/');
175 if (slash) {
176 char replaced;
177 replaced = slash[1];
178 slash[1] = 0;
179 if (realpath(path, real_path) == NULL) {
180 err(1, "realpath: %s", path);
181 }
182 slash[1] = replaced;
183 if (strlcat(real_path, slash, sizeof(real_path)) > sizeof(real_path)) {
184 errno = EINVAL;
185 err(1, "realpath: %s", path);
186 }
187
188 } else {
189 if (realpath(".", real_path) == NULL) {
190 err(1, "realpath: %s", path);
191 }
192 if (strlcat(real_path, "/", sizeof(real_path)) > sizeof(real_path)) {
193 errno = EINVAL;
194 err(1, "realpath: %s", path);
195 }
196 if (strlcat(real_path, path, sizeof(real_path)) > sizeof(real_path)) {
197 errno = EINVAL;
198 err(1, "realpath: %s", path);
199 }
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100200 }
Vinay Sajip90db6612012-07-17 17:33:46 +0100201
Ronald Oussoren044cf942020-03-22 19:31:46 +0100202 /*
203 * The environment variable is used to pass the value of real_path
204 * to the actual python interpreter, and is read by code in
205 * Python/coreconfig.c.
206 *
207 * This way the real interpreter knows how the user invoked the
208 * interpreter and can behave as if this launcher is the real
209 * interpreter (looking for pyvenv configuration, ...)
210 */
Vinay Sajip90db6612012-07-17 17:33:46 +0100211 setenv("__PYVENV_LAUNCHER__", real_path, 1);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100212 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000213
Ronald Oussoren82bfcc82010-06-02 03:50:56 +0000214 /*
215 * Let argv[0] refer to the new interpreter. This is needed to
216 * get the effect we want on OSX 10.5 or earlier. That is, without
217 * changing argv[0] the real interpreter won't have access to
218 * the Window Server.
219 */
220 argv[0] = exec_path;
221
Ronald Oussoren755740f2010-02-07 19:56:39 +0000222#ifdef HAVE_SPAWN_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 /* We're weak-linking to posix-spawnv to ensure that
224 * an executable build on 10.5 can work on 10.4.
225 */
Ronald Oussoren41761932020-11-08 10:05:27 +0100226
227 if (&posix_spawn != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 posix_spawnattr_t spawnattr = NULL;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 setup_spawnattr(&spawnattr);
231 posix_spawn(NULL, exec_path, NULL,
232 &spawnattr, argv, environ);
233 err(1, "posix_spawn: %s", exec_path);
234 }
Ronald Oussoren755740f2010-02-07 19:56:39 +0000235#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 execve(exec_path, argv, environ);
237 err(1, "execve: %s", argv[0]);
238 /* NOTREACHED */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000239}