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