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