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