Below is the file 'fritz_client.cc' from this revision. You can also download the file.


/* fritz.it
 * A framework for intercepting application input, altering it,
 * and checking for abormal changes in application behaviour.
 *
 * Copyright (C) 2007 Grahame Bowland.
 * All rights reserved.
 */

#include <sys/errno.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <fcntl.h>
#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <exception>

#include "fritz.hh"

class ErrnoGuard {

public:
    int __errno;

    ErrnoGuard() {
        __errno = errno;
    };

    ~ErrnoGuard() {
        errno = __errno;
    };

};

static FritzClient client;

typedef ssize_t ((*read_syscall)(int, void*, size_t));

ssize_t
read (int d, void *buf, size_t nbytes)
{
    static int depth;
    double (*cosine)(double);
    static read_syscall real_read;
    int rlen;

    depth++;
    if (depth != 1) {
        throw fritz_exception ("Recursion in override detected.");
    }
    if (!real_read) {
        real_read = reinterpret_cast<read_syscall> (client.grab_symbol_from ("FRITZ_LIBC", "read"));
    }
    rlen = (real_read) (d, buf, nbytes);
    ErrnoGuard e;
    client.copy_data (rlen, buf);
    client.pass_to_server ();
    rlen = client.copy_back (nbytes, buf);
    depth--;
    return rlen;
}