1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <dlfcn.h>
16
17 #include "pasteboard_hilog.h"
18 #include "pasteboard_lib_guard.h"
19
20 namespace OHOS::MiscServices {
LibGuard(const char * libPath)21 LibGuard::LibGuard(const char *libPath)
22 {
23 if (libPath == nullptr) {
24 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "lib path is null.");
25 return;
26 }
27 libPath_.assign(libPath);
28 libHandle = dlopen(libPath, RTLD_LAZY);
29 if (libHandle == nullptr) {
30 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "dlopen %{public}s failed.", libPath);
31 }
32 }
33
~LibGuard()34 LibGuard::~LibGuard()
35 {
36 if (libHandle == nullptr) {
37 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "lib handle is null.");
38 return;
39 }
40 if (dlclose(libHandle) == 0) {
41 return;
42 }
43 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "dlclose %{public}s error: %{public}s", libPath_.c_str(), dlerror());
44 }
45
Ready()46 bool LibGuard::Ready()
47 {
48 return libHandle ? true : false;
49 }
50
GetLibHandle() const51 void *LibGuard::GetLibHandle() const
52 {
53 return libHandle;
54 }
55 } // namespace OHOS::MiscServices