LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Making own modules for own bot (http://www.linuxquestions.org/questions/programming-9/making-own-modules-for-own-bot-274312/)

alitrix 01-05-2005 04:13 PM

Making own modules for own bot
 
Hi,

I'm working on a C bot at this moment, but a friend asked me if I can build a feature for dlopen modules.
Oke, after studying this nice tool I found out it may help me alot with my bot (for example, restarting a module instead of restarting the whole bot)

But now my problem, how can I make it possible to load functions if I don't know which functions I have to declear?
Oke, let me explain, this is what I have:
dltest.c
Code:

#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>


int main(int argc, char **argv) {
        void *handle;
        void (*foo) (void);
        void (*bar) (void);
        char *error;

        handle = dlopen("./bar.so", RTLD_LAZY);
        if (!handle) {
                printf("%s\n", dlerror());
                exit(0);
        }

        // clear existing errors
        dlerror();
        *(&foo) = dlsym(handle, "foo");
        if ((error = dlerror()) != NULL) {
                printf("%s\n", error);
                exit(0);
        }

        dlerror();
        *(&bar) = dlsym(handle, "bar");
        if ((error = dlerror()) != NULL) {
                printf("%s\n", error);
                exit(0);
        }

        (foo)();
        (bar)();

        dlclose(handle);
        return 0;
}

bar.c
Code:

#include <stdio.h>

void bar(void) {
        printf("foo ;)\n");
}

void foo(void) {
        printf("bar ;)\n");
}


Makefile(incase that's required)
Code:

all: dltest bar.so

dltest: dltest.c
        gcc -rdynamic -o dltest dltest.c -ldl

bar.so: bar.c
        gcc -shared -nostartfiles -o bar.so bar.c

clean:
        rm dltest *.so

Oke, as you see in dltest.c this:
Code:

void (*foo) (void);
        void (*bar) (void);

But how if I don't know what functions are in bar.so?

Does anybody has some tips for me? (It's oke if I have to make a structure with a list of functions that are supported in bar.so, but can you show me a example how?)
(Btw, using _init and _fini is oke with me)

Greetz,
alitrix

alitrix 01-06-2005 02:13 AM

Nobody can help me :(?

alitrix 01-06-2005 02:43 AM

Forget it, somebody else could help me a bit! :)
And for the people that want to know the answer, here are the results:

plugins.h
Code:

#ifndef _PLUGINS_H_
#define _PLUGINS_H_
struct Plugin {
 
        void (*foo)(void);
        void (*bar)(void);
};


int init_plugin(struct Plugin *ini);
#define WELL_KNOWN_INIT_FUNCTION "init_plugin"
 
#endif



dltest.c
Code:

#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include "plugins.h"
 
int main(int argc, char **argv) {
        void *handle;
        char *error;
        int (*theplug)(struct Plugin *p);
        struct Plugin plug;
 
        handle = dlopen("./bar.so", RTLD_LAZY);
        if (!handle) {
                printf("%s\n", dlerror());
                exit(0);
        }
 
        // clear existing errors
        dlerror();
        theplug = dlsym(handle, WELL_KNOWN_INIT_FUNCTION);
        if ((error = dlerror()) != NULL) {
                printf("%s\n", error);
                exit(0);
        }
        if((*theplug)(&plug) != 0) {
                puts("Plugin error :-(");
                abort();
        }
 
        dlerror();
        plug.foo();
        plug.bar();
 
        dlclose(handle);
        return 0;
}



bar.c
Code:

#include <stdio.h>
#include "plugins.h"
 
static void bar(void) {
        printf("foo ;)\n");
}
 
static void foo(void) {
        printf("bar ;)\n");
}
 
int init_plugin(struct Plugin *init)
{
        init->foo = foo;
        init->bar = bar;
 
        return 0; //Ok!
}


Good luck with it! (tnx to noselasd @ #c (freenode.net))


All times are GMT -5. The time now is 09:22 AM.