blob: 40de4f8c7c313ea119d71d1f1b5a47f37bf444fc [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 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
Harald Welte96e2a002017-06-12 21:44:18 +020026/*! \addtogroup utils
27 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020028 * \file plugin.c */
Harald Weltede6e4982012-12-06 21:25:27 +010029
Harald Welteb9ce51c2010-06-30 19:43:11 +020030#include "../config.h"
31
32#if HAVE_DLFCN_H
33
Harald Welteb9ce51c2010-06-30 19:43:11 +020034#include <dirent.h>
35#include <dlfcn.h>
36#include <stdio.h>
37#include <errno.h>
Holger Hans Peter Freytherbe0f7fa2010-08-31 17:14:04 +080038#include <limits.h>
Harald Welteb9ce51c2010-06-30 19:43:11 +020039
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010040#include <osmocom/core/plugin.h>
Harald Welteb9ce51c2010-06-30 19:43:11 +020041
Neels Hofmeyr87e45502017-06-20 00:17:59 +020042/*! Load all plugins available in given directory
Harald Weltede6e4982012-12-06 21:25:27 +010043 * \param[in] directory full path name of directory containing plug-ins
44 * \returns number of plugins loaded in case of success, negative in case of error
45 */
Pablo Neira Ayuso2c348672011-05-07 12:50:08 +020046int osmo_plugin_load_all(const char *directory)
Harald Welteb9ce51c2010-06-30 19:43:11 +020047{
48 unsigned int num = 0;
49 char fname[PATH_MAX];
50 DIR *dir;
51 struct dirent *entry;
52
53 dir = opendir(directory);
54 if (!dir)
55 return -errno;
56
57 while ((entry = readdir(dir))) {
58 snprintf(fname, sizeof(fname), "%s/%s", directory,
59 entry->d_name);
60 if (dlopen(fname, RTLD_NOW))
61 num++;
62 }
63
64 closedir(dir);
65
66 return num;
67}
68#else
Pablo Neira Ayuso2c348672011-05-07 12:50:08 +020069int osmo_plugin_load_all(const char *directory)
Harald Welteb9ce51c2010-06-30 19:43:11 +020070{
71 return 0;
72}
73#endif /* HAVE_DLFCN_H */
Harald Welte96e2a002017-06-12 21:44:18 +020074
75/*! @} */