Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 26 | static int |
| 27 | load_one_plugin (plugin_main_t * pm, plugin_info_t * pi) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | { |
| 29 | void *handle, *register_handle; |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 30 | clib_error_t *(*fp) (vat_main_t *); |
| 31 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 32 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 33 | handle = dlopen ((char *) pi->name, RTLD_LAZY); |
| 34 | |
| 35 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 42 | clib_warning ("%s", dlerror ()); |
Dave Barach | 3464c86 | 2018-03-12 17:38:31 -0400 | [diff] [blame] | 43 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | } |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 45 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 46 | pi->handle = handle; |
| 47 | |
| 48 | register_handle = dlsym (pi->handle, "vat_plugin_register"); |
| 49 | if (register_handle == 0) |
Dave Barach | 3464c86 | 2018-03-12 17:38:31 -0400 | [diff] [blame] | 50 | { |
| 51 | clib_warning ("%s: symbol vat_plugin_register not found", pi->name); |
| 52 | dlclose (handle); |
| 53 | return 0; |
| 54 | } |
| 55 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 56 | |
| 57 | fp = register_handle; |
| 58 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 59 | error = (*fp) (pm->vat_main); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | |
| 61 | if (error) |
| 62 | { |
| 63 | clib_error_report (error); |
| 64 | dlclose (handle); |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | clib_warning ("Loaded plugin: %s", pi->name); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 73 | static u8 ** |
| 74 | split_plugin_path (plugin_main_t * pm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | { |
| 76 | int i; |
| 77 | u8 **rv = 0; |
| 78 | u8 *path = pm->plugin_path; |
| 79 | u8 *this = 0; |
| 80 | |
| 81 | for (i = 0; i < vec_len (pm->plugin_path); i++) |
| 82 | { |
| 83 | if (path[i] != ':') |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 84 | { |
| 85 | vec_add1 (this, path[i]); |
| 86 | continue; |
| 87 | } |
| 88 | vec_add1 (this, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | vec_add1 (rv, this); |
| 90 | this = 0; |
| 91 | } |
| 92 | if (this) |
| 93 | { |
| 94 | vec_add1 (this, 0); |
| 95 | vec_add1 (rv, this); |
| 96 | } |
| 97 | return rv; |
| 98 | } |
| 99 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 100 | int |
| 101 | vat_load_new_plugins (plugin_main_t * pm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | { |
| 103 | DIR *dp; |
| 104 | struct dirent *entry; |
| 105 | struct stat statb; |
| 106 | uword *p; |
| 107 | plugin_info_t *pi; |
| 108 | u8 **plugin_path; |
| 109 | int i; |
| 110 | |
| 111 | plugin_path = split_plugin_path (pm); |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 112 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 113 | for (i = 0; i < vec_len (plugin_path); i++) |
| 114 | { |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 115 | dp = opendir ((char *) plugin_path[i]); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 117 | if (dp == 0) |
| 118 | continue; |
| 119 | |
| 120 | while ((entry = readdir (dp))) |
| 121 | { |
| 122 | u8 *plugin_name; |
| 123 | |
| 124 | if (pm->plugin_name_filter) |
| 125 | { |
| 126 | int j; |
| 127 | for (j = 0; j < vec_len (pm->plugin_name_filter); j++) |
| 128 | if (entry->d_name[j] != pm->plugin_name_filter[j]) |
| 129 | goto next; |
| 130 | } |
| 131 | |
| 132 | plugin_name = format (0, "%s/%s%c", plugin_path[i], |
| 133 | entry->d_name, 0); |
| 134 | |
| 135 | /* unreadable */ |
| 136 | if (stat ((char *) plugin_name, &statb) < 0) |
| 137 | { |
| 138 | ignore: |
| 139 | vec_free (plugin_name); |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | /* a dir or other things which aren't plugins */ |
| 144 | if (!S_ISREG (statb.st_mode)) |
| 145 | goto ignore; |
| 146 | |
| 147 | p = hash_get_mem (pm->plugin_by_name_hash, plugin_name); |
| 148 | if (p == 0) |
| 149 | { |
| 150 | vec_add2 (pm->plugin_info, pi, 1); |
| 151 | pi->name = plugin_name; |
| 152 | pi->file_info = statb; |
| 153 | |
| 154 | if (load_one_plugin (pm, pi)) |
| 155 | { |
| 156 | vec_free (plugin_name); |
| 157 | _vec_len (pm->plugin_info) = vec_len (pm->plugin_info) - 1; |
| 158 | continue; |
| 159 | } |
| 160 | memset (pi, 0, sizeof (*pi)); |
| 161 | hash_set_mem (pm->plugin_by_name_hash, plugin_name, |
| 162 | pi - pm->plugin_info); |
| 163 | } |
| 164 | next: |
| 165 | ; |
| 166 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | closedir (dp); |
| 168 | vec_free (plugin_path[i]); |
| 169 | } |
| 170 | vec_free (plugin_path); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | #define QUOTE_(x) #x |
| 175 | #define QUOTE(x) QUOTE_(x) |
| 176 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 177 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | * Load plugins from /usr/lib/vpp_api_test_plugins by default |
| 179 | */ |
| 180 | char *vat_plugin_path = "/usr/lib/vpp_api_test_plugins"; |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 181 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 182 | char *vat_plugin_name_filter = 0; |
| 183 | |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 184 | int |
| 185 | vat_plugin_init (vat_main_t * vam) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 186 | { |
| 187 | plugin_main_t *pm = &vat_plugin_main; |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 188 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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); |
| 192 | |
| 193 | pm->plugin_by_name_hash = hash_create_string (0, sizeof (uword)); |
| 194 | pm->vat_main = vam; |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 195 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 196 | return vat_load_new_plugins (pm); |
| 197 | } |
Dave Barach | 72d7223 | 2016-08-04 10:15:08 -0400 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * fd.io coding-style-patch-verification: ON |
| 201 | * |
| 202 | * Local Variables: |
| 203 | * eval: (c-set-style "gnu") |
| 204 | * End: |
| 205 | */ |