comparison checker.cpp @ 0:a3c75a438ff7

Initial commit with basic program
author Chris Cannam
date Wed, 06 Apr 2016 16:10:11 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a3c75a438ff7
1
2 /**
3 * Vamp Plugin Load Checker
4 *
5 * This program reads a list of Vamp plugin library paths from stdin,
6 * one per line. For each path read, it attempts to load that library
7 * and retrieve its descriptor function, printing a line to stdout
8 * reporting whether this was successful or not and then flushing
9 * stdout. The output line format is described below. The program
10 * exits with code 0 if all libraries were loaded successfully and
11 * non-zero otherwise.
12 *
13 * Note that library paths must be ready to pass to dlopen() or
14 * equivalent; this usually means they should be absolute paths.
15 *
16 * Output line for successful load of library libname.so:
17 * SUCCESS|/path/to/libname.so|
18 *
19 * Output line for failed load of library libname.so:
20 * FAILURE|/path/to/libname.so|Reason for failure if available
21 *
22 * Note that sometimes plugins will crash completely on load, bringing
23 * down this program with them. If the program exits before all listed
24 * plugins have been checked, this means that the plugin following the
25 * last reported one has crashed. Typically the caller may want to run
26 * it again, omitting that plugin.
27 */
28
29 #ifdef _WIN32
30 #include <windows.h>
31 #include <process.h>
32 #define DLOPEN(a,b) LoadLibrary((a).toStdWString().c_str())
33 #define DLSYM(a,b) GetProcAddress((HINSTANCE)(a),(b))
34 #define DLCLOSE(a) (!FreeLibrary((HINSTANCE)(a)))
35 #define DLERROR() ""
36 #else
37 #include <dlfcn.h>
38 #define DLOPEN(a,b) dlopen((a).c_str(),(b))
39 #define DLSYM(a,b) dlsym((a),(b).c_str())
40 #define DLCLOSE(a) dlclose((a))
41 #define DLERROR() dlerror()
42 #endif
43
44 #include <string>
45 #include <iostream>
46
47 using namespace std;
48
49 string error()
50 {
51 string e = dlerror();
52 if (e == "") return "(unknown error)";
53 else return e;
54 }
55
56 string check(string soname)
57 {
58 void *handle = DLOPEN(soname, RTLD_NOW | RTLD_LOCAL);
59 if (!handle) {
60 return "Unable to open plugin library: " + error();
61 }
62
63 string descriptor = "vampGetPluginDescriptor"; //!!! todo: make an arg
64 void *fn = DLSYM(handle, descriptor);
65 if (!fn) {
66 return "Failed to find plugin descriptor " + descriptor +
67 " in library: " + error();
68 }
69
70 return "";
71 }
72
73 int main(int argc, char **argv)
74 {
75 bool allGood = true;
76 string soname;
77
78 while (getline(cin, soname)) {
79 string report = check(soname);
80 if (report != "") {
81 cout << "FAILURE|" << soname << "|" << report << endl;
82 allGood = false;
83 } else {
84 cout << "SUCCESS|" << soname << "|" << endl;
85 }
86 }
87
88 return allGood ? 0 : 1;
89 }