2 * Copyright (c) 2020 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.
18 #include <sys/types.h>
21 #include <vlib/vlib.h>
28 struct stat file_info;
32 /* loaded plugin info */
33 plugin_info_t *plugin_info;
36 load_one_plugin (plugin_info_t * pi)
38 void *handle, *register_handle;
39 clib_error_t *(*fp) (void);
42 handle = dlopen ((char *) pi->name, RTLD_LAZY);
45 * Note: this can happen if the plugin has an undefined symbol reference,
46 * so print a warning. Otherwise, the poor slob won't know what happened.
47 * Ask me how I know that...
51 clib_warning ("%s", dlerror ());
57 register_handle = dlsym (pi->handle, "vat2_register_plugin");
58 if (register_handle == 0)
60 clib_warning ("%s: symbol vat2_register_plugin not found", pi->name);
71 clib_error_report (error);
79 /* Takes a vector as argument */
81 split_plugin_path (u8 *plugin_path)
85 u8 *path = (u8 *) plugin_path;
88 for (i = 0; i < vec_len (plugin_path); i++)
92 vec_add1 (this, path[i]);
108 vat2_load_plugins (u8 *path, char *filter, int *loaded)
111 struct dirent *entry;
118 uword *plugin_by_name_hash = hash_create_string (0, sizeof (uword));
121 plugin_path = split_plugin_path (path);
123 for (i = 0; i < vec_len (plugin_path); i++)
125 DBG ("Opening path: %s\n", plugin_path[i]);
126 dp = opendir ((char *) plugin_path[i]);
131 while ((entry = readdir (dp)))
138 for (j = 0; j < vec_len (filter); j++)
139 if (entry->d_name[j] != filter[j])
143 plugin_name = format (0, "%s/%s%c", plugin_path[i],
147 if (stat ((char *) plugin_name, &statb) < 0)
150 vec_free (plugin_name);
154 /* a dir or other things which aren't plugins */
155 if (!S_ISREG (statb.st_mode))
158 p = hash_get_mem (plugin_by_name_hash, plugin_name);
161 vec_add2 (plugin_info, pi, 1);
162 pi->name = plugin_name;
163 pi->file_info = statb;
165 if (load_one_plugin (pi))
168 vec_free (plugin_name);
169 vec_set_len (plugin_info, vec_len (plugin_info) - 1);
172 clib_memset (pi, 0, sizeof (*pi));
173 hash_set_mem (plugin_by_name_hash, plugin_name,
175 *loaded = *loaded + 1;
181 vec_free (plugin_path[i]);
183 vec_free (plugin_path);
188 #define QUOTE(x) QUOTE_(x)
191 * fd.io coding-style-patch-verification: ON
194 * eval: (c-set-style "gnu")