1 /*
2 * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #include <cstdio>
31 #include <climits>
32 #include <gtest/gtest.h>
33 #include <sys/types.h>
34 #include <dirent.h>
35 #include <unistd.h>
36 #include "It_process_fs_test.h"
37
38 static const int STACK_SIZE = 1024 * 1024;
39 static const int CHILD_FUNC_ARG = 0x2088;
40
child_process(void *arg)41 static int child_process(void *arg)
42 {
43 (void)arg;
44 pid_t pid = getpid();
45 std::ostringstream buf;
46 buf << "/proc/" << pid;
47 DIR *dirp = opendir(buf.str().data());
48 if (dirp == nullptr) {
49 return 1;
50 }
51
52 (void)closedir(dirp);
53 return 0;
54 }
55
ItProcessFs012(void)56 void ItProcessFs012(void)
57 {
58 int ret = 0;
59 int status;
60 char *stack = (char *)mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
61 -1, 0);
62 ASSERT_NE(stack, nullptr);
63 char *stackTop = stack + STACK_SIZE;
64 int arg = CHILD_FUNC_ARG;
65 pid_t pid = clone(child_process, stackTop, SIGCHLD, &arg);
66 ASSERT_NE(pid, -1);
67
68 ret = waitpid(pid, &status, 0);
69 ASSERT_EQ(ret, pid);
70
71 ret = WIFEXITED(status);
72 int exitCode = WEXITSTATUS(status);
73 ASSERT_EQ(exitCode, 0);
74
75 std::ostringstream buf;
76 buf << "/proc/" << pid;
77 DIR *dirp = opendir(buf.str().data());
78 ASSERT_EQ(dirp, nullptr);
79 }
80