Initial commit of vpp code.
[vpp.git] / vpp-api-test / vat / plugin.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * plugin.c: plugin handling
17  */
18
19 #include <vat/vat.h>
20 #include <vat/plugin.h>
21 #include <dlfcn.h>
22 #include <dirent.h>
23
24 plugin_main_t vat_plugin_main;
25
26 static int 
27 load_one_plugin (plugin_main_t *pm, plugin_info_t *pi)
28 {
29   void *handle, *register_handle;
30   clib_error_t * (*fp)(vat_main_t *);
31   clib_error_t * error;
32   
33   handle = dlopen ((char *)pi->name, RTLD_LAZY);
34
35   /* 
36    * Note: this can happen if the plugin has an undefined symbol reference,
37    * so print a warning. Otherwise, the poor slob won't know what happened.
38    * Ask me how I know that...
39    */
40   if (handle == 0)
41     {
42       clib_warning ("%s", dlerror());
43       return -1;
44     }
45   
46   pi->handle = handle;
47
48   register_handle = dlsym (pi->handle, "vat_plugin_register");
49   if (register_handle == 0)
50     return 0;
51
52   fp = register_handle;
53
54   error = (*fp)(pm->vat_main);
55
56   if (error)
57     {
58       clib_error_report (error);
59       dlclose (handle);
60       return 1;
61     }
62
63   clib_warning ("Loaded plugin: %s", pi->name);
64
65   return 0;
66 }
67
68 static u8 **split_plugin_path (plugin_main_t *pm)
69 {
70   int i;
71   u8 **rv = 0;
72   u8 *path = pm->plugin_path;
73   u8 *this = 0;
74
75   for (i = 0; i < vec_len (pm->plugin_path); i++)
76     {
77       if (path[i] != ':')
78         {
79           vec_add1(this, path[i]);
80           continue;
81         }
82       vec_add1(this, 0);
83       vec_add1 (rv, this);
84       this = 0;
85     }
86   if (this)
87     {
88       vec_add1 (this, 0);
89       vec_add1 (rv, this);
90     }
91   return rv;
92 }
93
94 int vat_load_new_plugins (plugin_main_t *pm)
95 {
96   DIR *dp;
97   struct dirent *entry;
98   struct stat statb;
99   uword *p;
100   plugin_info_t *pi;
101   u8 **plugin_path;
102   int i;
103
104   plugin_path = split_plugin_path (pm);
105   
106   for (i = 0; i < vec_len (plugin_path); i++)
107     {
108       dp = opendir ((char *)plugin_path[i]);
109   
110       if (dp == 0)
111         continue;
112       
113       while ((entry = readdir (dp)))
114         {
115           u8 *plugin_name;
116           
117           if (pm->plugin_name_filter)
118             {
119               int j;
120               for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
121                 if (entry->d_name[j] != pm->plugin_name_filter[j])
122                   goto next;
123             }
124
125           plugin_name = format (0, "%s/%s%c", plugin_path[i],
126                                 entry->d_name, 0);
127           
128           /* unreadable */
129           if (stat ((char *)plugin_name, &statb) < 0)
130             {
131             ignore:
132               vec_free (plugin_name);
133               continue;
134             }
135           
136           /* a dir or other things which aren't plugins */
137           if (!S_ISREG(statb.st_mode))
138             goto ignore;
139           
140           p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
141           if (p == 0) 
142             {
143               vec_add2 (pm->plugin_info, pi, 1);
144               pi->name = plugin_name;
145               pi->file_info = statb;
146               
147               if (load_one_plugin (pm, pi))
148                 {
149                   vec_free (plugin_name);
150                   _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1;
151                   continue;
152                 }
153               memset (pi, 0, sizeof (*pi));
154               hash_set_mem (pm->plugin_by_name_hash, plugin_name, 
155                             pi - pm->plugin_info);
156             }
157         next:
158           ;
159         }
160       closedir (dp);
161       vec_free (plugin_path[i]);
162     }
163   vec_free (plugin_path);
164   return 0;
165 }
166
167 #define QUOTE_(x) #x
168 #define QUOTE(x) QUOTE_(x)
169
170 /* 
171  * Load plugins from /usr/lib/vpp_api_test_plugins by default
172  */
173 char *vat_plugin_path = "/usr/lib/vpp_api_test_plugins";
174                          
175 char *vat_plugin_name_filter = 0;
176
177 int vat_plugin_init (vat_main_t * vam)
178 {
179   plugin_main_t *pm = &vat_plugin_main;
180   
181   clib_warning ("plugin-path %s", vat_plugin_path);
182
183   pm->plugin_path = format (0, "%s%c", vat_plugin_path, 0);
184   if (vat_plugin_name_filter)
185     pm->plugin_name_filter = format (0, "%s%c", vat_plugin_name_filter, 0);
186
187   pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
188   pm->vat_main = vam;
189   
190   return vat_load_new_plugins (pm);
191 }