• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef APPSPAWN_UTILS_H
17 #define APPSPAWN_UTILS_H
18 
19 #include <ctype.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <limits.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 
31 #include "hilog/log.h"
32 #include "appspawn_error.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif  // __cplusplus
37 
38 #ifndef APPSPAWN_TEST
39 #define APPSPAWN_STATIC static
40 #else
41 #define APPSPAWN_STATIC
42 #endif
43 
44 #ifndef APPSPAWN_BASE_DIR
45 #define APPSPAWN_BASE_DIR ""
46 #endif
47 #if defined(__MUSL__)
48 #define APPSPAWN_SOCKET_DIR APPSPAWN_BASE_DIR "/dev/unix/socket/"
49 #define APPSPAWN_MSG_DIR APPSPAWN_BASE_DIR "/data/service/el1/startup/"
50 #else
51 #define APPSPAWN_SOCKET_DIR APPSPAWN_BASE_DIR "/dev/socket/"
52 #define APPSPAWN_MSG_DIR APPSPAWN_BASE_DIR "/data/service/el1/startup/"
53 #endif
54 
55 #define DEVICE_VIRTUAL_NET_IO_FLAGS "/sys/devices/virtual/net/lo/flags"
56 #define IFF_LOOPBACK_VALUE "9\n"
57 #define IFF_LOOPBACK_SIZE 2
58 
59 #define APPSPAWN_CHECK_EXIT "AppSpawnCheckUnexpectedExitCall"
60 #define UNUSED(x) (void)(x)
61 
62 #define APP_COLD_START 0x01
63 #define APP_ASAN_DETECTOR 0x02
64 #define APP_DEVELOPER_MODE 0x04
65 #define APP_JITFORT_MODE 0x08
66 #define APP_BEGETCTL_BOOT 0x400
67 
68 #define MAX_LEN_SHORT_NAME 16
69 #define DEFAULT_UMASK 0002
70 #define UID_BASE 200000 // 20010029
71 #define DEFAULT_DIR_MODE 0711
72 #define USER_ID_BUFFER_SIZE 32
73 
74 #define APPSPAWN_SEC_TO_NSEC 1000000000
75 #define APPSPAWN_MSEC_TO_NSEC 1000000
76 #define APPSPAWN_USEC_TO_NSEC 1000
77 #define APPSPAWN_SEC_TO_MSEC 1000
78 
79 #define CHECK_FLAGS_BY_INDEX(flags, index) ((((flags) >> (index)) & 0x1) == 0x1)
80 #ifndef ARRAY_LENGTH
81 #define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0]))
82 #endif
83 
84 #define INVALID_PERMISSION_INDEX (-1)
85 
86 typedef struct {
87     int flag;
88     const char *ldPreload;
89     const char *asanOptions;
90     const char *tsanOptions;
91     const char *ubsanOptions;
92     const char *hwasanOptions;
93 } EnvConfig;
94 
95 #define MAX_ENV_VALUE_LEN 1024
96 
97 typedef struct TagAppSpawnCommonEnv {
98     const char *envName;
99     const char *envValue;
100     int developerModeEnable;
101 } AppSpawnCommonEnv;
102 
103 /* spawner permission */
104 static const char *g_spawnerPermissionList[] = {
105     "ohos.permission.FOWNER",
106     "ohos.permission.ALLOW_IOURING"
107 };
108 
109 typedef enum {
110     APPSPAWN_OK = 0,
111     APPSPAWN_SYSTEM_ERROR = 0xD000000,
112     APPSPAWN_ARG_INVALID,
113     APPSPAWN_MSG_INVALID,
114     APPSPAWN_MSG_TOO_LONG,
115     APPSPAWN_TLV_NOT_SUPPORT,
116     APPSPAWN_TLV_NONE,
117     APPSPAWN_SANDBOX_NONE,
118     APPSPAWN_SANDBOX_LOAD_FAIL,
119     APPSPAWN_SANDBOX_MOUNT_FAIL,
120     APPSPAWN_SPAWN_TIMEOUT,
121     APPSPAWN_CHILD_CRASH,
122     APPSPAWN_NATIVE_NOT_SUPPORT,
123     APPSPAWN_ACCESS_TOKEN_INVALID,
124     APPSPAWN_PERMISSION_NOT_SUPPORT,
125     APPSPAWN_BUFFER_NOT_ENOUGH,
126     APPSPAWN_TIMEOUT,
127     APPSPAWN_FORK_FAIL,
128     APPSPAWN_DEBUG_MODE_NOT_SUPPORT,
129     APPSPAWN_ERROR_UTILS_MEM_FAIL,
130     APPSPAWN_ERROR_FILE_RMDIR_FAIL,
131     APPSPAWN_PIDMGR_DEFAULT_PID_MAX,
132     APPSPAWN_ERROR_UTILS_DECODE_JSON_FAIL,
133     APPSPAWN_ERROR_UTILS_CREATE_JSON_FAIL,
134     APPSPAWN_ERROR_UTILS_ADD_JSON_FAIL,
135     /* sandbox errno num */
136     APPSPAWN_SANDBOX_INVALID,
137     APPSPAWN_SANDBOX_ERROR_MKDIR_FAIL,
138     APPSPAWN_SANDBOX_ERROR_MOUNT_FAIL,
139     APPSPAWN_SANDBOX_ERROR_SET_PERMISSION_FLAG_FAIL,
140     APPSPAWN_NODE_EXIST,
141     /* devicedebug errno */
142     APPSPAWN_DEVICEDEBUG_ERROR_APP_NOT_EXIST,
143     APPSPAWN_DEVICEDEBUG_ERROR_APP_NOT_DEBUGGABLE,
144 } AppSpawnErrorCode;
145 
146 uint64_t DiffTime(const struct timespec *startTime, const struct timespec *endTime);
147 void AppSpawnDump(const char *fmt, ...);
148 void SetDumpToStream(FILE *stream);
149 typedef int (*SplitStringHandle)(const char *str, void *context);
150 int32_t StringSplit(const char *str, const char *separator, void *context, SplitStringHandle handle);
151 char *GetLastStr(const char *str, const char *dst);
152 uint32_t GetSpawnTimeout(uint32_t def);
153 void DumpCurrentDir(char *buffer, uint32_t bufferLen, const char *dirPath);
154 int IsDeveloperModeOpen();
155 void InitCommonEnv(void);
156 int ConvertEnvValue(const char *srcEnv, char *dstEnv, int len);
157 
158 int EnableNewNetNamespace(void);
159 
160 #ifndef APP_FILE_NAME
161 #define APP_FILE_NAME   (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__))
162 #endif
163 
164 #ifndef OHOS_LITE
165 #define APPSPAWN_DOMAIN (0xD002C00 + 0x11)  // 0xD002C11
166 #ifndef APPSPAWN_LABEL
167 #define APPSPAWN_LABEL "APPSPAWN"
168 #endif
169 
170 #undef LOG_TAG
171 #define LOG_TAG APPSPAWN_LABEL
172 #undef LOG_DOMAIN
173 #define LOG_DOMAIN APPSPAWN_DOMAIN
174 
175 #define APPSPAWN_LOGI(fmt, ...) \
176     HILOG_INFO(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
177 #define APPSPAWN_LOGE(fmt, ...) \
178     HILOG_ERROR(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
179 #define APPSPAWN_LOGV(fmt, ...) \
180     HILOG_DEBUG(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
181 #define APPSPAWN_LOGW(fmt, ...) \
182     HILOG_WARN(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
183 #define APPSPAWN_LOGF(fmt, ...) \
184     HILOG_FATAL(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
185 #define APPSPAWN_DUMP_LOGI(fmt, ...) \
186     HILOG_INFO(LOG_CORE, fmt, ##__VA_ARGS__)
187 #define APPSPAWN_DUMP(fmt, ...) \
188     do { \
189         HILOG_INFO(LOG_CORE, fmt, ##__VA_ARGS__); \
190         AppSpawnDump(fmt "\n", ##__VA_ARGS__); \
191     } while (0)
192 #define APPSPAWN_KLOGI(fmt, ...) \
193     ((void)HiLogPrint(LOG_KMSG, LOG_INFO, LOG_DOMAIN, LOG_TAG, \
194     "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__))
195 #else
196 
197 #define APPSPAWN_LOGI(fmt, ...) \
198     HILOG_INFO(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
199 #define APPSPAWN_LOGE(fmt, ...) \
200     HILOG_ERROR(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
201 #define APPSPAWN_LOGV(fmt, ...) \
202     HILOG_DEBUG(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
203 #define APPSPAWN_LOGW(fmt, ...) \
204     HILOG_FATAL(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
205 #define APPSPAWN_DUMP_LOGI(fmt, ...) \
206     HILOG_INFO(HILOG_MODULE_HIVIEW, fmt, ##__VA_ARGS__)
207 #define APPSPAWN_KLOGI(fmt, ...) \
208     HILOG_INFO(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
209 #endif
210 
211 #define APPSPAWN_CHECK(retCode, exper, fmt, ...) \
212     if (!(retCode)) {                    \
213         APPSPAWN_LOGE(fmt, ##__VA_ARGS__);         \
214         exper;                           \
215     }
216 
217 #define APPSPAWN_CHECK_LOGV(retCode, exper, fmt, ...) \
218     if (!(retCode)) {                    \
219         APPSPAWN_LOGV(fmt, ##__VA_ARGS__);         \
220         exper;                           \
221     }
222 
223 #define APPSPAWN_CHECK_LOGW(retCode, exper, fmt, ...) \
224     if (!(retCode)) {                    \
225         APPSPAWN_LOGW(fmt, ##__VA_ARGS__);         \
226         exper;                           \
227     }
228 
229 #define APPSPAWN_CHECK_LOGI(retCode, exper, fmt, ...) \
230     if (!(retCode)) {                    \
231         APPSPAWN_LOGI(fmt, ##__VA_ARGS__);         \
232         exper;                           \
233     }
234 
235 #define APPSPAWN_CHECK_ONLY_EXPER(retCode, exper) \
236     if (!(retCode)) {                  \
237         exper;                 \
238     }                         \
239 
240 #define APPSPAWN_ONLY_EXPER(retCode, exper) \
241     if ((retCode)) {                  \
242         exper;                 \
243     }
244 
245 #define APPSPAWN_CHECK_ONLY_LOG(retCode, fmt, ...) \
246     if (!(retCode)) {                    \
247         APPSPAWN_LOGE(fmt, ##__VA_ARGS__);      \
248     }
249 
250 #define APPSPAWN_CHECK_ONLY_LOGW(retCode, fmt, ...) \
251     if (!(retCode)) {                    \
252         APPSPAWN_LOGW(fmt, ##__VA_ARGS__);      \
253     }
254 
255 #ifdef __cplusplus
256 }
257 #endif  // __cplusplus
258 
259 #endif  // APPSPAWN_UTILS_H
260