-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend_manager.cpp
More file actions
160 lines (144 loc) · 4.31 KB
/
backend_manager.cpp
File metadata and controls
160 lines (144 loc) · 4.31 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "backend_manager.hpp"
#include "plugin_types.h"
#include "configuration.hpp"
#include "logger.hpp"
#include <dirent.h>
#include <stdlib.h>
#include <error.h>
#include <errno.h>
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
#include <limits.h>
#include <list>
extern Log main_log;
static std::list<backend_t> plugin_list;
/* Returns a pointer to the entry that is currently used for the given extension,
* of NULL if no backend is yet registered.
*/
backend_t* extension_in_list ( std::list<backend_t*> list, const std::string &ext )
{
backend_t *best_so_far,dummy = {"",UINT_MAX,nullptr};
best_so_far = &dummy;
for ( auto current : list )
{
if ( current->str == ext || current->str == "de\fault" )
{
if ( current->priority <= best_so_far->priority )
{
best_so_far = current;
}
}
}
if ( best_so_far != &dummy )
return best_so_far;
else
return nullptr;
}
/* Scans the backend plugin directory and links to any plugins found there. This
* should be called from the main() function, prior to any calls being made to
* get_resource().
*/
int register_backend_plugins()
{
std::string backend_dir = get_config ( "backend_plugin_dir" );
main_log ( "Getting backend plugins from" + backend_dir, NOTICE );
DIR* backends;
struct dirent* next_file;
std::string filepath;
void* handle;
char *dl_status_ptr;;
std::string dl_status;
if ( ! ( backends=opendir ( backend_dir.c_str() ) ) )
{
int e_no = errno;
main_log ( "Failed to open backend plugin directory " + backend_dir + ". Exiting...", ERROR );
error ( EXIT_FAILURE,e_no,"Opening directory failed" );
}
errno = 0;
while ( ( next_file = readdir ( backends ) ) )
{
filepath = backend_dir + "/" + next_file->d_name;
main_log ( "Considering " + filepath + " as a plugin.", DEBUG );
if ( filepath.substr ( filepath.size()-3 ) != ".so" )
{
continue;
}
main_log << INFO << filepath << " seems to be a plugin. Linking...\n" ;
handle = dlopen ( filepath.c_str(),RTLD_NOW );
if ( !handle )
{
main_log ( std::string ( "Failed to link to plugin " ) + next_file->d_name + ": " + dlerror(),ERROR );
error ( EXIT_FAILURE,0,"Linking plugin failed" );
}
else
{
main_log << DEBUG << "Opened plugin " << filepath
<< " for linking.\n";
}
std::list<backend_t> ( *register_backend ) ();
// Clear the dlerror
dlerror();
main_log ( "Cleared the error buffer. Trying to link...\n",INFO );
register_backend = ( register_backend_fn ) ( void* ) dlsym ( handle,"register_backend" );
main_log ( "Link complete. Checking status...", DEBUG );
dl_status_ptr = dlerror();
if ( dl_status_ptr ) dl_status = dl_status_ptr;
main_log ( "Status is " + dl_status );
if ( dl_status.size() )
{
main_log ( std::string ( "Failed to get symbol for 'register_backend': " ) + dl_status, ERROR );
error ( EXIT_FAILURE,0,"Getting symbol failed." );
}
else
{
main_log ( "Got the 'register_backend' symbol.", DEBUG );
}
main_log ( "Attempting to register backends...", DEBUG );
std::list<backend_t> result_list = register_backend();
main_log ( "Got backend list.", DEBUG );
/* Attempt to add each file extension returned to the handler list. */
for ( auto candidate : result_list )
{
for ( auto current : plugin_list )
{
if ( candidate.str == current.str && candidate.priority < current.priority )
{
current = candidate;
main_log ( "Replaced handler with extension " + current.str + " to list.", DEBUG );
goto success;
}
}
plugin_list.push_back ( candidate );
main_log ( "Added handler with extension " + candidate.str + " to list.", DEBUG );
success:
;
}
main_log ( "Finished getting backends.",DEBUG );
}
closedir ( backends );
return 0;
}
std::string get_resource ( const std::string &path, struct client_t* client, int &error_code )
{
main_log ( "Attempting to get response from " + path, DEBUG );
std::string ext = strrchr ( path.c_str(),'.' ) + 1;
main_log ( "Using extension: " + ext,DEBUG );
if ( !ext.size() ) ext.erase ( 1 );
else ext = "";
backend_t handler;
for ( auto i : plugin_list )
{
if ( ext == i.str )
{
handler = i;
break;
}
else if ( "de\fault" == i.str )
{
handler = i;
}
}
main_log ( "Using handler with extension " + handler.str, DEBUG );
return handler.provider ( path,client,error_code );
}