Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file plugin.c |
| 2 | * Routines for loading and managing shared library plug-ins. */ |
| 3 | /* |
| 4 | * (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 10 | * 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 Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 26 | /*! \addtogroup utils |
| 27 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 28 | * \file plugin.c */ |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 29 | |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 30 | #include "../config.h" |
| 31 | |
| 32 | #if HAVE_DLFCN_H |
| 33 | |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 34 | #include <dirent.h> |
| 35 | #include <dlfcn.h> |
| 36 | #include <stdio.h> |
| 37 | #include <errno.h> |
Holger Hans Peter Freyther | be0f7fa | 2010-08-31 17:14:04 +0800 | [diff] [blame] | 38 | #include <limits.h> |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 39 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 40 | #include <osmocom/core/plugin.h> |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 41 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 42 | /*! Load all plugins available in given directory |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 43 | * \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 Ayuso | 2c34867 | 2011-05-07 12:50:08 +0200 | [diff] [blame] | 46 | int osmo_plugin_load_all(const char *directory) |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 47 | { |
| 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 Ayuso | 2c34867 | 2011-05-07 12:50:08 +0200 | [diff] [blame] | 69 | int osmo_plugin_load_all(const char *directory) |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 70 | { |
| 71 | return 0; |
| 72 | } |
| 73 | #endif /* HAVE_DLFCN_H */ |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 74 | |
| 75 | /*! @} */ |