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 APPSPAWN_CHECK_EXIT "AppSpawnCheckUnexpectedExitCall" 55 #define UNUSED(x) (void)(x) 56 57 #define APP_COLD_START 0x01 58 #define APP_ASAN_DETECTOR 0x02 59 #define APP_DEVELOPER_MODE 0x04 60 #define APP_BEGETCTL_BOOT 0x400 61 62 #define MAX_LEN_SHORT_NAME 16 63 #define DEFAULT_UMASK 0002 64 #define UID_BASE 200000 // 20010029 65 #define DEFAULT_DIR_MODE 0711 66 #define USER_ID_BUFFER_SIZE 32 67 68 #define APPSPAWN_SEC_TO_NSEC 1000000000 69 #define APPSPAWN_MSEC_TO_NSEC 1000000 70 #define APPSPAWN_USEC_TO_NSEC 1000 71 #define APPSPAWN_SEC_TO_MSEC 1000 72 73 #define CHECK_FLAGS_BY_INDEX(flags, index) ((((flags) >> (index)) & 0x1) == 0x1) 74 #ifndef ARRAY_LENGTH 75 #define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0])) 76 #endif 77 78 #define INVALID_PERMISSION_INDEX (-1) 79 80 #define MAX_ENV_VALUE_LEN 1024 81 82 typedef struct TagAppSpawnCommonEnv { 83 const char *envName; 84 const char *envValue; 85 int developerModeEnable; 86 } AppSpawnCommonEnv; 87 88 typedef enum { 89 APPSPAWN_OK = 0, 90 APPSPAWN_SYSTEM_ERROR = 0xD000000, 91 APPSPAWN_ARG_INVALID, 92 APPSPAWN_MSG_INVALID, 93 APPSPAWN_MSG_TOO_LONG, 94 APPSPAWN_TLV_NOT_SUPPORT, 95 APPSPAWN_TLV_NONE, 96 APPSPAWN_SANDBOX_NONE, 97 APPSPAWN_SANDBOX_LOAD_FAIL, 98 APPSPAWN_SANDBOX_INVALID, 99 APPSPAWN_SANDBOX_MOUNT_FAIL, // 0xD00000a 100 APPSPAWN_SPAWN_TIMEOUT, // 0xD00000a 101 APPSPAWN_CHILD_CRASH, // 0xD00000b 102 APPSPAWN_NATIVE_NOT_SUPPORT, 103 APPSPAWN_ACCESS_TOKEN_INVALID, 104 APPSPAWN_PERMISSION_NOT_SUPPORT, 105 APPSPAWN_BUFFER_NOT_ENOUGH, 106 APPSPAWN_TIMEOUT, 107 APPSPAWN_FORK_FAIL, 108 APPSPAWN_DEBUG_MODE_NOT_SUPPORT, 109 APPSPAWN_NODE_EXIST, 110 } AppSpawnErrorCode; 111 112 uint64_t DiffTime(const struct timespec *startTime, const struct timespec *endTime); 113 void AppSpawnDump(const char *fmt, ...); 114 void SetDumpToStream(FILE *stream); 115 typedef int (*SplitStringHandle)(const char *str, void *context); 116 int32_t StringSplit(const char *str, const char *separator, void *context, SplitStringHandle handle); 117 char *GetLastStr(const char *str, const char *dst); 118 uint32_t GetSpawnTimeout(uint32_t def); 119 void DumpCurrentDir(char *buffer, uint32_t bufferLen, const char *dirPath); 120 int IsDeveloperModeOpen(); 121 void InitCommonEnv(void); 122 int ConvertEnvValue(const char *srcEnv, char *dstEnv, int len); 123 124 #ifndef APP_FILE_NAME 125 #define APP_FILE_NAME (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__)) 126 #endif 127 128 #ifndef OHOS_LITE 129 #define APPSPAWN_DOMAIN (0xD002C00 + 0x11) 130 #ifndef APPSPAWN_LABEL 131 #define APPSPAWN_LABEL "APPSPAWN" 132 #endif 133 134 #undef LOG_TAG 135 #define LOG_TAG APPSPAWN_LABEL 136 #undef LOG_DOMAIN 137 #define LOG_DOMAIN APPSPAWN_DOMAIN 138 139 #define APPSPAWN_LOGI(fmt, ...) \ 140 HILOG_INFO(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 141 #define APPSPAWN_LOGE(fmt, ...) \ 142 HILOG_ERROR(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 143 #define APPSPAWN_LOGV(fmt, ...) \ 144 HILOG_DEBUG(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 145 #define APPSPAWN_LOGW(fmt, ...) \ 146 HILOG_WARN(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 147 #define APPSPAWN_LOGF(fmt, ...) \ 148 HILOG_FATAL(LOG_CORE, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 149 150 #define APPSPAPWN_DUMP(fmt, ...) \ 151 do { \ 152 HILOG_INFO(LOG_CORE, fmt, ##__VA_ARGS__); \ 153 AppSpawnDump(fmt "\n", ##__VA_ARGS__); \ 154 } while (0) 155 156 #else 157 158 #define APPSPAWN_LOGI(fmt, ...) \ 159 HILOG_INFO(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 160 #define APPSPAWN_LOGE(fmt, ...) \ 161 HILOG_ERROR(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 162 #define APPSPAWN_LOGV(fmt, ...) \ 163 HILOG_DEBUG(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 164 #define APPSPAWN_LOGW(fmt, ...) \ 165 HILOG_FATAL(HILOG_MODULE_HIVIEW, "[%{public}s:%{public}d]" fmt, (APP_FILE_NAME), (__LINE__), ##__VA_ARGS__) 166 #endif 167 168 #define APPSPAWN_CHECK(retCode, exper, fmt, ...) \ 169 if (!(retCode)) { \ 170 APPSPAWN_LOGE(fmt, ##__VA_ARGS__); \ 171 exper; \ 172 } 173 174 #define APPSPAWN_CHECK_ONLY_EXPER(retCode, exper) \ 175 if (!(retCode)) { \ 176 exper; \ 177 } \ 178 179 #define APPSPAWN_ONLY_EXPER(retCode, exper) \ 180 if ((retCode)) { \ 181 exper; \ 182 } 183 184 #define APPSPAWN_CHECK_ONLY_LOG(retCode, fmt, ...) \ 185 if (!(retCode)) { \ 186 APPSPAWN_LOGE(fmt, ##__VA_ARGS__); \ 187 } 188 #ifdef __cplusplus 189 } 190 #endif // __cplusplus 191 192 #endif // APPSPAWN_UTILS_H 193