Commit 1a36b32e authored by Julius Plenz's avatar Julius Plenz

Supply “lazy initializers” in every intercepted function

The friendly folks at #musl say: 20:52 < dalias> i think it's good to have both that and the ctor 20:52 < Feh> This should increase robustness if it is in every function. 20:52 < dalias> because... 20:52 < dalias> several of these functions are required to be async-signal-safe 20:53 < dalias> so if the first call happened to be from a signal handler, you'd be in trouble if you didn't already have the ctor 20:54 < dalias> in theory another ctor in another lib or the app could run before yours and install and run a signal handler :-p but that's pretty unlikely and pathological 20:54 < dalias> so i think having both the ctor and the lazy-init keeps you safe in situations that will arise in the real world
parent 7a0b64d5
......@@ -154,6 +154,10 @@ static void destroy(void)
int open(const char *pathname, int flags, mode_t mode)
{
int fd;
if(!_original_open)
_original_open = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "open");
if((fd = _original_open(pathname, flags, mode)) != -1)
store_pageinfo(fd);
return fd;
......@@ -162,6 +166,10 @@ int open(const char *pathname, int flags, mode_t mode)
int open64(const char *pathname, int flags, mode_t mode)
{
int fd;
if(!_original_open64)
_original_open64 = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "open64");
if((fd = _original_open64(pathname, flags, mode)) != -1)
store_pageinfo(fd);
return fd;
......@@ -170,6 +178,10 @@ int open64(const char *pathname, int flags, mode_t mode)
int creat(const char *pathname, int flags, mode_t mode)
{
int fd;
if(!_original_creat)
_original_creat = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "creat");
if((fd = _original_creat(pathname, flags, mode)) != -1)
store_pageinfo(fd);
return fd;
......@@ -178,6 +190,10 @@ int creat(const char *pathname, int flags, mode_t mode)
int creat64(const char *pathname, int flags, mode_t mode)
{
int fd;
if(!_original_creat64)
_original_creat64 = (int (*)(const char *, int, mode_t)) dlsym(RTLD_NEXT, "creat64");
if((fd = _original_creat64(pathname, flags, mode)) != -1)
store_pageinfo(fd);
return fd;
......@@ -186,6 +202,10 @@ int creat64(const char *pathname, int flags, mode_t mode)
int openat(int dirfd, const char *pathname, int flags, mode_t mode)
{
int fd;
if(!_original_openat)
_original_openat = (int (*)(int, const char *, int, mode_t)) dlsym(RTLD_NEXT, "openat");
if((fd = _original_openat(dirfd, pathname, flags, mode)) != -1)
store_pageinfo(fd);
return fd;
......@@ -194,6 +214,10 @@ int openat(int dirfd, const char *pathname, int flags, mode_t mode)
int openat64(int dirfd, const char *pathname, int flags, mode_t mode)
{
int fd;
if(!_original_openat64)
_original_openat64 = (int (*)(int, const char *, int, mode_t)) dlsym(RTLD_NEXT, "openat64");
if((fd = _original_openat64(dirfd, pathname, flags, mode)) != -1)
store_pageinfo(fd);
return fd;
......@@ -202,6 +226,10 @@ int openat64(int dirfd, const char *pathname, int flags, mode_t mode)
int dup(int oldfd)
{
int fd;
if(!_original_dup)
_original_dup = (int (*)(int)) dlsym(RTLD_NEXT, "dup");
if((fd = _original_dup(oldfd)) != -1)
store_pageinfo(fd);
return fd;
......@@ -217,6 +245,9 @@ int dup2(int oldfd, int newfd)
if(valid_fd(newfd))
free_unclaimed_pages(newfd);
if(!_original_dup2)
_original_dup2 = (int (*)(int, int)) dlsym(RTLD_NEXT, "dup2");
if((ret = _original_dup2(oldfd, newfd)) != -1)
store_pageinfo(newfd);
return ret;
......@@ -224,6 +255,9 @@ int dup2(int oldfd, int newfd)
int close(int fd)
{
if(!_original_close)
_original_close = (int (*)(int)) dlsym(RTLD_NEXT, "close");
free_unclaimed_pages(fd);
return _original_close(fd);
}
......@@ -236,11 +270,9 @@ FILE *fopen(const char *path, const char *mode)
if(!_original_fopen)
_original_fopen = (FILE *(*)(const char *, const char *)) dlsym(RTLD_NEXT, "fopen");
if(_original_fopen) {
if((fp = _original_fopen(path, mode)) != NULL)
if((fd = fileno(fp)) != -1)
store_pageinfo(fd);
}
if((fp = _original_fopen(path, mode)) != NULL)
if((fd = fileno(fp)) != -1)
store_pageinfo(fd);
return fp;
}
......@@ -254,11 +286,9 @@ FILE *fopen64(const char *path, const char *mode)
if(!_original_fopen64)
_original_fopen64 = (FILE *(*)(const char *, const char *)) dlsym(RTLD_NEXT, "fopen64");
if(_original_fopen64) {
if((fp = _original_fopen64(path, mode)) != NULL)
if((fd = fileno(fp)) != -1)
store_pageinfo(fd);
}
if((fp = _original_fopen64(path, mode)) != NULL)
if((fd = fileno(fp)) != -1)
store_pageinfo(fd);
return fp;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment