• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "beget_ext.h"
19 
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #ifdef OHOS_DEBUG
25 #include <time.h>
26 #endif  // OHOS_DEBUG
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #define UNUSED(x) (void)(x)
33 #define APP_COLD_START 0x01
34 #define ERR_PIPE_FAIL (-100)
35 #define MAX_LEN_SHORT_NAME 16
36 #define WAIT_DELAY_US (100 * 1000)  // 100ms
37 #define GID_FILE_ACCESS 1006  // only used for ExternalFileManager.hap
38 #define GID_USER_DATA_RW 1008
39 
40 typedef struct AppSpawnClient_ {
41     uint32_t id;
42     uint32_t flags;
43     uint32_t cloneFlags;
44 #ifndef APPSPAWN_TEST
45 #ifndef OHOS_LITE
46     uint8_t setAllowInternet;
47     uint8_t allowInternet;
48     uint8_t reserved1;
49     uint8_t reserved2;
50 #endif
51 #endif
52 } AppSpawnClient;
53 
54 #define MAX_SOCKEYT_NAME_LEN 128
55 typedef struct AppSpawnContent_ {
56     char *longProcName;
57     uint32_t longProcNameLen;
58 
59     // system
60     void (*loadExtendLib)(struct AppSpawnContent_ *content);
61     void (*initAppSpawn)(struct AppSpawnContent_ *content);
62     void (*runAppSpawn)(struct AppSpawnContent_ *content, int argc, char *const argv[]);
63     void (*setUidGidFilter)(struct AppSpawnContent_ *content);
64 
65     // for child
66     void (*clearEnvironment)(struct AppSpawnContent_ *content, AppSpawnClient *client);
67     void (*initDebugParams)(struct AppSpawnContent_ *content, AppSpawnClient *client);
68     void (*setAppAccessToken)(struct AppSpawnContent_ *content, AppSpawnClient *client);
69     int (*setAppSandbox)(struct AppSpawnContent_ *content, AppSpawnClient *client);
70     int (*setKeepCapabilities)(struct AppSpawnContent_ *content, AppSpawnClient *client);
71     int (*setFileDescriptors)(struct AppSpawnContent_ *content, AppSpawnClient *client);
72     int (*setProcessName)(struct AppSpawnContent_ *content, AppSpawnClient *client,
73         char *longProcName, uint32_t longProcNameLen);
74     int (*setUidGid)(struct AppSpawnContent_ *content, AppSpawnClient *client);
75     int (*setCapabilities)(struct AppSpawnContent_ *content, AppSpawnClient *client);
76 
77     void (*notifyResToParent)(struct AppSpawnContent_ *content, AppSpawnClient *client, int result);
78     void (*runChildProcessor)(struct AppSpawnContent_ *content, AppSpawnClient *client);
79     int (*setAsanEnabledEnv)(struct AppSpawnContent_ *content, AppSpawnClient *client);
80     // for cold start
81     int (*coldStartApp)(struct AppSpawnContent_ *content, AppSpawnClient *client);
82 #ifdef ASAN_DETECTOR
83     int (*getWrapBundleNameValue)(struct AppSpawnContent_ *content, AppSpawnClient *client);
84 #endif
85     void (*setSeccompFilter)(struct AppSpawnContent_ *content, 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 int AppSpawnChild(void *arg);
97 
98 #ifdef OHOS_DEBUG
99 void GetCurTime(struct timespec* tmCur);
100 #endif
101 
102 typedef enum {
103     DEBUG = 0,
104     INFO,
105     WARN,
106     ERROR,
107     FATAL,
108 } AppspawnLogLevel;
109 
110 void AppspawnLogPrint(AppspawnLogLevel logLevel, const char *file, int line, const char *fmt, ...);
111 
112 #ifndef FILE_NAME
113 #define FILE_NAME   (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__))
114 #endif
115 
116 #define UNUSED(x) (void)(x)
117 
118 #ifndef APPSPAWN_LABEL
119 #define APPSPAWN_LABEL "APPSPAWN"
120 #endif
121 #define APPSPAWN_DOMAIN (BASE_DOMAIN + 0x11)
122 
123 #define APPSPAWN_LOGI(fmt, ...) STARTUP_LOGI(APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__)
124 #define APPSPAWN_LOGE(fmt, ...) STARTUP_LOGE(APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__)
125 #define APPSPAWN_LOGV(fmt, ...) STARTUP_LOGV(APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__)
126 #define APPSPAWN_LOGW(fmt, ...) STARTUP_LOGW(APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__)
127 #define APPSPAWN_LOGF(fmt, ...) STARTUP_LOGF(APPSPAWN_DOMAIN, APPSPAWN_LABEL, fmt, ##__VA_ARGS__)
128 
129 #define APPSPAWN_CHECK(retCode, exper, ...) \
130     if (!(retCode)) {                    \
131         APPSPAWN_LOGE(__VA_ARGS__);         \
132         exper;                           \
133     }
134 
135 #define APPSPAWN_CHECK_ONLY_EXPER(retCode, exper) \
136     if (!(retCode)) {                  \
137         exper;                 \
138     }                         \
139 
140 #define APPSPAWN_CHECK_ONLY_LOG(retCode, ...) \
141     if (!(retCode)) {                    \
142         APPSPAWN_LOGE(__VA_ARGS__);      \
143     }
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 #endif  // APPSPAWN_SERVER_H
149