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 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 24 | /*! \addtogroup utils |
| 25 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 26 | * \file plugin.c */ |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 27 | |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 28 | #include "../config.h" |
| 29 | |
| 30 | #if HAVE_DLFCN_H |
| 31 | |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 32 | #include <dirent.h> |
| 33 | #include <dlfcn.h> |
| 34 | #include <stdio.h> |
| 35 | #include <errno.h> |
Holger Hans Peter Freyther | be0f7fa | 2010-08-31 17:14:04 +0800 | [diff] [blame] | 36 | #include <limits.h> |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 37 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 38 | #include <osmocom/core/plugin.h> |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 39 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 40 | /*! Load all plugins available in given directory |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 41 | * \param[in] directory full path name of directory containing plug-ins |
| 42 | * \returns number of plugins loaded in case of success, negative in case of error |
| 43 | */ |
Pablo Neira Ayuso | 2c34867 | 2011-05-07 12:50:08 +0200 | [diff] [blame] | 44 | int osmo_plugin_load_all(const char *directory) |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 45 | { |
| 46 | unsigned int num = 0; |
| 47 | char fname[PATH_MAX]; |
| 48 | DIR *dir; |
| 49 | struct dirent *entry; |
| 50 | |
| 51 | dir = opendir(directory); |
| 52 | if (!dir) |
| 53 | return -errno; |
| 54 | |
| 55 | while ((entry = readdir(dir))) { |
| 56 | snprintf(fname, sizeof(fname), "%s/%s", directory, |
| 57 | entry->d_name); |
| 58 | if (dlopen(fname, RTLD_NOW)) |
| 59 | num++; |
| 60 | } |
| 61 | |
| 62 | closedir(dir); |
| 63 | |
| 64 | return num; |
| 65 | } |
| 66 | #else |
Pablo Neira Ayuso | 2c34867 | 2011-05-07 12:50:08 +0200 | [diff] [blame] | 67 | int osmo_plugin_load_all(const char *directory) |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 68 | { |
| 69 | return 0; |
| 70 | } |
| 71 | #endif /* HAVE_DLFCN_H */ |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 72 | |
| 73 | /*! @} */ |