TIVI-1924: Initial commit of IVI settings daemon.
[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
33 #include <stdexcept>
34 #include <dlfcn.h>
35
36
37 #ifdef __GNUC__
38 /// We use this macro to silence @c -pedantic warnings from g++ that
39 /// there is no way to properly fix.
40 #  define IVI_SETTINGS_EXTENSION __extension__
41 #else
42 #  define IVI_SETTINGS_EXTENSION
43 #endif  /* __GNUC__ */
44
45
46 ivi::settings::loader::loader(std::string const & plugin_name,
47                               ivi::settings::manager & mgr)
48   : handle_(dlopen(plugin_name.c_str(), RTLD_LAZY | RTLD_GLOBAL))
49 {
50   if (handle_ == nullptr) {
51     throw std::logic_error(dlerror());
52   }
53
54   // Reset any lingering dynamic linking related errors (see
55   // dlopen(3)).
56   dlerror();
57
58   // Retrieve the settings factory and destroyer functions.
59   typedef bool(*factory_type)(registrar &);
60   factory_type const register_settings =
61     IVI_SETTINGS_EXTENSION reinterpret_cast<factory_type>(
62       dlsym(handle_,
63             "register_settings"));
64
65   if (register_settings == nullptr)
66     throw std::logic_error(dlerror());
67
68   // Now create the underlying settings implementation.
69
70   registrar r(mgr);
71   if (!register_settings(r))
72     throw std::runtime_error("Unable to make \""
73                              + plugin_name
74                              + "\" settings plugin");
75 }
76
77 ivi::settings::loader::~loader()
78 {
79   dlclose(handle_);
80 }
81
82
83 // Local Variables:
84 // mode:c++
85 // c-basic-offset:2
86 // indent-tabs-mode: nil
87 // End: