• 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 
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_NODE_EXIST,
126 } AppSpawnErrorCode;
127 
128 uint64_t DiffTime(const struct timespec *startTime, const struct timespec *endTime);
129 void AppSpawnDump(const char *fmt, ...);
130 void SetDumpToStream(FILE *stream);
131 typedef int (*SplitStringHandle)(const char *str, void *context);
132 int32_t StringSplit(const char *str, const char *separator, void *context, SplitStringHandle handle);
133 char *GetLastStr(const char *str, const char *dst);
134 uint32_t GetSpawnTimeout(uint32_t def);
135 void DumpCurrentDir(char *buffer, uint32_t bufferLen, const char *dirPath);
136 int IsDeveloperModeOpen();
137 void InitCommonEnv(void);
138 int ConvertEnvValue(const char *srcEnv, char *dstEnv, int len);
139 
140 int EnableNewNetNamespace(void);
141 
142 #ifndef APP_FILE_NAME
143 #define APP_FILE_NAME   (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__))
144 #endif
145 
146 #ifndef OHOS_LITE
147 #define APPSPAWN_DOMAIN (0xD002C00 + 0x11)
148 #ifndef APPSPAWN_LABEL
149 #define APPSPAWN_LABEL "APPSPAWN"
150 #endif
151 
152 #undef LOG_TAG
153 #define LOG_TAG APPSPAWN_LABEL
154 #undef LOG_DOMAIN
155 #define LOG_DOMAIN APPSPAWN_DOMAIN
156 
157 #define APPSPAWN_LOGI(fmt, ...) \
158     HILOG_INFO(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
159 #define APPSPAWN_LOGE(fmt, ...) \
160     HILOG_ERROR(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
161 #define APPSPAWN_LOGV(fmt, ...) \
162     HILOG_DEBUG(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
163 #define APPSPAWN_LOGW(fmt, ...) \
164     HILOG_WARN(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
165 #define APPSPAWN_LOGF(fmt, ...) \
166     HILOG_FATAL(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
167 
168 #define APPSPAPWN_DUMP(fmt, ...) \
169     do { \
170         HILOG_INFO(LOG_CORE, fmt, ##__VA_ARGS__); \
171         AppSpawnDump(fmt "\n", ##__VA_ARGS__); \
172     } while (0)
173 
174 #else
175 
176 #define APPSPAWN_LOGI(fmt, ...) \
177     HILOG_INFO(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
178 #define APPSPAWN_LOGE(fmt, ...) \
179     HILOG_ERROR(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
180 #define APPSPAWN_LOGV(fmt, ...) \
181     HILOG_DEBUG(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
182 #define APPSPAWN_LOGW(fmt, ...) \
183     HILOG_FATAL(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt,  (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__)
184 #endif
185 
186 #define APPSPAWN_CHECK(retCode, exper, fmt, ...) \
187     if (!(retCode)) {                    \
188         APPSPAWN_LOGE(fmt, ##__VA_ARGS__);         \
189         exper;                           \
190     }
191 
192 #define APPSPAWN_CHECK_ONLY_EXPER(retCode, exper) \
193     if (!(retCode)) {                  \
194         exper;                 \
195     }                         \
196 
197 #define APPSPAWN_ONLY_EXPER(retCode, exper) \
198     if ((retCode)) {                  \
199         exper;                 \
200     }
201 
202 #define APPSPAWN_CHECK_ONLY_LOG(retCode, fmt, ...) \
203     if (!(retCode)) {                    \
204         APPSPAWN_LOGE(fmt, ##__VA_ARGS__);      \
205     }
206 #ifdef __cplusplus
207 }
208 #endif  // __cplusplus
209 
210 #endif  // APPSPAWN_UTILS_H
211