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
21 #define TAG "nStackXUtil"
22
23 static const char *g_illegalPathString[] = {
24 "/../",
25 };
26
27 static const char *g_illegalPathHeadString[] = {
28 "../",
29 };
30
IsFileNameLegal(const char * fileName)31 uint8_t IsFileNameLegal(const char *fileName)
32 {
33 if (fileName == NULL || strlen(fileName) == 0) {
34 return NSTACKX_FALSE;
35 }
36
37 for (uint32_t idx = 0; idx < sizeof(g_illegalPathHeadString) / sizeof(g_illegalPathHeadString[0]); idx++) {
38 if (g_illegalPathHeadString[idx] == NULL || strlen(fileName) < strlen(g_illegalPathHeadString[idx])) {
39 continue;
40 }
41 if (memcmp(fileName, g_illegalPathHeadString[idx], strlen(g_illegalPathHeadString[idx])) == 0) {
42 LOGE(TAG, "illegal filename");
43 return NSTACKX_FALSE;
44 }
45 }
46
47 for (uint32_t idx = 0; idx < sizeof(g_illegalPathString) / sizeof(g_illegalPathString[0]); idx++) {
48 if (g_illegalPathString[idx] == NULL || strlen(fileName) < strlen(g_illegalPathString[idx])) {
49 continue;
50 }
51 if (strstr(fileName, g_illegalPathString[idx]) != NULL) {
52 LOGE(TAG, "illegal filename");
53 return NSTACKX_FALSE;
54 }
55 }
56 return NSTACKX_TRUE;
57 }
58
GetCpuNum(void)59 int32_t GetCpuNum(void)
60 {
61 return (int)sysconf(_SC_NPROCESSORS_CONF);
62 }
63
StartThreadBindCore(int32_t cpu)64 void StartThreadBindCore(int32_t cpu)
65 {
66 int32_t cpus;
67 cpu_set_t mask;
68 int32_t syscallres;
69 pid_t tid = gettid();
70 if (cpu < 0) {
71 return;
72 }
73 cpus = GetCpuNum();
74 if (cpus < cpu + 1) {
75 return;
76 }
77 CPU_ZERO(&mask);
78 CPU_SET(cpu, &mask);
79 syscallres = (int32_t)syscall(__NR_sched_setaffinity, tid, sizeof(mask), &mask);
80 if (syscallres < 0) {
81 LOGE(TAG, "set thread affinity failed, ret %d, error(%d)", syscallres, errno);
82 return;
83 }
84 }
85
BindThreadToTargetMask(pid_t tid,uint32_t cpuMask)86 void BindThreadToTargetMask(pid_t tid, uint32_t cpuMask)
87 {
88 if (tid == 0) {
89 LOGE(TAG, "invalid tid");
90 return;
91 }
92
93 int32_t cpus = GetCpuNum();
94 if (cpus < 0) {
95 return;
96 }
97
98 if (cpuMask == 0 || cpuMask >= (1U << (uint32_t)cpus)) {
99 LOGE(TAG, "invalid cpu mask");
100 return;
101 }
102 cpu_set_t mask;
103 int32_t syscallres;
104 uint32_t cpu = CPU_IDX_0;
105 CPU_ZERO(&mask);
106 LOGI(TAG, "bind thread %d to target core :%x", tid, cpuMask);
107 while (cpuMask > 0) {
108 if ((cpuMask & 1) == 1) {
109 CPU_SET(cpu, &mask);
110 }
111 cpu++;
112 cpuMask = cpuMask >> 1;
113 }
114 syscallres = (int32_t)syscall(__NR_sched_setaffinity, tid, sizeof(mask), &mask);
115 if (syscallres < 0) {
116 LOGE(TAG, "set thread affinity failed, ret %d, error(%d)", syscallres, errno);
117 return;
118 }
119 }
120
SetThreadName(const char * name)121 void SetThreadName(const char *name)
122 {
123 if (name == NULL || strlen(name) == 0 || strlen(name) >= MAX_THREAD_NAME_LEN) {
124 LOGE(TAG, "invalid input");
125 }
126 if (prctl(PR_SET_NAME, name) < 0) {
127 LOGE(TAG, "prctl errno %d", errno);
128 }
129 }
130
SetMaximumPriorityForThread(void)131 void SetMaximumPriorityForThread(void)
132 {
133 if (nice(THREAD_MAXIMUM_PRIORITY) == -1) {
134 LOGE(TAG, "nice set error: %d", errno);
135 }
136 }
137
ClockGetTime(clockid_t id,struct timespec * tp)138 void ClockGetTime(clockid_t id, struct timespec *tp)
139 {
140 if (clock_gettime(id, tp) != 0) {
141 LOGE(TAG, "clock_gettime error: %d", errno);
142 }
143 }
144
SemInit(sem_t * sem,int pshared,unsigned int value)145 int32_t SemInit(sem_t *sem, int pshared, unsigned int value)
146 {
147 return sem_init(sem, pshared, value);
148 }
149
SemGetValue(sem_t * sem,int * sval)150 void SemGetValue(sem_t *sem, int *sval)
151 {
152 if (sem_getvalue(sem, sval) != 0) {
153 LOGE(TAG, "sem get error: %d", errno);
154 }
155 }
156
SemPost(sem_t * sem)157 void SemPost(sem_t *sem)
158 {
159 if (sem_post(sem) != 0) {
160 LOGE(TAG, "sem post error: %d", errno);
161 }
162 }
163
SemWait(sem_t * sem)164 void SemWait(sem_t *sem)
165 {
166 if (sem_wait(sem) != 0) {
167 LOGE(TAG, "sem wait error: %d", errno);
168 }
169 }
170
SemDestroy(sem_t * sem)171 void SemDestroy(sem_t *sem)
172 {
173 if (sem_destroy(sem) != 0) {
174 LOGE(TAG, "sem destroy error: %d", errno);
175 }
176 }
177
PthreadCreate(pthread_t * tid,const pthread_attr_t * attr,void * (* entry)(void *),void * arg)178 int32_t PthreadCreate(pthread_t *tid, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
179 {
180 return pthread_create(tid, attr, entry, arg);
181 }
182
PthreadJoin(pthread_t thread,void ** retval)183 void PthreadJoin(pthread_t thread, void **retval)
184 {
185 if (pthread_join(thread, retval) != 0) {
186 LOGE(TAG, "pthread_join failed error: %d", errno);
187 }
188 }
189
PthreadMutexInit(pthread_mutex_t * mutex,const pthread_mutexattr_t * attr)190 int32_t PthreadMutexInit(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
191 {
192 return pthread_mutex_init(mutex, attr);
193 }
194
PthreadMutexDestroy(pthread_mutex_t * mutex)195 void PthreadMutexDestroy(pthread_mutex_t *mutex)
196 {
197 if (pthread_mutex_destroy(mutex) != 0) {
198 LOGE(TAG, "pthread mutex destroy error: %d", errno);
199 }
200 }
201
PthreadMutexLock(pthread_mutex_t * mutex)202 int32_t PthreadMutexLock(pthread_mutex_t *mutex)
203 {
204 return pthread_mutex_lock(mutex);
205 }
206
PthreadMutexUnlock(pthread_mutex_t * mutex)207 int32_t PthreadMutexUnlock(pthread_mutex_t *mutex)
208 {
209 return pthread_mutex_unlock(mutex);
210 }
211
CloseDesc(int32_t desc)212 void CloseDesc(int32_t desc)
213 {
214 if (close(desc) != 0) {
215 LOGE(TAG, "close desc error : %d", errno);
216 }
217 }
218