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