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 "nstackx_util.h"
17 #include "nstackx_error.h"
18 #include "nstackx_log.h"
19 #include "securec.h"
20 #include "sys_epoll.h"
21 
22 #define TAG "nStackXUtil"
23 
24 static const char *g_illegalPathString[] = {
25     "/../",
26 };
27 
28 static const char *g_illegalPathHeadString[] = {
29     "../",
30 };
31 
IsFileNameLegal(const char * fileName)32 uint8_t IsFileNameLegal(const char *fileName)
33 {
34     if (fileName == NULL || strlen(fileName) == 0) {
35         return NSTACKX_FALSE;
36     }
37 
38     for (uint32_t idx = 0; idx < sizeof(g_illegalPathHeadString) / sizeof(g_illegalPathHeadString[0]); idx++) {
39         if (g_illegalPathHeadString[idx] == NULL || strlen(fileName) < strlen(g_illegalPathHeadString[idx])) {
40             continue;
41         }
42         if (memcmp(fileName, g_illegalPathHeadString[idx], strlen(g_illegalPathHeadString[idx])) == 0) {
43             LOGE(TAG, "illegal filename");
44             return NSTACKX_FALSE;
45         }
46     }
47 
48     for (uint32_t idx = 0; idx < sizeof(g_illegalPathString) / sizeof(g_illegalPathString[0]); idx++) {
49         if (g_illegalPathString[idx] == NULL || strlen(fileName) < strlen(g_illegalPathString[idx])) {
50             continue;
51         }
52         if (strstr(fileName, g_illegalPathString[idx]) != NULL) {
53             LOGE(TAG, "illegal filename");
54             return NSTACKX_FALSE;
55         }
56     }
57     return NSTACKX_TRUE;
58 }
59 
GetCpuNum(void)60 int32_t GetCpuNum(void)
61 {
62     return 1;
63 }
64 
StartThreadBindCore(int32_t cpu)65 void StartThreadBindCore(int32_t cpu)
66 {
67     (void)cpu;
68 }
69 
SetThreadName(const char * name)70 void SetThreadName(const char *name)
71 {
72     /* liteos only support set thread name when create */
73     (void)name;
74 }
75 
BindThreadToTargetMask(pid_t tid,uint32_t cpuMask)76 void BindThreadToTargetMask(pid_t tid, uint32_t cpuMask)
77 {
78     (void)tid;
79     (void)cpuMask;
80 }
81 
SetMaximumPriorityForThread(void)82 void SetMaximumPriorityForThread(void)
83 {
84 }
85 
ClockGetTime(clockid_t id,struct timespec * tp)86 void ClockGetTime(clockid_t id, struct timespec *tp)
87 {
88     if (clock_gettime(id, tp) != 0) {
89         LOGE(TAG, "clock_gettime error: %d", errno);
90     }
91 }
92 
SemInit(sem_t * sem,int pshared,unsigned int value)93 int32_t SemInit(sem_t *sem, int pshared, unsigned int value)
94 {
95     return sem_init(sem, pshared, value);
96 }
97 
SemGetValue(sem_t * sem,int * sval)98 void SemGetValue(sem_t *sem, int *sval)
99 {
100     if (sem_getvalue(sem, sval) != 0) {
101         LOGE(TAG, "sem get error: %d", errno);
102     }
103 }
104 
SemPost(sem_t * sem)105 void SemPost(sem_t *sem)
106 {
107     if (sem_post(sem) != 0) {
108         LOGE(TAG, "sem post error: %d", errno);
109     }
110 }
111 
SemWait(sem_t * sem)112 void SemWait(sem_t *sem)
113 {
114     if (sem_wait(sem) != 0) {
115         LOGE(TAG, "sem wait error: %d", errno);
116     }
117 }
118 
SemDestroy(sem_t * sem)119 void SemDestroy(sem_t *sem)
120 {
121     if (sem_destroy(sem) != 0) {
122         LOGE(TAG, "sem destroy error: %d", errno);
123     }
124 }
125 
PthreadCreate(pthread_t * tid,const pthread_attr_t * attr,void * (* entry)(void *),void * arg)126 int32_t PthreadCreate(pthread_t *tid, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
127 {
128     return pthread_create(tid, attr, entry, arg);
129 }
130 
PthreadJoin(pthread_t thread,void ** retval)131 void PthreadJoin(pthread_t thread, void **retval)
132 {
133     if (pthread_join(thread, retval) != 0) {
134         LOGE(TAG, "pthread_join failed error: %d", errno);
135     }
136 }
137 
PthreadMutexInit(pthread_mutex_t * mutex,const pthread_mutexattr_t * attr)138 int32_t PthreadMutexInit(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
139 {
140     return pthread_mutex_init(mutex, attr);
141 }
142 
PthreadMutexDestroy(pthread_mutex_t * mutex)143 void PthreadMutexDestroy(pthread_mutex_t *mutex)
144 {
145     if (pthread_mutex_destroy(mutex) != 0) {
146         LOGE(TAG, "pthread mutex destroy error: %d", errno);
147     }
148 }
149 
PthreadMutexLock(pthread_mutex_t * mutex)150 int32_t PthreadMutexLock(pthread_mutex_t *mutex)
151 {
152     return pthread_mutex_lock(mutex);
153 }
154 
PthreadMutexUnlock(pthread_mutex_t * mutex)155 int32_t PthreadMutexUnlock(pthread_mutex_t *mutex)
156 {
157     return pthread_mutex_unlock(mutex);
158 }
159 
CloseDesc(int32_t desc)160 void CloseDesc(int32_t desc)
161 {
162     CloseDescClearEpollPtr(desc);
163     if (close(desc) != 0) {
164         LOGE(TAG, "close desc error : %d", errno);
165     }
166 }
167