Verified Commit 7eeb58d4 authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

intercept fdopendir

parent 80c1e9b0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -73,6 +73,7 @@ extern void* libc_dup2;
extern void* libc_dup3;

extern void* libc_opendir;
extern void* libc_fdopendir;
extern void* libc_readdir;
extern void* libc_closedir;

+18 −0
Original line number Diff line number Diff line
@@ -836,6 +836,24 @@ DIR* opendir(const char* path){
    return (reinterpret_cast<decltype(&opendir)>(libc_opendir))(path);
}

DIR* fdopendir(int fd){
    init_passthrough_if_needed();
    if(CTX->initialized()) {
        CTX->log()->trace("{}() called with fd {}", __func__, fd);
        if (CTX->file_map()->exist(fd)) {
            auto open_file = CTX->file_map()->get(fd);
            auto open_dir = static_pointer_cast<OpenDir>(open_file);
            if(!open_dir){
                //Cast did not succeeded: open_file is a regular file
                errno = EBADF;
                return nullptr;
            }
            return fd_to_dirp(fd);
        }
    }
    return (reinterpret_cast<decltype(&fdopendir)>(libc_fdopendir))(fd);
}

struct dirent* intcp_readdir(DIR* dirp){
    init_passthrough_if_needed();
    if(CTX->initialized()) {
+2 −0
Original line number Diff line number Diff line
@@ -78,6 +78,7 @@ void* libc_dup2;
void* libc_dup3;

void* libc_opendir;
void* libc_fdopendir;
void* libc_readdir;
void* libc_closedir;

@@ -159,6 +160,7 @@ void init_passthrough_() {
    libc_dup3 = dlsym(libc, "dup3");

    libc_opendir = dlsym(libc, "opendir");
    libc_fdopendir = dlsym(libc, "fdopendir");
    libc_readdir = dlsym(libc, "readdir");
    libc_closedir = dlsym(libc, "closedir");