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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 * plugin.c: plugin handling
20 #include <vat/plugin.h>
24 plugin_main_t vat_plugin_main;
27 load_one_plugin (plugin_main_t * pm, plugin_info_t * pi)
29 void *handle, *register_handle;
30 clib_error_t *(*fp) (vat_main_t *);
33 handle = dlopen ((char *) pi->name, RTLD_LAZY);
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...
42 clib_warning ("%s", dlerror ());
48 register_handle = dlsym (pi->handle, "vat_plugin_register");
49 if (register_handle == 0)
51 clib_warning ("%s: symbol vat_plugin_register not found", pi->name);
59 error = (*fp) (pm->vat_main);
63 clib_error_report (error);
68 clib_warning ("Loaded plugin: %s", pi->name);
74 split_plugin_path (plugin_main_t * pm)
78 u8 *path = pm->plugin_path;
81 for (i = 0; i < vec_len (pm->plugin_path); i++)
85 vec_add1 (this, path[i]);
101 vat_load_new_plugins (plugin_main_t * pm)
104 struct dirent *entry;
111 plugin_path = split_plugin_path (pm);
113 for (i = 0; i < vec_len (plugin_path); i++)
115 dp = opendir ((char *) plugin_path[i]);
120 while ((entry = readdir (dp)))
124 if (pm->plugin_name_filter)
127 for (j = 0; j < vec_len (pm->plugin_name_filter); j++)
128 if (entry->d_name[j] != pm->plugin_name_filter[j])
132 plugin_name = format (0, "%s/%s%c", plugin_path[i],
136 if (stat ((char *) plugin_name, &statb) < 0)
139 vec_free (plugin_name);
143 /* a dir or other things which aren't plugins */
144 if (!S_ISREG (statb.st_mode))
147 p = hash_get_mem (pm->plugin_by_name_hash, plugin_name);
150 vec_add2 (pm->plugin_info, pi, 1);
151 clib_memset (pi, 0, sizeof (*pi));
152 pi->name = plugin_name;
153 pi->file_info = statb;
155 if (load_one_plugin (pm, pi))
157 vec_free (plugin_name);
158 vec_set_len (pm->plugin_info, vec_len (pm->plugin_info) - 1);
161 hash_set_mem (pm->plugin_by_name_hash, plugin_name,
162 pi - pm->plugin_info);
168 vec_free (plugin_path[i]);
170 vec_free (plugin_path);
175 #define QUOTE(x) QUOTE_(x)
178 * Load plugins from /usr/lib/vpp_api_test_plugins by default
180 char *vat_plugin_path = "/usr/lib/vpp_api_test_plugins";
182 char *vat_plugin_name_filter = 0;
185 vat_plugin_init (vat_main_t * vam)
187 plugin_main_t *pm = &vat_plugin_main;
189 pm->plugin_path = format (0, "%s%c", vat_plugin_path, 0);
190 if (vat_plugin_name_filter)
191 pm->plugin_name_filter = format (0, "%s%c", vat_plugin_name_filter, 0);
193 pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword));
196 return vat_load_new_plugins (pm);
200 * fd.io coding-style-patch-verification: ON
203 * eval: (c-set-style "gnu")