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
24extern "C" {
25#endif
26
27typedef 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
37typedef 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
47typedef enum {
48    CJPROCESS_FOR_APP_SPAWN,
49    CJPROCESS_FOR_APP_COLD_RUN,
50    CJPROCESS_INVALID
51} CJRunProcess;
52
53typedef struct AppSpawnClient {
54    uint32_t id;
55    uint32_t flags;  // Save negotiated flags
56} AppSpawnClient;
57
58typedef struct AppSpawnContent {
59    char *longProcName;
60    uint32_t longProcNameLen;
61    uint32_t sandboxNsFlags;
62    int wdgOpened;
63#ifdef USE_ENCAPS
64    int fdEncaps;
65#endif
66    RunMode mode;
67#ifndef OHOS_LITE
68    int32_t preforkFd[2];
69    int32_t parentToChildFd[2];
70    char *propertyBuffer;
71    pid_t reservedPid;
72    int enablePerfork;
73#endif
74    // system
75    void (*runAppSpawn)(struct AppSpawnContent *content, int argc, char *const argv[]);
76    void (*notifyResToParent)(struct AppSpawnContent *content, AppSpawnClient *client, int result);
77    int (*runChildProcessor)(struct AppSpawnContent *content, AppSpawnClient *client);
78    // for cold start
79    int (*coldStartApp)(struct AppSpawnContent *content, AppSpawnClient *client);
80} AppSpawnContent;
81
82typedef struct TagAppSpawnForkArg {
83    struct AppSpawnContent *content;
84    AppSpawnClient *client;
85} AppSpawnForkArg;
86
87AppSpawnContent *AppSpawnCreateContent(const char *socketName, char *longProcName, uint32_t longProcNameLen, int cold);
88int AppSpawnExecuteClearEnvHook(AppSpawnContent *content, AppSpawnClient *client);
89int AppSpawnExecuteSpawningHook(AppSpawnContent *content, AppSpawnClient *client);
90int AppSpawnExecutePreReplyHook(AppSpawnContent *content, AppSpawnClient *client);
91int AppSpawnExecutePostReplyHook(AppSpawnContent *content, AppSpawnClient *client);
92void AppSpawnEnvClear(AppSpawnContent *content, AppSpawnClient *client);
93int AppSpawnProcessMsg(AppSpawnContent *content, AppSpawnClient *client, pid_t *childPid);
94void ProcessExit(int code);
95int AppSpawnChild(AppSpawnContent *content, AppSpawnClient *client);
96#ifdef __cplusplus
97}
98#endif
99#endif  // APPSPAWN_SERVER_H
100