| 1 |
#define PY_SSIZE_T_CLEAN |
| 2 |
#include "util.h" |
| 3 |
#include <stdlib.h> |
| 4 |
#include <libproc.h> |
| 5 |
#include <unistd.h> |
| 6 |
#include <errno.h> |
| 7 |
#include <string.h> |
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
#define fatal(...) \ |
| 11 |
{ \ |
| 12 |
fprintf(stderr, __VA_ARGS__); \ |
| 13 |
exit(EXIT_FAILURE); \ |
| 14 |
} |
| 15 |
#define arraysz(x) (sizeof(x) / sizeof(x[0])) |
| 16 |
|
| 17 |
int |
| 18 |
main(int argc, char *const *argv) { |
| 19 |
char pathbuf[PROC_PIDPATHINFO_MAXSIZE], realpath_buf[PROC_PIDPATHINFO_MAXSIZE * 5]; |
| 20 |
pid_t pid = getpid(); |
| 21 |
int ret = proc_pidpath(pid, pathbuf, arraysz(pathbuf)); |
| 22 |
if (ret <= 0) fatal("failed to get executable path for current pid with error: %s", strerror(errno)); |
| 23 |
char *path = realpath(pathbuf, realpath_buf); |
| 24 |
if (path == NULL) fatal("failed to get realpath for executable path with error: %s", strerror(errno)); |
| 25 |
// We re-exec using an absolute path because the Qt WebEngine sandbox does not work |
| 26 |
// when running via symlink |
| 27 |
if (!IS_GUI && wcscmp(PROGRAM, L"calibre-parallel") != 0 && strcmp(argv[0], path) != 0) { |
| 28 |
char *new_argv[1024] = {0}; |
| 29 |
new_argv[0] = path; |
| 30 |
for (int i = 1; i < argc && i < arraysz(new_argv) - 1; i++) new_argv[i] = argv[i]; |
| 31 |
execv(path, new_argv); |
| 32 |
} |
| 33 |
run(PROGRAM, MODULE, FUNCTION, IS_GUI, argc, argv, path); |
| 34 |
return 0; |
| 35 |
} |