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 uint32_t sandboxNsFlags; 52 bool isNweb; 53 54 // system 55 void (*loadExtendLib)(struct AppSpawnContent_ *content); 56 int (*initAppSpawn)(struct AppSpawnContent_ *content); 57 void (*runAppSpawn)(struct AppSpawnContent_ *content, int argc, char *const argv[]); 58 void (*setUidGidFilter)(struct AppSpawnContent_ *content); 59 60 // for child 61 void (*clearEnvironment)(struct AppSpawnContent_ *content, AppSpawnClient *client); 62 void (*initDebugParams)(struct AppSpawnContent_ *content, AppSpawnClient *client); 63 int (*setAppAccessToken)(struct AppSpawnContent_ *content, AppSpawnClient *client); 64 int (*setAppSandbox)(struct AppSpawnContent_ *content, AppSpawnClient *client); 65 int (*setKeepCapabilities)(struct AppSpawnContent_ *content, AppSpawnClient *client); 66 int (*setFileDescriptors)(struct AppSpawnContent_ *content, AppSpawnClient *client); 67 int (*setProcessName)(struct AppSpawnContent_ *content, AppSpawnClient *client, 68 char *longProcName, uint32_t longProcNameLen); 69 int (*setUidGid)(struct AppSpawnContent_ *content, AppSpawnClient *client); 70 int (*setCapabilities)(struct AppSpawnContent_ *content, AppSpawnClient *client); 71 int (*setXpmConfig)(struct AppSpawnContent_ *content, AppSpawnClient *client); 72 73 void (*notifyResToParent)(struct AppSpawnContent_ *content, AppSpawnClient *client, int result); 74 void (*runChildProcessor)(struct AppSpawnContent_ *content, AppSpawnClient *client); 75 int (*setAsanEnabledEnv)(struct AppSpawnContent_ *content, AppSpawnClient *client); 76 77 // for pid namespace 78 int (*enablePidNs)(struct AppSpawnContent_ *content); 79 80 // for cold start 81 int (*coldStartApp)(struct AppSpawnContent_ *content, AppSpawnClient *client); 82 int (*getWrapBundleNameValue)(struct AppSpawnContent_ *content, AppSpawnClient *client); 83 int (*setSeccompFilter)(struct AppSpawnContent_ *content, AppSpawnClient *client); 84 void (*handleInternetPermission)(const AppSpawnClient *client); 85 int (*waitForDebugger)(AppSpawnClient *client); 86 } AppSpawnContent; 87 88 typedef struct { 89 struct AppSpawnContent_ *content; 90 AppSpawnClient *client; 91 } AppSandboxArg; 92 93 AppSpawnContent *AppSpawnCreateContent(const char *socketName, char *longProcName, uint32_t longProcNameLen, int cold); 94 int AppSpawnProcessMsg(AppSandboxArg *sandbox, pid_t *childPid); 95 int DoStartApp(struct AppSpawnContent_ *content, AppSpawnClient *client, char *longProcName, uint32_t longProcNameLen); 96 long long DiffTime(struct timespec *startTime); 97 pid_t AppSpawnFork(int (*childFunc)(void *arg), void *args); 98 #define UNUSED(x) (void)(x) 99 100 #ifndef OHOS_LITE 101 #define APPSPAWN_LOG(logLevel, domain, tag, fmt, ...) \ 102 HiLogPrint(LOG_CORE, (LogLevel)logLevel, domain, tag, \ 103 "[%{public}s:%{public}d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 104 105 #ifndef APPSPAWN_LABEL 106 #define APPSPAWN_LABEL "APPSPAWN" 107 #endif 108 #define APPSPAWN_DOMAIN (BASE_DOMAIN + 0x11) 109 110 #define APPSPAWN_LOGI(fmt, ...) \ 111 APPSPAWN_LOG(LOG_INFO, APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) 112 #define APPSPAWN_LOGE(fmt, ...) \ 113 APPSPAWN_LOG(LOG_ERROR, APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) 114 #define APPSPAWN_LOGV(fmt, ...) \ 115 APPSPAWN_LOG(LOG_DEBUG, APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) 116 #define APPSPAWN_LOGW(fmt, ...) \ 117 APPSPAWN_LOG(LOG_WARN, APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) 118 #define APPSPAWN_LOGF(fmt, ...) \ 119 APPSPAWN_LOG(LOG_FATAL, APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__) 120 121 #else 122 123 #define APPSPAWN_LOGI(fmt, ...) \ 124 HILOG_INFO(HILOG_MODULE_HIVIEW, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 125 #define APPSPAWN_LOGE(fmt, ...) \ 126 HILOG_ERROR(HILOG_MODULE_HIVIEW, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 127 #define APPSPAWN_LOGV(fmt, ...) \ 128 HILOG_DEBUG(HILOG_MODULE_HIVIEW, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 129 #define APPSPAWN_LOGW(fmt, ...) \ 130 HILOG_FATAL(HILOG_MODULE_HIVIEW, "[%s:%d]" fmt, (FILE_NAME), (__LINE__), ##__VA_ARGS__) 131 #endif 132 133 #define APPSPAWN_CHECK(retCode, exper, fmt, ...) \ 134 if (!(retCode)) { \ 135 APPSPAWN_LOGE(fmt, ##__VA_ARGS__); \ 136 exper; \ 137 } 138 139 #define APPSPAWN_CHECK_ONLY_EXPER(retCode, exper) \ 140 if (!(retCode)) { \ 141 exper; \ 142 } \ 143 144 #define APPSPAWN_CHECK_ONLY_LOG(retCode, fmt, ...) \ 145 if (!(retCode)) { \ 146 APPSPAWN_LOGE(fmt, ##__VA_ARGS__); \ 147 } 148 149 #ifdef __cplusplus 150 } 151 #endif 152 #endif // APPSPAWN_SERVER_H 153