| 1 |
/* |
| 2 |
* launcher.c |
| 3 |
* Copyright (C) 2014 Kovid Goyal <kovid at kovidgoyal.net> |
| 4 |
* |
| 5 |
* Distributed under terms of the GPL3 license. |
| 6 |
*/ |
| 7 |
|
| 8 |
#define PY_SSIZE_T_CLEAN |
| 9 |
#include <stdio.h> |
| 10 |
#include <string.h> |
| 11 |
#include <errno.h> |
| 12 |
#include <libgen.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <unistd.h> |
| 15 |
|
| 16 |
#define PATHLEN 1023 |
| 17 |
|
| 18 |
#define SET(x, y) \ |
| 19 |
if (setenv(x, y, 1) != 0) { \ |
| 20 |
fprintf(stderr, "Failed to set environment variable with error: %s\n", strerror(errno)); \ |
| 21 |
return 1; \ |
| 22 |
} |
| 23 |
|
| 24 |
int |
| 25 |
main(int argc, char **argv) { |
| 26 |
static char buf[PATHLEN + 1] = {0}, lib[PATHLEN + 1] = {0}, base[PATHLEN + 1] = {0}, exe[PATHLEN + 1] = {0}, *ldp = NULL; |
| 27 |
|
| 28 |
if (readlink("/proc/self/exe", buf, PATHLEN) == -1) { |
| 29 |
fprintf(stderr, "Failed to read path of executable with error: %s\n", strerror(errno)); |
| 30 |
return 1; |
| 31 |
} |
| 32 |
strncpy(lib, buf, PATHLEN); |
| 33 |
strncpy(base, dirname(lib), PATHLEN); |
| 34 |
int ret = snprintf(exe, PATHLEN, "%s/bin/%s", base, basename(buf)); |
| 35 |
if (ret < 0 || ret > (PATHLEN - 2)) { |
| 36 |
fprintf(stderr, "Path to executable too long: %s/bin/%s", base, basename(buf)); |
| 37 |
return 1; |
| 38 |
} |
| 39 |
memset(lib, 0, PATHLEN); |
| 40 |
ret = snprintf(lib, PATHLEN, "%s/lib", base); |
| 41 |
if (ret < 0 || ret > (PATHLEN - 2)) { |
| 42 |
fprintf(stderr, "Path to lib too long: %s/lib", base); |
| 43 |
return 1; |
| 44 |
} |
| 45 |
|
| 46 |
SET("CALIBRE_QT_PREFIX", base) |
| 47 |
|
| 48 |
memset(buf, 0, PATHLEN); |
| 49 |
ldp = getenv("LD_LIBRARY_PATH"); |
| 50 |
if (ldp == NULL) strncpy(buf, lib, PATHLEN); |
| 51 |
else { |
| 52 |
ret = snprintf(buf, PATHLEN, "%s:%s", lib, ldp); |
| 53 |
if (ret < 0 || ret > (PATHLEN - 2)) { |
| 54 |
fprintf(stderr, "LD_LIBRARY_PATH too long: %s:%s", lib, ldp); |
| 55 |
return 1; |
| 56 |
} |
| 57 |
} |
| 58 |
SET("LD_LIBRARY_PATH", buf) |
| 59 |
ret = snprintf(buf, PATHLEN, "%s/ossl-modules", lib); |
| 60 |
if (ret < 0 || ret > (PATHLEN - 2)) { |
| 61 |
fprintf(stderr, "OPENSSL_MODULES too long: %s/ossl-modules", lib); |
| 62 |
return 1; |
| 63 |
} |
| 64 |
SET("OPENSSL_MODULES", buf) |
| 65 |
|
| 66 |
argv[0] = exe; |
| 67 |
if (execv(exe, argv) == -1) { |
| 68 |
fprintf(stderr, "Failed to execute binary: %s with error: %s\n", exe, strerror(errno)); |
| 69 |
return 1; |
| 70 |
} |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |