• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "app_spawn_stub.h"
17 
18 #include <fcntl.h>
19 #include <pthread.h>
20 #include <cstdarg>
21 #include <cstdbool>
22 #include <cstdlib>
23 #include <cerrno>
24 #include <unistd.h>
25 #include <csignal>
26 #include <ctime>
27 #include <sys/time.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/un.h>
31 #include <sys/capability.h>
32 
33 #include "appspawn_server.h"
34 #include "beget_ext.h"
35 #include "securec.h"
36 
HapContextStub()37 HapContextStub::HapContextStub() {}
~HapContextStub()38 HapContextStub::~HapContextStub() {}
39 static int g_testHapDomainSetcontext = 0;
HapDomainSetcontext(HapDomainInfo & hapDomainInfo)40 int HapContextStub::HapDomainSetcontext(HapDomainInfo& hapDomainInfo)
41 {
42     if (g_testHapDomainSetcontext == 0) {
43         return 0;
44     } else if (g_testHapDomainSetcontext == 1) {
45         sleep(2); // 2 is sleep wait time
46     }
47     return g_testHapDomainSetcontext;
48 }
49 #ifdef __cplusplus
50     extern "C" {
51 #endif
SetHapDomainSetcontextResult(int result)52 void SetHapDomainSetcontextResult(int result)
53 {
54     g_testHapDomainSetcontext = result;
55 }
56 
DlopenStub(const char * pathname,int mode)57 void *DlopenStub(const char *pathname, int mode)
58 {
59     UNUSED(pathname);
60     UNUSED(mode);
61     static size_t index = 0;
62     return &index;
63 }
64 
InitEnvironmentParamStub(const char * name)65 bool InitEnvironmentParamStub(const char *name)
66 {
67     UNUSED(name);
68     return true;
69 }
70 
DlsymStub(void * handle,const char * symbol)71 void *DlsymStub(void *handle, const char *symbol)
72 {
73     UNUSED(handle);
74     if (strcmp(symbol, "InitEnvironmentParam") == 0) {
75         return reinterpret_cast<void *>(InitEnvironmentParamStub);
76     }
77     return nullptr;
78 }
79 
DlcloseStub(void * handle)80 int DlcloseStub(void *handle)
81 {
82     UNUSED(handle);
83     return 0;
84 }
85 
WaitpidStub(pid_t * pid,int * status,int opt)86 pid_t WaitpidStub(pid_t *pid, int *status, int opt)
87 {
88     UNUSED(pid);
89     UNUSED(opt);
90     static int count = 0;
91     static int statusCount = 0;
92     *status = (statusCount % 2 == 0) ? 0x0e007f : 0; // 2 is to judge whether it is even
93     count++;
94     printf("waitpid stub %d\n", GetTestPid());
95     if ((count % 2) == 1) { // 2 is to judge whether it is odd
96         statusCount++;
97         return GetTestPid();
98     }
99     return -1;
100 }
101 
DisallowInternet(void)102 void DisallowInternet(void)
103 {
104 }
105 
BindStub(int sockfd,const struct sockaddr * addr,socklen_t addrlen)106 int BindStub(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
107 {
108     UNUSED(sockfd);
109     UNUSED(addr);
110     UNUSED(addrlen);
111     return 0;
112 }
113 
ListenStub(int fd,int backlog)114 int ListenStub(int fd, int backlog)
115 {
116     UNUSED(fd);
117     UNUSED(backlog);
118     return 0;
119 }
120 
LchownStub(const char * pathname,uid_t owner,gid_t group)121 int LchownStub(const char *pathname, uid_t owner, gid_t group)
122 {
123     UNUSED(pathname);
124     UNUSED(owner);
125     UNUSED(group);
126     return 0;
127 }
128 
LchmodStub(const char * pathname,mode_t mode)129 int LchmodStub(const char *pathname, mode_t mode)
130 {
131     UNUSED(pathname);
132     UNUSED(mode);
133     return 0;
134 }
135 
GetsockoptStub(int sockfd,int level,int optname,void * optval,socklen_t * optlen)136 int GetsockoptStub(int sockfd, int level, int optname, void *optval, socklen_t *optlen)
137 {
138     UNUSED(sockfd);
139     UNUSED(level);
140     UNUSED(optlen);
141     if (optval == nullptr) {
142         return -1;
143     }
144     if (optname == SO_PEERCRED) {
145         struct ucred *cred = reinterpret_cast<struct ucred *>(optval);
146         cred->uid = 0;
147     }
148     return 0;
149 }
150 
SetgroupsStub(size_t size,const gid_t * list)151 int SetgroupsStub(size_t size, const gid_t *list)
152 {
153     UNUSED(size);
154     UNUSED(list);
155     return 0;
156 }
157 
SetresuidStub(uid_t ruid,uid_t euid,uid_t suid)158 int SetresuidStub(uid_t ruid, uid_t euid, uid_t suid)
159 {
160     UNUSED(ruid);
161     UNUSED(euid);
162     UNUSED(suid);
163     return 0;
164 }
165 
SetresgidStub(gid_t rgid,gid_t egid,gid_t sgid)166 int SetresgidStub(gid_t rgid, gid_t egid, gid_t sgid)
167 {
168     UNUSED(rgid);
169     UNUSED(egid);
170     UNUSED(sgid);
171     return 0;
172 }
173 
CapsetStub(cap_user_header_t hdrp,const cap_user_data_t datap)174 int CapsetStub(cap_user_header_t hdrp, const cap_user_data_t datap)
175 {
176     UNUSED(hdrp);
177     UNUSED(datap);
178     return 0;
179 }
180 
181 struct ForkArgs {
182     int (*childFunc)(void *arg);
183     void *args;
184 };
185 
ThreadFunc(void * arg)186 static void *ThreadFunc(void *arg)
187 {
188     struct ForkArgs *forkArg = reinterpret_cast<struct ForkArgs *>(arg);
189     forkArg->childFunc(forkArg->args);
190     free(forkArg);
191     return nullptr;
192 }
193 
194 static pid_t g_pid = 1000;
AppSpawnFork(int (* childFunc)(void * arg),void * args)195 pid_t AppSpawnFork(int (*childFunc)(void *arg), void *args)
196 {
197     static pthread_t thread = 0;
198     struct ForkArgs *forkArg = reinterpret_cast<struct ForkArgs *>(malloc(sizeof(struct ForkArgs)));
199     if (forkArg == nullptr) {
200         return -1;
201     }
202     printf("ThreadFunc TestFork args %p forkArg %p\n", args, forkArg);
203     forkArg->childFunc = childFunc;
204     forkArg->args = args;
205     int ret = pthread_create(&thread, nullptr, ThreadFunc, forkArg);
206     if (ret != 0) {
207         printf("Failed to create thread %d \n", errno);
208         return -1;
209     }
210     g_pid++;
211     return g_pid;
212 }
213 
GetTestPid(void)214 pid_t GetTestPid(void)
215 {
216     return g_pid;
217 }
218 
CloneStub(int (* fn)(void *),void * stack,int flags,void * arg,...)219 int CloneStub(int (*fn)(void *), void *stack, int flags, void *arg, ...)
220 {
221     static int testResult = 0;
222     testResult++;
223     return testResult == 1 ? AppSpawnFork(fn, arg) : -1;
224 }
225 
StartupLog_stub(InitLogLevel logLevel,uint32_t domain,const char * tag,const char * fmt,...)226 void StartupLog_stub(InitLogLevel logLevel, uint32_t domain, const char *tag, const char *fmt, ...)
227 {
228     char tmpFmt[1024] = {0};
229     va_list vargs;
230     va_start(vargs, fmt);
231     if (vsnprintf_s(tmpFmt, sizeof(tmpFmt), sizeof(tmpFmt) - 1, fmt, vargs) == -1) {
232         tmpFmt[sizeof(tmpFmt) - 2] = '\n'; // 2 add \n to tail
233         tmpFmt[sizeof(tmpFmt) - 1] = '\0';
234     }
235     va_end(vargs);
236 
237     struct timespec curr;
238     (void)clock_gettime(CLOCK_REALTIME, &curr);
239     struct tm t;
240     char dateTime[80] = {"00-00-00 00:00:00"}; // 80 data time
241     if (localtime_r(&curr.tv_sec, &t) != nullptr) {
242         strftime(dateTime, sizeof(dateTime), "%Y-%m-%d %H:%M:%S", &t);
243     }
244     (void)fprintf(stdout, "[%s.%ld][pid=%d %d][%s]%s \n", dateTime, curr.tv_nsec, getpid(), gettid(), tag, tmpFmt);
245     (void)fflush(stdout);
246 }
247 
SetSeccompPolicyWithName(SeccompFilterType filter,const char * filterName)248 bool SetSeccompPolicyWithName(SeccompFilterType filter, const char *filterName)
249 {
250     static int result = 0;
251     result++;
252     return (result % 3) == 0; // 3 is test data
253 }
254 
255 #ifdef __cplusplus
256     }
257 #endif
258