LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-05-2005, 04:13 PM   #1
alitrix
Member
 
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169

Rep: Reputation: 30
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
 
Old 01-06-2005, 02:13 AM   #2
alitrix
Member
 
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169

Original Poster
Rep: Reputation: 30
Nobody can help me ?
 
Old 01-06-2005, 02:43 AM   #3
alitrix
Member
 
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169

Original Poster
Rep: Reputation: 30
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))
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is a BOT? sharathkv Programming 5 11-22-2003 12:21 PM
Re: modprobe: Note: /etc/modules.conf is more recent than lib/modules/2.4.9/modules.d Andy.M Linux - General 1 01-24-2002 01:50 AM
Re: modprobe: Note: /etc/modules.conf is more recent than lib/modules/2.4.9/modules.d Andy.M Linux - Newbie 2 01-24-2002 01:40 AM
Making custom kernel..but drivers and modules wont load on boot skoal Linux - General 10 12-06-2001 06:59 PM
problem with a sound card when making modules pibevalera Linux - General 4 08-08-2001 09:38 AM


All times are GMT -5. The time now is 05:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration