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 typedef enum { 104 APPSPAWN_OK = 0, 105 APPSPAWN_SYSTEM_ERROR = 0xD000000, 106 APPSPAWN_ARG_INVALID, 107 APPSPAWN_MSG_INVALID, 108 APPSPAWN_MSG_TOO_LONG, 109 APPSPAWN_TLV_NOT_SUPPORT, 110 APPSPAWN_TLV_NONE, 111 APPSPAWN_SANDBOX_NONE, 112 APPSPAWN_SANDBOX_LOAD_FAIL, 113 APPSPAWN_SANDBOX_INVALID, 114 APPSPAWN_SANDBOX_MOUNT_FAIL, // 0xD00000a 115 APPSPAWN_SPAWN_TIMEOUT, // 0xD00000a 116 APPSPAWN_CHILD_CRASH, // 0xD00000b 117 APPSPAWN_NATIVE_NOT_SUPPORT, 118 APPSPAWN_ACCESS_TOKEN_INVALID, 119 APPSPAWN_PERMISSION_NOT_SUPPORT, 120 APPSPAWN_BUFFER_NOT_ENOUGH, 121 APPSPAWN_TIMEOUT, 122 APPSPAWN_FORK_FAIL, 123 APPSPAWN_DEBUG_MODE_NOT_SUPPORT, 124 APPSPAWN_ERROR_UTILS_MEM_FAIL, 125 APPSPAWN_ERROR_FILE_RMDIR_FAIL, 126 APPSPAWN_PIDMGR_DEFAULT_PID_MAX, 127 APPSPAWN_ERROR_UTILS_DECODE_JSON_FAIL, 128 APPSPAWN_ERROR_UTILS_CREATE_JSON_FAIL, 129 APPSPAWN_ERROR_UTILS_ADD_JSON_FAIL, 130 /* sandbox errno num */ 131 APPSPAWN_SANDBOX_ERROR_MKDIR_FAIL, 132 APPSPAWN_SANDBOX_ERROR_MOUNT_FAIL, 133 APPSPAWN_SANDBOX_ERROR_SET_PERMISSION_FLAG_FAIL, 134 APPSPAWN_NODE_EXIST, 135 /* devicedebug errno */ 136 APPSPAWN_DEVICEDEBUG_ERROR_APP_NOT_EXIST, 137 APPSPAWN_DEVICEDEBUG_ERROR_APP_NOT_DEBUGGABLE, 138 } AppSpawnErrorCode; 139 140 uint64_t DiffTime(const struct timespec *startTime, const struct timespec *endTime); 141 void AppSpawnDump(const char *fmt, ...); 142 void SetDumpToStream(FILE *stream); 143 typedef int (*SplitStringHandle)(const char *str, void *context); 144 int32_t StringSplit(const char *str, const char *separator, void *context, SplitStringHandle handle); 145 char *GetLastStr(const char *str, const char *dst); 146 uint32_t GetSpawnTimeout(uint32_t def); 147 void DumpCurrentDir(char *buffer, uint32_t bufferLen, const char *dirPath); 148 int IsDeveloperModeOpen(); 149 void InitCommonEnv(void); 150 int ConvertEnvValue(const char *srcEnv, char *dstEnv, int len); 151 152 int EnableNewNetNamespace(void); 153 154 #ifndef APP_FILE_NAME 155 #define APP_FILE_NAME (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__)) 156 #endif 157 158 #ifndef OHOS_LITE 159 #define APPSPAWN_DOMAIN (0xD002C00 + 0x11) // 0xD002C11 160 #ifndef APPSPAWN_LABEL 161 #define APPSPAWN_LABEL "APPSPAWN" 162 #endif 163 164 #undef LOG_TAG 165 #define LOG_TAG APPSPAWN_LABEL 166 #undef LOG_DOMAIN 167 #define LOG_DOMAIN APPSPAWN_DOMAIN 168 169 #define APPSPAWN_LOGI(fmt, ...) \ 170 HILOG_INFO(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 171 #define APPSPAWN_LOGE(fmt, ...) \ 172 HILOG_ERROR(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 173 #define APPSPAWN_LOGV(fmt, ...) \ 174 HILOG_DEBUG(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 175 #define APPSPAWN_LOGW(fmt, ...) \ 176 HILOG_WARN(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 177 #define APPSPAWN_LOGF(fmt, ...) \ 178 HILOG_FATAL(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 179 180 #define APPSPAWN_DUMP(fmt, ...) \ 181 do { \ 182 HILOG_INFO(LOG_CORE, fmt, ##__VA_ARGS__); \ 183 AppSpawnDump(fmt "\n", ##__VA_ARGS__); \ 184 } while (0) 185 186 #else 187 188 #define APPSPAWN_LOGI(fmt, ...) \ 189 HILOG_INFO(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 190 #define APPSPAWN_LOGE(fmt, ...) \ 191 HILOG_ERROR(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 192 #define APPSPAWN_LOGV(fmt, ...) \ 193 HILOG_DEBUG(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 194 #define APPSPAWN_LOGW(fmt, ...) \ 195 HILOG_FATAL(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 196 #endif 197 198 #define APPSPAWN_CHECK(retCode, exper, fmt, ...) \ 199 if (!(retCode)) { \ 200 APPSPAWN_LOGE(fmt, ##__VA_ARGS__); \ 201 exper; \ 202 } 203 204 #define APPSPAWN_CHECK_ONLY_EXPER(retCode, exper) \ 205 if (!(retCode)) { \ 206 exper; \ 207 } \ 208 209 #define APPSPAWN_ONLY_EXPER(retCode, exper) \ 210 if ((retCode)) { \ 211 exper; \ 212 } 213 214 #define APPSPAWN_CHECK_ONLY_LOG(retCode, fmt, ...) \ 215 if (!(retCode)) { \ 216 APPSPAWN_LOGE(fmt, ##__VA_ARGS__); \ 217 } 218 #ifdef __cplusplus 219 } 220 #endif // __cplusplus 221 222 #endif // APPSPAWN_UTILS_H 223