From 840049332933425e655c8a517a4beb2fe7150a66 Mon Sep 17 00:00:00 2001 From: king Date: Sat, 10 Aug 2024 12:24:02 +0000 Subject: [PATCH] add linux dlsym hook --- test_so/posix.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++ test_so/test_so.c | 29 +++++++++++++++++ zv2404fs.c | 63 ++++++++++++++++++++++++++++++++++++- 3 files changed, 170 insertions(+), 1 deletion(-) create mode 100755 test_so/posix.c create mode 100755 test_so/test_so.c diff --git a/test_so/posix.c b/test_so/posix.c new file mode 100755 index 0000000..fd900cc --- /dev/null +++ b/test_so/posix.c @@ -0,0 +1,79 @@ + + +// hook --> + +// open + + +// write + + +// read + + +// close + +#define _GNU_SOURCE +#include + + +#include + + + +typedef int (*open_t)(const char *pathname, int flags); +open_t open_f = NULL; + +typedef ssize_t (*read_t)(int fd, void *buf, size_t count); +read_t read_f = NULL; + + +typedef ssize_t (*write_t)(int fd, const void *buf, size_t count); +write_t write_f = NULL; + +typedef int (*close_t)(int fd); +close_t close_f = NULL; + + +int open(const char *pathname, int flags) { + + if (open_f == NULL) { + open_f = dlsym(RTLD_NEXT, "open"); + } + + printf("open .. %s\n", pathname); + + return open_f(pathname, flags); +} + +ssize_t read(int fd, void *buf, size_t count) { + + if (read_f == NULL) { + read_f = dlsym(RTLD_NEXT, "read"); + } + + printf("read ..\n"); + return read_f(fd, buf, count); +} + +ssize_t write(int fd, const void *buf, size_t count) { + + if (write_f == NULL) { + write_f = dlsym(RTLD_NEXT, "write"); + } + + printf("write ..\n"); + return write_f(fd, buf, count); +} + +int close(int fd) { + if (close_f == NULL) { + close_f = dlsym(RTLD_NEXT, "close"); + } + + printf("close ..\n"); + return close_f(fd); +} + + + diff --git a/test_so/test_so.c b/test_so/test_so.c new file mode 100755 index 0000000..35b7c5b --- /dev/null +++ b/test_so/test_so.c @@ -0,0 +1,29 @@ + + +#include +#include +#include +#include +#include +#include + +// posix +int main() { + + int fd = open("a.txt", O_CREAT | O_RDWR); + + char *buffer = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + write(fd, buffer, strlen(buffer)); + + char rbuffer[128] = {0}; + read(fd, buffer, 128); + + printf("rbuffer: %s\n", buffer); + + close(fd); +} + + + + diff --git a/zv2404fs.c b/zv2404fs.c index 04b79dd..32fe20d 100755 --- a/zv2404fs.c +++ b/zv2404fs.c @@ -255,12 +255,73 @@ int main(int argc, char *argv[]) { } + + #else + + +typedef int (*open_t)(const char *pathname, int flags); +open_t open_f = NULL; + +typedef ssize_t (*read_t)(int fd, void *buf, size_t count); +read_t read_f = NULL; + + +typedef ssize_t (*write_t)(int fd, const void *buf, size_t count); +write_t write_f = NULL; + +typedef int (*close_t)(int fd); +close_t close_f = NULL; + + +int open(const char *pathname, int flags) { + + if (open_f == NULL) { + open_f = dlsym(RTLD_NEXT, "open"); + } + + printf("open .. %s\n", pathname); + + return open_f(pathname, flags); +} + +ssize_t read(int fd, void *buf, size_t count) { + + if (read_f == NULL) { + read_f = dlsym(RTLD_NEXT, "read"); + } + + printf("read ..\n"); + return read_f(fd, buf, count); +} + +ssize_t write(int fd, const void *buf, size_t count) { + + if (write_f == NULL) { + write_f = dlsym(RTLD_NEXT, "write"); + } + + printf("write ..\n"); + return write_f(fd, buf, count); +} + +int close(int fd) { + if (close_f == NULL) { + close_f = dlsym(RTLD_NEXT, "close"); + } + + printf("close ..\n"); + return close_f(fd); +} + + + + // posix int main() { - int fd = open("a.txt", O_CREAT | O_READ); + int fd = open("a.txt", O_CREAT | O_RDWR); char *buffer = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";