Unverified Commit 65f6327b authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

intercept fopen

parent af8c9be9
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -9,8 +9,8 @@
extern void* libc;

extern void* libc_open;
extern void* libc_fopen; // XXX Does not work with streaming pointers. If used will block forever
extern void* libc_fopen64; // XXX Does not work with streaming pointers. If used will block forever
extern void* libc_fopen;
extern void* libc_fopen64;

extern void* libc_mkdir;
extern void* libc_mkdirat;
+27 −13
Original line number Diff line number Diff line
@@ -52,19 +52,33 @@ int open64(const char* path, int flags, ...) {
    return open(path, flags | O_LARGEFILE, mode);
}

//// TODO This function somehow always blocks forever if one puts anything between the paththru...
//FILE* fopen(const char* path, const char* mode) {
////    init_passthrough_if_needed();
////    DAEMON_DEBUG(debug_fd, "fopen called with path %s\n", path);
//    return (reinterpret_cast<decltype(&fopen)>(libc_fopen))(path, mode);
//}

//// TODO This function somehow always blocks forever if one puts anything between the paththru...
//FILE* fopen64(const char* path, const char* mode) {
////    init_passthrough_if_needed();
////    DAEMON_DEBUG(debug_fd, "fopen64 called with path %s\n", path);
//    return (reinterpret_cast<decltype(&fopen)>(libc_fopen))(path, mode);
//}
FILE* fopen(const char* path, const char* mode) {
    init_passthrough_if_needed();
    if(CTX->initialized()) {
        CTX->log()->trace("{}() called with path '{}' with mode '{}'", __func__, path, mode);
        std::string rel_path(path);
        if (CTX->relativize_path(rel_path)) {
            CTX->log()->error("{}() NOT SUPPORTED", __func__);
            errno = ENOTSUP;
            return nullptr;
        }
    }
    return (reinterpret_cast<decltype(&fopen)>(libc_fopen))(path, mode);
}

FILE* fopen64(const char* path, const char* mode) {
    init_passthrough_if_needed();
    if(CTX->initialized()) {
        CTX->log()->trace("{}() called with path '{}' with mode '{}'", __func__, path, mode);
        std::string rel_path(path);
        if (CTX->relativize_path(rel_path)) {
            CTX->log()->error("{}() NOT SUPPORTED", __func__);
            errno = ENOTSUP;
            return nullptr;
        }
    }
    return (reinterpret_cast<decltype(&fopen)>(libc_fopen64))(path, mode);
}

#undef creat

+2 −2
Original line number Diff line number Diff line
@@ -76,8 +76,8 @@ void init_passthrough_() {
    }

    libc_open = dlsym(libc, "open");
//    libc_fopen = dlsym(libc, "fopen");
//    libc_fopen64 = dlsym(libc, "fopen64");
    libc_fopen = dlsym(libc, "fopen");
    libc_fopen64 = dlsym(libc, "fopen64");
    libc_mkdir = dlsym(libc, "mkdir");
    libc_mkdirat = dlsym(libc, "mkdirat");