Commit 57304401 authored by Ramon Nou's avatar Ramon Nou
Browse files

Support dup3 and freopen (hexdump)

parent 803e9a90
Loading
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -18,8 +18,8 @@
 *  along with Expand.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Modifications to support GekkoFS done by :
 *   Copyright 2018-2024, Barcelona Supercomputing Center (BSC), Spain
 *   Copyright 2015-2024, Johannes Gutenberg Universitaet Mainz, Germany
 *   Copyright 2018-2025, Barcelona Supercomputing Center (BSC), Spain
 *   Copyright 2015-2025, Johannes Gutenberg Universitaet Mainz, Germany
 *
 * This software was partially supported by the
 * EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
@@ -59,8 +59,8 @@ struct __dirstream {
    off_t filepos;     // Position of next entry to read.
    // Directory block.
    char* path;
    char data[0] __attribute__((aligned(__alignof__(void*))));
};
    char data[1] __attribute__((aligned(__alignof__(void*))));
}; // Originally its 0, but C++ does not permit it

int
open64(const char* path, int flags, ...);
@@ -99,7 +99,7 @@ dup(int oldfd) __THROW __wur;
int
dup2(int oldfd, int newfd) __THROW;
int
dup3(int oldfd, int newfd, int flags) __THROW;
dup3(int __fd, int __fd2, int __flags) __THROW;
ssize_t
pread(int fd, void* buf, size_t count, off_t offset);
ssize_t
@@ -180,6 +180,8 @@ clone(int (*fn)(void*), void* stack, int flags, void* arg, ...);
FILE*
fopen(const char* filename, const char* mode);

FILE*
freopen64(const char* pathname, const char* mode, FILE* stream);

size_t
fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
+47 −13
Original line number Diff line number Diff line
@@ -18,8 +18,8 @@
 *  along with Expand.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Modifications to support GekkoFS done by :
 *   Copyright 2018-2024, Barcelona Supercomputing Center (BSC), Spain
 *   Copyright 2015-2024, Johannes Gutenberg Universitaet Mainz, Germany
 *   Copyright 2018-2025, Barcelona Supercomputing Center (BSC), Spain
 *   Copyright 2015-2025, Johannes Gutenberg Universitaet Mainz, Germany
 *
 * This software was partially supported by the
 * EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
@@ -196,6 +196,7 @@ DLSYM_WRAPPER(int, fork, (void), (), "fork")
DLSYM_WRAPPER(int, pipe, (int pipefd[2]), (pipefd), "pipe")
DLSYM_WRAPPER(int, dup, (int fd), (fd), "dup")
DLSYM_WRAPPER(int, dup2, (int fd, int fd2), (fd, fd2), "dup2")
DLSYM_WRAPPER(int, dup3, (int fd, int fd2, int flags), (fd, fd2, flags), "dup3")
DLSYM_WRAPPER(void, exit, (int status), (status), "exit")
DLSYM_WRAPPER(int, chdir, (char* path), (path), "chdir")
// DLSYM_WRAPPER(int, chmod, (char* path, mode_t mode), (path, mode), "chmod")
@@ -213,6 +214,9 @@ DLSYM_WRAPPER(int, flock, (int fd, int operation), (fd, operation), "flock")
DLSYM_WRAPPER(FILE*, fopen, (const char* filename, const char* mode),
              (filename, mode), "fopen")
DLSYM_WRAPPER(FILE*, fdopen, (int fd, const char* mode), (fd, mode), "fdopen")
DLSYM_WRAPPER(FILE*, freopen64,
              (const char* filename, const char* mode, FILE* stream),
              (filename, mode, stream), "freopen")
DLSYM_WRAPPER(int, fclose, (FILE * stream), (stream), "fclose")
DLSYM_WRAPPER(size_t, fread,
              (void* ptr, size_t size, size_t nmemb, FILE* stream),
@@ -231,8 +235,8 @@ DLSYM_WRAPPER(char*, fgets, (char* str, int n, FILE* stream), (str, n, stream),
              "fgets")
DLSYM_WRAPPER(int, fflush, (FILE * stream), (stream), "fflush")


static void
/* not used */
/*static void
convert(struct stat64* src, struct stat* dest) {
    dest->st_dev = static_cast<__dev_t>(src->st_dev);
    dest->st_ino = static_cast<__ino_t>(src->st_ino);
@@ -247,7 +251,7 @@ convert(struct stat64* src, struct stat* dest) {
    dest->st_atime = static_cast<__time_t>(src->st_atime);
    dest->st_mtime = static_cast<__time_t>(src->st_mtime);
    dest->st_ctime = static_cast<__time_t>(src->st_ctime);
}
}*/


static void
@@ -843,18 +847,19 @@ dup2(int fd, int fd2) {
    GKFS_FALLBACK(dup2, fd, fd2);
}

// TODO: Implement CLOEXEC
int
dup3(int fd, int fd2, int flags) {
    initializeGekko();
    DEBUG_INFO("[BYPASS] >> Begin dup3() %d - %d\n", fd, fd2);
    GKFS_OPERATION(dup2, fd, fd2);
    GKFS_FALLBACK(dup3, fd, fd2, flags);
}

void
exit(int status) {
    DEBUG_INFO("[BYPASS] >> Begin exit...\n");
    DEBUG_INFO("[BYPASS]    1) status %d\n", status);


    DEBUG_INFO("[BYPASS] dlsym_exit\n");

    dlsym_exit(status);
    __builtin_unreachable();

    DEBUG_INFO("[BYPASS] << After exit()\n");
}

// Manager API
@@ -1311,6 +1316,35 @@ fopen(const char* path, const char* mode) {
    GKFS_FALLBACK(fopen, path, mode);
}

FILE*
freopen64(const char* path, const char* mode, FILE* stream) {
    DEBUG_INFO("[BYPASS] >> freopen.... %s \n", path);
    initializeGekko();
    if(CTX->interception_enabled()) {
        std::string resolved;
        if(resolve_gkfs_path(AT_FDCWD, path, resolved) ==
           PathStatus::Internal) {
            int flags = 0;
            if(strchr(mode, 'r') != NULL)
                flags |= O_RDONLY;
            if(strchr(mode, 'w'))
                flags = O_WRONLY | O_CREAT | O_TRUNC;
            if(strchr(mode, 'a'))
                flags = O_WRONLY | O_CREAT | O_APPEND;
            if(strchr(mode, '+'))
                flags = O_RDWR | O_CREAT;

            const int fd = gkfs::syscall::gkfs_open(resolved, 0666, flags);
            if(fd < 0)
                return nullptr;

            stream->_fileno = fd;
            return stream;
        }
    }
    GKFS_FALLBACK(freopen64, path, mode, stream);
}

/* We return nmems, not size*/
size_t
fread(void* ptr, size_t size, size_t nmemb, FILE* stream) {