Hi!

I'd like to override the open() function.

I need to get device name argument and return handle back to original open() function.
That's all!

I have wrote little code:

#include <dlfcn.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

int open(const char *path, int oflag, ... )
{
    static int (*orig_open)(const char *path, int oflag, ... );
    static void *handle = NULL;
    char *error;
    va_list opt;
    void *arg;

    va_start(opt, oflag);
    arg = va_arg(opt, void *);
    va_end(opt);

    if(path == "/dev/ttyS0") {
        printf("Application using ttyS0.\n");
    }else if(path == "/dev/ttyS1") {
        printf("Application using ttyS1.\n");
    }
    if(handle == NULL) {
        handle = dlopen("/lib/libc.so.0", RTLD_LAZY);
        if (!handle) {
            fputs(dlerror(), stderr);
            exit(1);
        }

        orig_open = dlsym(handle, "open");
        if ((error = dlerror()) != NULL) {
            fprintf(stderr, "%s\n", error);
            exit(1);
        }
    }
    return orig_open(path, oflag, arg);
}

Is it correct overriding?

Thanks!

-Alex