blob: 5f44a40737ba317dc922d57a02bae490c0e63cdc [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file plugin.c
2 * Routines for loading and managing shared library plug-ins. */
3/*
4 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
Harald Welteb9ce51c2010-06-30 19:43:11 +02005 *
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Welteb9ce51c2010-06-30 19:43:11 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
Harald Welteb9ce51c2010-06-30 19:43:11 +020020 */
21
Harald Welte96e2a002017-06-12 21:44:18 +020022/*! \addtogroup utils
23 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020024 * \file plugin.c */
Harald Weltede6e4982012-12-06 21:25:27 +010025
Harald Welteb9ce51c2010-06-30 19:43:11 +020026#include "../config.h"
27
28#if HAVE_DLFCN_H
29
Harald Welteb9ce51c2010-06-30 19:43:11 +020030#include <dirent.h>
31#include <dlfcn.h>
32#include <stdio.h>
33#include <errno.h>
Holger Hans Peter Freytherbe0f7fa2010-08-31 17:14:04 +080034#include <limits.h>
Harald Welteb9ce51c2010-06-30 19:43:11 +020035
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010036#include <osmocom/core/plugin.h>
Harald Welteb9ce51c2010-06-30 19:43:11 +020037
Neels Hofmeyr87e45502017-06-20 00:17:59 +020038/*! Load all plugins available in given directory
Harald Weltede6e4982012-12-06 21:25:27 +010039 * \param[in] directory full path name of directory containing plug-ins
40 * \returns number of plugins loaded in case of success, negative in case of error
41 */
Pablo Neira Ayuso2c348672011-05-07 12:50:08 +020042int osmo_plugin_load_all(const char *directory)
Harald Welteb9ce51c2010-06-30 19:43:11 +020043{
44 unsigned int num = 0;
45 char fname[PATH_MAX];
46 DIR *dir;
47 struct dirent *entry;
48
49 dir = opendir(directory);
50 if (!dir)
51 return -errno;
52
53 while ((entry = readdir(dir))) {
54 snprintf(fname, sizeof(fname), "%s/%s", directory,
55 entry->d_name);
56 if (dlopen(fname, RTLD_NOW))
57 num++;
58 }
59
60 closedir(dir);
61
62 return num;
63}
64#else
Pablo Neira Ayuso2c348672011-05-07 12:50:08 +020065int osmo_plugin_load_all(const char *directory)
Harald Welteb9ce51c2010-06-30 19:43:11 +020066{
67 return 0;
68}
69#endif /* HAVE_DLFCN_H */
Harald Welte96e2a002017-06-12 21:44:18 +020070
71/*! @} */