169570cc8Sopenharmony_ci/*
269570cc8Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
369570cc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
469570cc8Sopenharmony_ci * you may not use this file except in compliance with the License.
569570cc8Sopenharmony_ci * You may obtain a copy of the License at
669570cc8Sopenharmony_ci *
769570cc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
869570cc8Sopenharmony_ci *
969570cc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1069570cc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1169570cc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1269570cc8Sopenharmony_ci * See the License for the specific language governing permissions and
1369570cc8Sopenharmony_ci * limitations under the License.
1469570cc8Sopenharmony_ci */
1569570cc8Sopenharmony_ci
1669570cc8Sopenharmony_ci#ifndef APPSPAWN_SERVER_H
1769570cc8Sopenharmony_ci#define APPSPAWN_SERVER_H
1869570cc8Sopenharmony_ci#include <stdint.h>
1969570cc8Sopenharmony_ci#include <stdio.h>
2069570cc8Sopenharmony_ci#include <unistd.h>
2169570cc8Sopenharmony_ci
2269570cc8Sopenharmony_ci#include "appspawn_utils.h"
2369570cc8Sopenharmony_ci#ifdef __cplusplus
2469570cc8Sopenharmony_ciextern "C" {
2569570cc8Sopenharmony_ci#endif
2669570cc8Sopenharmony_ci
2769570cc8Sopenharmony_citypedef enum {
2869570cc8Sopenharmony_ci    MODE_FOR_APP_SPAWN,
2969570cc8Sopenharmony_ci    MODE_FOR_NWEB_SPAWN,
3069570cc8Sopenharmony_ci    MODE_FOR_APP_COLD_RUN,
3169570cc8Sopenharmony_ci    MODE_FOR_NWEB_COLD_RUN,
3269570cc8Sopenharmony_ci    MODE_FOR_NATIVE_SPAWN,
3369570cc8Sopenharmony_ci    MODE_FOR_CJAPP_SPAWN,
3469570cc8Sopenharmony_ci    MODE_INVALID
3569570cc8Sopenharmony_ci} RunMode;
3669570cc8Sopenharmony_ci
3769570cc8Sopenharmony_citypedef enum {
3869570cc8Sopenharmony_ci    PROCESS_FOR_APP_SPAWN,
3969570cc8Sopenharmony_ci    PROCESS_FOR_NWEB_SPAWN,
4069570cc8Sopenharmony_ci    PROCESS_FOR_APP_COLD_RUN,
4169570cc8Sopenharmony_ci    PROCESS_FOR_NWEB_COLD_RUN,
4269570cc8Sopenharmony_ci    PROCESS_FOR_NATIVE_SPAWN,
4369570cc8Sopenharmony_ci    PROCESS_FOR_NWEB_RESTART,
4469570cc8Sopenharmony_ci    PROCESS_INVALID
4569570cc8Sopenharmony_ci} RunProcess;
4669570cc8Sopenharmony_ci
4769570cc8Sopenharmony_citypedef enum {
4869570cc8Sopenharmony_ci    CJPROCESS_FOR_APP_SPAWN,
4969570cc8Sopenharmony_ci    CJPROCESS_FOR_APP_COLD_RUN,
5069570cc8Sopenharmony_ci    CJPROCESS_INVALID
5169570cc8Sopenharmony_ci} CJRunProcess;
5269570cc8Sopenharmony_ci
5369570cc8Sopenharmony_citypedef struct AppSpawnClient {
5469570cc8Sopenharmony_ci    uint32_t id;
5569570cc8Sopenharmony_ci    uint32_t flags;  // Save negotiated flags
5669570cc8Sopenharmony_ci} AppSpawnClient;
5769570cc8Sopenharmony_ci
5869570cc8Sopenharmony_citypedef struct AppSpawnContent {
5969570cc8Sopenharmony_ci    char *longProcName;
6069570cc8Sopenharmony_ci    uint32_t longProcNameLen;
6169570cc8Sopenharmony_ci    uint32_t sandboxNsFlags;
6269570cc8Sopenharmony_ci    int wdgOpened;
6369570cc8Sopenharmony_ci#ifdef USE_ENCAPS
6469570cc8Sopenharmony_ci    int fdEncaps;
6569570cc8Sopenharmony_ci#endif
6669570cc8Sopenharmony_ci    RunMode mode;
6769570cc8Sopenharmony_ci#ifndef OHOS_LITE
6869570cc8Sopenharmony_ci    int32_t preforkFd[2];
6969570cc8Sopenharmony_ci    int32_t parentToChildFd[2];
7069570cc8Sopenharmony_ci    char *propertyBuffer;
7169570cc8Sopenharmony_ci    pid_t reservedPid;
7269570cc8Sopenharmony_ci    int enablePerfork;
7369570cc8Sopenharmony_ci#endif
7469570cc8Sopenharmony_ci    // system
7569570cc8Sopenharmony_ci    void (*runAppSpawn)(struct AppSpawnContent *content, int argc, char *const argv[]);
7669570cc8Sopenharmony_ci    void (*notifyResToParent)(struct AppSpawnContent *content, AppSpawnClient *client, int result);
7769570cc8Sopenharmony_ci    int (*runChildProcessor)(struct AppSpawnContent *content, AppSpawnClient *client);
7869570cc8Sopenharmony_ci    // for cold start
7969570cc8Sopenharmony_ci    int (*coldStartApp)(struct AppSpawnContent *content, AppSpawnClient *client);
8069570cc8Sopenharmony_ci} AppSpawnContent;
8169570cc8Sopenharmony_ci
8269570cc8Sopenharmony_citypedef struct TagAppSpawnForkArg {
8369570cc8Sopenharmony_ci    struct AppSpawnContent *content;
8469570cc8Sopenharmony_ci    AppSpawnClient *client;
8569570cc8Sopenharmony_ci} AppSpawnForkArg;
8669570cc8Sopenharmony_ci
8769570cc8Sopenharmony_ciAppSpawnContent *AppSpawnCreateContent(const char *socketName, char *longProcName, uint32_t longProcNameLen, int cold);
8869570cc8Sopenharmony_ciint AppSpawnExecuteClearEnvHook(AppSpawnContent *content, AppSpawnClient *client);
8969570cc8Sopenharmony_ciint AppSpawnExecuteSpawningHook(AppSpawnContent *content, AppSpawnClient *client);
9069570cc8Sopenharmony_ciint AppSpawnExecutePreReplyHook(AppSpawnContent *content, AppSpawnClient *client);
9169570cc8Sopenharmony_ciint AppSpawnExecutePostReplyHook(AppSpawnContent *content, AppSpawnClient *client);
9269570cc8Sopenharmony_civoid AppSpawnEnvClear(AppSpawnContent *content, AppSpawnClient *client);
9369570cc8Sopenharmony_ciint AppSpawnProcessMsg(AppSpawnContent *content, AppSpawnClient *client, pid_t *childPid);
9469570cc8Sopenharmony_civoid ProcessExit(int code);
9569570cc8Sopenharmony_ciint AppSpawnChild(AppSpawnContent *content, AppSpawnClient *client);
9669570cc8Sopenharmony_ci#ifdef __cplusplus
9769570cc8Sopenharmony_ci}
9869570cc8Sopenharmony_ci#endif
9969570cc8Sopenharmony_ci#endif  // APPSPAWN_SERVER_H
100