1 /*
2  * Copyright (c) 2024 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 
16 #include "netstack_bundle_utils.h"
17 
18 #include <dlfcn.h>
19 
20 #include "netstack_log.h"
21 
22 namespace OHOS::NetStack::BundleUtils {
23 
24 #ifdef __LP64__
25     const std::string LIB_NET_BUNDL_UTILS_SO_PATH = "/system/lib64/libnet_bundle_utils.z.so";
26 #else
27     const std::string LIB_NET_BUNDL_UTILS_SO_PATH = "/system/lib/libnet_bundle_utils.z.so";
28 #endif
29 
30 using IsAtomicServiceFunc = bool (*)(std::string&);
31 
IsAtomicService(std::string & bundleName)32 __attribute__((no_sanitize("cfi"))) bool IsAtomicService(std::string &bundleName)
33 {
34     void *handler = dlopen(LIB_NET_BUNDL_UTILS_SO_PATH.c_str(), RTLD_NOW);
35     if (handler == nullptr) {
36         const char *err = dlerror();
37         NETSTACK_LOGE("load failed, reason: %{public}s", err ? err : "unknown");
38         return false;
39     }
40     IsAtomicServiceFunc func = (IsAtomicServiceFunc) dlsym(handler, "IsAtomicService");
41     if (func == nullptr) {
42         const char *err = dlerror();
43         NETSTACK_LOGE("dlsym IsAtomicService failed, reason: %{public}s", err ? err : "unknown");
44         dlclose(handler);
45         return false;
46     }
47     auto ret = func(bundleName);
48     NETSTACK_LOGD("netBundleUtils IsAtomicService result=%{public}d, bundle_name=%{public}s", ret, bundleName.c_str());
49     dlclose(handler);
50     return ret;
51 }
52 }