summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/io/qprocess/testUnixProcessParameters/main.cpp
blob: 42a173debe6f2d099e3bcb562b01050ced854192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (C) 2023 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <string_view>

#include <errno.h>
#include <fcntl.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    if (argc < 2) {
        printf("Usage: %s command [extra]\nSee source code for commands\n",
               argv[0]);
        return EXIT_FAILURE;
    }

    std::string_view cmd = argv[1];
    errno = 0;

    if (cmd.size() == 0) {
        // just checking that we did get here
        return EXIT_SUCCESS;
    }

    if (cmd == "reset-ids") {
        if (getuid() == geteuid() && getgid() == getegid())
            return EXIT_SUCCESS;
        fprintf(stderr, "Real: %d %d; Effective: %d %d\n",
                getuid(), getgid(), geteuid(), getegid());
        return EXIT_FAILURE;
    }

    if (cmd == "reset-sighand") {
        bool ok = true;

        // confirm our signal block mask is empty
        sigset_t set;
        sigprocmask(SIG_SETMASK, nullptr, &set);
        for (int signo = 1; signo < NSIG; ++signo) {
            if (sigismember(&set, signo)) {
                fprintf(stderr, "'%s' is blocked.\n", strsignal(signo));
                ok = false;
            }
        }

        // confirm SIGUSR1 was not ignored
        struct sigaction action;
        sigaction(SIGUSR1, nullptr, &action);
        if (action.sa_handler != SIG_DFL) {
            fprintf(stderr, "SIGUSR1 is SIG_IGN\n");
            ok = false;
        }
        return ok ? EXIT_SUCCESS : EXIT_FAILURE;
    }

    if (cmd == "ignore-sigpipe") {
        // confirm SIGPIPE was ignored
        struct sigaction action;
        sigaction(SIGPIPE, nullptr, &action);
        if (action.sa_handler == SIG_IGN)
            return EXIT_SUCCESS;
        fprintf(stderr, "SIGPIPE is SIG_DFL\n");
        return EXIT_FAILURE;
    }

    if (cmd == "file-descriptors") {
        int fd = atoi(argv[2]);
        if (close(fd) < 0 && errno == EBADF)
            return EXIT_SUCCESS;
        fprintf(stderr, "%d is a valid file descriptor\n", fd);
        return EXIT_FAILURE;
    }

    if (cmd == "file-descriptors2") {
        int fd1 = atoi(argv[2]);    // should be open
        int fd2 = atoi(argv[3]);    // should be closed
        if (close(fd1) < 0)
            fprintf(stderr, "%d was not a valid file descriptor\n", fd1);
        if (close(fd2) == 0 || errno != EBADF)
            fprintf(stderr, "%d is a valid file descriptor\n", fd2);
        return EXIT_SUCCESS;
    }

    if (cmd == "noctty") {
        int fd = open("/dev/tty", O_RDONLY);
        if (fd == -1)
            return EXIT_SUCCESS;
        fprintf(stderr, "Could open /dev/tty\n");
        return EXIT_FAILURE;
    }

    if (cmd == "setsid") {
        pid_t pgid = getpgrp();
        if (pgid == getpid())
            return EXIT_SUCCESS;
        fprintf(stderr, "Process group was %d\n", pgid);
        return EXIT_FAILURE;
    }

    fprintf(stderr, "Unknown command \"%s\"", cmd.data());
    return EXIT_FAILURE;
}