• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include "ActsCapabilityTest.h"
16 #include <csignal>
17 #include <dirent.h>
18 #include <securec.h>
19 #include <sys/capability.h>
20 #include <unistd.h>
21 
Sigac(int i)22 void Sigac(int i)
23 {
24     _exit(0);
25 }
26 
ChildSleep()27 void ChildSleep()
28 {
29     signal(SIGXFSZ, Sigac);
30     usleep(LONG_SLEEP_NUM);
31     exit(0);
32 }
33 
CapInit()34 int CapInit()
35 {
36     // Init capabilities
37     struct __user_cap_header_struct capheader;
38     (void)memset_s(&capheader, sizeof(struct __user_cap_header_struct),
39         0, sizeof(struct __user_cap_header_struct));
40     capheader.version = _LINUX_CAPABILITY_VERSION_3;
41     capheader.pid = 0;
42     struct __user_cap_data_struct capdata[CAP_NUM];
43     (void)memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
44         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
45     capdata[0].permitted = LINUX_FULL_CAP;
46     capdata[0].effective = LINUX_FULL_CAP;
47     capdata[0].inheritable = LINUX_FULL_CAP;
48     // Set capabilities
49     int ret = capset(&capheader, &capdata[0]);
50     if (ret != 0) {
51         return FALSE;
52     }
53     return 0;
54 }
55 
DropCAPCHOWN()56 int DropCAPCHOWN()
57 {
58     struct __user_cap_header_struct capheader = { 0 };
59     errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
60         0, sizeof(struct __user_cap_header_struct));
61     if (result != EOK) {
62         LOG("DropCAPCHOWN memset_s failed");
63         return FALSE;
64     };
65     capheader.version = _LINUX_CAPABILITY_VERSION_3;
66     capheader.pid = 0;
67     struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
68     result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
69         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
70     if (result != EOK) {
71         LOG("DropCAPCHOWN memset_s failed");
72         return FALSE;
73     };
74     // Drop the capabilities of CAP_CHOWN
75     capdata[CAP_TO_INDEX(CAP_CHOWN)].permitted &= ~CAP_TO_MASK(CAP_CHOWN);
76     capdata[CAP_TO_INDEX(CAP_CHOWN)].effective &= ~CAP_TO_MASK(CAP_CHOWN);
77     capdata[CAP_TO_INDEX(CAP_CHOWN)].inheritable &= ~CAP_TO_MASK(CAP_CHOWN);
78     int ret = capset(&capheader, &capdata[0]);
79     if (ret != 0) {
80         return FALSE;
81     }
82     return 0;
83 }
84 
DropCAPDACOVERRIDE()85 int DropCAPDACOVERRIDE()
86 {
87     struct __user_cap_header_struct capheader = { 0, 0 };
88     errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
89         0, sizeof(struct __user_cap_header_struct));
90     if (result != EOK) {
91         LOG("DropCAPDACOVERRIDE memset_s failed");
92         return FALSE;
93     };
94     capheader.version = _LINUX_CAPABILITY_VERSION_3;
95     capheader.pid = 0;
96     struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
97     result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
98         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
99     if (result != EOK) {
100         LOG("DropCAPDACOVERRIDE memset_s failed");
101         return FALSE;
102     };
103     // Drop the capabilities of CAP_DAC_OVERRIDE
104     capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted &= ~CAP_TO_MASK(CAP_DAC_OVERRIDE);
105     capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].effective &= ~CAP_TO_MASK(CAP_DAC_OVERRIDE);
106     capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].inheritable &= ~CAP_TO_MASK(CAP_DAC_OVERRIDE);
107     int ret = capset(&capheader, &capdata[0]);
108     if (ret != 0) {
109         return FALSE;
110     }
111     return 0;
112 }
113 
DropCAPDACREADSEARCH()114 int DropCAPDACREADSEARCH()
115 {
116     struct __user_cap_header_struct capheader = { 0, 0 };
117     errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
118         0, sizeof(struct __user_cap_header_struct));
119     if (result != EOK) {
120         LOG("DropCAPDACREADSEARCH memset_s failed");
121         return FALSE;
122     };
123     capheader.version = _LINUX_CAPABILITY_VERSION_3;
124     capheader.pid = 0;
125     struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
126     result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
127         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
128     if (result != EOK) {
129         LOG("DropCAPDACREADSEARCH memset_s failed");
130         return FALSE;
131     };
132     // Drop the capabilities of CAP_DAC_READ_SEARCH
133     capdata[CAP_TO_INDEX(CAP_DAC_READ_SEARCH)].permitted &= ~CAP_TO_MASK(CAP_DAC_READ_SEARCH);
134     capdata[CAP_TO_INDEX(CAP_DAC_READ_SEARCH)].effective &= ~CAP_TO_MASK(CAP_DAC_READ_SEARCH);
135     capdata[CAP_TO_INDEX(CAP_DAC_READ_SEARCH)].inheritable &= ~CAP_TO_MASK(CAP_DAC_READ_SEARCH);
136     int ret = capset(&capheader, &capdata[0]);
137     if (ret != 0) {
138         return FALSE;
139     }
140     return 0;
141 }
142 
DropCAPDACOVERRIDEAndREADSEARCH()143 int DropCAPDACOVERRIDEAndREADSEARCH()
144 {
145     struct __user_cap_header_struct capheader = { 0, 0 };
146     errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
147         0, sizeof(struct __user_cap_header_struct));
148     if (result != EOK) {
149         LOG("DropCAPDACOVERRIDEAndREADSEARCH memset_s failed");
150         return FALSE;
151     };
152     capheader.version = _LINUX_CAPABILITY_VERSION_3;
153     capheader.pid = 0;
154     struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
155     result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
156         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
157     if (result != EOK) {
158         LOG("DropCAPDACOVERRIDEAndREADSEARCH memset_s failed");
159         return FALSE;
160     };
161     // Drop the capabilities of CAP_DAC_OVERRIDE and CAP_DAC_READ_SEARCH
162     capdata[CAP_TO_INDEX(CAP_DAC_READ_SEARCH)].permitted &= ~CAP_TO_MASK(CAP_DAC_READ_SEARCH);
163     capdata[CAP_TO_INDEX(CAP_DAC_READ_SEARCH)].effective &= ~CAP_TO_MASK(CAP_DAC_READ_SEARCH);
164     capdata[CAP_TO_INDEX(CAP_DAC_READ_SEARCH)].inheritable &= ~CAP_TO_MASK(CAP_DAC_READ_SEARCH);
165     capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].permitted &= ~CAP_TO_MASK(CAP_DAC_OVERRIDE);
166     capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].effective &= ~CAP_TO_MASK(CAP_DAC_OVERRIDE);
167     capdata[CAP_TO_INDEX(CAP_DAC_OVERRIDE)].inheritable &= ~CAP_TO_MASK(CAP_DAC_OVERRIDE);
168     int ret = capset(&capheader, &capdata[0]);
169     if (ret != 0) {
170         return FALSE;
171     }
172     return 0;
173 }
174 
DropCAPFOWNER()175 int DropCAPFOWNER()
176 {
177     struct __user_cap_header_struct capheader = { 0, 0 };
178     errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
179         0, sizeof(struct __user_cap_header_struct));
180     if (result != EOK) {
181         LOG("DropCAPFOWNER memset_s failed");
182         return FALSE;
183     };
184     capheader.version = _LINUX_CAPABILITY_VERSION_3;
185     capheader.pid = 0;
186     struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
187     result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
188         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
189     if (result != EOK) {
190         LOG("DropCAPFOWNER memset_s failed");
191         return FALSE;
192     };
193     // Drop the capabilities of CAP_FOWNER
194     capdata[CAP_TO_INDEX(CAP_FOWNER)].permitted &= ~CAP_TO_MASK(CAP_FOWNER);
195     capdata[CAP_TO_INDEX(CAP_FOWNER)].effective &= ~CAP_TO_MASK(CAP_FOWNER);
196     capdata[CAP_TO_INDEX(CAP_FOWNER)].inheritable &= ~CAP_TO_MASK(CAP_FOWNER);
197     int ret = capset(&capheader, &capdata[0]);
198     if (ret != 0) {
199         return FALSE;
200     }
201     return 0;
202 }
203 
DropCAPKILL()204 int DropCAPKILL()
205 {
206     struct __user_cap_header_struct capheader = { 0, 0 };
207     errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
208         0, sizeof(struct __user_cap_header_struct));
209     if (result != EOK) {
210         LOG("DropCAPKILL memset_s failed");
211         return FALSE;
212     };
213     capheader.version = _LINUX_CAPABILITY_VERSION_3;
214     capheader.pid = 0;
215     struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
216     result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
217         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
218     if (result != EOK) {
219         LOG("DropCAPKILL memset_s failed");
220         return FALSE;
221     };
222     // Drop the capabilities of CAP_KILL
223     capdata[CAP_TO_INDEX(CAP_KILL)].permitted &= ~CAP_TO_MASK(CAP_KILL);
224     capdata[CAP_TO_INDEX(CAP_KILL)].effective &= ~CAP_TO_MASK(CAP_KILL);
225     capdata[CAP_TO_INDEX(CAP_KILL)].inheritable &= ~CAP_TO_MASK(CAP_KILL);
226     int ret = capset(&capheader, &capdata[0]);
227     if (ret != 0) {
228         return FALSE;
229     }
230     return 0;
231 }
232 
DropCAPSETGID()233 int DropCAPSETGID()
234 {
235     struct __user_cap_header_struct capheader = { 0, 0 };
236     errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
237         0, sizeof(struct __user_cap_header_struct));
238     if (result != EOK) {
239         LOG("DropCAPSETGID memset_s failed");
240         return FALSE;
241     };
242     capheader.version = _LINUX_CAPABILITY_VERSION_3;
243     capheader.pid = 0;
244     struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
245     result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
246         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
247     if (result != EOK) {
248         LOG("DropCAPSETGID memset_s failed");
249         return FALSE;
250     };
251     // Drop the capabilities of CAP_SETGID
252     capdata[CAP_TO_INDEX(CAP_SETGID)].permitted &= ~CAP_TO_MASK(CAP_SETGID);
253     capdata[CAP_TO_INDEX(CAP_SETGID)].effective &= ~CAP_TO_MASK(CAP_SETGID);
254     capdata[CAP_TO_INDEX(CAP_SETGID)].inheritable &= ~CAP_TO_MASK(CAP_SETGID);
255     int ret = capset(&capheader, &capdata[0]);
256     if (ret != 0) {
257         return FALSE;
258     }
259     return 0;
260 }
261 
DropCAPSETUID()262 int DropCAPSETUID()
263 {
264     struct __user_cap_header_struct capheader = { 0, 0 };
265     errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
266         0, sizeof(struct __user_cap_header_struct));
267     if (result != EOK) {
268         LOG("DropCAPSETUID memset_s failed");
269         return FALSE;
270     };
271     capheader.version = _LINUX_CAPABILITY_VERSION_3;
272     capheader.pid = 0;
273     struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
274     result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
275         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
276     if (result != EOK) {
277         LOG("DropCAPSETUID memset_s failed");
278         return FALSE;
279     };
280     // Drop the capabilities of CAP_SETUID
281     capdata[CAP_TO_INDEX(CAP_SETUID)].permitted &= ~CAP_TO_MASK(CAP_SETUID);
282     capdata[CAP_TO_INDEX(CAP_SETUID)].effective &= ~CAP_TO_MASK(CAP_SETUID);
283     capdata[CAP_TO_INDEX(CAP_SETUID)].inheritable &= ~CAP_TO_MASK(CAP_SETUID);
284     int ret = capset(&capheader, &capdata[0]);
285     if (ret != 0) {
286         return FALSE;
287     }
288     return 0;
289 }
290 
DropCAPSETPCAP()291 int DropCAPSETPCAP()
292 {
293     struct __user_cap_header_struct capheader = { 0, 0 };
294     errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
295         0, sizeof(struct __user_cap_header_struct));
296     if (result != EOK) {
297         LOG("DropCAPSETPCAP memset_s failed");
298         return FALSE;
299     };
300     capheader.version = _LINUX_CAPABILITY_VERSION_3;
301     capheader.pid = 0;
302     struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
303     result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
304         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
305     if (result != EOK) {
306         LOG("DropCAPSETPCAP memset_s failed");
307         return FALSE;
308     };
309     // Drop the capabilities of CAP_SETPCAP
310     capdata[CAP_TO_INDEX(CAP_SETPCAP)].permitted &= ~CAP_TO_MASK(CAP_SETPCAP);
311     capdata[CAP_TO_INDEX(CAP_SETPCAP)].effective &= ~CAP_TO_MASK(CAP_SETPCAP);
312     capdata[CAP_TO_INDEX(CAP_SETPCAP)].inheritable &= ~CAP_TO_MASK(CAP_SETPCAP);
313     int ret = capset(&capheader, &capdata[0]);
314     if (ret != 0) {
315         return FALSE;
316     }
317     return 0;
318 }
319 
DropCAPSYSNICE()320 int DropCAPSYSNICE()
321 {
322     struct __user_cap_header_struct capheader = { 0, 0 };
323     errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
324         0, sizeof(struct __user_cap_header_struct));
325     if (result != EOK) {
326         LOG("DropCAPSYSNICE memset_s failed");
327         return FALSE;
328     };
329     capheader.version = _LINUX_CAPABILITY_VERSION_3;
330     capheader.pid = 0;
331     struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
332     result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
333         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
334     if (result != EOK) {
335         LOG("DropCAPSYSNICE memset_s failed");
336         return FALSE;
337     };
338     // Drop the capabilities of CAP_SYS_NICE
339     capdata[CAP_TO_INDEX(CAP_SYS_NICE)].permitted &= ~CAP_TO_MASK(CAP_SYS_NICE);
340     capdata[CAP_TO_INDEX(CAP_SYS_NICE)].effective &= ~CAP_TO_MASK(CAP_SYS_NICE);
341     capdata[CAP_TO_INDEX(CAP_SYS_NICE)].inheritable &= ~CAP_TO_MASK(CAP_SYS_NICE);
342     int ret = capset(&capheader, &capdata[0]);
343     if (ret != 0) {
344         return FALSE;
345     }
346     return 0;
347 }
348 
DropCAPSYSTIME()349 int DropCAPSYSTIME()
350 {
351     struct __user_cap_header_struct capheader = { 0, 0 };
352     errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
353         0, sizeof(struct __user_cap_header_struct));
354     if (result != EOK) {
355         LOG("DropCAPSYSTIME memset_s failed");
356         return FALSE;
357     };
358     capheader.version = _LINUX_CAPABILITY_VERSION_3;
359     capheader.pid = 0;
360     struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
361     result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
362         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
363     if (result != EOK) {
364         LOG("DropCAPSYSTIME memset_s failed");
365         return FALSE;
366     };
367     // Drop the capabilities of CAP_SYS_TIME
368     capdata[CAP_TO_INDEX(CAP_SYS_TIME)].permitted &= ~CAP_TO_MASK(CAP_SYS_TIME);
369     capdata[CAP_TO_INDEX(CAP_SYS_TIME)].effective &= ~CAP_TO_MASK(CAP_SYS_TIME);
370     capdata[CAP_TO_INDEX(CAP_SYS_TIME)].inheritable &= ~CAP_TO_MASK(CAP_SYS_TIME);
371     int ret = capset(&capheader, &capdata[0]);
372     if (ret != 0) {
373         return FALSE;
374     }
375     return 0;
376 }
377 
DropAllCAP()378 int DropAllCAP()
379 {
380     struct __user_cap_header_struct capheader = { 0, 0 };
381     errno_t result = memset_s(&capheader, sizeof(struct __user_cap_header_struct),
382         0, sizeof(struct __user_cap_header_struct));
383     if (result != EOK) {
384         LOG("DropAllCAP memset_s failed");
385         return FALSE;
386     };
387     capheader.version = _LINUX_CAPABILITY_VERSION_3;
388     capheader.pid = 0;
389     struct __user_cap_data_struct capdata[CAP_NUM] = { { 0 }, { 0 } };
390     result = memset_s(capdata, CAP_NUM * sizeof(struct __user_cap_data_struct),
391         0xff, CAP_NUM * sizeof(struct __user_cap_data_struct));
392     if (result != EOK) {
393         LOG("DropAllCAP memset_s failed");
394         return FALSE;
395     };
396     // Drop all the capabilities
397     capdata[0].permitted = NO_CAP;
398     capdata[0].effective = NO_CAP;
399     capdata[0].inheritable = NO_CAP;
400     int ret = capset(&capheader, &capdata[0]);
401     if (ret != 0) {
402         return FALSE;
403     }
404     return 0;
405 }
406 
RemoveDir(const char * dirname)407 int RemoveDir(const char *dirname)
408 {
409     int ret;
410     char curDir[] = ".";
411     char upDir[] = "..";
412     char updirname[SIZE512];
413     DIR *dir = nullptr;
414     struct dirent *dp = nullptr;
415     struct stat upDirStat = { 0 };
416     // Init capabilities
417     CapInit();
418     // The directory transferred by the parameter does not exist
419     if (access(dirname, F_OK != 0)) {
420         return 0;
421     } else {
422         chmod(dirname, RWX);
423     }
424     // Open the current-level directory
425     dir = opendir(dirname);
426     while ((dp = readdir(dir)) != nullptr) {
427         // The system skips the judgment and continues looping when the . or .. directory is encountered
428         if ((strcmp(curDir, dp->d_name) == 0) || (strcmp(upDir, dp->d_name) == 0)) {
429             continue;
430         }
431         // Combine the upper-level directory content
432         int spr = sprintf_s(updirname, SIZE512, "%s/%s", dirname, dp->d_name);
433         if (spr == FALSE) {
434             closedir(dir);
435             return FALSE;
436         }
437         // Obtains the status of the upper-level, file or directory
438         stat(updirname, &upDirStat);
439         // Directory files, recursively deleting contents in the directory
440         if (upDirStat.st_mode & S_IFDIR) {
441             // Invoke the function recursively to delete the content of the upper-level directory
442             RemoveDir(updirname);
443         } else {
444             // Common files, directly deleting
445             ret = unlink(updirname);
446             if (ret != 0) {
447                 // Failed to delete the file
448                 perror("Failed to delete the file");
449                 LOG("ErrInfo: The file name is %s", updirname);
450                 closedir(dir);
451                 return FALSE;
452             }
453         }
454     }
455     // Close the current-level directory
456     closedir(dir);
457     // Delete the empty directory
458     ret = rmdir(dirname);
459     if (ret != 0) {
460         // Failed to delete the empty directory
461         perror("Failed to delete the empty directory");
462         LOG("ErrInfo: The directory name is %s", dirname);
463         return FALSE;
464     }
465     return 0;
466 }
467 
SetUidGid(uid_t uid,gid_t gid)468 int SetUidGid(uid_t uid, gid_t gid)
469 {
470     // Set the process uid and gid
471     int retsetuid = setuid(uid);
472     if (retsetuid != 0) {
473         LOG("ErrInfo: Set uid=%d failed, now uid=%d", uid, getuid());
474         return FALSE;
475     }
476     int retsetgid = setgid(gid);
477     if (retsetgid != 0) {
478         LOG("ErrInfo: Set gid=%d failed, now gid=%d", gid, getgid());
479         return FALSE;
480     }
481     return 0;
482 }
483 
CompareTime(timespec start,timespec end)484 timespec CompareTime(timespec start, timespec end)
485 {
486     // Compare time 'start' and time 'end'
487     timespec ret = { 0 };
488     int tp = 1000000000;
489     if ((end.tv_nsec - start.tv_nsec) < 0) {
490         ret.tv_sec = end.tv_sec - start.tv_sec - 1;
491         ret.tv_nsec = tp + end.tv_nsec - start.tv_nsec;
492     } else {
493         ret.tv_sec = end.tv_sec - start.tv_sec;
494         ret.tv_nsec = end.tv_nsec - start.tv_nsec;
495     }
496     return ret;
497 }
498 
GetCurrentPath()499 char *GetCurrentPath()
500 {
501     // Obtain the current working directory
502     static char path[MAX_PATH_SIZE];
503     getcwd(path, MAX_PATH_SIZE);
504     return path;
505 }
506 
507 // Check topDir file system, 0 is exist, -1 is non-exist
CheckFsMount(const char * topDir,const char * topDirMountInfo)508 int CheckFsMount(const char *topDir, const char *topDirMountInfo)
509 {
510     const int lenMax = 100;
511     int len = 0;
512     char buf[lenMax] = {0};
513     const char mountInfoFile[] = "/proc/mounts";
514     // check topDir exist
515     if (access(topDir, F_OK) != 0) {
516         LOG("ErrInfo: '%s' not accessable, Test Stop!", topDir);
517         return -1;
518     }
519     FILE *fp = fopen(mountInfoFile, "r");
520     if (fp != nullptr) {
521         while (fgets(buf, sizeof(buf), fp) != NULL) {
522             len = strlen(buf);
523             if (strstr(buf, topDirMountInfo) != nullptr) {
524                 fclose(fp);
525                 return 0;
526             }
527         }
528         fclose(fp);
529     }
530     LOG("'%s' is not a '%s' not mount properly, Test Stop! please change another file type!", topDir, topDirMountInfo);
531     return -1;
532 }