1 /*
2 * Copyright (c) 2021 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 "platform/dl_operation/include/aie_dl_operation.h"
17
18 #include <climits>
19 #include <cstdlib>
20 #include <cstring>
21 #include <dlfcn.h>
22
23 #include "utils/aie_macros.h"
24 #include "utils/log/aie_log.h"
25
26 namespace {
27 const int LOCAL = (RTLD_NOW | RTLD_LOCAL);
28 const int GLOBAL = (RTLD_NOW | RTLD_GLOBAL);
29 const char *LIB_PATH = "/usr/";
30 }
31
AieDlopen(const char * libName,int local)32 void *AieDlopen(const char *libName, int local)
33 {
34 int flag = local ? LOCAL : GLOBAL;
35 unsigned int length = strlen(LIB_PATH);
36 if ((libName == nullptr) || (strlen(libName) < length)) {
37 HILOGD("[AieDlOperation] libName is invalid.");
38 return nullptr;
39 }
40
41 char realLibPath[PATH_MAX + 1] = {0};
42 if (realpath(libName, realLibPath) == nullptr) {
43 HILOGD("[AieDlOperation] get libName realpath failed.");
44 return nullptr;
45 }
46
47 int retCode = strncmp(realLibPath, LIB_PATH, length);
48 if (retCode != 0) {
49 HILOGD("[AieDlOperation] lib path is error.");
50 return nullptr;
51 }
52
53 void *res = dlopen(realLibPath, flag);
54 return res;
55 }
56
AieDlclose(void * lib)57 void AieDlclose(void *lib)
58 {
59 dlclose(lib);
60 }
61
AieDlsym(void * handle,const char * funName)62 void *AieDlsym(void *handle, const char *funName)
63 {
64 return dlsym(handle, funName);
65 }
66
AieDlerror()67 const char *AieDlerror()
68 {
69 return dlerror();
70 }
71