[TIVI-2848] Fix tethering configuration.
[profile/ivi/settings-daemon.git] / lib / loader.cpp
1 /**
2  * @file loader.cpp
3  *
4  * @brief Settings plugin loader implementation.
5  *
6  * @author Ossama Othman @<ossama.othman@@intel.com@>
7  *
8  * @copyright @par
9  * Copyright 2012, 2013 Intel Corporation All Rights Reserved.
10  * @par
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation;
14  * version 2.1 of the License.
15  * @par
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  * @par
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA  02110-1301  USA
25  */
26
27
28 #include "loader.hpp"
29 #include "manager.hpp"
30 #include <settingsd/plugin.hpp>
31 #include <settingsd/registrar.hpp>
32 #include <settingsd/event_callback.hpp>
33
34 #include <system_error>
35 #include <dlfcn.h>
36
37
38 #ifdef __GNUC__
39 /// We use this macro to silence @c -pedantic warnings from g++ that
40 /// there is no way to properly fix.
41 #  define IVI_SETTINGS_EXTENSION __extension__
42 #else
43 #  define IVI_SETTINGS_EXTENSION
44 #endif  /* __GNUC__ */
45
46
47 ivi::settings::loader::loader(std::string const & plugin_name,
48                               ivi::settings::manager & mgr)
49   : handle_(dlopen(plugin_name.c_str(), RTLD_LAZY | RTLD_GLOBAL))
50 {
51   if (handle_ == nullptr) {
52     throw std::system_error(std::error_code(), dlerror());
53   }
54
55   // Reset any lingering dynamic linking related errors (see
56   // dlopen(3)).
57   dlerror();
58
59   // Retrieve the settings factory and destroyer functions.
60   typedef bool(*factory_type)(registrar &, event_callback const &);
61   factory_type const register_settings =
62     IVI_SETTINGS_EXTENSION reinterpret_cast<factory_type>(
63       dlsym(handle_,
64             "register_settings"));
65
66   if (register_settings == nullptr)
67     throw std::system_error(std::error_code(), dlerror());
68
69   // Now create the underlying settings implementation.
70
71   registrar r(mgr);
72   event_callback e(mgr);
73   if (!register_settings(r, e))
74     throw std::runtime_error("Unable to make \""
75                              + plugin_name
76                              + "\" settings plugin");
77 }
78
79 ivi::settings::loader::~loader()
80 {
81   dlclose(handle_);
82 }
83
84
85 // Local Variables:
86 // mode:c++
87 // c-basic-offset:2
88 // indent-tabs-mode: nil
89 // End: