Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file plugins.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief Manipulate with the type and extension plugins. |
| 5 | * |
| 6 | * Copyright (c) 2021 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
Radek Krejci | 968d755 | 2021-03-26 20:33:51 +0100 | [diff] [blame] | 15 | #define _GNU_SOURCE |
| 16 | |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 17 | #include "plugins.h" |
| 18 | #include "plugins_internal.h" |
| 19 | |
| 20 | #include <assert.h> |
Radek Krejci | 968d755 | 2021-03-26 20:33:51 +0100 | [diff] [blame] | 21 | #include <dirent.h> |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 22 | #include <dlfcn.h> |
Radek Krejci | 968d755 | 2021-03-26 20:33:51 +0100 | [diff] [blame] | 23 | #include <errno.h> |
| 24 | #include <limits.h> |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 25 | #include <pthread.h> |
Radek Krejci | bf940f9 | 2021-03-24 21:04:13 +0100 | [diff] [blame] | 26 | #include <stddef.h> |
Radek Krejci | 0aa1f70 | 2021-04-01 16:16:19 +0200 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
| 32 | #include "common.h" |
Radek Krejci | 12a28c7 | 2021-04-06 17:23:37 +0200 | [diff] [blame] | 33 | #include "config.h" |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 34 | #include "plugins_exts.h" |
| 35 | #include "plugins_types.h" |
Radek Krejci | 0aa1f70 | 2021-04-01 16:16:19 +0200 | [diff] [blame] | 36 | #include "set.h" |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * internal type plugins records |
| 40 | */ |
| 41 | extern const struct lyplg_type_record plugins_binary[]; |
| 42 | extern const struct lyplg_type_record plugins_bits[]; |
| 43 | extern const struct lyplg_type_record plugins_boolean[]; |
| 44 | extern const struct lyplg_type_record plugins_decimal64[]; |
| 45 | extern const struct lyplg_type_record plugins_empty[]; |
| 46 | extern const struct lyplg_type_record plugins_enumeration[]; |
| 47 | extern const struct lyplg_type_record plugins_identityref[]; |
| 48 | extern const struct lyplg_type_record plugins_instanceid[]; |
| 49 | extern const struct lyplg_type_record plugins_integer[]; |
| 50 | extern const struct lyplg_type_record plugins_leafref[]; |
| 51 | extern const struct lyplg_type_record plugins_string[]; |
| 52 | extern const struct lyplg_type_record plugins_union[]; |
| 53 | |
Michal Vasko | de4a341 | 2021-04-14 15:38:27 +0200 | [diff] [blame] | 54 | /* |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 55 | * yang |
| 56 | */ |
| 57 | extern const struct lyplg_type_record plugins_instanceid_keys[]; |
| 58 | |
| 59 | /* |
Michal Vasko | de4a341 | 2021-04-14 15:38:27 +0200 | [diff] [blame] | 60 | * ietf-inet-types |
| 61 | */ |
Michal Vasko | 3159e78 | 2021-05-03 15:12:35 +0200 | [diff] [blame] | 62 | extern const struct lyplg_type_record plugins_ipv4_address[]; |
Michal Vasko | 82bf15e | 2021-05-06 16:01:56 +0200 | [diff] [blame] | 63 | extern const struct lyplg_type_record plugins_ipv4_address_no_zone[]; |
Michal Vasko | 7caa3e6 | 2021-05-03 14:59:25 +0200 | [diff] [blame] | 64 | extern const struct lyplg_type_record plugins_ipv6_address[]; |
Michal Vasko | 18a4a73 | 2021-05-06 16:21:44 +0200 | [diff] [blame] | 65 | extern const struct lyplg_type_record plugins_ipv6_address_no_zone[]; |
Michal Vasko | 15dc9fa | 2021-05-03 14:33:05 +0200 | [diff] [blame] | 66 | extern const struct lyplg_type_record plugins_ipv4_prefix[]; |
| 67 | extern const struct lyplg_type_record plugins_ipv6_prefix[]; |
Michal Vasko | 3e52de5 | 2021-04-13 13:45:55 +0200 | [diff] [blame] | 68 | |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 69 | /* |
Michal Vasko | de4a341 | 2021-04-14 15:38:27 +0200 | [diff] [blame] | 70 | * ietf-yang-types |
| 71 | */ |
| 72 | extern const struct lyplg_type_record plugins_date_and_time[]; |
Michal Vasko | de4a341 | 2021-04-14 15:38:27 +0200 | [diff] [blame] | 73 | extern const struct lyplg_type_record plugins_xpath10[]; |
| 74 | |
| 75 | /* |
Michal Vasko | 7a53c7d | 2021-08-06 11:28:57 +0200 | [diff] [blame] | 76 | * ietf-netconf-acm |
| 77 | */ |
| 78 | extern const struct lyplg_type_record plugins_node_instanceid[]; |
| 79 | |
| 80 | /* |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 81 | * internal extension plugins records |
| 82 | */ |
| 83 | extern struct lyplg_ext_record plugins_metadata[]; |
| 84 | extern struct lyplg_ext_record plugins_nacm[]; |
| 85 | extern struct lyplg_ext_record plugins_yangdata[]; |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 86 | extern struct lyplg_ext_record plugins_schema_mount[]; |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 87 | |
| 88 | static pthread_mutex_t plugins_guard = PTHREAD_MUTEX_INITIALIZER; |
| 89 | |
| 90 | /** |
| 91 | * @brief Counter for currently present contexts able to refer to the loaded plugins. |
| 92 | * |
| 93 | * Plugins are shared among all the created contexts. They are loaded with the creation of the very first context and |
| 94 | * unloaded with the destroy of the last context. Therefore, to reload the list of plugins, all the contexts must be |
| 95 | * destroyed and with the creation of a first new context after that, the plugins will be reloaded. |
| 96 | */ |
| 97 | static uint32_t context_refcount = 0; |
| 98 | |
| 99 | /** |
| 100 | * @brief Record describing an implemented extension. |
| 101 | * |
| 102 | * Matches ::lyplg_ext_record and ::lyplg_type_record |
| 103 | */ |
| 104 | struct lyplg_record { |
| 105 | const char *module; /**< name of the module where the extension/type is defined */ |
| 106 | const char *revision; /**< optional module revision - if not specified, the plugin applies to any revision, |
| 107 | which is not an optimal approach due to a possible future revisions of the module. |
| 108 | Instead, there should be defined multiple items in the plugins list, each with the |
| 109 | different revision, but all with the same pointer to the plugin functions. The |
| 110 | only valid use case for the NULL revision is the case the module has no revision. */ |
| 111 | const char *name; /**< name of the extension/typedef */ |
| 112 | int8_t plugin[]; /**< specific plugin type's data - ::lyplg_ext or ::lyplg_type */ |
| 113 | }; |
| 114 | |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 115 | #ifndef STATIC |
Radek Krejci | bf940f9 | 2021-03-24 21:04:13 +0100 | [diff] [blame] | 116 | static struct ly_set plugins_handlers = {0}; |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 117 | #endif |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 118 | static struct ly_set plugins_types = {0}; |
| 119 | static struct ly_set plugins_extensions = {0}; |
| 120 | |
| 121 | /** |
| 122 | * @brief Iterate over list of loaded plugins of the given @p type. |
| 123 | * |
| 124 | * @param[in] type Type of the plugins to iterate. |
| 125 | * @param[in,out] index The iterator - set to 0 for the first call. |
| 126 | * @return The plugin records, NULL if no more record is available. |
| 127 | */ |
| 128 | static struct lyplg_record * |
| 129 | plugins_iter(enum LYPLG type, uint32_t *index) |
| 130 | { |
| 131 | struct ly_set *plugins; |
| 132 | |
| 133 | assert(index); |
| 134 | |
| 135 | if (type == LYPLG_EXTENSION) { |
| 136 | plugins = &plugins_extensions; |
| 137 | } else { |
| 138 | plugins = &plugins_types; |
| 139 | } |
| 140 | |
| 141 | if (*index == plugins->count) { |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | *index += 1; |
| 146 | return plugins->objs[*index - 1]; |
| 147 | } |
| 148 | |
| 149 | void * |
| 150 | lyplg_find(enum LYPLG type, const char *module, const char *revision, const char *name) |
| 151 | { |
| 152 | uint32_t i = 0; |
| 153 | struct lyplg_record *item; |
| 154 | |
| 155 | assert(module); |
| 156 | assert(name); |
| 157 | |
| 158 | while ((item = plugins_iter(type, &i)) != NULL) { |
| 159 | if (!strcmp(item->module, module) && !strcmp(item->name, name)) { |
| 160 | if (item->revision && revision && strcmp(item->revision, revision)) { |
| 161 | continue; |
| 162 | } else if (!revision && item->revision) { |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | return &item->plugin; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @brief Insert the provided extension plugin records into the internal set of extension plugins for use by libyang. |
| 175 | * |
| 176 | * @param[in] recs An array of plugin records provided by the plugin implementation. The array must be terminated by a zeroed |
| 177 | * record. |
| 178 | * @return LY_SUCCESS in case of success |
| 179 | * @return LY_EINVAL for invalid information in @p recs. |
| 180 | * @return LY_EMEM in case of memory allocation failure. |
| 181 | */ |
| 182 | static LY_ERR |
| 183 | plugins_insert(enum LYPLG type, const void *recs) |
| 184 | { |
| 185 | if (!recs) { |
| 186 | return LY_SUCCESS; |
| 187 | } |
| 188 | |
| 189 | if (type == LYPLG_EXTENSION) { |
| 190 | const struct lyplg_ext_record *rec = (const struct lyplg_ext_record *)recs; |
| 191 | |
| 192 | for (uint32_t i = 0; rec[i].name; i++) { |
| 193 | LY_CHECK_RET(ly_set_add(&plugins_extensions, (void *)&rec[i], 0, NULL)); |
| 194 | } |
Radek Krejci | bf940f9 | 2021-03-24 21:04:13 +0100 | [diff] [blame] | 195 | } else { /* LYPLG_TYPE */ |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 196 | const struct lyplg_type_record *rec = (const struct lyplg_type_record *)recs; |
| 197 | |
| 198 | for (uint32_t i = 0; rec[i].name; i++) { |
| 199 | LY_CHECK_RET(ly_set_add(&plugins_types, (void *)&rec[i], 0, NULL)); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return LY_SUCCESS; |
| 204 | } |
| 205 | |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 206 | #ifndef STATIC |
| 207 | |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 208 | static void |
Michal Vasko | e43a34e | 2021-04-13 13:43:04 +0200 | [diff] [blame] | 209 | lyplg_close_cb(void *handle) |
| 210 | { |
| 211 | dlclose(handle); |
| 212 | } |
| 213 | |
| 214 | static void |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 215 | lyplg_clean_(void) |
| 216 | { |
| 217 | if (--context_refcount) { |
| 218 | /* there is still some other context, do not remove the plugins */ |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | ly_set_erase(&plugins_types, NULL); |
| 223 | ly_set_erase(&plugins_extensions, NULL); |
Michal Vasko | e43a34e | 2021-04-13 13:43:04 +0200 | [diff] [blame] | 224 | ly_set_erase(&plugins_handlers, lyplg_close_cb); |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 227 | #endif |
| 228 | |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 229 | void |
| 230 | lyplg_clean(void) |
| 231 | { |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 232 | #ifndef STATIC |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 233 | pthread_mutex_lock(&plugins_guard); |
| 234 | lyplg_clean_(); |
| 235 | pthread_mutex_unlock(&plugins_guard); |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 236 | #endif |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 239 | #ifndef STATIC |
| 240 | |
| 241 | /** |
| 242 | * @brief Just a variadic data to cover extension and type plugins by a single ::plugins_load() function. |
| 243 | * |
| 244 | * The values are taken from ::LY_PLUGINS_EXTENSIONS and ::LYPLG_TYPES macros. |
| 245 | */ |
| 246 | static const struct { |
| 247 | const char *id; /**< string identifier: type/extension */ |
| 248 | const char *apiver_var; /**< expected variable name holding API version value */ |
| 249 | const char *plugins_var; /**< expected variable name holding plugin records */ |
| 250 | const char *envdir; /**< environment variable containing directory with the plugins */ |
| 251 | const char *dir; /**< default directory with the plugins (has less priority than envdir) */ |
| 252 | uint32_t apiver; /**< expected API version */ |
| 253 | } plugins_load_info[] = { |
| 254 | { /* LYPLG_TYPE */ |
| 255 | .id = "type", |
| 256 | .apiver_var = "plugins_types_apiver__", |
| 257 | .plugins_var = "plugins_types__", |
| 258 | .envdir = "LIBYANG_TYPES_PLUGINS_DIR", |
| 259 | .dir = LYPLG_TYPE_DIR, |
| 260 | .apiver = LYPLG_TYPE_API_VERSION |
| 261 | }, {/* LYPLG_EXTENSION */ |
| 262 | .id = "extension", |
| 263 | .apiver_var = "plugins_extensions_apiver__", |
| 264 | .plugins_var = "plugins_extensions__", |
| 265 | .envdir = "LIBYANG_EXTENSIONS_PLUGINS_DIR", |
| 266 | .dir = LYPLG_EXT_DIR, |
| 267 | .apiver = LYPLG_EXT_API_VERSION |
| 268 | } |
| 269 | }; |
| 270 | |
Radek Krejci | bf940f9 | 2021-03-24 21:04:13 +0100 | [diff] [blame] | 271 | /** |
| 272 | * @brief Get the expected plugin objects from the loaded dynamic object and add the defined plugins into the lists of |
| 273 | * available extensions/types plugins. |
| 274 | * |
| 275 | * @param[in] dlhandler Loaded dynamic library handler. |
| 276 | * @param[in] pathname Path of the loaded library for logging. |
| 277 | * @param[in] type Type of the plugins to get from the dynamic library. Note that a single library can hold both types |
| 278 | * and extensions plugins implementations, so this function should be called twice (once for each plugin type) with |
| 279 | * different @p type values |
| 280 | * @return LY_ERR values. |
| 281 | */ |
| 282 | static LY_ERR |
| 283 | plugins_load(void *dlhandler, const char *pathname, enum LYPLG type) |
| 284 | { |
| 285 | const void *plugins; |
| 286 | uint32_t *version; |
| 287 | |
| 288 | /* type plugin */ |
| 289 | version = dlsym(dlhandler, plugins_load_info[type].apiver_var); |
| 290 | if (version) { |
| 291 | /* check version ... */ |
| 292 | if (*version != plugins_load_info[type].apiver) { |
| 293 | LOGERR(NULL, LY_EINVAL, "Processing user %s plugin \"%s\" failed, wrong API version - %d expected, %d found.", |
| 294 | plugins_load_info[type].id, pathname, plugins_load_info[type].apiver, *version); |
| 295 | return LY_EINVAL; |
| 296 | } |
| 297 | |
| 298 | /* ... get types plugins information ... */ |
| 299 | if (!(plugins = dlsym(dlhandler, plugins_load_info[type].plugins_var))) { |
| 300 | char *errstr = dlerror(); |
| 301 | LOGERR(NULL, LY_EINVAL, "Processing user %s plugin \"%s\" failed, missing %s plugins information (%s).", |
| 302 | plugins_load_info[type].id, pathname, plugins_load_info[type].id, errstr); |
| 303 | return LY_EINVAL; |
| 304 | } |
| 305 | |
| 306 | /* ... and load all the types plugins */ |
| 307 | LY_CHECK_RET(plugins_insert(type, plugins)); |
| 308 | } |
| 309 | |
| 310 | return LY_SUCCESS; |
| 311 | } |
| 312 | |
| 313 | static LY_ERR |
| 314 | plugins_load_module(const char *pathname) |
| 315 | { |
| 316 | LY_ERR ret = LY_SUCCESS; |
| 317 | void *dlhandler; |
| 318 | uint32_t types_count = 0, extensions_count = 0; |
| 319 | |
| 320 | dlerror(); /* Clear any existing error */ |
| 321 | |
| 322 | dlhandler = dlopen(pathname, RTLD_NOW); |
| 323 | if (!dlhandler) { |
| 324 | LOGERR(NULL, LY_ESYS, "Loading \"%s\" as a plugin failed (%s).", pathname, dlerror()); |
| 325 | return LY_ESYS; |
| 326 | } |
| 327 | |
| 328 | if (ly_set_contains(&plugins_handlers, dlhandler, NULL)) { |
| 329 | /* the plugin is already loaded */ |
| 330 | LOGVRB("Plugin \"%s\" already loaded.", pathname); |
| 331 | |
| 332 | /* keep the correct refcount */ |
| 333 | dlclose(dlhandler); |
| 334 | return LY_SUCCESS; |
| 335 | } |
| 336 | |
| 337 | /* remember the current plugins lists for recovery */ |
| 338 | types_count = plugins_types.count; |
| 339 | extensions_count = plugins_extensions.count; |
| 340 | |
| 341 | /* type plugin */ |
| 342 | ret = plugins_load(dlhandler, pathname, LYPLG_TYPE); |
| 343 | LY_CHECK_GOTO(ret, error); |
| 344 | |
| 345 | /* extension plugin */ |
| 346 | ret = plugins_load(dlhandler, pathname, LYPLG_EXTENSION); |
| 347 | LY_CHECK_GOTO(ret, error); |
| 348 | |
| 349 | /* remember the dynamic plugin */ |
| 350 | ret = ly_set_add(&plugins_handlers, dlhandler, 1, NULL); |
| 351 | LY_CHECK_GOTO(ret, error); |
| 352 | |
| 353 | return LY_SUCCESS; |
| 354 | |
| 355 | error: |
| 356 | dlclose(dlhandler); |
| 357 | |
| 358 | /* revert changes in the lists */ |
| 359 | while (plugins_types.count > types_count) { |
| 360 | ly_set_rm_index(&plugins_types, plugins_types.count - 1, NULL); |
| 361 | } |
| 362 | while (plugins_extensions.count > extensions_count) { |
| 363 | ly_set_rm_index(&plugins_extensions, plugins_extensions.count - 1, NULL); |
| 364 | } |
| 365 | |
| 366 | return ret; |
| 367 | } |
| 368 | |
Radek Krejci | 968d755 | 2021-03-26 20:33:51 +0100 | [diff] [blame] | 369 | static LY_ERR |
| 370 | plugins_insert_dir(enum LYPLG type) |
| 371 | { |
| 372 | LY_ERR ret = LY_SUCCESS; |
| 373 | const char *pluginsdir; |
| 374 | DIR *dir; |
| 375 | ly_bool default_dir = 0; |
| 376 | |
| 377 | /* try to get the plugins directory from environment variable */ |
| 378 | pluginsdir = getenv(plugins_load_info[type].envdir); |
| 379 | if (!pluginsdir) { |
| 380 | /* remember that we are going to a default dir and do not print warning if the directory doesn't exist */ |
| 381 | default_dir = 1; |
| 382 | pluginsdir = plugins_load_info[type].dir; |
| 383 | } |
| 384 | |
| 385 | dir = opendir(pluginsdir); |
| 386 | if (!dir) { |
| 387 | /* no directory (or no access to it), no extension plugins */ |
| 388 | if (!default_dir || (errno != ENOENT)) { |
| 389 | LOGWRN(NULL, "Failed to open libyang %s plugins directory \"%s\" (%s).", plugins_load_info[type].id, |
| 390 | pluginsdir, strerror(errno)); |
| 391 | } |
| 392 | } else { |
| 393 | struct dirent *file; |
| 394 | |
| 395 | while ((file = readdir(dir))) { |
| 396 | size_t len; |
| 397 | char pathname[PATH_MAX]; |
| 398 | |
| 399 | /* required format of the filename is *LYPLG_SUFFIX */ |
| 400 | len = strlen(file->d_name); |
| 401 | if ((len < LYPLG_SUFFIX_LEN + 1) || strcmp(&file->d_name[len - LYPLG_SUFFIX_LEN], LYPLG_SUFFIX)) { |
| 402 | continue; |
| 403 | } |
| 404 | |
| 405 | /* and construct the filepath */ |
| 406 | snprintf(pathname, PATH_MAX, "%s/%s", pluginsdir, file->d_name); |
| 407 | |
| 408 | ret = plugins_load_module(pathname); |
| 409 | if (ret) { |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | closedir(dir); |
| 414 | } |
| 415 | |
| 416 | return ret; |
| 417 | } |
| 418 | |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 419 | #endif |
| 420 | |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 421 | LY_ERR |
| 422 | lyplg_init(void) |
| 423 | { |
| 424 | LY_ERR ret; |
| 425 | |
| 426 | pthread_mutex_lock(&plugins_guard); |
| 427 | /* let only the first context to initiate plugins, but let others wait for finishing the initiation */ |
| 428 | if (context_refcount++) { |
| 429 | /* already initiated */ |
| 430 | pthread_mutex_unlock(&plugins_guard); |
| 431 | return LY_SUCCESS; |
| 432 | } |
| 433 | |
| 434 | /* internal types */ |
| 435 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_binary), error); |
| 436 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_bits), error); |
| 437 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_boolean), error); |
| 438 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_decimal64), error); |
| 439 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_empty), error); |
| 440 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_enumeration), error); |
| 441 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_identityref), error); |
| 442 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_instanceid), error); |
| 443 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_integer), error); |
| 444 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_leafref), error); |
| 445 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_string), error); |
| 446 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_union), error); |
| 447 | |
Michal Vasko | 79a7a87 | 2022-06-17 09:00:48 +0200 | [diff] [blame] | 448 | /* yang */ |
| 449 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_instanceid_keys), error); |
| 450 | |
Michal Vasko | de4a341 | 2021-04-14 15:38:27 +0200 | [diff] [blame] | 451 | /* ietf-inet-types */ |
Michal Vasko | 3159e78 | 2021-05-03 15:12:35 +0200 | [diff] [blame] | 452 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv4_address), error); |
Michal Vasko | 82bf15e | 2021-05-06 16:01:56 +0200 | [diff] [blame] | 453 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv4_address_no_zone), error); |
Michal Vasko | 7caa3e6 | 2021-05-03 14:59:25 +0200 | [diff] [blame] | 454 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv6_address), error); |
Michal Vasko | 18a4a73 | 2021-05-06 16:21:44 +0200 | [diff] [blame] | 455 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv6_address_no_zone), error); |
Michal Vasko | 15dc9fa | 2021-05-03 14:33:05 +0200 | [diff] [blame] | 456 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv4_prefix), error); |
| 457 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv6_prefix), error); |
Michal Vasko | 3e52de5 | 2021-04-13 13:45:55 +0200 | [diff] [blame] | 458 | |
Michal Vasko | de4a341 | 2021-04-14 15:38:27 +0200 | [diff] [blame] | 459 | /* ietf-yang-types */ |
| 460 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_date_and_time), error); |
Michal Vasko | de4a341 | 2021-04-14 15:38:27 +0200 | [diff] [blame] | 461 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_xpath10), error); |
| 462 | |
Michal Vasko | 7a53c7d | 2021-08-06 11:28:57 +0200 | [diff] [blame] | 463 | /* ietf-netconf-acm */ |
| 464 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_node_instanceid), error); |
| 465 | |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 466 | /* internal extensions */ |
| 467 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_EXTENSION, plugins_metadata), error); |
| 468 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_EXTENSION, plugins_nacm), error); |
| 469 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_EXTENSION, plugins_yangdata), error); |
tadeas-vintrlik | 2aa36b4 | 2021-11-03 13:07:34 +0100 | [diff] [blame] | 470 | LY_CHECK_GOTO(ret = plugins_insert(LYPLG_EXTENSION, plugins_schema_mount), error); |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 471 | |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 472 | #ifndef STATIC |
Radek Krejci | 968d755 | 2021-03-26 20:33:51 +0100 | [diff] [blame] | 473 | /* external types */ |
| 474 | LY_CHECK_GOTO(ret = plugins_insert_dir(LYPLG_TYPE), error); |
| 475 | |
| 476 | /* external extensions */ |
| 477 | LY_CHECK_GOTO(ret = plugins_insert_dir(LYPLG_EXTENSION), error); |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 478 | #endif |
Radek Krejci | 968d755 | 2021-03-26 20:33:51 +0100 | [diff] [blame] | 479 | |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 480 | /* initiation done, wake-up possibly waiting threads creating another contexts */ |
| 481 | pthread_mutex_unlock(&plugins_guard); |
| 482 | |
| 483 | return LY_SUCCESS; |
| 484 | |
| 485 | error: |
| 486 | /* initiation was not successful - cleanup (and let others to try) */ |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 487 | #ifndef STATIC |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 488 | lyplg_clean_(); |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 489 | #endif |
Radek Krejci | 3e6632f | 2021-03-22 22:08:21 +0100 | [diff] [blame] | 490 | pthread_mutex_unlock(&plugins_guard); |
| 491 | |
| 492 | if (ret == LY_EINVAL) { |
| 493 | /* all the plugins here are internal, invalid record actually means an internal libyang error */ |
| 494 | ret = LY_EINT; |
| 495 | } |
| 496 | return ret; |
| 497 | } |
Radek Krejci | bf940f9 | 2021-03-24 21:04:13 +0100 | [diff] [blame] | 498 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 499 | LIBYANG_API_DEF LY_ERR |
Radek Krejci | bf940f9 | 2021-03-24 21:04:13 +0100 | [diff] [blame] | 500 | lyplg_add(const char *pathname) |
| 501 | { |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 502 | #ifdef STATIC |
| 503 | (void)pathname; |
| 504 | |
| 505 | LOGERR(NULL, LY_EINVAL, "Plugins are not supported in statically built library."); |
| 506 | return LY_EINVAL; |
Jan Kundrát | 323c312 | 2021-12-14 11:44:57 +0100 | [diff] [blame] | 507 | #elif defined (_WIN32) |
| 508 | (void)pathname; |
| 509 | |
| 510 | LOGERR(NULL, LY_EINVAL, "Plugins are not (yet) supported on Windows."); |
| 511 | return LY_EINVAL; |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 512 | #else |
Radek Krejci | bf940f9 | 2021-03-24 21:04:13 +0100 | [diff] [blame] | 513 | LY_ERR ret = LY_SUCCESS; |
| 514 | |
| 515 | LY_CHECK_ARG_RET(NULL, pathname, LY_EINVAL); |
| 516 | |
| 517 | /* works only in case a context exists */ |
| 518 | pthread_mutex_lock(&plugins_guard); |
| 519 | if (!context_refcount) { |
| 520 | /* no context */ |
| 521 | pthread_mutex_unlock(&plugins_guard); |
| 522 | LOGERR(NULL, LY_EDENIED, "To add a plugin, at least one context must exists."); |
| 523 | return LY_EDENIED; |
| 524 | } |
| 525 | |
| 526 | ret = plugins_load_module(pathname); |
| 527 | |
| 528 | pthread_mutex_unlock(&plugins_guard); |
| 529 | |
| 530 | return ret; |
Michal Vasko | 9e2bc70 | 2021-06-09 11:43:36 +0200 | [diff] [blame] | 531 | #endif |
Radek Krejci | bf940f9 | 2021-03-24 21:04:13 +0100 | [diff] [blame] | 532 | } |