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_SERVER_H
17 #define APPSPAWN_SERVER_H
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 
22 #include "appspawn_utils.h"
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 typedef enum {
28     MODE_FOR_APP_SPAWN,
29     MODE_FOR_NWEB_SPAWN,
30     MODE_FOR_APP_COLD_RUN,
31     MODE_FOR_NWEB_COLD_RUN,
32     MODE_FOR_NATIVE_SPAWN,
33     MODE_FOR_CJAPP_SPAWN,
34     MODE_INVALID
35 } RunMode;
36 
37 typedef enum {
38     PROCESS_FOR_APP_SPAWN,
39     PROCESS_FOR_NWEB_SPAWN,
40     PROCESS_FOR_APP_COLD_RUN,
41     PROCESS_FOR_NWEB_COLD_RUN,
42     PROCESS_FOR_NATIVE_SPAWN,
43     PROCESS_FOR_NWEB_RESTART,
44     PROCESS_INVALID
45 } RunProcess;
46 
47 typedef enum {
48     CJPROCESS_FOR_APP_SPAWN,
49     CJPROCESS_FOR_APP_COLD_RUN,
50     CJPROCESS_INVALID
51 } CJRunProcess;
52 
53 typedef struct AppSpawnClient {
54     uint32_t id;
55     uint32_t flags;  // Save negotiated flags
56 } AppSpawnClient;
57 
58 typedef struct AppSpawnContent {
59     char *longProcName;
60     uint32_t longProcNameLen;
61     uint32_t sandboxNsFlags;
62     int wdgOpened;
63     RunMode mode;
64 #ifndef OHOS_LITE
65     int32_t preforkFd[2];
66     int32_t parentToChildFd[2];
67     char *propertyBuffer;
68     pid_t reservedPid;
69     int enablePerfork;
70 #endif
71     // system
72     void (*runAppSpawn)(struct AppSpawnContent *content, int argc, char *const argv[]);
73     void (*notifyResToParent)(struct AppSpawnContent *content, AppSpawnClient *client, int result);
74     int (*runChildProcessor)(struct AppSpawnContent *content, AppSpawnClient *client);
75     // for cold start
76     int (*coldStartApp)(struct AppSpawnContent *content, AppSpawnClient *client);
77 } AppSpawnContent;
78 
79 typedef struct TagAppSpawnForkArg {
80     struct AppSpawnContent *content;
81     AppSpawnClient *client;
82 } AppSpawnForkArg;
83 
84 AppSpawnContent *AppSpawnCreateContent(const char *socketName, char *longProcName, uint32_t longProcNameLen, int cold);
85 int AppSpawnExecuteClearEnvHook(AppSpawnContent *content, AppSpawnClient *client);
86 int AppSpawnExecuteSpawningHook(AppSpawnContent *content, AppSpawnClient *client);
87 int AppSpawnExecutePreReplyHook(AppSpawnContent *content, AppSpawnClient *client);
88 int AppSpawnExecutePostReplyHook(AppSpawnContent *content, AppSpawnClient *client);
89 void AppSpawnEnvClear(AppSpawnContent *content, AppSpawnClient *client);
90 int AppSpawnProcessMsg(AppSpawnContent *content, AppSpawnClient *client, pid_t *childPid);
91 void ProcessExit(int code);
92 int AppSpawnChild(AppSpawnContent *content, AppSpawnClient *client);
93 #ifdef __cplusplus
94 }
95 #endif
96 #endif  // APPSPAWN_SERVER_H
97