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#include <errno.h>
16#include <signal.h>
17#include <stdlib.h>
18#include <string.h>
19#include <sys/stat.h>
20#include <sys/wait.h>
21#include <sigchain.h>
22#include "fortify_test.h"
23#include "test.h"
24
25/**
26 * @tc.name     : umask_0010
27 * @tc.desc     : test umask normal condition
28 * @tc.level    : Level 0
29 */
30static void umask_0010(void)
31{
32    mode_t mask = atoi("077");
33    umask(mask);
34
35    return;
36}
37
38/**
39 * @tc.name     : umask_0020
40 * @tc.desc     : test umask suppress compiler optimizations
41 * @tc.level    : Level 2
42 */
43static void umask_0020(void)
44{
45    struct sigaction sigabrt = {
46        .sa_handler = SignalHandler,
47    };
48    sigaction(SIGABRT, &sigabrt, NULL);
49
50    mode_t mask = atoi("1023");  // 01777 in octal
51    int status;
52    int pid = fork();
53    switch (pid) {
54        case -1:
55            t_error("fork failed: %s\n", strerror(errno));
56            break;
57        case 0:
58            umask(mask);
59            exit(0);
60        default:
61            waitpid(pid, &status, WUNTRACED);
62            TEST(WIFEXITED(status) == 0);
63            TEST(WIFSTOPPED(status) == 1);
64            TEST(WSTOPSIG(status) == SIGSTOP);
65            kill(pid, SIGCONT);
66            break;
67    }
68
69    return;
70}
71
72int main(int argc, char *argv[]) {
73    remove_all_special_handler(SIGABRT);
74    umask_0010();
75    umask_0020();
76
77    return t_status;
78}