diff options
-rw-r--r-- | CHANGELOG.rst | 5 | ||||
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | libgcompat/realpath.c | 51 |
3 files changed, 55 insertions, 2 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2a4b1d3..188d75c 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -35,9 +35,10 @@ math * Add most __*_finite functions, courtesy of Elizabeth Myers. -readlink --------- +readlink/realpath +----------------- +* Intercept realpath as well as readlink. * Report ENOSYS on dlsym failure. @@ -16,6 +16,7 @@ LIBGCOMPAT_SRC = \ libgcompat/pthread.c \ libgcompat/pwd.c \ libgcompat/readlink.c \ + libgcompat/realpath.c \ libgcompat/resolv.c \ libgcompat/resource.c \ libgcompat/setjmp.c \ 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); +} |