diff options
author | Samuel Holland <samuel@sholland.org> | 2018-09-11 22:28:24 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2018-09-11 22:50:44 -0500 |
commit | e9224395eba97e86d8d411f016972b2d70cd6ff3 (patch) | |
tree | 3298e2079d913f7742016220c1d2145e8c401804 /libgcompat | |
parent | 71d20ba411d3923934409936ba20f6ead8a6cbcd (diff) | |
download | gcompat-e9224395eba97e86d8d411f016972b2d70cd6ff3.tar.gz gcompat-e9224395eba97e86d8d411f016972b2d70cd6ff3.tar.bz2 gcompat-e9224395eba97e86d8d411f016972b2d70cd6ff3.tar.xz gcompat-e9224395eba97e86d8d411f016972b2d70cd6ff3.zip |
realpath: Intercept realpath(/proc/self/exe)
Reuse the machinery from readlink
Signed-off-by: Samuel Holland <samuel@sholland.org>
Diffstat (limited to 'libgcompat')
-rw-r--r-- | libgcompat/realpath.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/libgcompat/realpath.c b/libgcompat/realpath.c new file mode 100644 index 0000000..ab68cc4 --- /dev/null +++ b/libgcompat/realpath.c @@ -0,0 +1,51 @@ +#include <dlfcn.h> +#include <errno.h> +#include <fcntl.h> +#include <limits.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#ifndef LINKER +#error LINKER must be defined +#endif + +static char *(*real_realpath)(const char *restrict, char *restrict); + +char *realpath(const char *restrict path, char *restrict resolved) +{ + if (real_realpath == NULL) { + real_realpath = dlsym(RTLD_NEXT, "realpath"); + if (real_realpath == NULL) { + errno = ENOSYS; + return NULL; + } + } + + if (!strcmp(path, "/proc/self/exe")) { + char *fixed = resolved; + + if (fixed == NULL) { + fixed = malloc(PATH_MAX); + if (fixed == NULL) { + errno = ENOMEM; + return NULL; + } + } + + /* If passed in, the buffer is at least PATH_MAX per POSIX. */ + ssize_t written = readlink(path, fixed, PATH_MAX - 1); + if (written == -1) { + /* Free the buffer iff we allocated it. */ + if (fixed != resolved) + free(fixed); + return NULL; + } + fixed[written] = '\0'; + + return fixed; + } + + return real_realpath(path, resolved); +} |