DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 26th October 2008
corey_james corey_james is offline
Uber Geek
 
Join Date: Apr 2008
Location: Brisbane, Australia
Posts: 238
Thanked 4 Times in 10 Posts
Default c programming - Modules

I'm in the process of planning an application which i intend on marketing and I'd like to implement modules that can be loaded dynamically into the application.

For example, someone buys the core system and wants to add ... say MP3 support, so they'd buy this module and install it.

How would something like this actually work ?

Would this be achieved using 'dlopen' ?
__________________
"No, that's wrong, Cartman. But don't worry, there are no stupid answers, just stupid people." -- Mr. Garrison

Forum Netiquette

Last edited by corey_james; 26th October 2008 at 09:36 AM.
Reply With Quote
  #2   (View Single Post)  
Old 26th October 2008
corey_james corey_james is offline
Uber Geek
 
Join Date: Apr 2008
Location: Brisbane, Australia
Posts: 238
Thanked 4 Times in 10 Posts
Default

Well, i have had a play and i think using dlopen and dlsym is the best way to go about this. Here's some examples if anyone is interested:

libtest.c
Code:
#include <stdio.h>
void testfunc() {

printf("Test plugin\n");
}
Compiled as follows:
gcc -fPIC -g -c -Wall libtest.c

main.c
Code:
#include <stdio.h>
#include <dlfcn.h>

int main () {

void *handle = NULL;
void (*testlib)(void) = NULL;

handle = dlopen("./libtest.so" ,RTLD_LAZY);
testlib = dlsym(handle, "testfunc");

if ( testlib == NULL ) 
{
	printf("Error: %s \n", dlerror());
}
else 
{
	testlib();
}
}
compiled with: gcc main.c

making the library into a shared library ( required )

Code:
gcc -shared -Wl,-soname,libtest.so.1 -o libtest.so libtest.o -lc
__________________
"No, that's wrong, Cartman. But don't worry, there are no stupid answers, just stupid people." -- Mr. Garrison

Forum Netiquette
Reply With Quote
  #3   (View Single Post)  
Old 30th October 2008
mdh's Avatar
mdh mdh is offline
Real Name: Matt D. Harris
FreeBSD 2.2.6 User
 
Join Date: Oct 2008
Location: West Virginia
Posts: 139
Thanked 8 Times in 8 Posts
Default

One thing to keep in mind is that dlfunc() isn't portable outside of the BSD realm. Creating modular code using dlopen() and friends is surprisingly easy. The man pages are good, which helps a lot, as well. Generally, the model used is thus:

dlopen() the module. If this succeeds, use dlfunc() or dlsym() (depending on whether portability matters) to find an initialization function. You may also want to have a structure for modules in which to store a pointer to a de-initialization function to be executed when a module is unloaded. Do this, then once everything's been successful, execute the initialization function.

The initialization function should call some function(s) in your code to add itself to arrays of whatever it wants - for example, with an http server, you may want to do something like:
Code:
module_register_handler(".php", mod_php_filehandler);
Where module_register_handler() inserts a directive that when a file ending in .php is requested, the function pointed to by the second argument - in this case, mod_php_filehandler() - shall be called.

The module_register_handler() function should insert that into a table, against which requests for files are checked. When that occurs, the function mod_php_filehandler() or any other handler, such as a module which just sends a file down the line with associated mime type or whatever, shall be called with the request environment (client info, URI path, environment variables, stdin data if POST or somesuch was used, etc) as an argument or arguments.

Of course, this is just one way to use it. Be creative.
Reply With Quote
  #4   (View Single Post)  
Old 6th November 2008
nfries88's Avatar
nfries88 nfries88 is offline
Port Guard
 
Join Date: Sep 2008
Posts: 24
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by corey_james View Post
Would this be achieved using 'dlopen' ?
Yes, and on windows it would be achieved with LoadLibrary, GetProcAddress, and FreeLibrary.
(Since it seems you plan to market this application I can only assume Windows is a target for you too)

The basic implementation is:
1) open the library (dlopen/LoadLibrary)
2) get the pointer to the function you want to get from the module, IE "filetype_handler" (dlsym/GetProcAddress)
3) use the function as needed
4) at shutdown or when the module is no longer needed, close the module (dlclose/FreeLibrary).
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
NetBSD New kernel modules in NetBSD-CURRENT (instead LKM) vermaden News 0 5th October 2009 05:28 PM
Problem setting up perl modules badguy OpenBSD Installation and Upgrading 4 10th August 2009 09:46 PM
kernel modules Mr-Biscuit FreeBSD General 0 2nd March 2009 06:18 AM
What's the difference between these two memory modules? Carpetsmoker General Hardware 2 25th November 2008 03:13 PM
Are certain kernel modules permanent? davidgurvich FreeBSD General 3 6th June 2008 06:14 PM


All times are GMT. The time now is 12:53 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick