blob: 794cfcf5ae60027ce874289972e402cbea4a75f3 [file] [log] [blame]
Radek Krejci3e6632f2021-03-22 22:08:21 +01001/**
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 Krejci968d7552021-03-26 20:33:51 +010015#define _GNU_SOURCE
16
Radek Krejci3e6632f2021-03-22 22:08:21 +010017#include "plugins.h"
18#include "plugins_internal.h"
19
20#include <assert.h>
Radek Krejci968d7552021-03-26 20:33:51 +010021#include <dirent.h>
Radek Krejci3e6632f2021-03-22 22:08:21 +010022#include <dlfcn.h>
Radek Krejci968d7552021-03-26 20:33:51 +010023#include <errno.h>
24#include <limits.h>
Radek Krejci3e6632f2021-03-22 22:08:21 +010025#include <pthread.h>
Radek Krejcibf940f92021-03-24 21:04:13 +010026#include <stddef.h>
Radek Krejci0aa1f702021-04-01 16:16:19 +020027#include <stdint.h>
28#include <stdio.h>
29#include <stdlib.h>
Radek Krejci3e6632f2021-03-22 22:08:21 +010030#include <string.h>
31
32#include "common.h"
Radek Krejci12a28c72021-04-06 17:23:37 +020033#include "config.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010034#include "plugins_exts.h"
35#include "plugins_types.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020036#include "set.h"
Radek Krejci3e6632f2021-03-22 22:08:21 +010037
38/*
39 * internal type plugins records
40 */
41extern const struct lyplg_type_record plugins_binary[];
42extern const struct lyplg_type_record plugins_bits[];
43extern const struct lyplg_type_record plugins_boolean[];
44extern const struct lyplg_type_record plugins_decimal64[];
45extern const struct lyplg_type_record plugins_empty[];
46extern const struct lyplg_type_record plugins_enumeration[];
47extern const struct lyplg_type_record plugins_identityref[];
48extern const struct lyplg_type_record plugins_instanceid[];
49extern const struct lyplg_type_record plugins_integer[];
50extern const struct lyplg_type_record plugins_leafref[];
51extern const struct lyplg_type_record plugins_string[];
52extern const struct lyplg_type_record plugins_union[];
53
Michal Vaskode4a3412021-04-14 15:38:27 +020054/*
Michal Vasko79a7a872022-06-17 09:00:48 +020055 * yang
56 */
57extern const struct lyplg_type_record plugins_instanceid_keys[];
58
59/*
Michal Vaskode4a3412021-04-14 15:38:27 +020060 * ietf-inet-types
61 */
Michal Vasko3159e782021-05-03 15:12:35 +020062extern const struct lyplg_type_record plugins_ipv4_address[];
Michal Vasko82bf15e2021-05-06 16:01:56 +020063extern const struct lyplg_type_record plugins_ipv4_address_no_zone[];
Michal Vasko7caa3e62021-05-03 14:59:25 +020064extern const struct lyplg_type_record plugins_ipv6_address[];
Michal Vasko18a4a732021-05-06 16:21:44 +020065extern const struct lyplg_type_record plugins_ipv6_address_no_zone[];
Michal Vasko15dc9fa2021-05-03 14:33:05 +020066extern const struct lyplg_type_record plugins_ipv4_prefix[];
67extern const struct lyplg_type_record plugins_ipv6_prefix[];
Michal Vasko3e52de52021-04-13 13:45:55 +020068
Radek Krejci3e6632f2021-03-22 22:08:21 +010069/*
Michal Vaskode4a3412021-04-14 15:38:27 +020070 * ietf-yang-types
71 */
72extern const struct lyplg_type_record plugins_date_and_time[];
Michal Vaskode4a3412021-04-14 15:38:27 +020073extern const struct lyplg_type_record plugins_xpath10[];
74
75/*
Michal Vasko7a53c7d2021-08-06 11:28:57 +020076 * ietf-netconf-acm
77 */
78extern const struct lyplg_type_record plugins_node_instanceid[];
79
80/*
Radek Krejci3e6632f2021-03-22 22:08:21 +010081 * internal extension plugins records
82 */
83extern struct lyplg_ext_record plugins_metadata[];
84extern struct lyplg_ext_record plugins_nacm[];
85extern struct lyplg_ext_record plugins_yangdata[];
tadeas-vintrlik2aa36b42021-11-03 13:07:34 +010086extern struct lyplg_ext_record plugins_schema_mount[];
Radek Krejci3e6632f2021-03-22 22:08:21 +010087
88static 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 */
97static 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 */
104struct 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 Vasko9e2bc702021-06-09 11:43:36 +0200115#ifndef STATIC
Radek Krejcibf940f92021-03-24 21:04:13 +0100116static struct ly_set plugins_handlers = {0};
Michal Vasko9e2bc702021-06-09 11:43:36 +0200117#endif
Radek Krejci3e6632f2021-03-22 22:08:21 +0100118static struct ly_set plugins_types = {0};
119static 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 */
128static struct lyplg_record *
129plugins_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
149void *
150lyplg_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 */
182static LY_ERR
183plugins_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 Krejcibf940f92021-03-24 21:04:13 +0100195 } else { /* LYPLG_TYPE */
Radek Krejci3e6632f2021-03-22 22:08:21 +0100196 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 Vasko9e2bc702021-06-09 11:43:36 +0200206#ifndef STATIC
207
Radek Krejci3e6632f2021-03-22 22:08:21 +0100208static void
Michal Vaskoe43a34e2021-04-13 13:43:04 +0200209lyplg_close_cb(void *handle)
210{
211 dlclose(handle);
212}
213
214static void
Radek Krejci3e6632f2021-03-22 22:08:21 +0100215lyplg_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 Vaskoe43a34e2021-04-13 13:43:04 +0200224 ly_set_erase(&plugins_handlers, lyplg_close_cb);
Radek Krejci3e6632f2021-03-22 22:08:21 +0100225}
226
Michal Vasko9e2bc702021-06-09 11:43:36 +0200227#endif
228
Radek Krejci3e6632f2021-03-22 22:08:21 +0100229void
230lyplg_clean(void)
231{
Michal Vasko9e2bc702021-06-09 11:43:36 +0200232#ifndef STATIC
Radek Krejci3e6632f2021-03-22 22:08:21 +0100233 pthread_mutex_lock(&plugins_guard);
234 lyplg_clean_();
235 pthread_mutex_unlock(&plugins_guard);
Michal Vasko9e2bc702021-06-09 11:43:36 +0200236#endif
Radek Krejci3e6632f2021-03-22 22:08:21 +0100237}
238
Michal Vasko9e2bc702021-06-09 11:43:36 +0200239#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 */
246static 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 Krejcibf940f92021-03-24 21:04:13 +0100271/**
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 */
282static LY_ERR
283plugins_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
313static LY_ERR
314plugins_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
355error:
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 Krejci968d7552021-03-26 20:33:51 +0100369static LY_ERR
370plugins_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 Vasko9e2bc702021-06-09 11:43:36 +0200419#endif
420
Radek Krejci3e6632f2021-03-22 22:08:21 +0100421LY_ERR
422lyplg_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 Vasko79a7a872022-06-17 09:00:48 +0200448 /* yang */
449 LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_instanceid_keys), error);
450
Michal Vaskode4a3412021-04-14 15:38:27 +0200451 /* ietf-inet-types */
Michal Vasko3159e782021-05-03 15:12:35 +0200452 LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv4_address), error);
Michal Vasko82bf15e2021-05-06 16:01:56 +0200453 LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv4_address_no_zone), error);
Michal Vasko7caa3e62021-05-03 14:59:25 +0200454 LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv6_address), error);
Michal Vasko18a4a732021-05-06 16:21:44 +0200455 LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_ipv6_address_no_zone), error);
Michal Vasko15dc9fa2021-05-03 14:33:05 +0200456 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 Vasko3e52de52021-04-13 13:45:55 +0200458
Michal Vaskode4a3412021-04-14 15:38:27 +0200459 /* ietf-yang-types */
460 LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_date_and_time), error);
Michal Vaskode4a3412021-04-14 15:38:27 +0200461 LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_xpath10), error);
462
Michal Vasko7a53c7d2021-08-06 11:28:57 +0200463 /* ietf-netconf-acm */
464 LY_CHECK_GOTO(ret = plugins_insert(LYPLG_TYPE, plugins_node_instanceid), error);
465
Radek Krejci3e6632f2021-03-22 22:08:21 +0100466 /* 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-vintrlik2aa36b42021-11-03 13:07:34 +0100470 LY_CHECK_GOTO(ret = plugins_insert(LYPLG_EXTENSION, plugins_schema_mount), error);
Radek Krejci3e6632f2021-03-22 22:08:21 +0100471
Michal Vasko9e2bc702021-06-09 11:43:36 +0200472#ifndef STATIC
Radek Krejci968d7552021-03-26 20:33:51 +0100473 /* 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 Vasko9e2bc702021-06-09 11:43:36 +0200478#endif
Radek Krejci968d7552021-03-26 20:33:51 +0100479
Radek Krejci3e6632f2021-03-22 22:08:21 +0100480 /* initiation done, wake-up possibly waiting threads creating another contexts */
481 pthread_mutex_unlock(&plugins_guard);
482
483 return LY_SUCCESS;
484
485error:
486 /* initiation was not successful - cleanup (and let others to try) */
Michal Vasko9e2bc702021-06-09 11:43:36 +0200487#ifndef STATIC
Radek Krejci3e6632f2021-03-22 22:08:21 +0100488 lyplg_clean_();
Michal Vasko9e2bc702021-06-09 11:43:36 +0200489#endif
Radek Krejci3e6632f2021-03-22 22:08:21 +0100490 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 Krejcibf940f92021-03-24 21:04:13 +0100498
Jan Kundrátc53a7ec2021-12-09 16:01:19 +0100499LIBYANG_API_DEF LY_ERR
Radek Krejcibf940f92021-03-24 21:04:13 +0100500lyplg_add(const char *pathname)
501{
Michal Vasko9e2bc702021-06-09 11:43:36 +0200502#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át323c3122021-12-14 11:44:57 +0100507#elif defined (_WIN32)
508 (void)pathname;
509
510 LOGERR(NULL, LY_EINVAL, "Plugins are not (yet) supported on Windows.");
511 return LY_EINVAL;
Michal Vasko9e2bc702021-06-09 11:43:36 +0200512#else
Radek Krejcibf940f92021-03-24 21:04:13 +0100513 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 Vasko9e2bc702021-06-09 11:43:36 +0200531#endif
Radek Krejcibf940f92021-03-24 21:04:13 +0100532}