C plugin architecture using libdl

I went around writing a small IRC structure in C, which I am going to expand with some basic functions, but I would also like it to be extensible with plugins!

Until now, when I wrote something related to IRC (and I wrote a lot, in about 6 different languages โ€‹โ€‹now ... I'm on fire!) And actually went ahead to implement the plugin architecture, it was inside (read: abuse), so that like clogging up the entire script file of the eval file in Ruby (bad!).

Now I want to abuse something in C!

Basically I could do three things

  • define a simple script language inside my program
  • use an existing one by embedding an interpreter
  • use libdl to load * .so modules at runtime

I am addicted to the third, and if possible, avoid the other two options. Maybe I'm some kind of masochist, but I think it can be fun and useful for educational purposes.

Logical thinking, the obvious โ€œchain of painโ€ will be (from low to high) 2 โ†’ 1 โ†’ 3, for the simple reason that libdl deals with raw code that can (and will) explode on my face more often than not.

So this question is right for you, other stackoverflow users, do you think libdl is appropriate for this task, or even a realistic thought?

+6
c plugins
source share
3 answers

libdl very well suited for pluggable architectures - within certain limits :-). It is used a lot for this purpose in many different programs. It works well in situations where there is a clearly defined API / interface between the main program and the plugin, and a number of different plugins implement the same API / interface. For example, your IRC client may have plugins that implement gateways for different IM protocols (Jabber, MSN, Sametime, etc.) - they are all very similar, so you can define APIs with features such as "send message" " , "check for an answer", etc. - and write a bunch of plug-ins in which each implemented a different protocol.

The situation in which it works worse is where you want the plug-ins to make arbitrary changes to the behavior of the main program - for example, Firefox plugins can change the behavior of browser tabs, their appearance, add / remove buttons, etc. d. These kinds of things are much easier to achieve in a dynamic language (hence why most of Firefox is implemented in javascript), and if this is the setting you want, you might be better off with your option (2) and write a lot of your interface in a scripting language ...

+3
source share

dlopen() / dlsym() is probably the easiest way. Some silly psuedo code:

 int run_module(const char *path, char **args) { void *module; void (*initfunc)(char **agrs); int rc = 0; module = dlopen(path, RTLD_NOW); if (module == NULL) err_out("Could not open module %s", path); initfunc = dlsym(module, "module_init"); if (initfunc == NULL) { dlclose(module); err_out("Could not find symbol init_func in %s", path); } rc = initfunc(args); dlclose(module); return rc; } 

Of course, you would like much more in the way of error checking, as well as code that really did something useful :). However, itโ€™s very easy and convenient to write a plugin architecture around a couple and publish a simple specification for others to do the same.

You probably need something more along the lines of load_module() , loads SO above everything, looks for the entry point and blocks until this entry point exits.

This is not to say that writing your own scripting language is a bad idea. People can write complex filters, respondents, etc., without experiencing any particular problems. Perhaps both of them will be a good idea. I do not know if you need a full LUA interpreter. Perhaps you could come up with something that would simplify regular expression-based actions.

However, plug-ins will not only simplify your life, but also help you develop a community of people who develop material around what you do.

+3
source share

There are many existing C programs that use dlopen() / dlsym() to implement the plugin architecture (including more than one associated with IRC); so yes, it definitely depends on the task.

+1
source share

All Articles