From 2a2f641856739f981acc5dbe08c0b57720918e63 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 24 Dec 2019 20:01:29 +0530 Subject: [PATCH] Port the commit to master to fix PDF Output with ebook-convert via symlink on macOS --- bypy/macos/launcher.c | 17 +++++++++++++++-- bypy/macos/util.c | 13 +++---------- bypy/macos/util.h | 2 +- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/bypy/macos/launcher.c b/bypy/macos/launcher.c index 805330cb15..1cd0a77fe7 100644 --- a/bypy/macos/launcher.c +++ b/bypy/macos/launcher.c @@ -9,9 +9,22 @@ #define fatal(...) { fprintf(stderr, __VA_ARGS__); exit(EXIT_FAILURE); } #define arraysz(x) (sizeof(x)/sizeof(x[0])) - int main(int argc, char * const *argv) { - run(PROGRAM, MODULE, FUNCTION, IS_GUI, argc, argv); + char pathbuf[PROC_PIDPATHINFO_MAXSIZE], realpath_buf[PROC_PIDPATHINFO_MAXSIZE * 5]; + pid_t pid = getpid(); + int ret = proc_pidpath(pid, pathbuf, arraysz(pathbuf)); + if (ret <= 0) fatal("failed to get executable path for current pid with error: %s", strerror(errno)); + char *path = realpath(pathbuf, realpath_buf); + if (path == NULL) fatal("failed to get realpath for executable path with error: %s", strerror(errno)); + // We re-exec using an absolute path because the Qt WebEngine sandbox does not work + // when running via symlink + if (!IS_GUI && strcmp(PROGRAM, "calibre-parallel") != 0 && strcmp(argv[0], path) != 0) { + char* new_argv[1024] = {0}; + new_argv[0] = path; + for (int i = 1; i < argc && i < arraysz(new_argv) - 1; i++) new_argv[i] = argv[i]; + execv(path, new_argv); + } + run(PROGRAM, MODULE, FUNCTION, IS_GUI, argc, argv, path); return 0; } diff --git a/bypy/macos/util.c b/bypy/macos/util.c index 95f63c0291..4a9dd35072 100644 --- a/bypy/macos/util.c +++ b/bypy/macos/util.c @@ -2,7 +2,6 @@ #include "../run-python.h" #include #include -#include #define EXPORT __attribute__((visibility("default"))) @@ -28,13 +27,7 @@ set_env_vars(const char* contents_path) { } static void -get_paths() { - char pathbuf[PROC_PIDPATHINFO_MAXSIZE], realpath_buf[PROC_PIDPATHINFO_MAXSIZE * 5]; - pid_t pid = getpid(); - int ret = proc_pidpath(pid, pathbuf, sizeof(pathbuf)); - if (ret <= 0) fatal("failed to get executable path for current pid with error: %s", strerror(errno)); - char *path = realpath(pathbuf, realpath_buf); - if (path == NULL) fatal("failed to get realpath for executable path with error: %s", strerror(errno)); +get_paths(char *path) { decode_char_buf(path, interpreter_data.exe_path); for (unsigned i = 0; i < 3; i++) { char *t = rindex(path, '/'); @@ -68,11 +61,11 @@ get_paths() { EXPORT void -run(const wchar_t *program, const wchar_t *module, const wchar_t *function, bool gui_app, int argc, char * const *argv) { +run(const wchar_t *program, const wchar_t *module, const wchar_t *function, bool gui_app, int argc, char * const *argv, char* exe_path) { interpreter_data.argc = argc; interpreter_data.argv = argv; interpreter_data.basename = program; interpreter_data.module = module; interpreter_data.function = function; pre_initialize_interpreter(gui_app); - get_paths(); + get_paths(exe_path); run_interpreter(); } diff --git a/bypy/macos/util.h b/bypy/macos/util.h index be506030f6..c414e2ff70 100644 --- a/bypy/macos/util.h +++ b/bypy/macos/util.h @@ -2,4 +2,4 @@ #include #include -void run(const wchar_t *program, const wchar_t *module, const wchar_t *function, bool is_gui, int argc, char * const *argv); +void run(const wchar_t *program, const wchar_t *module, const wchar_t *function, bool is_gui, int argc, char * const *argv, char* exe_path);