1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci// tested api in this file: getpid getppid getpgrp setpgrp  getpgid setpgid
17f6603c60Sopenharmony_ci
18f6603c60Sopenharmony_ci#include <unistd.h>
19f6603c60Sopenharmony_ci#include <sys/types.h>
20f6603c60Sopenharmony_ci#include <sys/shm.h>
21f6603c60Sopenharmony_ci#include <gtest/gtest.h>
22f6603c60Sopenharmony_ci#include "log.h"
23f6603c60Sopenharmony_ci#include "utils.h"
24f6603c60Sopenharmony_ci#include "KernelConstants.h"
25f6603c60Sopenharmony_ci
26f6603c60Sopenharmony_ciusing namespace testing::ext;
27f6603c60Sopenharmony_ci
28f6603c60Sopenharmony_ciclass PidTest : public testing::Test {
29f6603c60Sopenharmony_ciprotected:
30f6603c60Sopenharmony_ci    const char* mChildELF = RES_DIR_KERNEL "process/executor";
31f6603c60Sopenharmony_ci};
32f6603c60Sopenharmony_ci
33f6603c60Sopenharmony_ci/**
34f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_PM_PID_Getppid_0100
35f6603c60Sopenharmony_ci * @tc.name   getpid and getppid test
36f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
37f6603c60Sopenharmony_ci */
38f6603c60Sopenharmony_ciHWTEST_F(PidTest, testGetPpid, Function | MediumTest | Level1)
39f6603c60Sopenharmony_ci{
40f6603c60Sopenharmony_ci    pid_t parentPid = getpid();
41f6603c60Sopenharmony_ci    pid_t pid = fork();
42f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
43f6603c60Sopenharmony_ci    if (pid == 0) { // child
44f6603c60Sopenharmony_ci        pid_t pPid = getppid();
45f6603c60Sopenharmony_ci        if (pPid != parentPid) {
46f6603c60Sopenharmony_ci            LOG("getppid fail, expect:%d, but get:%d", parentPid, pPid);
47f6603c60Sopenharmony_ci            exit(1);
48f6603c60Sopenharmony_ci        }
49f6603c60Sopenharmony_ci        exit(0);
50f6603c60Sopenharmony_ci    } else { // parent
51f6603c60Sopenharmony_ci        Msleep(20);
52f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
53f6603c60Sopenharmony_ci    }
54f6603c60Sopenharmony_ci}
55f6603c60Sopenharmony_ci
56f6603c60Sopenharmony_ci/**
57f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_PM_PID_Getppid_0200
58f6603c60Sopenharmony_ci * @tc.name   test that a orphaned process will inherite by init.
59f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
60f6603c60Sopenharmony_ci */
61f6603c60Sopenharmony_ciHWTEST_F(PidTest, testOrphanProcess, Function | MediumTest | Level1)
62f6603c60Sopenharmony_ci{
63f6603c60Sopenharmony_ci    const int retPass = 1;
64f6603c60Sopenharmony_ci    const int retFail = 2;
65f6603c60Sopenharmony_ci    const int memSize = 32;
66f6603c60Sopenharmony_ci    int shmID = shmget(IPC_PRIVATE, memSize, 0666 | IPC_CREAT);
67f6603c60Sopenharmony_ci    ASSERT_NE(shmID, -1) << "get share mem fail, errno = " << errno;
68f6603c60Sopenharmony_ci    int *shared = (int*)(shmat(shmID, nullptr, 0));
69f6603c60Sopenharmony_ci    ASSERT_NE(shared, reinterpret_cast<int*>(-1)) << "shmat fail, errno = " << errno;
70f6603c60Sopenharmony_ci    shared[0] = retPass;
71f6603c60Sopenharmony_ci    shared[1] = 0;
72f6603c60Sopenharmony_ci
73f6603c60Sopenharmony_ci    LOG("parent process id:%d", getpid());
74f6603c60Sopenharmony_ci    pid_t pid = fork();
75f6603c60Sopenharmony_ci    EXPECT_TRUE(pid >= 0) << "======== Fork Error! =========";
76f6603c60Sopenharmony_ci    if (pid == 0) { // child
77f6603c60Sopenharmony_ci        LOG("sub parent process id:%d", getpid());
78f6603c60Sopenharmony_ci        pid_t pid2 = fork();
79f6603c60Sopenharmony_ci        if (pid2 < 0) {
80f6603c60Sopenharmony_ci            LOG("======== Fork Error! =========");
81f6603c60Sopenharmony_ci            exit(1);
82f6603c60Sopenharmony_ci        }
83f6603c60Sopenharmony_ci
84f6603c60Sopenharmony_ci        if (pid2 == 0) { // child
85f6603c60Sopenharmony_ci            LOG("orphane process id:%d", getpid());
86f6603c60Sopenharmony_ci            int *shmAddr = (int*)(shmat(shmID, nullptr, 0));
87f6603c60Sopenharmony_ci            LOG("before while child child %d", shmAddr[1]);
88f6603c60Sopenharmony_ci            while (shmAddr[1] != 1) {
89f6603c60Sopenharmony_ci                Msleep(50);
90f6603c60Sopenharmony_ci            }
91f6603c60Sopenharmony_ci            LOG("after while child child %d", shmAddr[1]);
92f6603c60Sopenharmony_ci            pid_t pPid = getppid();
93f6603c60Sopenharmony_ci            if (pPid != 1) {
94f6603c60Sopenharmony_ci                LOG("getppid orphaned process fail, expect:1, but get:%d", pPid);
95f6603c60Sopenharmony_ci                // transfer result to main process
96f6603c60Sopenharmony_ci                shmAddr[0] = retFail;
97f6603c60Sopenharmony_ci            }
98f6603c60Sopenharmony_ci            shmAddr[1] =2;
99f6603c60Sopenharmony_ci            LOG("child child exit %d", shmAddr[1]);
100f6603c60Sopenharmony_ci            shmdt(shmAddr);
101f6603c60Sopenharmony_ci            exit(0);
102f6603c60Sopenharmony_ci        } else { // sub parent
103f6603c60Sopenharmony_ci            exit(0);
104f6603c60Sopenharmony_ci        }
105f6603c60Sopenharmony_ci    }
106f6603c60Sopenharmony_ci    // parent
107f6603c60Sopenharmony_ci    WaitProcExitedOK(pid);
108f6603c60Sopenharmony_ci    shared[1] = 1;
109f6603c60Sopenharmony_ci    Msleep(200);
110f6603c60Sopenharmony_ci    EXPECT_EQ(shared[0], retPass);
111f6603c60Sopenharmony_ci    LOG("before while paret %d", shared[1]);
112f6603c60Sopenharmony_ci    while (shared[1] != 2) {
113f6603c60Sopenharmony_ci        Msleep(50);
114f6603c60Sopenharmony_ci    }
115f6603c60Sopenharmony_ci    shmdt(shared);
116f6603c60Sopenharmony_ci    shmctl(shmID, IPC_RMID, nullptr);
117f6603c60Sopenharmony_ci}
118f6603c60Sopenharmony_ci
119f6603c60Sopenharmony_ci/**
120f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_PM_PID_SetGetPgrp_0100
121f6603c60Sopenharmony_ci * @tc.name   setpgrp and getpgrp test
122f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
123f6603c60Sopenharmony_ci */
124f6603c60Sopenharmony_ciHWTEST_F(PidTest, testSetGetPgrp, Function | MediumTest | Level1)
125f6603c60Sopenharmony_ci{
126f6603c60Sopenharmony_ci    pid_t parentPid = getpid();
127f6603c60Sopenharmony_ci    int rt = setpgrp();
128f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
129f6603c60Sopenharmony_ci
130f6603c60Sopenharmony_ci    pid_t pgid = getpgrp();
131f6603c60Sopenharmony_ci    ASSERT_EQ(pgid, parentPid);
132f6603c60Sopenharmony_ci
133f6603c60Sopenharmony_ci    pid_t pid = fork();
134f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
135f6603c60Sopenharmony_ci    if (pid == 0) { // child
136f6603c60Sopenharmony_ci        pid_t childPgid = getpgrp();
137f6603c60Sopenharmony_ci        if (childPgid != pgid) {
138f6603c60Sopenharmony_ci            LOG("child getpgrp fail, expect:%d, but get:%d", pgid, childPgid);
139f6603c60Sopenharmony_ci            exit(1);
140f6603c60Sopenharmony_ci        }
141f6603c60Sopenharmony_ci        exit(0);
142f6603c60Sopenharmony_ci    } else { // parent
143f6603c60Sopenharmony_ci        Msleep(20);
144f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
145f6603c60Sopenharmony_ci    }
146f6603c60Sopenharmony_ci}
147f6603c60Sopenharmony_ci
148f6603c60Sopenharmony_ci/**
149f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_PM_PID_SetGetPgid_0100
150f6603c60Sopenharmony_ci * @tc.name   setpgid and getpgid basic test
151f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
152f6603c60Sopenharmony_ci */
153f6603c60Sopenharmony_ciHWTEST_F(PidTest, testSetGetPgid, Function | MediumTest | Level1)
154f6603c60Sopenharmony_ci{
155f6603c60Sopenharmony_ci    pid_t parentPid = getpid();
156f6603c60Sopenharmony_ci    int rt = setpgid(0, parentPid);
157f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
158f6603c60Sopenharmony_ci    rt = setpgid(0, 0);
159f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
160f6603c60Sopenharmony_ci
161f6603c60Sopenharmony_ci    pid_t pgid = getpgid(0);
162f6603c60Sopenharmony_ci    ASSERT_EQ(pgid, parentPid);
163f6603c60Sopenharmony_ci
164f6603c60Sopenharmony_ci    pid_t pid1 = fork();
165f6603c60Sopenharmony_ci    ASSERT_TRUE(pid1 >= 0) << "======== Fork Error! =========";
166f6603c60Sopenharmony_ci    if (pid1 == 0) { // child
167f6603c60Sopenharmony_ci        pid_t childPgid = getpgid(0);
168f6603c60Sopenharmony_ci        if (childPgid != pgid) {
169f6603c60Sopenharmony_ci            LOG("child getpgid fail, expect:%d, but get:%d", pgid, childPgid);
170f6603c60Sopenharmony_ci            exit(1);
171f6603c60Sopenharmony_ci        }
172f6603c60Sopenharmony_ci        Msleep(40);
173f6603c60Sopenharmony_ci        childPgid = getpgid(0);
174f6603c60Sopenharmony_ci        pid_t childPid = getpid();
175f6603c60Sopenharmony_ci        if (childPgid != childPid) {
176f6603c60Sopenharmony_ci            LOG("child new pgid check fail, pid=%d, pgid:%d", childPid, childPgid);
177f6603c60Sopenharmony_ci            exit(1);
178f6603c60Sopenharmony_ci        }
179f6603c60Sopenharmony_ci        exit(0);
180f6603c60Sopenharmony_ci    } else { // parent
181f6603c60Sopenharmony_ci        Msleep(30);
182f6603c60Sopenharmony_ci        rt = setpgid(pid1, pid1);
183f6603c60Sopenharmony_ci        ASSERT_EQ(rt, 0);
184f6603c60Sopenharmony_ci        pid_t pgid2 = getpgid(pid1);
185f6603c60Sopenharmony_ci        ASSERT_EQ(pgid2, pid1);
186f6603c60Sopenharmony_ci        Msleep(50);
187f6603c60Sopenharmony_ci        WaitProcExitedOK(pid1);
188f6603c60Sopenharmony_ci    }
189f6603c60Sopenharmony_ci}
190f6603c60Sopenharmony_ci/**
191f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_PM_PID_SetGetPgid_0200
192f6603c60Sopenharmony_ci * @tc.name   setpgid and getpgid test for sibling process
193f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
194f6603c60Sopenharmony_ci */
195f6603c60Sopenharmony_ciHWTEST_F(PidTest, testSetGetSiblingPgid, Function | MediumTest | Level1)
196f6603c60Sopenharmony_ci{
197f6603c60Sopenharmony_ci    pid_t parentPid = getpid();
198f6603c60Sopenharmony_ci    int rt = setpgid(0, parentPid);
199f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
200f6603c60Sopenharmony_ci    rt = setpgid(0, 0);
201f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
202f6603c60Sopenharmony_ci    pid_t pgid = getpgid(0);
203f6603c60Sopenharmony_ci    ASSERT_EQ(pgid, parentPid);
204f6603c60Sopenharmony_ci
205f6603c60Sopenharmony_ci    pid_t pid1 = fork();
206f6603c60Sopenharmony_ci    ASSERT_TRUE(pid1 >= 0) << "======== Fork Error! =========";
207f6603c60Sopenharmony_ci    if (pid1 == 0) { // child1
208f6603c60Sopenharmony_ci        Msleep(20);
209f6603c60Sopenharmony_ci        exit(0);
210f6603c60Sopenharmony_ci    } else { // parent
211f6603c60Sopenharmony_ci        pid_t pid2 = fork();
212f6603c60Sopenharmony_ci        ASSERT_TRUE(pid2 >= 0) << "======== Fork Error! =========";
213f6603c60Sopenharmony_ci        if (pid2 == 0) { // child2
214f6603c60Sopenharmony_ci            int exitCode = 0;
215f6603c60Sopenharmony_ci            pid_t siblingPgid = getpgid(pid1);
216f6603c60Sopenharmony_ci            if (siblingPgid != parentPid) {
217f6603c60Sopenharmony_ci                LOG("child2: get sibling pgid fail, rt=%d, errno=%d", siblingPgid, errno);
218f6603c60Sopenharmony_ci                exitCode = 1;
219f6603c60Sopenharmony_ci            }
220f6603c60Sopenharmony_ci            rt = setpgid(pid1, pid1);
221f6603c60Sopenharmony_ci            if (rt != -1) {
222f6603c60Sopenharmony_ci                LOG("child2: setpgid for sibling should fail");
223f6603c60Sopenharmony_ci                exitCode = 1;
224f6603c60Sopenharmony_ci            }
225f6603c60Sopenharmony_ci            if (errno != ESRCH) {
226f6603c60Sopenharmony_ci                LOG("child2: setpgid errno fail, expected %d, but get %d", ESRCH, errno);
227f6603c60Sopenharmony_ci                exitCode = 1;
228f6603c60Sopenharmony_ci            }
229f6603c60Sopenharmony_ci            exit(exitCode);
230f6603c60Sopenharmony_ci        }
231f6603c60Sopenharmony_ci        // parent
232f6603c60Sopenharmony_ci        Msleep(50);
233f6603c60Sopenharmony_ci        WaitProcExitedOK(pid1);
234f6603c60Sopenharmony_ci        WaitProcExitedOK(pid2);
235f6603c60Sopenharmony_ci    }
236f6603c60Sopenharmony_ci}
237f6603c60Sopenharmony_ci/**
238f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_PM_PID_SetGetPgid_0300
239f6603c60Sopenharmony_ci * @tc.name   getpgid fail test
240f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
241f6603c60Sopenharmony_ci */
242f6603c60Sopenharmony_ciHWTEST_F(PidTest, testGetpgidFail, Function | MediumTest | Level3)
243f6603c60Sopenharmony_ci{
244f6603c60Sopenharmony_ci    pid_t pgid = getpgid(-1);
245f6603c60Sopenharmony_ci    EXPECT_EQ(pgid, -1);
246f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
247f6603c60Sopenharmony_ci
248f6603c60Sopenharmony_ci    pgid = getpgid(-1000001);
249f6603c60Sopenharmony_ci    EXPECT_EQ(pgid, -1);
250f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
251f6603c60Sopenharmony_ci
252f6603c60Sopenharmony_ci    pid_t nonExitPid = GetNonExistPid(); // valid but not exist pid
253f6603c60Sopenharmony_ci    if (nonExitPid != -1) {
254f6603c60Sopenharmony_ci        pgid = getpgid(nonExitPid);
255f6603c60Sopenharmony_ci        EXPECT_EQ(pgid, -1);
256f6603c60Sopenharmony_ci        EXPECT_EQ(errno, ESRCH);
257f6603c60Sopenharmony_ci    }
258f6603c60Sopenharmony_ci
259f6603c60Sopenharmony_ci    pgid = getpgid(MAX_PROCESS_NUMBER + 1);
260f6603c60Sopenharmony_ci    EXPECT_EQ(pgid, -1);
261f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
262f6603c60Sopenharmony_ci
263f6603c60Sopenharmony_ci    pgid = getpgid(100000);
264f6603c60Sopenharmony_ci    EXPECT_EQ(pgid, -1);
265f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
266f6603c60Sopenharmony_ci
267f6603c60Sopenharmony_ci    pgid = getpgid(1);
268f6603c60Sopenharmony_ci    EXPECT_EQ(pgid, 1) << "get init process-groups-ID fail\n";
269f6603c60Sopenharmony_ci}
270f6603c60Sopenharmony_ci
271f6603c60Sopenharmony_ci/**
272f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_PM_PID_SetGetPgid_0400
273f6603c60Sopenharmony_ci * @tc.name   setpgid fail test
274f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
275f6603c60Sopenharmony_ci */
276f6603c60Sopenharmony_ciHWTEST_F(PidTest, testSetpgidFail, Function | MediumTest | Level3)
277f6603c60Sopenharmony_ci{
278f6603c60Sopenharmony_ci    pid_t pid = getpid();
279f6603c60Sopenharmony_ci    int rt = setpgrp();
280f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
281f6603c60Sopenharmony_ci
282f6603c60Sopenharmony_ci    LOG("invalid pid test...");
283f6603c60Sopenharmony_ci    rt = setpgid(-1, 0);
284f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
285f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
286f6603c60Sopenharmony_ci
287f6603c60Sopenharmony_ci    rt = setpgid(-1000001, 0);
288f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
289f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
290f6603c60Sopenharmony_ci
291f6603c60Sopenharmony_ci    pid_t nonExitPid = GetNonExistPid(); // valid but not exist pid
292f6603c60Sopenharmony_ci    if (nonExitPid != -1) {
293f6603c60Sopenharmony_ci        rt = setpgid(nonExitPid, 0);
294f6603c60Sopenharmony_ci        EXPECT_EQ(rt, -1);
295f6603c60Sopenharmony_ci        EXPECT_EQ(errno, ESRCH);
296f6603c60Sopenharmony_ci    }
297f6603c60Sopenharmony_ci
298f6603c60Sopenharmony_ci    rt = setpgid(MAX_PROCESS_NUMBER + 1, 0);
299f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
300f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
301f6603c60Sopenharmony_ci
302f6603c60Sopenharmony_ci    rt = setpgid(100000, 0);
303f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
304f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
305f6603c60Sopenharmony_ci
306f6603c60Sopenharmony_ci    rt = setpgid(1, pid); // init
307f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
308f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EPERM);
309f6603c60Sopenharmony_ci
310f6603c60Sopenharmony_ci    rt = setpgid(2, pid); // kProcess
311f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
312f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EPERM);
313f6603c60Sopenharmony_ci
314f6603c60Sopenharmony_ci    LOG("invalid pgid test...");
315f6603c60Sopenharmony_ci    rt = setpgid(0, -1);
316f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
317f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
318f6603c60Sopenharmony_ci
319f6603c60Sopenharmony_ci    nonExitPid = GetNonExistPid(); // valid but not exist pid
320f6603c60Sopenharmony_ci    if (nonExitPid != -1) {
321f6603c60Sopenharmony_ci        rt = setpgid(0, nonExitPid);
322f6603c60Sopenharmony_ci        EXPECT_EQ(rt, -1);
323f6603c60Sopenharmony_ci        EXPECT_EQ(errno, EPERM);
324f6603c60Sopenharmony_ci    }
325f6603c60Sopenharmony_ci
326f6603c60Sopenharmony_ci    rt = setpgid(0, 10001);
327f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
328f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
329f6603c60Sopenharmony_ci
330f6603c60Sopenharmony_ci    rt = setpgid(0, 1); // init
331f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
332f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EPERM);
333f6603c60Sopenharmony_ci
334f6603c60Sopenharmony_ci    rt = setpgid(0, 2); // kProcess
335f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
336f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EPERM);
337f6603c60Sopenharmony_ci}
338f6603c60Sopenharmony_ci
339f6603c60Sopenharmony_ci/**
340f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_PM_PID_SetGetPgid_0500
341f6603c60Sopenharmony_ci * @tc.name   setpgid fail test: EACCES
342f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
343f6603c60Sopenharmony_ci */
344f6603c60Sopenharmony_ciHWTEST_F(PidTest, testSetpgidFailEACCES, Function | MediumTest | Level3)
345f6603c60Sopenharmony_ci{
346f6603c60Sopenharmony_ci    pid_t pid = fork();
347f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
348f6603c60Sopenharmony_ci    if (pid == 0) { // child
349f6603c60Sopenharmony_ci        int rt = execve(mChildELF, NULL, NULL);
350f6603c60Sopenharmony_ci        if (rt == -1) {
351f6603c60Sopenharmony_ci            LOG("ERROR: execve return -1, errno=%d, err=%s\n", errno, strerror(errno));
352f6603c60Sopenharmony_ci        }
353f6603c60Sopenharmony_ci        exit(1);
354f6603c60Sopenharmony_ci    } else { // parent
355f6603c60Sopenharmony_ci        sleep(2);
356f6603c60Sopenharmony_ci        AssertProcAlive(pid);
357f6603c60Sopenharmony_ci        int rt = setpgid(pid, pid);
358f6603c60Sopenharmony_ci        EXPECT_EQ(rt, -1) << "setpgid should fail after child execve.";
359f6603c60Sopenharmony_ci        EXPECT_EQ(errno, EACCES) << "set errno fail.";
360f6603c60Sopenharmony_ci
361f6603c60Sopenharmony_ci        // recycle child
362f6603c60Sopenharmony_ci        int status;
363f6603c60Sopenharmony_ci        waitpid(pid, &status, 0);
364f6603c60Sopenharmony_ci    }
365f6603c60Sopenharmony_ci}
366