Debian Codegraph @

bypy/linux/util.c

raw
1 #include "util.h"
2 #include "../run-python.h"
3
4 static char exe_path_char[PATH_MAX];
5
6 static void
7 get_paths() {
8 char linkname[256]; /* /proc/<pid>/exe */
9 wchar_t *p;
10 pid_t pid;
11 int ret;
12
13 pid = getpid();
14
15 if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", pid) < 0) {
16 /* This should only happen on large word systems. I'm not sure
17 what the proper response is here.
18 Since it really is an assert-like condition, aborting the
19 program seems to be in order. */
20 fatal("PID too large");
21 }
22
23 ret = readlink(linkname, exe_path_char, sizeof(exe_path_char));
24 if (ret == -1) fatal("Failed to read exe path from: %s", exe_path_char);
25 if ((size_t)ret >= sizeof(exe_path_char)) fatal("exe path buffer too small.");
26 exe_path_char[ret] = 0;
27 decode_char_buf(exe_path_char, interpreter_data.exe_path);
28
29 p = wcsrchr(interpreter_data.exe_path, '/');
30 if (p == NULL) fatal("No path separators in executable path: %s", exe_path_char);
31 wcsncat(interpreter_data.python_home_path, interpreter_data.exe_path, p - interpreter_data.exe_path);
32 p = wcsrchr(interpreter_data.python_home_path, '/');
33 if (p == NULL) fatal("Only one path separator in executable path: %s", exe_path_char);
34 *p = 0;
35 size_t home_len = p - interpreter_data.python_home_path;
36 if (home_len == 0) fatal("base directory empty");
37 wcsncat(interpreter_data.executables_path, interpreter_data.python_home_path, home_len);
38
39 swprintf(
40 interpreter_data.python_lib_path,
41 arraysz(interpreter_data.python_lib_path),
42 L"%ls/lib/python%d.%d",
43 interpreter_data.python_home_path,
44 PY_VERSION_MAJOR,
45 PY_VERSION_MINOR);
46 swprintf(interpreter_data.resources_path, arraysz(interpreter_data.resources_path), L"%ls/resources", interpreter_data.python_home_path);
47 swprintf(interpreter_data.extensions_path, arraysz(interpreter_data.extensions_path), L"%ls/lib/calibre-extensions", interpreter_data.python_home_path);
48 }
49
50 void
51 execute_python_entrypoint(int argc, char *const *argv, const wchar_t *basename, const wchar_t *module, const wchar_t *function, const bool gui_app) {
52 interpreter_data.argc = argc;
53 interpreter_data.argv = argv;
54 interpreter_data.basename = basename;
55 interpreter_data.module = module;
56 interpreter_data.function = function;
57 pre_initialize_interpreter(gui_app);
58 get_paths();
59 run_interpreter();
60 }