linux-sysprog · intermediate · ~15 min
The set-difference primitive that every plugin loader runs.
Check a plugin against the symbols a loader requires, and count how many required symbols the plugin does not export — the validation a dlopen-based loader runs before calling into a module.
Implement int count_missing_symbols(const char *plugin_symbols[], int n_plug, const char *required[], int n_req) that returns how many required symbols are absent.
plugin_symbols, n_plug: the names of symbols the plugin actually exports.required, n_req: the names the loader needs.Returns the number of entries in required that do not appear in plugin_symbols (compared by string value).
plug = {"init","tick","shutdown"}, req = {"init","tick","shutdown"}
count_missing_symbols(plug, 3, req, 3) -> 0
plug = {"init"}, req = {"init","tick","shutdown"}
count_missing_symbols(plug, 1, req, 3) -> 2
count_missing_symbols(NULL, 0, req, 3) -> 3 (nothing exported)
count_missing_symbols(plug, 3, NULL, 0) -> 0 (nothing required)
required list returns 0.n_req (all required symbols missing).dlopen lets a program load a shared library at runtime and resolve named symbols. A defensive plugin loader checks every required symbol BEFORE running any code from the module.
plugin_symbols[n_plug] (symbols the plugin exports) and required[n_req] (symbols the loader needs).
The count of required symbols not present in plugin_symbols.
Compare symbol names with strcmp. Empty required -> 0; empty plugin -> n_req.
#include <stddef.h>
int count_missing_symbols(const char *plugin_symbols[], int n_plug, const char *required[], int n_req) { /* TODO */ (void)plugin_symbols; (void)n_plug; (void)required; (void)n_req; return 0; }
Returning the count present instead of missing.
Empty required (return 0). Empty plugin (return n_req).
O(n_req * n_plug).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.