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 <unistd.h>
17#include <signal.h>
18#include <sys/wait.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include "functionalext.h"
22
23static int exit_flag = 0;
24
25void ChildFunc(int signal)
26{
27    switch (signal) {
28        case SIGCHLD:
29            exit_flag = 1;
30            break;
31        default:
32            break;
33    }
34    return;
35}
36
37/**
38 * @tc.name      : killpg_0100
39 * @tc.desc      : Determine whether the child process successfully receives the signal to
40 *                 the main process and respond accordingly
41 * @tc.level     : Level 1
42 */
43void killpg_0100(void)
44{
45    pid_t pid;
46    int status;
47    sigset_t sigset;
48    sigemptyset(&sigset);
49    sigaddset(&sigset, SIGCHLD);
50    sigprocmask(SIG_BLOCK, &sigset, NULL);
51    pid = fork();
52    if (pid == 0) {
53        sleep(1);
54        int ret = killpg(getpgrp(), SIGCHLD);
55        EXPECT_EQ("killpg_0100", ret, 0);
56        exit(EXIT_SUCCESS);
57    } else if (pid > 0) {
58        signal(SIGCHLD, ChildFunc);
59        sigprocmask(SIG_UNBLOCK, &sigset, NULL);
60        while (!exit_flag) {
61            sleep(1);
62        }
63        wait(NULL);
64        EXPECT_EQ("killpg_0100", exit_flag, 1);
65    } else {
66        printf("Fork wrong\n");
67        exit(EXIT_FAILURE);
68    }
69}
70
71/**
72 * @tc.name      : killpg_0200
73 * @tc.desc      : Outlier judgment
74 * @tc.level     : Level 2
75 */
76void killpg_0200(void)
77{
78    int ret = killpg(-1, -1);
79    EXPECT_EQ("killpg_0200", ret, -1);
80}
81
82int main(void)
83{
84    killpg_0100();
85    killpg_0200();
86    return t_status;
87}