Research source · c

proc-self-compat.c

site/public/formal/boundary-context/proc-self-compat.c

13 lines. Source is displayed for inspection; it is not executed by this page.

File fingerprint

SHA-256: 6ee11fe1995a880c299af53a18b97ac88b6e0138f219a4d3e63e24c05da987ca

#define _GNU_SOURCE
#include <unistd.h>
#include <dlfcn.h>
#include <stdio.h>
#include <string.h>
ssize_t readlink(const char *path, char *buf, size_t bufsiz) {
    static ssize_t (*real_readlink)(const char *, char *, size_t) = NULL;
    if (!real_readlink) real_readlink = dlsym(RTLD_NEXT, "readlink");
    char own_exe[64];
    snprintf(own_exe, sizeof own_exe, "/proc/%d/exe", (int)getpid());
    if (strcmp(path, own_exe) == 0) path = "/proc/self/exe";
    return real_readlink(path, buf, bufsiz);
}