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_TEST_STUB_H
17#define APPSPAWN_TEST_STUB_H
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <stdbool.h>
22#include <sys/signalfd.h>
23#include <sys/socket.h>
24#include <sys/wait.h>
25
26#include "cJSON.h"
27#include "appspawn_client.h"
28#include "appspawn_hook.h"
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34typedef struct AppSpawnContent AppSpawnContent;
35typedef struct AppSpawnClient AppSpawnClient;
36typedef struct TagAppSpawnReqMsgNode AppSpawnReqMsgNode;
37typedef void *AppSpawnClientHandle;
38typedef struct TagAppSpawnReqMsgMgr AppSpawnReqMsgMgr;
39typedef struct TagAppSpawningCtx AppSpawningCtx;
40typedef struct TagAppSpawnMsg AppSpawnMsg;
41typedef struct TagAppSpawnSandboxCfg  AppSpawnSandboxCfg;
42typedef struct TagAppSpawnExtData AppSpawnExtData;
43typedef struct TagSandboxContext SandboxContext;
44typedef struct TagAppSpawnedProcess AppSpawnedProcess;
45typedef struct TagAppSpawnForkArg AppSpawnForkArg;
46typedef struct TagAppSpawnMsgNode AppSpawnMsgNode;
47typedef struct TagAppSpawnMgr AppSpawnMgr;
48typedef struct TagPathMountNode PathMountNode;
49typedef struct TagMountArg MountArg;
50typedef struct TagVarExtraData VarExtraData;
51typedef struct TagSandboxSection SandboxSection;
52typedef struct TagAppSpawnNamespace AppSpawnNamespace;
53typedef struct TagAppSpawnedProcess AppSpawnedProcessInfo;
54
55AppSpawnNamespace *GetAppSpawnNamespace(const AppSpawnMgr *content);
56void DeleteAppSpawnNamespace(AppSpawnNamespace *ns);
57void FreeAppSpawnNamespace(struct TagAppSpawnExtData *data);
58int PreForkSetPidNamespace(AppSpawnMgr *content, AppSpawningCtx *property);
59int PostForkSetPidNamespace(AppSpawnMgr *content, AppSpawningCtx *property);
60int ProcessMgrRemoveApp(const AppSpawnMgr *content, const AppSpawnedProcessInfo *appInfo);
61int ProcessMgrAddApp(const AppSpawnMgr *content, const AppSpawnedProcessInfo *appInfo);
62void TryCreateSocket(AppSpawnReqMsgMgr *reqMgr);
63
64int MountAllGroup(const SandboxContext *context, const cJSON *groups);
65int MountAllHsp(const SandboxContext *context, const cJSON *hsps);
66
67void CheckAndCreateSandboxFile(const char *file);
68int VarPackageNameReplace(const SandboxContext *context,
69    const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData);
70int ReplaceVariableForDepSandboxPath(const SandboxContext *context,
71    const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData);
72int ReplaceVariableForDepSrcPath(const SandboxContext *context,
73    const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData);
74int ReplaceVariableForDepPath(const SandboxContext *context,
75    const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData);
76int SpawnPrepareSandboxCfg(AppSpawnMgr *content, AppSpawningCtx *property);
77unsigned long GetMountModeFromConfig(const cJSON *config, const char *key, unsigned long def);
78uint32_t GetFlagIndexFromJson(const cJSON *config);
79int ParseMountPathsConfig(AppSpawnSandboxCfg *sandbox,
80    const cJSON *mountConfigs, SandboxSection *section, uint32_t type);
81int ParseSymbolLinksConfig(AppSpawnSandboxCfg *sandbox, const cJSON *symbolLinkConfigs,
82    SandboxSection *section);
83int ParseGidTableConfig(AppSpawnSandboxCfg *sandbox, const cJSON *configs, SandboxSection *section);
84
85int AppSpawnColdStartApp(struct AppSpawnContent *content, AppSpawnClient *client);
86void ProcessSignal(const struct signalfd_siginfo *siginfo);
87int CreateClientSocket(uint32_t type, int block);
88void CloseClientSocket(int socketId);
89int ParseAppSandboxConfig(const cJSON *appSandboxConfig, AppSpawnSandboxCfg *sandbox);
90AppSpawnSandboxCfg *CreateAppSpawnSandbox(ExtDataType type);
91void AddDefaultVariable(void);
92bool CheckDirRecursive(const char *path);
93void CreateDemandSrc(const SandboxContext *context, const PathMountNode *sandboxNode, const MountArg *args);
94int CheckSandboxMountNode(const SandboxContext *context,
95    const SandboxSection *section, const PathMountNode *sandboxNode, uint32_t operation);
96int AppSpawnClearEnv(AppSpawnMgr *content, AppSpawningCtx *property);
97int AppSpawnChild(AppSpawnContent *content, AppSpawnClient *client);
98int WriteMsgToChild(AppSpawningCtx *property, bool isNweb);
99int WriteToFile(const char *path, int truncated, pid_t pids[], uint32_t count);
100int GetCgroupPath(const AppSpawnedProcess *appInfo, char *buffer, uint32_t buffLen);
101void SetDeveloperMode(bool mode);
102int LoadPermission(AppSpawnClientType type);
103void DeletePermission(AppSpawnClientType type);
104int SetProcessName(const AppSpawnMgr *content, const AppSpawningCtx *property);
105int SetFdEnv(AppSpawnMgr *content, AppSpawningCtx *property);
106int PreLoadEnablePidNs(AppSpawnMgr *content);
107int NsInitFunc();
108int GetNsPidFd(pid_t pid);
109int PreLoadEnablePidNs(AppSpawnMgr *content);
110pid_t GetPidByName(const char *name);
111int RunBegetctlBootApp(AppSpawnMgr *content, AppSpawningCtx *property);
112void SetSystemEnv(void);
113void RunAppSandbox(const char *ptyName);
114HOOK_MGR *GetAppSpawnHookMgr(void);
115#define STUB_NEED_CHECK 0x01
116typedef int (*ExecvFunc)(const char *pathname, char *const argv[]);
117enum {
118    STUB_MOUNT,
119    STUB_EXECV,
120    STUB_MAX,
121};
122
123typedef struct {
124    uint16_t type;
125    uint16_t flags;
126    int result;
127    void *arg;
128} StubNode;
129StubNode *GetStubNode(int type);
130#ifdef __cplusplus
131}
132#endif
133int SetSelinuxConNweb(const AppSpawnMgr *content, const AppSpawningCtx *property);
134#endif // APPSPAWN_TEST_STUB_H
135