Unverified Commit 95052b4f authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

intercept writev

parent 5000d3e0
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -22,8 +22,13 @@ int hook_close(int fd);
int hook_stat(const char* path, struct stat* buf);
int hook_lstat(const char* path, struct stat* buf);
int hook_fstat(unsigned int, struct stat* buf);
int hook_read(int fd, void* buf, size_t count);
int hook_write(int fd, void* buf, size_t count);
int hook_read(unsigned int fd, void* buf, size_t count);
int hook_pread(unsigned int fd, char * buf, size_t count, loff_t pos);
int hook_write(unsigned int fd, const char * buf, size_t count);
int hook_pwrite(unsigned int fd, const char * buf, size_t count, loff_t pos);
int hook_writev(unsigned long fd, const struct iovec * iov, unsigned long iovcnt);
int hook_pwritev(unsigned long fd, const struct iovec * iov, unsigned long iovcnt,
                 unsigned long pos_l, unsigned long pos_h);
int hook_unlinkat(int dirfd, const char * cpath, int flags);
int hook_access(const char* path, int mask);
int hook_lseek(unsigned int fd, off_t offset, unsigned int whence);
+43 −12
Original line number Diff line number Diff line
@@ -91,30 +91,61 @@ int hook_fstat(unsigned int fd, struct stat* buf) {
    return syscall_no_intercept(SYS_fstat, fd, buf);
}

int hook_read(int fd, void* buf, size_t count) {
int hook_read(unsigned int fd, void* buf, size_t count) {
    CTX->log()->trace("{}() called with fd {}, count {}", __func__, fd, count);
    if (CTX->file_map()->exist(fd)) {
        auto ret = adafs_read(fd, buf, count);
        if(ret < 0) {
            return -errno;
        }
        return ret;
        return  with_errno(adafs_read(fd, buf, count));
    }
    return syscall_no_intercept(SYS_read, fd, buf, count);
}

int hook_write(int fd, void* buf, size_t count) {
    CTX->log()->trace("{}() called with fd {}, count {}", __func__, fd, count);
int hook_pread(unsigned int fd, char * buf, size_t count, loff_t pos) {
    CTX->log()->trace("{}() called with fd {}, count {}, pos {}",
                      __func__, fd, count, pos);
    if (CTX->file_map()->exist(fd)) {
        auto ret = adafs_write(fd, buf, count);
        if(ret < 0) {
            return -errno;
        return with_errno(adafs_pread_ws(fd, buf, count, pos));
    }
        return ret;
    /* Since kernel 2.6: pread() became pread64(), and pwrite() became pwrite64(). */
    return syscall_no_intercept(SYS_pread64, fd, buf, count, pos);
}

int hook_write(unsigned int fd, const char * buf, size_t count) {
    CTX->log()->trace("{}() called with fd {}, count {}", __func__, fd, count);
    if (CTX->file_map()->exist(fd)) {
        return with_errno(adafs_write(fd, buf, count));
    }
    return syscall_no_intercept(SYS_write, fd, buf, count);
}

int hook_pwrite(unsigned int fd, const char * buf, size_t count, loff_t pos) {
    CTX->log()->trace("{}() called with fd {}, count {}, pos {}",
                      __func__, fd, count, pos);
    if (CTX->file_map()->exist(fd)) {
        return with_errno(adafs_pwrite_ws(fd, buf, count, pos));
    }
    /* Since kernel 2.6: pread() became pread64(), and pwrite() became pwrite64(). */
    return syscall_no_intercept(SYS_pwrite64, fd, buf, count, pos);
}

int hook_writev(unsigned long fd, const struct iovec * iov, unsigned long iovcnt) {
    CTX->log()->trace("{}() called with fd {}, ops_num {}", __func__, fd, iovcnt);
    if (CTX->file_map()->exist(fd)) {
        return with_errno(adafs_writev(fd, iov, iovcnt));
    }
    return syscall_no_intercept(SYS_writev, fd, iov, iovcnt);
}

int hook_pwritev(unsigned long fd, const struct iovec * iov, unsigned long iovcnt,
                 unsigned long pos_l, unsigned long pos_h) {
    CTX->log()->trace("{}() called with fd {}, ops_num {}, low position {},"
                      "high postion {}", __func__, fd, iovcnt, pos_l, pos_h);
    if (CTX->file_map()->exist(fd)) {
        CTX->log()->warn("{}() Not supported", __func__);
        return -ENOTSUP;
    }
    return syscall_no_intercept(SYS_pwritev, fd, iov, iovcnt);
}

int hook_unlinkat(int dirfd, const char * cpath, int flags) {
    CTX->log()->trace("{}() called with path '{}' dirfd {}, flags {}",
                      __func__, cpath, dirfd, flags);
+30 −3
Original line number Diff line number Diff line
@@ -70,17 +70,44 @@ static inline int hook(long syscall_number,
        break;

    case SYS_read:
        *result = hook_read(static_cast<int>(arg0),
        *result = hook_read(static_cast<unsigned int>(arg0),
                            reinterpret_cast<void*>(arg1),
                            static_cast<size_t>(arg2));
        break;

    case SYS_pread64:
        *result = hook_pread(static_cast<unsigned int>(arg0),
                             reinterpret_cast<char *>(arg1),
                             static_cast<size_t>(arg2),
                             static_cast<loff_t>(arg3));
        break;

    case SYS_pwrite64:
        *result = hook_pwrite(static_cast<unsigned int>(arg0),
                              reinterpret_cast<const char *>(arg1),
                              static_cast<size_t>(arg2),
                              static_cast<loff_t>(arg3));
        break;
    case SYS_write:
        *result = hook_write(static_cast<int>(arg0),
                             reinterpret_cast<void*>(arg1),
        *result = hook_write(static_cast<unsigned int>(arg0),
                             reinterpret_cast<const char *>(arg1),
                             static_cast<size_t>(arg2));
        break;

    case SYS_writev:
        *result = hook_writev(static_cast<unsigned long>(arg0),
                              reinterpret_cast<const struct iovec *>(arg1),
                              static_cast<unsigned long>(arg2));
        break;

    case SYS_pwritev:
        *result = hook_pwritev(static_cast<unsigned long>(arg0),
                               reinterpret_cast<const struct iovec *>(arg1),
                               static_cast<unsigned long>(arg2),
                               static_cast<unsigned long>(arg3),
                               static_cast<unsigned long>(arg4));
        break;

    case SYS_unlink:
        *result = hook_unlinkat(AT_FDCWD,
                                reinterpret_cast<const char *>(arg0),