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