blob: 1d2db383f943cc70d896fb26488e041abe326866 [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#ifdef __LP64__
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 int ch;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000100#endif
101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 if ((errno = posix_spawnattr_init(spawnattr)) != 0) {
103 err(2, "posix_spawnattr_int");
104 /* NOTREACHTED */
105 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 count = 1;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000108
Ezio Melotti4969f702011-03-15 05:59:46 +0200109 /* Run the real python executable using the same architecture as this
110 * executable, this allows users to control the architecture using
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 * "arch -ppc python"
112 */
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000113
114#if defined(__ppc64__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 cpu_types[0] = CPU_TYPE_POWERPC64;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000116
117#elif defined(__x86_64__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 cpu_types[0] = CPU_TYPE_X86_64;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000119
120#elif defined(__ppc__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 cpu_types[0] = CPU_TYPE_POWERPC;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000122#elif defined(__i386__)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 cpu_types[0] = CPU_TYPE_X86;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000124#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125# error "Unknown CPU"
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000126#endif
127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 if (posix_spawnattr_setbinpref_np(spawnattr, count,
129 cpu_types, &ocount) == -1) {
130 err(1, "posix_spawnattr_setbinpref");
131 /* NOTREACHTED */
132 }
133 if (count != ocount) {
134 fprintf(stderr, "posix_spawnattr_setbinpref failed to copy\n");
135 exit(1);
136 /* NOTREACHTED */
137 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000138
139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 /*
141 * Set flag that causes posix_spawn to behave like execv
142 */
143 flags |= POSIX_SPAWN_SETEXEC;
144 if ((errno = posix_spawnattr_setflags(spawnattr, flags)) != 0) {
145 err(1, "posix_spawnattr_setflags");
146 /* NOTREACHTED */
147 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000148}
Ronald Oussoren755740f2010-02-07 19:56:39 +0000149#endif
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151int
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000152main(int argc, char **argv) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 char* exec_path = get_python_path();
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100154 static char path[PATH_MAX * 2];
155 static char real_path[PATH_MAX * 2];
156 int status;
157 uint32_t size = PATH_MAX * 2;
158
159 /* Set the original executable path in the environment. */
160 status = _NSGetExecutablePath(path, &size);
161 if (status == 0) {
Vinay Sajip90db6612012-07-17 17:33:46 +0100162 /*
163 * Note: don't call 'realpath', that will
164 * erase symlink information, and that
165 * breaks "pyvenv --symlink"
166 *
167 * It is nice to have the directory name
168 * as a cleaned up absolute path though,
169 * therefore call realpath on dirname(path)
170 */
171 char* slash = strrchr(path, '/');
172 if (slash) {
173 char replaced;
174 replaced = slash[1];
175 slash[1] = 0;
176 if (realpath(path, real_path) == NULL) {
177 err(1, "realpath: %s", path);
178 }
179 slash[1] = replaced;
180 if (strlcat(real_path, slash, sizeof(real_path)) > sizeof(real_path)) {
181 errno = EINVAL;
182 err(1, "realpath: %s", path);
183 }
184
185 } else {
186 if (realpath(".", real_path) == NULL) {
187 err(1, "realpath: %s", path);
188 }
189 if (strlcat(real_path, "/", sizeof(real_path)) > sizeof(real_path)) {
190 errno = EINVAL;
191 err(1, "realpath: %s", path);
192 }
193 if (strlcat(real_path, path, sizeof(real_path)) > sizeof(real_path)) {
194 errno = EINVAL;
195 err(1, "realpath: %s", path);
196 }
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100197 }
Vinay Sajip90db6612012-07-17 17:33:46 +0100198
199 setenv("__PYVENV_LAUNCHER__", real_path, 1);
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100200 }
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000201
Ronald Oussoren82bfcc82010-06-02 03:50:56 +0000202 /*
203 * Let argv[0] refer to the new interpreter. This is needed to
204 * get the effect we want on OSX 10.5 or earlier. That is, without
205 * changing argv[0] the real interpreter won't have access to
206 * the Window Server.
207 */
208 argv[0] = exec_path;
209
Ronald Oussoren755740f2010-02-07 19:56:39 +0000210#ifdef HAVE_SPAWN_H
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 /* We're weak-linking to posix-spawnv to ensure that
212 * an executable build on 10.5 can work on 10.4.
213 */
214 if (posix_spawn != NULL) {
215 posix_spawnattr_t spawnattr = NULL;
Ronald Oussoren6f6c5622009-12-24 14:03:19 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 setup_spawnattr(&spawnattr);
218 posix_spawn(NULL, exec_path, NULL,
219 &spawnattr, argv, environ);
220 err(1, "posix_spawn: %s", exec_path);
221 }
Ronald Oussoren755740f2010-02-07 19:56:39 +0000222#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 execve(exec_path, argv, environ);
224 err(1, "execve: %s", argv[0]);
225 /* NOTREACHED */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000226}