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