Commit 477e69c8 authored by Julius Plenz's avatar Julius Plenz

little code cleanup

parent fd9969ec
#ifndef _FCNTL_HELPERS_H
#define _FCNTL_HELPERS_H
extern int fadv_dontneed(int fd, off_t offset, off_t len);
extern int fadv_noreuse(int fd, off_t offset, off_t len);
extern int valid_fd(int fd);
extern void sync_if_writable(int fd);
extern int fcntl_dupfd(int fd, int arg);
#endif
...@@ -9,18 +9,16 @@ ...@@ -9,18 +9,16 @@
#include <string.h> #include <string.h>
#include <pthread.h> #include <pthread.h>
int (*_original_open)(const char *pathname, int flags, mode_t mode); #include "fcntl_helpers.h"
int (*_original_creat)(const char *pathname, int flags, mode_t mode);
int (*_original_openat)(int dirfd, const char *pathname, int flags, mode_t mode);
int (*_original_dup)(int fd);
int (*_original_dup2)(int newfd, int oldfd);
int (*_original_close)(int fd);
static void init(void) __attribute__((constructor)); static void init(void) __attribute__((constructor));
static void destroy(void) __attribute__((destructor)); static void destroy(void) __attribute__((destructor));
static void init_mutex(void); static void init_mutex(void);
static void handle_stdout(void); static void handle_stdout(void);
static void store_pageinfo(int fd);
static void free_unclaimed_pages(int fd);
int open(const char *pathname, int flags, mode_t mode); int open(const char *pathname, int flags, mode_t mode);
int open64(const char *pathname, int flags, mode_t mode) int open64(const char *pathname, int flags, mode_t mode)
__attribute__(( alias("open"))); __attribute__(( alias("open")));
...@@ -36,14 +34,12 @@ int dup(int oldfd); ...@@ -36,14 +34,12 @@ int dup(int oldfd);
int dup2(int oldfd, int newfd); int dup2(int oldfd, int newfd);
int close(int fd); int close(int fd);
static void store_pageinfo(int fd); int (*_original_open)(const char *pathname, int flags, mode_t mode);
static void free_unclaimed_pages(int fd); int (*_original_creat)(const char *pathname, int flags, mode_t mode);
int (*_original_openat)(int dirfd, const char *pathname, int flags, mode_t mode);
extern int fadv_dontneed(int fd, off_t offset, off_t len); int (*_original_dup)(int fd);
extern int fadv_noreuse(int fd, off_t offset, off_t len); int (*_original_dup2)(int newfd, int oldfd);
extern int valid_fd(int fd); int (*_original_close)(int fd);
extern void sync_if_writable(int fd);
extern int fcntl_dupfd(int fd, int arg);
#define _MAX_FDS 1024 #define _MAX_FDS 1024
...@@ -120,36 +116,32 @@ static void destroy(void) ...@@ -120,36 +116,32 @@ static void destroy(void)
int open(const char *pathname, int flags, mode_t mode) int open(const char *pathname, int flags, mode_t mode)
{ {
int fd; int fd;
if((fd = _original_open(pathname, flags, mode)) != -1) { if((fd = _original_open(pathname, flags, mode)) != -1)
store_pageinfo(fd); store_pageinfo(fd);
}
return fd; return fd;
} }
int creat(const char *pathname, int flags, mode_t mode) int creat(const char *pathname, int flags, mode_t mode)
{ {
int fd; int fd;
if((fd = _original_creat(pathname, flags, mode)) != -1) { if((fd = _original_creat(pathname, flags, mode)) != -1)
store_pageinfo(fd); store_pageinfo(fd);
}
return fd; return fd;
} }
int openat(int dirfd, const char *pathname, int flags, mode_t mode) int openat(int dirfd, const char *pathname, int flags, mode_t mode)
{ {
int fd; int fd;
if((fd = _original_openat(dirfd, pathname, flags, mode)) != -1) { if((fd = _original_openat(dirfd, pathname, flags, mode)) != -1)
store_pageinfo(fd); store_pageinfo(fd);
}
return fd; return fd;
} }
int dup(int oldfd) int dup(int oldfd)
{ {
int fd; int fd;
if((fd = _original_dup(oldfd)) != -1) { if((fd = _original_dup(oldfd)) != -1)
store_pageinfo(fd); store_pageinfo(fd);
}
return fd; return fd;
} }
...@@ -163,9 +155,8 @@ int dup2(int oldfd, int newfd) ...@@ -163,9 +155,8 @@ int dup2(int oldfd, int newfd)
if(valid_fd(newfd)) if(valid_fd(newfd))
free_unclaimed_pages(newfd); free_unclaimed_pages(newfd);
if((ret = _original_dup2(oldfd, newfd)) != -1) { if((ret = _original_dup2(oldfd, newfd)) != -1)
store_pageinfo(newfd); store_pageinfo(newfd);
}
return ret; return ret;
} }
...@@ -183,9 +174,7 @@ static void store_pageinfo(int fd) ...@@ -183,9 +174,7 @@ static void store_pageinfo(int fd)
void *file = NULL; void *file = NULL;
unsigned char *pageinfo = NULL; unsigned char *pageinfo = NULL;
if(fstat(fd, &st) == -1) if(fstat(fd, &st) == -1 || !S_ISREG(st.st_mode))
return;
if(!S_ISREG(st.st_mode))
return; return;
/* Hint we'll be using this file only once; /* Hint we'll be using this file only once;
......
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