summaryrefslogtreecommitdiffstats
path: root/pcilib/plugin.c
blob: 141016c1dd28619bd4933a142132030bb13d683f (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
112
113
114
115
116
117
118
119
#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 ((PCILIB_VERSION_GET_MAJOR(model_info->interface_version) != PCILIB_VERSION_MAJOR)||(PCILIB_VERSION_GET_MINOR(model_info->interface_version) != PCILIB_VERSION_MINOR)) {
	printf("%u %u\n", model_info->interface_version, PCILIB_EVENT_INTERFACE_VERSION);
	pcilib_warning("Plugin %s exposes outdated interface version (%u.%u.%u), pcilib interface version is (%u.%u.%u)", model_info->name, 
		PCILIB_VERSION_GET_MAJOR(model_info->interface_version),
		PCILIB_VERSION_GET_MINOR(model_info->interface_version),
		PCILIB_VERSION_GET_MICRO(model_info->interface_version),
		PCILIB_VERSION_GET_MAJOR(PCILIB_EVENT_INTERFACE_VERSION),
		PCILIB_VERSION_GET_MINOR(PCILIB_EVENT_INTERFACE_VERSION),
		PCILIB_VERSION_GET_MICRO(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;
}