Defining a caller when working with dlopen () objects

I am writing a program (C) that uses a plugin system through dlopen (). The stumbling block that I am facing is that the main program exports several functions that the plugin that called them really needs to know (mainly for writing, and therefore the plugin can be unloaded properly as they add such things, as pointers to functions to the main program).

I don't seem to know how to do this. The options that I have come up with so far are:

  • it requires the plugin to provide its name, or some data that I give it when I load it as an argument to the functions.
    • I do not like this parameter because not all functions care about who they were called with, so it makes it inconsistent and messy. Plus, I would like to make it as difficult as possible so that the plugin lied about who it is.
  • Use backtrace () to determine the object name of the previous function.
    • It just seems pretty ugly and not portable.
  • Require the plugin to place the file level structure (or another variable) containing its name (lets call it “plugin_info” for discussion). Then use dlsym () when loading the plugin to view the variable and index it (for example, in a hash) by its name. Then add the #define macros that plugins use to call functions, and add the macro &plugin_infoas an argument.
    • This is what I'm using right now, but it seems like hacks. For one, you must have a macro pass "& plugin_info", if you just pass "plugin_info", then it pulls "plugin_info" from the main program, not from the plugin. Linking to it at the address makes it so that it is compiled with the correct one, and that it does not move. This makes me dislike this option, as it looks like its undefined behavior, however it does work. In addition, macros can make it somewhat confusing when the plugin developer has problems with calling the function (skipping the wrong argument type or something else).

If there are any other ideas or methods, I would love to know.

+3
source share
1 answer
  • , , , , rm -rf ~, . , .
  • , .
  • , , , . static plugin_info , .
+2

Source: https://habr.com/ru/post/1787111/


All Articles