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);
80 split_plugin_path (char *plugin_path)
84 u8 *path = (u8 *) plugin_path;
87 for (i = 0; i < vec_len (plugin_path); i++)
91 vec_add1 (this, path[i]);
107 vat2_load_plugins (char *path, char *filter, int *loaded)
110 struct dirent *entry;
117 uword *plugin_by_name_hash = hash_create_string (0, sizeof (uword));
120 plugin_path = split_plugin_path (path);
122 for (i = 0; i < vec_len (plugin_path); i++)
124 DBG ("Opening path: %s\n", plugin_path[i]);
125 dp = opendir ((char *) plugin_path[i]);
130 while ((entry = readdir (dp)))
137 for (j = 0; j < vec_len (filter); j++)
138 if (entry->d_name[j] != filter[j])
142 plugin_name = format (0, "%s/%s%c", plugin_path[i],
146 if (stat ((char *) plugin_name, &statb) < 0)
149 vec_free (plugin_name);
153 /* a dir or other things which aren't plugins */
154 if (!S_ISREG (statb.st_mode))
157 p = hash_get_mem (plugin_by_name_hash, plugin_name);
160 vec_add2 (plugin_info, pi, 1);
161 pi->name = plugin_name;
162 pi->file_info = statb;
164 if (load_one_plugin (pi))
167 vec_free (plugin_name);
168 vec_set_len (plugin_info, vec_len (plugin_info) - 1);
171 clib_memset (pi, 0, sizeof (*pi));
172 hash_set_mem (plugin_by_name_hash, plugin_name,
174 *loaded = *loaded + 1;
180 vec_free (plugin_path[i]);
182 vec_free (plugin_path);
187 #define QUOTE(x) QUOTE_(x)
190 * fd.io coding-style-patch-verification: ON
193 * eval: (c-set-style "gnu")