Verified Commit 0d5c532e authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

intercept writev

parent e155dd60
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -9,8 +9,9 @@ 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_write(unsigned int fd, void* buf, size_t count);
int hook_writev(unsigned long fd, const struct iovec * iov, unsigned long iovcnt);
int hook_unlink(const char* path);
int hook_access(const char* path, int mask);
int hook_lseek(unsigned int fd, off_t offset, unsigned int whence);
+38 −12
Original line number Diff line number Diff line
@@ -100,30 +100,56 @@ 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) {
int hook_write(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_write(fd, buf, count);
        if(ret < 0) {
            return -errno;
        }
        return ret;
        return with_errno(adafs_write(fd, buf, count));
    }
    return syscall_no_intercept(SYS_write, fd, buf, count);
}

int hook_writev(unsigned long fd, const struct iovec * iov, unsigned long iovcnt) {
    CTX->log()->trace("{}() called with fd {}", __func__, fd);
    if (CTX->file_map()->exist(fd)) {
        auto adafs_fd = CTX->file_map()->get(fd);
        auto pos = adafs_fd->pos(); // retrieve the current offset
        ssize_t written = 0;
        ssize_t ret;
        for (unsigned long i = 0; i < iovcnt; ++i){
            auto count = (iov+i)->iov_len;
            if(count == 0) {
                continue;
            }
            auto buf = (iov+i)->iov_base;
            ret = adafs_pwrite_ws(fd, buf, count, pos);
            if(ret == -1) {
                break;
            }
            written += ret;
            pos += ret;

            if(static_cast<size_t>(ret) < count){
                break;
            }
        }

        if(written == 0){
            return -1;
        }
        adafs_fd->pos(pos);
        return written;
    }
    return syscall_no_intercept(SYS_writev, fd, iov, iovcnt);
}

int hook_unlink(const char* path) {
    CTX->log()->trace("{}() called with path '{}'", __func__, path);
    std::string rel_path;
+8 −2
Original line number Diff line number Diff line
@@ -57,17 +57,23 @@ 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_write:
        *result = hook_write(static_cast<int>(arg0),
        *result = hook_write(static_cast<unsigned int>(arg0),
                             reinterpret_cast<void*>(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_unlink:
        *result = hook_unlink(reinterpret_cast<char*>(arg0));
        break;