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_TEST_STUB_H 1769570cc8Sopenharmony_ci#define APPSPAWN_TEST_STUB_H 1869570cc8Sopenharmony_ci 1969570cc8Sopenharmony_ci#include <stdio.h> 2069570cc8Sopenharmony_ci#include <stdlib.h> 2169570cc8Sopenharmony_ci#include <stdbool.h> 2269570cc8Sopenharmony_ci#include <sys/signalfd.h> 2369570cc8Sopenharmony_ci#include <sys/socket.h> 2469570cc8Sopenharmony_ci#include <sys/wait.h> 2569570cc8Sopenharmony_ci 2669570cc8Sopenharmony_ci#include "cJSON.h" 2769570cc8Sopenharmony_ci#include "appspawn_client.h" 2869570cc8Sopenharmony_ci#include "appspawn_hook.h" 2969570cc8Sopenharmony_ci 3069570cc8Sopenharmony_ci#ifdef __cplusplus 3169570cc8Sopenharmony_ciextern "C" { 3269570cc8Sopenharmony_ci#endif 3369570cc8Sopenharmony_ci 3469570cc8Sopenharmony_citypedef struct AppSpawnContent AppSpawnContent; 3569570cc8Sopenharmony_citypedef struct AppSpawnClient AppSpawnClient; 3669570cc8Sopenharmony_citypedef struct TagAppSpawnReqMsgNode AppSpawnReqMsgNode; 3769570cc8Sopenharmony_citypedef void *AppSpawnClientHandle; 3869570cc8Sopenharmony_citypedef struct TagAppSpawnReqMsgMgr AppSpawnReqMsgMgr; 3969570cc8Sopenharmony_citypedef struct TagAppSpawningCtx AppSpawningCtx; 4069570cc8Sopenharmony_citypedef struct TagAppSpawnMsg AppSpawnMsg; 4169570cc8Sopenharmony_citypedef struct TagAppSpawnSandboxCfg AppSpawnSandboxCfg; 4269570cc8Sopenharmony_citypedef struct TagAppSpawnExtData AppSpawnExtData; 4369570cc8Sopenharmony_citypedef struct TagSandboxContext SandboxContext; 4469570cc8Sopenharmony_citypedef struct TagAppSpawnedProcess AppSpawnedProcess; 4569570cc8Sopenharmony_citypedef struct TagAppSpawnForkArg AppSpawnForkArg; 4669570cc8Sopenharmony_citypedef struct TagAppSpawnMsgNode AppSpawnMsgNode; 4769570cc8Sopenharmony_citypedef struct TagAppSpawnMgr AppSpawnMgr; 4869570cc8Sopenharmony_citypedef struct TagPathMountNode PathMountNode; 4969570cc8Sopenharmony_citypedef struct TagMountArg MountArg; 5069570cc8Sopenharmony_citypedef struct TagVarExtraData VarExtraData; 5169570cc8Sopenharmony_citypedef struct TagSandboxSection SandboxSection; 5269570cc8Sopenharmony_citypedef struct TagAppSpawnNamespace AppSpawnNamespace; 5369570cc8Sopenharmony_citypedef struct TagAppSpawnedProcess AppSpawnedProcessInfo; 5469570cc8Sopenharmony_ci 5569570cc8Sopenharmony_ciAppSpawnNamespace *GetAppSpawnNamespace(const AppSpawnMgr *content); 5669570cc8Sopenharmony_civoid DeleteAppSpawnNamespace(AppSpawnNamespace *ns); 5769570cc8Sopenharmony_civoid FreeAppSpawnNamespace(struct TagAppSpawnExtData *data); 5869570cc8Sopenharmony_ciint PreForkSetPidNamespace(AppSpawnMgr *content, AppSpawningCtx *property); 5969570cc8Sopenharmony_ciint PostForkSetPidNamespace(AppSpawnMgr *content, AppSpawningCtx *property); 6069570cc8Sopenharmony_ciint ProcessMgrRemoveApp(const AppSpawnMgr *content, const AppSpawnedProcessInfo *appInfo); 6169570cc8Sopenharmony_ciint ProcessMgrAddApp(const AppSpawnMgr *content, const AppSpawnedProcessInfo *appInfo); 6269570cc8Sopenharmony_civoid TryCreateSocket(AppSpawnReqMsgMgr *reqMgr); 6369570cc8Sopenharmony_ci 6469570cc8Sopenharmony_ciint MountAllGroup(const SandboxContext *context, const cJSON *groups); 6569570cc8Sopenharmony_ciint MountAllHsp(const SandboxContext *context, const cJSON *hsps); 6669570cc8Sopenharmony_ci 6769570cc8Sopenharmony_civoid CheckAndCreateSandboxFile(const char *file); 6869570cc8Sopenharmony_ciint VarPackageNameReplace(const SandboxContext *context, 6969570cc8Sopenharmony_ci const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData); 7069570cc8Sopenharmony_ciint ReplaceVariableForDepSandboxPath(const SandboxContext *context, 7169570cc8Sopenharmony_ci const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData); 7269570cc8Sopenharmony_ciint ReplaceVariableForDepSrcPath(const SandboxContext *context, 7369570cc8Sopenharmony_ci const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData); 7469570cc8Sopenharmony_ciint ReplaceVariableForDepPath(const SandboxContext *context, 7569570cc8Sopenharmony_ci const char *buffer, uint32_t bufferLen, uint32_t *realLen, const VarExtraData *extraData); 7669570cc8Sopenharmony_ciint SpawnPrepareSandboxCfg(AppSpawnMgr *content, AppSpawningCtx *property); 7769570cc8Sopenharmony_ciunsigned long GetMountModeFromConfig(const cJSON *config, const char *key, unsigned long def); 7869570cc8Sopenharmony_ciuint32_t GetFlagIndexFromJson(const cJSON *config); 7969570cc8Sopenharmony_ciint ParseMountPathsConfig(AppSpawnSandboxCfg *sandbox, 8069570cc8Sopenharmony_ci const cJSON *mountConfigs, SandboxSection *section, uint32_t type); 8169570cc8Sopenharmony_ciint ParseSymbolLinksConfig(AppSpawnSandboxCfg *sandbox, const cJSON *symbolLinkConfigs, 8269570cc8Sopenharmony_ci SandboxSection *section); 8369570cc8Sopenharmony_ciint ParseGidTableConfig(AppSpawnSandboxCfg *sandbox, const cJSON *configs, SandboxSection *section); 8469570cc8Sopenharmony_ci 8569570cc8Sopenharmony_ciint AppSpawnColdStartApp(struct AppSpawnContent *content, AppSpawnClient *client); 8669570cc8Sopenharmony_civoid ProcessSignal(const struct signalfd_siginfo *siginfo); 8769570cc8Sopenharmony_ciint CreateClientSocket(uint32_t type, int block); 8869570cc8Sopenharmony_civoid CloseClientSocket(int socketId); 8969570cc8Sopenharmony_ciint ParseAppSandboxConfig(const cJSON *appSandboxConfig, AppSpawnSandboxCfg *sandbox); 9069570cc8Sopenharmony_ciAppSpawnSandboxCfg *CreateAppSpawnSandbox(ExtDataType type); 9169570cc8Sopenharmony_civoid AddDefaultVariable(void); 9269570cc8Sopenharmony_cibool CheckDirRecursive(const char *path); 9369570cc8Sopenharmony_civoid CreateDemandSrc(const SandboxContext *context, const PathMountNode *sandboxNode, const MountArg *args); 9469570cc8Sopenharmony_ciint CheckSandboxMountNode(const SandboxContext *context, 9569570cc8Sopenharmony_ci const SandboxSection *section, const PathMountNode *sandboxNode, uint32_t operation); 9669570cc8Sopenharmony_ciint AppSpawnClearEnv(AppSpawnMgr *content, AppSpawningCtx *property); 9769570cc8Sopenharmony_ciint AppSpawnChild(AppSpawnContent *content, AppSpawnClient *client); 9869570cc8Sopenharmony_ciint WriteMsgToChild(AppSpawningCtx *property, bool isNweb); 9969570cc8Sopenharmony_ciint WriteToFile(const char *path, int truncated, pid_t pids[], uint32_t count); 10069570cc8Sopenharmony_ciint GetCgroupPath(const AppSpawnedProcess *appInfo, char *buffer, uint32_t buffLen); 10169570cc8Sopenharmony_civoid SetDeveloperMode(bool mode); 10269570cc8Sopenharmony_ciint LoadPermission(AppSpawnClientType type); 10369570cc8Sopenharmony_civoid DeletePermission(AppSpawnClientType type); 10469570cc8Sopenharmony_ciint SetProcessName(const AppSpawnMgr *content, const AppSpawningCtx *property); 10569570cc8Sopenharmony_ciint SetFdEnv(AppSpawnMgr *content, AppSpawningCtx *property); 10669570cc8Sopenharmony_ciint PreLoadEnablePidNs(AppSpawnMgr *content); 10769570cc8Sopenharmony_ciint NsInitFunc(); 10869570cc8Sopenharmony_ciint GetNsPidFd(pid_t pid); 10969570cc8Sopenharmony_ciint PreLoadEnablePidNs(AppSpawnMgr *content); 11069570cc8Sopenharmony_cipid_t GetPidByName(const char *name); 11169570cc8Sopenharmony_ciint RunBegetctlBootApp(AppSpawnMgr *content, AppSpawningCtx *property); 11269570cc8Sopenharmony_civoid SetSystemEnv(void); 11369570cc8Sopenharmony_civoid RunAppSandbox(const char *ptyName); 11469570cc8Sopenharmony_ciHOOK_MGR *GetAppSpawnHookMgr(void); 11569570cc8Sopenharmony_ci#define STUB_NEED_CHECK 0x01 11669570cc8Sopenharmony_citypedef int (*ExecvFunc)(const char *pathname, char *const argv[]); 11769570cc8Sopenharmony_cienum { 11869570cc8Sopenharmony_ci STUB_MOUNT, 11969570cc8Sopenharmony_ci STUB_EXECV, 12069570cc8Sopenharmony_ci STUB_MAX, 12169570cc8Sopenharmony_ci}; 12269570cc8Sopenharmony_ci 12369570cc8Sopenharmony_citypedef struct { 12469570cc8Sopenharmony_ci uint16_t type; 12569570cc8Sopenharmony_ci uint16_t flags; 12669570cc8Sopenharmony_ci int result; 12769570cc8Sopenharmony_ci void *arg; 12869570cc8Sopenharmony_ci} StubNode; 12969570cc8Sopenharmony_ciStubNode *GetStubNode(int type); 13069570cc8Sopenharmony_ci#ifdef __cplusplus 13169570cc8Sopenharmony_ci} 13269570cc8Sopenharmony_ci#endif 13369570cc8Sopenharmony_ciint SetSelinuxConNweb(const AppSpawnMgr *content, const AppSpawningCtx *property); 13469570cc8Sopenharmony_ci#endif // APPSPAWN_TEST_STUB_H 135