1 /* Return the initial module search path. */
2 
3 #include "Python.h"
4 #include "osdefs.h"
5 
6 #include <sys/types.h>
7 #include <string.h>
8 
9 #ifdef __APPLE__
10 #include <mach-o/dyld.h>
11 #endif
12 
13 /* Search in some common locations for the associated Python libraries.
14  *
15  * Two directories must be found, the platform independent directory
16  * (prefix), containing the common .py and .pyc files, and the platform
17  * dependent directory (exec_prefix), containing the shared library
18  * modules.  Note that prefix and exec_prefix can be the same directory,
19  * but for some installations, they are different.
20  *
21  * Py_GetPath() carries out separate searches for prefix and exec_prefix.
22  * Each search tries a number of different locations until a ``landmark''
23  * file or directory is found.  If no prefix or exec_prefix is found, a
24  * warning message is issued and the preprocessor defined PREFIX and
25  * EXEC_PREFIX are used (even though they will not work); python carries on
26  * as best as is possible, but most imports will fail.
27  *
28  * Before any searches are done, the location of the executable is
29  * determined.  If argv[0] has one or more slashes in it, it is used
30  * unchanged.  Otherwise, it must have been invoked from the shell's path,
31  * so we search $PATH for the named executable and use that.  If the
32  * executable was not found on $PATH (or there was no $PATH environment
33  * variable), the original argv[0] string is used.
34  *
35  * Next, the executable location is examined to see if it is a symbolic
36  * link.  If so, the link is chased (correctly interpreting a relative
37  * pathname if one is found) and the directory of the link target is used.
38  *
39  * Finally, argv0_path is set to the directory containing the executable
40  * (i.e. the last component is stripped).
41  *
42  * With argv0_path in hand, we perform a number of steps.  The same steps
43  * are performed for prefix and for exec_prefix, but with a different
44  * landmark.
45  *
46  * Step 1. Are we running python out of the build directory?  This is
47  * checked by looking for a different kind of landmark relative to
48  * argv0_path.  For prefix, the landmark's path is derived from the VPATH
49  * preprocessor variable (taking into account that its value is almost, but
50  * not quite, what we need).  For exec_prefix, the landmark is
51  * Modules/Setup.  If the landmark is found, we're done.
52  *
53  * For the remaining steps, the prefix landmark will always be
54  * lib/python$VERSION/os.py and the exec_prefix will always be
55  * lib/python$VERSION/lib-dynload, where $VERSION is Python's version
56  * number as supplied by the Makefile.  Note that this means that no more
57  * build directory checking is performed; if the first step did not find
58  * the landmarks, the assumption is that python is running from an
59  * installed setup.
60  *
61  * Step 2. See if the $PYTHONHOME environment variable points to the
62  * installed location of the Python libraries.  If $PYTHONHOME is set, then
63  * it points to prefix and exec_prefix.  $PYTHONHOME can be a single
64  * directory, which is used for both, or the prefix and exec_prefix
65  * directories separated by a colon.
66  *
67  * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
68  * backtracking up the path until it is exhausted.  This is the most common
69  * step to succeed.  Note that if prefix and exec_prefix are different,
70  * exec_prefix is more likely to be found; however if exec_prefix is a
71  * subdirectory of prefix, both will be found.
72  *
73  * Step 4. Search the directories pointed to by the preprocessor variables
74  * PREFIX and EXEC_PREFIX.  These are supplied by the Makefile but can be
75  * passed in as options to the configure script.
76  *
77  * That's it!
78  *
79  * Well, almost.  Once we have determined prefix and exec_prefix, the
80  * preprocessor variable PYTHONPATH is used to construct a path.  Each
81  * relative path on PYTHONPATH is prefixed with prefix.  Then the directory
82  * containing the shared library modules is appended.  The environment
83  * variable $PYTHONPATH is inserted in front of it all.  Finally, the
84  * prefix and exec_prefix globals are tweaked so they reflect the values
85  * expected by other code, by stripping the "lib/python$VERSION/..." stuff
86  * off.  If either points to the build directory, the globals are reset to
87  * the corresponding preprocessor variables (so sys.prefix will reflect the
88  * installation location, even though sys.path points into the build
89  * directory).  This seems to make more sense given that currently the only
90  * known use of sys.prefix and sys.exec_prefix is for the ILU installation
91  * process to find the installed Python tree.
92  */
93 
94 #ifdef __cplusplus
95  extern "C" {
96 #endif
97 
98 
99 #if !defined(PREFIX) || !defined(EXEC_PREFIX) || !defined(VERSION) || !defined(VPATH)
100 #error "PREFIX, EXEC_PREFIX, VERSION, and VPATH must be constant defined"
101 #endif
102 
103 #ifndef LANDMARK
104 #define LANDMARK "os.py"
105 #endif
106 
107 static char prefix[MAXPATHLEN+1];
108 static char exec_prefix[MAXPATHLEN+1];
109 static char progpath[MAXPATHLEN+1];
110 static char *module_search_path = NULL;
111 static char lib_python[] = "lib/python" VERSION;
112 
113 static void
reduce(char * dir)114 reduce(char *dir)
115 {
116     size_t i = strlen(dir);
117     while (i > 0 && dir[i] != SEP)
118         --i;
119     dir[i] = '\0';
120 }
121 
122 
123 static int
isfile(char * filename)124 isfile(char *filename)          /* Is file, not directory */
125 {
126     struct stat buf;
127     if (stat(filename, &buf) != 0)
128         return 0;
129     if (!S_ISREG(buf.st_mode))
130         return 0;
131     return 1;
132 }
133 
134 
135 static int
ismodule(char * filename)136 ismodule(char *filename)        /* Is module -- check for .pyc/.pyo too */
137 {
138     if (isfile(filename))
139         return 1;
140 
141     /* Check for the compiled version of prefix. */
142     if (strlen(filename) < MAXPATHLEN) {
143         strcat(filename, Py_OptimizeFlag ? "o" : "c");
144         if (isfile(filename))
145             return 1;
146     }
147     return 0;
148 }
149 
150 
151 static int
isxfile(char * filename)152 isxfile(char *filename)         /* Is executable file */
153 {
154     struct stat buf;
155     if (stat(filename, &buf) != 0)
156         return 0;
157     if (!S_ISREG(buf.st_mode))
158         return 0;
159     if ((buf.st_mode & 0111) == 0)
160         return 0;
161     return 1;
162 }
163 
164 
165 static int
isdir(char * filename)166 isdir(char *filename)                   /* Is directory */
167 {
168     struct stat buf;
169     if (stat(filename, &buf) != 0)
170         return 0;
171     if (!S_ISDIR(buf.st_mode))
172         return 0;
173     return 1;
174 }
175 
176 
177 /* Add a path component, by appending stuff to buffer.
178    buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
179    NUL-terminated string with no more than MAXPATHLEN characters (not counting
180    the trailing NUL).  It's a fatal error if it contains a string longer than
181    that (callers must be careful!).  If these requirements are met, it's
182    guaranteed that buffer will still be a NUL-terminated string with no more
183    than MAXPATHLEN characters at exit.  If stuff is too long, only as much of
184    stuff as fits will be appended.
185 */
186 static void
joinpath(char * buffer,char * stuff)187 joinpath(char *buffer, char *stuff)
188 {
189     size_t n, k;
190     if (stuff[0] == SEP)
191         n = 0;
192     else {
193         n = strlen(buffer);
194         if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN)
195             buffer[n++] = SEP;
196     }
197     if (n > MAXPATHLEN)
198         Py_FatalError("buffer overflow in getpath.c's joinpath()");
199     k = strlen(stuff);
200     if (n + k > MAXPATHLEN)
201         k = MAXPATHLEN - n;
202     strncpy(buffer+n, stuff, k);
203     buffer[n+k] = '\0';
204 }
205 
206 /* copy_absolute requires that path be allocated at least
207    MAXPATHLEN + 1 bytes and that p be no more than MAXPATHLEN bytes. */
208 static void
copy_absolute(char * path,char * p)209 copy_absolute(char *path, char *p)
210 {
211     if (p[0] == SEP)
212         strcpy(path, p);
213     else {
214         if (!getcwd(path, MAXPATHLEN)) {
215             /* unable to get the current directory */
216             strcpy(path, p);
217             return;
218         }
219         if (p[0] == '.' && p[1] == SEP)
220             p += 2;
221         joinpath(path, p);
222     }
223 }
224 
225 /* absolutize() requires that path be allocated at least MAXPATHLEN+1 bytes. */
226 static void
absolutize(char * path)227 absolutize(char *path)
228 {
229     char buffer[MAXPATHLEN + 1];
230 
231     if (path[0] == SEP)
232         return;
233     copy_absolute(buffer, path);
234     strcpy(path, buffer);
235 }
236 
237 /* search_for_prefix requires that argv0_path be no more than MAXPATHLEN
238    bytes long.
239 */
240 static int
search_for_prefix(char * argv0_path,char * home)241 search_for_prefix(char *argv0_path, char *home)
242 {
243     size_t n;
244     char *vpath;
245 
246     /* If PYTHONHOME is set, we believe it unconditionally */
247     if (home) {
248         char *delim;
249         strncpy(prefix, home, MAXPATHLEN);
250         delim = strchr(prefix, DELIM);
251         if (delim)
252             *delim = '\0';
253         joinpath(prefix, lib_python);
254         joinpath(prefix, LANDMARK);
255         return 1;
256     }
257 
258     /* Check to see if argv[0] is in the build directory */
259     strcpy(prefix, argv0_path);
260     joinpath(prefix, "Modules/Setup");
261     if (isfile(prefix)) {
262         /* Check VPATH to see if argv0_path is in the build directory. */
263         vpath = VPATH;
264         strcpy(prefix, argv0_path);
265         joinpath(prefix, vpath);
266         joinpath(prefix, "Lib");
267         joinpath(prefix, LANDMARK);
268         if (ismodule(prefix))
269             return -1;
270     }
271 
272     /* Search from argv0_path, until root is found */
273     copy_absolute(prefix, argv0_path);
274     do {
275         n = strlen(prefix);
276         joinpath(prefix, lib_python);
277         joinpath(prefix, LANDMARK);
278         if (ismodule(prefix))
279             return 1;
280         prefix[n] = '\0';
281         reduce(prefix);
282     } while (prefix[0]);
283 
284     /* Look at configure's PREFIX */
285     strncpy(prefix, PREFIX, MAXPATHLEN);
286     joinpath(prefix, lib_python);
287     joinpath(prefix, LANDMARK);
288     if (ismodule(prefix))
289         return 1;
290 
291     /* Fail */
292     return 0;
293 }
294 
295 
296 /* search_for_exec_prefix requires that argv0_path be no more than
297    MAXPATHLEN bytes long.
298 */
299 static int
search_for_exec_prefix(char * argv0_path,char * home)300 search_for_exec_prefix(char *argv0_path, char *home)
301 {
302     size_t n;
303 
304     /* If PYTHONHOME is set, we believe it unconditionally */
305     if (home) {
306         char *delim;
307         delim = strchr(home, DELIM);
308         if (delim)
309             strncpy(exec_prefix, delim+1, MAXPATHLEN);
310         else
311             strncpy(exec_prefix, home, MAXPATHLEN);
312         joinpath(exec_prefix, lib_python);
313         joinpath(exec_prefix, "lib-dynload");
314         return 1;
315     }
316 
317     /* Check to see if argv[0] is in the build directory. "pybuilddir.txt"
318        is written by setup.py and contains the relative path to the location
319        of shared library modules. */
320     strcpy(exec_prefix, argv0_path);
321     joinpath(exec_prefix, "pybuilddir.txt");
322     if (isfile(exec_prefix)) {
323       FILE *f = fopen(exec_prefix, "r");
324       if (f == NULL)
325 	errno = 0;
326       else {
327 	char rel_builddir_path[MAXPATHLEN+1];
328 	size_t n;
329 	n = fread(rel_builddir_path, 1, MAXPATHLEN, f);
330 	rel_builddir_path[n] = '\0';
331 	fclose(f);
332 	strcpy(exec_prefix, argv0_path);
333 	joinpath(exec_prefix, rel_builddir_path);
334 	return -1;
335       }
336     }
337 
338     /* Search from argv0_path, until root is found */
339     copy_absolute(exec_prefix, argv0_path);
340     do {
341         n = strlen(exec_prefix);
342         joinpath(exec_prefix, lib_python);
343         joinpath(exec_prefix, "lib-dynload");
344         if (isdir(exec_prefix))
345             return 1;
346         exec_prefix[n] = '\0';
347         reduce(exec_prefix);
348     } while (exec_prefix[0]);
349 
350     /* Look at configure's EXEC_PREFIX */
351     strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN);
352     joinpath(exec_prefix, lib_python);
353     joinpath(exec_prefix, "lib-dynload");
354     if (isdir(exec_prefix))
355         return 1;
356 
357     /* Fail */
358     return 0;
359 }
360 
361 
362 static void
calculate_path(void)363 calculate_path(void)
364 {
365     extern char *Py_GetProgramName(void);
366 
367     static char delimiter[2] = {DELIM, '\0'};
368     static char separator[2] = {SEP, '\0'};
369     char *pythonpath = PYTHONPATH;
370     char *rtpypath = Py_GETENV("PYTHONPATH");
371     char *home = Py_GetPythonHome();
372     char *path = getenv("PATH");
373     char *prog = Py_GetProgramName();
374     char argv0_path[MAXPATHLEN+1];
375     char zip_path[MAXPATHLEN+1];
376     int pfound, efound; /* 1 if found; -1 if found build directory */
377     char *buf;
378     size_t bufsz;
379     size_t prefixsz;
380     char *defpath = pythonpath;
381 #ifdef WITH_NEXT_FRAMEWORK
382     NSModule pythonModule;
383 #endif
384 #ifdef __APPLE__
385 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
386     uint32_t nsexeclength = MAXPATHLEN;
387 #else
388     unsigned long nsexeclength = MAXPATHLEN;
389 #endif
390 #endif
391 
392         /* If there is no slash in the argv0 path, then we have to
393          * assume python is on the user's $PATH, since there's no
394          * other way to find a directory to start the search from.  If
395          * $PATH isn't exported, you lose.
396          */
397         if (strchr(prog, SEP))
398                 strncpy(progpath, prog, MAXPATHLEN);
399 #ifdef __APPLE__
400      /* On Mac OS X, if a script uses an interpreter of the form
401       * "#!/opt/python2.3/bin/python", the kernel only passes "python"
402       * as argv[0], which falls through to the $PATH search below.
403       * If /opt/python2.3/bin isn't in your path, or is near the end,
404       * this algorithm may incorrectly find /usr/bin/python. To work
405       * around this, we can use _NSGetExecutablePath to get a better
406       * hint of what the intended interpreter was, although this
407       * will fail if a relative path was used. but in that case,
408       * absolutize() should help us out below
409       */
410      else if(0 == _NSGetExecutablePath(progpath, &nsexeclength) && progpath[0] == SEP)
411        ;
412 #endif /* __APPLE__ */
413         else if (path) {
414                 while (1) {
415                         char *delim = strchr(path, DELIM);
416 
417                         if (delim) {
418                                 size_t len = delim - path;
419                                 if (len > MAXPATHLEN)
420                                         len = MAXPATHLEN;
421                                 strncpy(progpath, path, len);
422                                 *(progpath + len) = '\0';
423                         }
424                         else
425                                 strncpy(progpath, path, MAXPATHLEN);
426 
427                         joinpath(progpath, prog);
428                         if (isxfile(progpath))
429                                 break;
430 
431                         if (!delim) {
432                                 progpath[0] = '\0';
433                                 break;
434                         }
435                         path = delim + 1;
436                 }
437         }
438         else
439                 progpath[0] = '\0';
440         if (progpath[0] != SEP && progpath[0] != '\0')
441                 absolutize(progpath);
442         strncpy(argv0_path, progpath, MAXPATHLEN);
443         argv0_path[MAXPATHLEN] = '\0';
444 
445 #ifdef WITH_NEXT_FRAMEWORK
446         /* On Mac OS X we have a special case if we're running from a framework.
447         ** This is because the python home should be set relative to the library,
448         ** which is in the framework, not relative to the executable, which may
449         ** be outside of the framework. Except when we're in the build directory...
450         */
451     pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize"));
452     /* Use dylib functions to find out where the framework was loaded from */
453     buf = (char *)NSLibraryNameForModule(pythonModule);
454     if (buf != NULL) {
455         /* We're in a framework. */
456         /* See if we might be in the build directory. The framework in the
457         ** build directory is incomplete, it only has the .dylib and a few
458         ** needed symlinks, it doesn't have the Lib directories and such.
459         ** If we're running with the framework from the build directory we must
460         ** be running the interpreter in the build directory, so we use the
461         ** build-directory-specific logic to find Lib and such.
462         */
463         strncpy(argv0_path, buf, MAXPATHLEN);
464         reduce(argv0_path);
465         joinpath(argv0_path, lib_python);
466         joinpath(argv0_path, LANDMARK);
467         if (!ismodule(argv0_path)) {
468                 /* We are in the build directory so use the name of the
469                    executable - we know that the absolute path is passed */
470                 strncpy(argv0_path, progpath, MAXPATHLEN);
471         }
472         else {
473                 /* Use the location of the library as the progpath */
474                 strncpy(argv0_path, buf, MAXPATHLEN);
475         }
476     }
477 #endif
478 
479 #if HAVE_READLINK
480     {
481         char tmpbuffer[MAXPATHLEN+1];
482         int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
483         while (linklen != -1) {
484             /* It's not null terminated! */
485             tmpbuffer[linklen] = '\0';
486             if (tmpbuffer[0] == SEP)
487                 /* tmpbuffer should never be longer than MAXPATHLEN,
488                    but extra check does not hurt */
489                 strncpy(argv0_path, tmpbuffer, MAXPATHLEN + 1);
490             else {
491                 /* Interpret relative to progpath */
492                 reduce(argv0_path);
493                 joinpath(argv0_path, tmpbuffer);
494             }
495             linklen = readlink(argv0_path, tmpbuffer, MAXPATHLEN);
496         }
497     }
498 #endif /* HAVE_READLINK */
499 
500     reduce(argv0_path);
501     /* At this point, argv0_path is guaranteed to be less than
502        MAXPATHLEN bytes long.
503     */
504 
505     if (!(pfound = search_for_prefix(argv0_path, home))) {
506         if (!Py_FrozenFlag)
507             fprintf(stderr,
508                 "Could not find platform independent libraries <prefix>\n");
509         strncpy(prefix, PREFIX, MAXPATHLEN);
510         joinpath(prefix, lib_python);
511     }
512     else
513         reduce(prefix);
514 
515     strncpy(zip_path, prefix, MAXPATHLEN);
516     zip_path[MAXPATHLEN] = '\0';
517     if (pfound > 0) { /* Use the reduced prefix returned by Py_GetPrefix() */
518         reduce(zip_path);
519         reduce(zip_path);
520     }
521     else
522         strncpy(zip_path, PREFIX, MAXPATHLEN);
523     joinpath(zip_path, "lib/python00.zip");
524     bufsz = strlen(zip_path);   /* Replace "00" with version */
525     zip_path[bufsz - 6] = VERSION[0];
526     zip_path[bufsz - 5] = VERSION[2];
527 
528     if (!(efound = search_for_exec_prefix(argv0_path, home))) {
529         if (!Py_FrozenFlag)
530             fprintf(stderr,
531                 "Could not find platform dependent libraries <exec_prefix>\n");
532         strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN);
533         joinpath(exec_prefix, "lib/lib-dynload");
534     }
535     /* If we found EXEC_PREFIX do *not* reduce it!  (Yet.) */
536 
537     if ((!pfound || !efound) && !Py_FrozenFlag)
538         fprintf(stderr,
539                 "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
540 
541     /* Calculate size of return buffer.
542      */
543     bufsz = 0;
544 
545     if (rtpypath)
546         bufsz += strlen(rtpypath) + 1;
547 
548     prefixsz = strlen(prefix) + 1;
549 
550     while (1) {
551         char *delim = strchr(defpath, DELIM);
552 
553         if (defpath[0] != SEP)
554             /* Paths are relative to prefix */
555             bufsz += prefixsz;
556 
557         if (delim)
558             bufsz += delim - defpath + 1;
559         else {
560             bufsz += strlen(defpath) + 1;
561             break;
562         }
563         defpath = delim + 1;
564     }
565 
566     bufsz += strlen(zip_path) + 1;
567     bufsz += strlen(exec_prefix) + 1;
568 
569     /* This is the only malloc call in this file */
570     buf = (char *)PyMem_Malloc(bufsz);
571 
572     if (buf == NULL) {
573         /* We can't exit, so print a warning and limp along */
574         fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
575         fprintf(stderr, "Using default static PYTHONPATH.\n");
576         module_search_path = PYTHONPATH;
577     }
578     else {
579         /* Run-time value of $PYTHONPATH goes first */
580         if (rtpypath) {
581             strcpy(buf, rtpypath);
582             strcat(buf, delimiter);
583         }
584         else
585             buf[0] = '\0';
586 
587         /* Next is the default zip path */
588         strcat(buf, zip_path);
589         strcat(buf, delimiter);
590 
591         /* Next goes merge of compile-time $PYTHONPATH with
592          * dynamically located prefix.
593          */
594         defpath = pythonpath;
595         while (1) {
596             char *delim = strchr(defpath, DELIM);
597 
598             if (defpath[0] != SEP) {
599                 strcat(buf, prefix);
600                 if (prefixsz >= 2 && prefix[prefixsz - 2] != SEP &&
601                     defpath[0] != (delim ? DELIM : L'\0')) {  /* not empty */
602                     strcat(buf, separator);
603                 }
604             }
605 
606             if (delim) {
607                 size_t len = delim - defpath + 1;
608                 size_t end = strlen(buf) + len;
609                 strncat(buf, defpath, len);
610                 *(buf + end) = '\0';
611             }
612             else {
613                 strcat(buf, defpath);
614                 break;
615             }
616             defpath = delim + 1;
617         }
618         strcat(buf, delimiter);
619 
620         /* Finally, on goes the directory for dynamic-load modules */
621         strcat(buf, exec_prefix);
622 
623         /* And publish the results */
624         module_search_path = buf;
625     }
626 
627     /* Reduce prefix and exec_prefix to their essence,
628      * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
629      * If we're loading relative to the build directory,
630      * return the compiled-in defaults instead.
631      */
632     if (pfound > 0) {
633         reduce(prefix);
634         reduce(prefix);
635         /* The prefix is the root directory, but reduce() chopped
636          * off the "/". */
637         if (!prefix[0])
638                 strcpy(prefix, separator);
639     }
640     else
641         strncpy(prefix, PREFIX, MAXPATHLEN);
642 
643     if (efound > 0) {
644         reduce(exec_prefix);
645         reduce(exec_prefix);
646         reduce(exec_prefix);
647         if (!exec_prefix[0])
648                 strcpy(exec_prefix, separator);
649     }
650     else
651         strncpy(exec_prefix, EXEC_PREFIX, MAXPATHLEN);
652 }
653 
654 
655 /* External interface */
656 
657 char *
Py_GetPath(void)658 Py_GetPath(void)
659 {
660     if (!module_search_path)
661         calculate_path();
662     return module_search_path;
663 }
664 
665 char *
Py_GetPrefix(void)666 Py_GetPrefix(void)
667 {
668     if (!module_search_path)
669         calculate_path();
670     return prefix;
671 }
672 
673 char *
Py_GetExecPrefix(void)674 Py_GetExecPrefix(void)
675 {
676     if (!module_search_path)
677         calculate_path();
678     return exec_prefix;
679 }
680 
681 char *
Py_GetProgramFullPath(void)682 Py_GetProgramFullPath(void)
683 {
684     if (!module_search_path)
685         calculate_path();
686     return progpath;
687 }
688 
689 
690 #ifdef __cplusplus
691 }
692 #endif
693 
694