File: plugin.c

package info (click to toggle)
cdebconf 0.227
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,552 kB
  • sloc: ansic: 16,568; sh: 549; makefile: 456; sql: 52; perl: 13
file content (140 lines) | stat: -rw-r--r-- 3,817 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "common.h"
#include "frontend.h"
#include "plugin.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#include <dlfcn.h>

/* Convert hyphens to underscores. Returns allocated string. */
static char *make_symbol_name(const char *name)
{
    char *symbol, *symbolp;

    symbol = strdup(name);
    for (symbolp = symbol; *symbolp; ++symbolp) {
        if (*symbolp == '-')
            *symbolp = '_';
    }
    return symbol;
}

struct plugin *plugin_new(const char *frontend, const char *filename)
{
    struct plugin *plugin;
    const char *base;
    size_t baselen, symbollen;
    char *typesymbol, *symbol;

    base = strrchr(filename, '/');
    if (base)
        ++base;
    else
        base = filename;

    baselen = strlen(base);
    /* base must be plugin-<type>.so */
    if (baselen < 11)
        return NULL;
    if (strncmp(base, "plugin-", 7) != 0)
        return NULL;
    if (strncmp(base + baselen - 3, ".so", 3) != 0)
        return NULL;

    plugin = NEW(struct plugin);

    plugin->name = malloc(baselen - 9);
    strncpy(plugin->name, base + 7, baselen - 10);
    plugin->name[baselen - 10] = '\0';
    typesymbol = make_symbol_name(plugin->name);

    plugin->module = dlopen(filename, RTLD_LAZY);
    if (plugin->module == NULL) {
        INFO(INFO_WARN, "Cannot load plugin module %s: %s",
             filename, dlerror());
        free(plugin->name);
        DELETE(plugin);
        return NULL;
    }

    symbollen = strlen(frontend) + 18 + strlen(plugin->name) + 1;
    symbol = malloc(symbollen);
    snprintf(symbol, symbollen, "cdebconf_%s_handler_%s", frontend,
             typesymbol);
    plugin->handler = dlsym(plugin->module, symbol);
    free(symbol);
    if (plugin->handler == NULL) {
        /* Let's try with the old style symbol name. */
        symbollen = strlen(frontend) + 9 + strlen(plugin->name) + 1;
        symbol = malloc(symbollen);
        snprintf(symbol, symbollen, "%s_handler_%s", frontend, typesymbol);
        plugin->handler = dlsym(plugin->module, symbol);
        free(symbol);
    }
    if (plugin->handler == NULL) {
        INFO(INFO_WARN, "Malformed plugin module %s", filename);
        plugin_delete(plugin);
        return NULL;
    }

    return plugin;
}

void plugin_delete(struct plugin *plugin)
{
    INFO(INFO_VERBOSE, "Unloading plugin module %s", plugin->name);
    dlclose(plugin->module);
    free(plugin->name);
    DELETE(plugin);
}

struct plugin *plugin_find(struct frontend *frontend, const char *name)
{
    char *filename;
    struct plugin *plugin;

    if (asprintf(&filename, "%s/plugin-%s.so",
                 frontend->plugin_path, name) == -1)
        DIE("Out of memory");
    INFO(INFO_VERBOSE, "Trying to load plugin from %s", filename);
    plugin = plugin_new(frontend->name, filename);
    free(filename);

    return plugin;
}

struct plugin *plugin_iterate(struct frontend *frontend, void **state)
{
    DIR *plugin_dir = *state;
    struct dirent *plugin_dirent;

    if (!plugin_dir) {
        *state = plugin_dir = opendir(frontend->plugin_path);
        if (!plugin_dir) {
            if (errno != ENOENT)
                INFO(INFO_WARN, "Cannot open plugin directory %s: %s",
                     frontend->plugin_path, strerror(errno));
            return NULL;
        }
    }

    while ((plugin_dirent = readdir(plugin_dir)) != NULL) {
        char *filename;
        struct plugin *plugin;

        if (asprintf(&filename, "%s/%s",
                     frontend->plugin_path, plugin_dirent->d_name) == -1)
            DIE("Out of memory");
        plugin = plugin_new(frontend->name, filename);
        free(filename);
        if (plugin)
            return plugin;
    }

    closedir(plugin_dir);
    return NULL;
}