summaryrefslogtreecommitdiffstats
path: root/pcilib/plugin.c
blob: 12dbf1d45ce475bf6ebb078db9a7a6ee23d1911b (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>

#include "model.h"
#include "error.h"
#include "pci.h"
#include "config.h"

void *pcilib_plugin_load(const char *name) {
    void *plug;
    char *fullname;
    const char *path;

    path = getenv("PCILIB_PLUGIN_DIR");
    if (!path) path = PCILIB_PLUGIN_DIR;

    fullname = malloc(strlen(path) + strlen(name) + 2);
    if (!fullname) return NULL;

    sprintf(fullname, "%s/%s", path, name);

    plug = dlopen(fullname, RTLD_LAZY|RTLD_LOCAL);
    free(fullname);

    return plug;
}

void pcilib_plugin_close(void *plug) {
    if (plug) 
	dlclose(plug);

}

void *pcilib_plugin_get_symbol(void *plug, const char *symbol) {
    if ((!plug)||(!symbol)) return NULL;
    return dlsym(plug, symbol);
}

const pcilib_model_description_t *pcilib_get_plugin_model(pcilib_t *pcilib, void *plug, unsigned short vendor_id, unsigned short device_id, const char *model) {
    void *get_model;
    const pcilib_model_description_t *model_info;

    if (!plug) return NULL;

    get_model = dlsym(plug, "pcilib_get_event_model");
    if (!get_model) return NULL;

    model_info = ((const pcilib_model_description_t *(*)(pcilib_t *pcilib, unsigned short vendor_id, unsigned short device_id, const char *model))get_model)(pcilib, vendor_id, device_id, model);
    if (!model_info) return model_info;

    if (model_info->interface_version != PCILIB_EVENT_INTERFACE_VERSION) {
	pcilib_warning("Plugin %s exposes outdated interface version (%lu), pcitool supports (%lu)", model_info->name, model_info->interface_version, PCILIB_EVENT_INTERFACE_VERSION);
	return NULL;
    }

    return model_info;
}

const pcilib_model_description_t *pcilib_find_plugin_model(pcilib_t *pcilib, unsigned short vendor_id, unsigned short device_id, const char *model) {
    DIR *dir;
    const char *path;
    struct dirent *entry;

    void *plugin;
    const pcilib_model_description_t *model_info = NULL;


    path = getenv("PCILIB_PLUGIN_DIR");
    if (!path) path = PCILIB_PLUGIN_DIR;

    if (model) {
	plugin = pcilib_plugin_load(model);
	if (plugin) {
	    model_info = pcilib_get_plugin_model(pcilib, plugin, vendor_id, device_id, model);
	    if (model_info) {
		pcilib->event_plugin = plugin;
		return model_info;
	    }
	    pcilib_plugin_close(plugin);
	}
    }

    dir = opendir(path);
    if (!dir) return NULL;

    while ((entry = readdir(dir))) {
	const char *suffix = strstr(entry->d_name, ".so");
	if ((!suffix)||(strlen(suffix) != 3)) continue;

	if ((model)&&(!strcmp(entry->d_name, model))) 
	    continue;

	plugin = pcilib_plugin_load(entry->d_name);
	if (plugin) {
	    model_info = pcilib_get_plugin_model(pcilib, plugin, vendor_id, device_id, model);
	    if (model_info) {
		pcilib->event_plugin = plugin;
		break;
	    }
	    pcilib_plugin_close(plugin);
	}
    }

    closedir(dir);
    return model_info;
}