Redesign policy management logic
[platform/core/security/vist.git] / src / vist / common / dynamic-loader.cpp
1 /*
2  *  Copyright (c) 2020-present Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16
17 #include <vist/dynamic-loader.hpp>
18
19 namespace vist {
20
21 DynamicLoader::DynamicLoader(const std::string& path, int flag)
22         : scopedHandle(init(path, flag))
23 {
24 }
25
26 DynamicLoader::ScopedHandle DynamicLoader::init(const std::string& path, int flag)
27 {
28         auto open = [&]() -> void* {
29                 ::dlerror();
30
31                 auto handle = ::dlopen(path.c_str(), flag);
32                 if (handle == nullptr) {
33                         if (auto error = ::dlerror(); error != nullptr)
34                                 ERROR(VIST) << "Failed to open library: " << error;
35
36                         THROW(ErrCode::RuntimeError) << "Failed to open library: " << path;
37                 }
38
39                 return handle;
40         };
41
42         auto close = [&](void* /*handle*/) {
43                 ::dlerror();
44
45 // Calling symbol & code after dlclose() makes SEGFAULT.
46 // TODO: Sync dynamic loading's life-cycle with program.
47 //
48 //              if (::dlclose(handle) != 0)
49 //                      THROW(ErrCode::RuntimeError) << "Failed to close library: " << ::dlerror();
50         };
51
52         return ScopedHandle(open(), close);
53 }
54
55 } // namespace vist