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 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 typedef struct AppSpawnContent AppSpawnContent;
34 typedef struct AppSpawnClient AppSpawnClient;
35 typedef struct TagAppSpawnReqMsgNode AppSpawnReqMsgNode;
36 typedef void *AppSpawnClientHandle;
37 typedef struct TagAppSpawnReqMsgMgr AppSpawnReqMsgMgr;
38 typedef struct TagAppSpawningCtx AppSpawningCtx;
39 typedef struct TagAppSpawnMsg AppSpawnMsg;
40 typedef struct TagAppSpawnSandboxCfg  AppSpawnSandboxCfg;
41 typedef struct TagAppSpawnExtData AppSpawnExtData;
42 typedef struct TagSandboxContext SandboxContext;
43 typedef struct TagAppSpawnedProcess AppSpawnedProcess;
44 typedef struct TagAppSpawnForkArg AppSpawnForkArg;
45 typedef struct TagAppSpawnMsgNode AppSpawnMsgNode;
46 typedef struct TagAppSpawnMgr AppSpawnMgr;
47 typedef struct TagPathMountNode PathMountNode;
48 typedef struct TagMountArg MountArg;
49 typedef struct TagVarExtraData VarExtraData;
50 typedef struct TagSandboxSection SandboxSection;
51 
52 void ProcessSignal(const struct signalfd_siginfo *siginfo);
53 int CreateClientSocket(uint32_t type, int block);
54 void CloseClientSocket(int socketId);
55 int ParseAppSandboxConfig(const cJSON *appSandboxConfig, AppSpawnSandboxCfg *sandbox);
56 AppSpawnSandboxCfg *CreateAppSpawnSandbox(void);
57 void AddDefaultVariable(void);
58 bool CheckDirRecursive(const char *path);
59 void CreateDemandSrc(const SandboxContext *context, const PathMountNode *sandboxNode, const MountArg *args);
60 int CheckSandboxMountNode(const SandboxContext *context,
61     const SandboxSection *section, const PathMountNode *sandboxNode, uint32_t operation);
62 int AppSpawnClearEnv(AppSpawnMgr *content, AppSpawningCtx *property);
63 int AppSpawnChild(AppSpawnContent *content, AppSpawnClient *client);
64 int WriteMsgToChild(AppSpawningCtx *property, bool isNweb);
65 int WriteToFile(const char *path, int truncated, pid_t pids[], uint32_t count);
66 int GetCgroupPath(const AppSpawnedProcess *appInfo, char *buffer, uint32_t buffLen);
67 void SetDeveloperMode(bool mode);
68 int LoadPermission(AppSpawnClientType type);
69 void DeletePermission(AppSpawnClientType type);
70 int SetProcessName(const AppSpawnMgr *content, const AppSpawningCtx *property);
71 int SetFdEnv(AppSpawnMgr *content, AppSpawningCtx *property);
72 int PreLoadEnablePidNs(AppSpawnMgr *content);
73 int NsInitFunc();
74 int GetNsPidFd(pid_t pid);
75 int PreLoadEnablePidNs(AppSpawnMgr *content);
76 pid_t GetPidByName(const char *name);
77 int RunBegetctlBootApp(AppSpawnMgr *content, AppSpawningCtx *property);
78 void SetSystemEnv(void);
79 void RunAppSandbox(const char *ptyName);
80 #define STUB_NEED_CHECK 0x01
81 typedef int (*ExecvFunc)(const char *pathname, char *const argv[]);
82 enum {
83     STUB_MOUNT,
84     STUB_EXECV,
85     STUB_MAX,
86 };
87 
88 typedef struct {
89     uint16_t type;
90     uint16_t flags;
91     int result;
92     void *arg;
93 } StubNode;
94 StubNode *GetStubNode(int type);
95 #ifdef __cplusplus
96 }
97 #endif
98 int SetSelinuxConNweb(const AppSpawnMgr *content, const AppSpawningCtx *property);
99 void InitAppCommonEnv(const AppSpawningCtx *property);
100 #endif // APPSPAWN_TEST_STUB_H
101