1 /* 2 * Copyright (c) 2021-2022 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_SERVER_H 17 #define APPSPAWN_SERVER_H 18 #include <stdint.h> 19 #include <stdio.h> 20 #include <string.h> 21 #include <unistd.h> 22 #include <time.h> 23 #include <stdbool.h> 24 #include "beget_ext.h" 25 #include "hilog/log.h" 26 #include "securec.h" 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 #define UNUSED(x) (void)(x) 32 #define APP_COLD_START 0x01 33 #define ERR_PIPE_FAIL (-100) 34 #define MAX_LEN_SHORT_NAME 16 35 #define WAIT_DELAY_US (100 * 1000) // 100ms 36 #define GID_FILE_ACCESS 1006 // only used for ExternalFileManager.hap 37 #define GID_USER_DATA_RW 1008 38 39 #define APPSPAWN_CHECK_EXIT "AppSpawnCheckUnexpectedExitCall" 40 41 typedef struct AppSpawnClient_ { 42 uint32_t id; 43 uint32_t flags; // Save negotiated flags 44 uint32_t cloneFlags; // for clone flags 45 } AppSpawnClient; 46 47 #define MAX_SOCKEYT_NAME_LEN 128 48 typedef struct AppSpawnContent_ { 49 char *longProcName; 50 uint32_t longProcNameLen; 51 bool isNweb; 52 53 // system 54 void (*loadExtendLib)(struct AppSpawnContent_ *content); 55 void (*initAppSpawn)(struct AppSpawnContent_ *content); 56 void (*runAppSpawn)(struct AppSpawnContent_ *content, int argc, char *const argv[]); 57 void (*setUidGidFilter)(struct AppSpawnContent_ *content); 58 59 // for child 60 void (*clearEnvironment)(struct AppSpawnContent_ *content, AppSpawnClient *client); 61 void (*initDebugParams)(struct AppSpawnContent_ *content, AppSpawnClient *client); 62 int (*setAppAccessToken)(struct AppSpawnContent_ *content, AppSpawnClient *client); 63 int (*setAppSandbox)(struct AppSpawnContent_ *content, AppSpawnClient *client); 64 int (*setKeepCapabilities)(struct AppSpawnContent_ *content, AppSpawnClient *client); 65 int (*setFileDescriptors)(struct AppSpawnContent_ *content, AppSpawnClient *client); 66 int (*setProcessName)(struct AppSpawnContent_ *content, AppSpawnClient *client, 67 char *longProcName, uint32_t longProcNameLen); 68 int (*setUidGid)(struct AppSpawnContent_ *content, AppSpawnClient *client); 69 int (*setCapabilities)(struct AppSpawnContent_ *content, AppSpawnClient *client); 70 int (*setXpmRegion)(struct AppSpawnContent_ *content); 71 72 void (*notifyResToParent)(struct AppSpawnContent_ *content, AppSpawnClient *client, int result); 73 void (*runChildProcessor)(struct AppSpawnContent_ *content, AppSpawnClient *client); 74 int (*setAsanEnabledEnv)(struct AppSpawnContent_ *content, AppSpawnClient *client); 75 // for cold start 76 int (*coldStartApp)(struct AppSpawnContent_ *content, AppSpawnClient *client); 77 int (*getWrapBundleNameValue)(struct AppSpawnContent_ *content, AppSpawnClient *client); 78 int (*setSeccompFilter)(struct AppSpawnContent_ *content, AppSpawnClient *client); 79 void (*handleInternetPermission)(const AppSpawnClient *client); 80 int (*waitForDebugger)(AppSpawnClient *client); 81 } AppSpawnContent; 82 83 typedef struct { 84 struct AppSpawnContent_ *content; 85 AppSpawnClient *client; 86 } AppSandboxArg; 87 88 AppSpawnContent *AppSpawnCreateContent(const char *socketName, char *longProcName, uint32_t longProcNameLen, int cold); 89 int AppSpawnProcessMsg(AppSandboxArg *sandbox, pid_t *childPid); 90 int DoStartApp(struct AppSpawnContent_ *content, AppSpawnClient *client, char *longProcName, uint32_t longProcNameLen); 91 long long DiffTime(struct timespec *startTime); 92 pid_t AppSpawnFork(int (*childFunc)(void *arg), void *args); 93 #define UNUSED(x) (void)(x) 94 95 #ifndef OHOS_LITE 96 #define APPSPAWN_LOG(logLevel, domain, tag, fmt, ...) \ 97 HiLogPrint(LOG_CORE, (LogLevel)logLevel, domain, tag, \ 98 "[%{public}s:%{public}d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 99 100 #ifndef APPSPAWN_LABEL 101 #define APPSPAWN_LABEL "APPSPAWN" 102 #endif 103 #define APPSPAWN_DOMAIN (BASE_DOMAIN + 0x11) 104 105 #define APPSPAWN_LOGI(fmt, ...) \ 106 APPSPAWN_LOG(LOG_INFO, APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) 107 #define APPSPAWN_LOGE(fmt, ...) \ 108 APPSPAWN_LOG(LOG_ERROR, APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) 109 #define APPSPAWN_LOGV(fmt, ...) \ 110 APPSPAWN_LOG(LOG_DEBUG, APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) 111 #define APPSPAWN_LOGW(fmt, ...) \ 112 APPSPAWN_LOG(LOG_WARN, APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) 113 #define APPSPAWN_LOGF(fmt, ...) \ 114 APPSPAWN_LOG(LOG_FATAL, APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) 115 116 #else 117 118 #define APPSPAWN_LOGI(fmt, ...) \ 119 HILOG_INFO(HILOG_MODULE_HIVIEW, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 120 #define APPSPAWN_LOGE(fmt, ...) \ 121 HILOG_ERROR(HILOG_MODULE_HIVIEW, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 122 #define APPSPAWN_LOGV(fmt, ...) \ 123 HILOG_DEBUG(HILOG_MODULE_HIVIEW, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 124 #define APPSPAWN_LOGW(fmt, ...) \ 125 HILOG_FATAL(HILOG_MODULE_HIVIEW, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 126 #endif 127 128 #define APPSPAWN_CHECK(retCode, exper, fmt, ...) \ 129 if (!(retCode)) { \ 130 APPSPAWN_LOGE(fmt, ##__VA_ARGS__); \ 131 exper; \ 132 } 133 134 #define APPSPAWN_CHECK_ONLY_EXPER(retCode, exper) \ 135 if (!(retCode)) { \ 136 exper; \ 137 } \ 138 139 #define APPSPAWN_CHECK_ONLY_LOG(retCode, fmt, ...) \ 140 if (!(retCode)) { \ 141 APPSPAWN_LOGE(fmt, ##__VA_ARGS__); \ 142 } 143 144 #ifdef __cplusplus 145 } 146 #endif 147 #endif // APPSPAWN_SERVER_H 148