View Single Post
  #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
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