blob: 71df88be08eba3086291de00db3fb3826840f592 [file] [log] [blame]
Harald Welteb9ce51c2010-06-30 19:43:11 +02001/* plugin infrastructure */
2
3/* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welte96e2a002017-06-12 21:44:18 +020023/*! \addtogroup utils
24 * @{
25 */
26
Harald Weltede6e4982012-12-06 21:25:27 +010027/*! \file plugin.c
28 * \brief Routines for loading and managing shared library plug-ins.
29 */
30
31
Harald Welteb9ce51c2010-06-30 19:43:11 +020032#include "../config.h"
33
34#if HAVE_DLFCN_H
35
Harald Welteb9ce51c2010-06-30 19:43:11 +020036#include <dirent.h>
37#include <dlfcn.h>
38#include <stdio.h>
39#include <errno.h>
Holger Hans Peter Freytherbe0f7fa2010-08-31 17:14:04 +080040#include <limits.h>
Harald Welteb9ce51c2010-06-30 19:43:11 +020041
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010042#include <osmocom/core/plugin.h>
Harald Welteb9ce51c2010-06-30 19:43:11 +020043
Harald Weltede6e4982012-12-06 21:25:27 +010044/*! \brief Load all plugins available in given directory
45 * \param[in] directory full path name of directory containing plug-ins
46 * \returns number of plugins loaded in case of success, negative in case of error
47 */
Pablo Neira Ayuso2c348672011-05-07 12:50:08 +020048int osmo_plugin_load_all(const char *directory)
Harald Welteb9ce51c2010-06-30 19:43:11 +020049{
50 unsigned int num = 0;
51 char fname[PATH_MAX];
52 DIR *dir;
53 struct dirent *entry;
54
55 dir = opendir(directory);
56 if (!dir)
57 return -errno;
58
59 while ((entry = readdir(dir))) {
60 snprintf(fname, sizeof(fname), "%s/%s", directory,
61 entry->d_name);
62 if (dlopen(fname, RTLD_NOW))
63 num++;
64 }
65
66 closedir(dir);
67
68 return num;
69}
70#else
Pablo Neira Ayuso2c348672011-05-07 12:50:08 +020071int osmo_plugin_load_all(const char *directory)
Harald Welteb9ce51c2010-06-30 19:43:11 +020072{
73 return 0;
74}
75#endif /* HAVE_DLFCN_H */
Harald Welte96e2a002017-06-12 21:44:18 +020076
77/*! @} */