File: catch_main.cpp

package info (click to toggle)
posixsignalmanager 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: cpp: 16,089; python: 137; ansic: 43; sh: 27; makefile: 21
file content (96 lines) | stat: -rw-r--r-- 2,284 bytes parent folder | download | duplicates (2)
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
// SPDX-License-Identifier: BSL-1.0

#define CATCH_CONFIG_RUNNER
#define CATCH_CONFIG_NO_POSIX_SIGNALS
#ifndef BUNDLED_CATCH2
#ifdef CATCH3
#include "catch2/catch_all.hpp"
#else
#include "catch2/catch.hpp"
#endif
#else
#include "catch.hpp"
#endif

#include <sys/resource.h>
#include <sys/wait.h>
#include <signal.h>

#ifdef __linux__
#include <sys/prctl.h>
#endif

int main( int argc, char* argv[] ) {
    rlimit lim;
    getrlimit(RLIMIT_CORE, &lim);
    lim.rlim_cur = 0;
    setrlimit(RLIMIT_CORE, &lim);

#ifdef __linux__
    // For linux setting the rlimit is not enough to avoid core dump processing when
    // the /proc/sys/kernel/core_pattern is a pipe. In that case only PR_SET_DUMPABLE
    // reliably disables core dump processing.
    // But debugging is harder with dumpable=0, so check first if this system has the
    // problem.

    bool needPrctl = false;

    pid_t pid = fork();
    if (pid == -1) {
        perror("fork");
        return 2;
    }
    if (pid) {
        siginfo_t info;
        errno = 0;
        int r = waitid(P_ALL, 0, &info, WEXITED | WSTOPPED | WCONTINUED);
        if (r != 0) {
            perror("waitid");
            return 2;
        }
        if (info.si_pid != pid || info.si_status != SIGSEGV) {
            puts("Error in linux coredump check, exiting");
            return 2;
        }
        if (info.si_code == CLD_DUMPED) {
            needPrctl = true;
        }
    } else {
        int *p = (int*)32;
        *p = 42;
        _exit(99);
    }

    if (needPrctl) {
        if (prctl(PR_SET_DUMPABLE, 0) < 0) {
            perror("can't prctl(PR_SET_DUMPABLE)");
            exit(2);
        }
    }
#endif

    printf("NSIG: %d, SIGRTMAX: %d\n", NSIG,
#ifdef SIGRTMAX
           SIGRTMAX
#else
           -1
#endif
           );

    // Reset signal handling state to all default and nothing blocked

    sigset_t newmask, oldmask;
    sigemptyset(&newmask);
    sigprocmask(SIG_SETMASK, &newmask, &oldmask);
    for (int i = 1; i < NSIG; i++) {
        signal(i, SIG_DFL);
        if (sigismember(&oldmask, i)) {
            printf("signal masked %d\n", i);
        }
    }

    // Stash away stderr somewhere to go around redirections in test framework.
    dup2(1, 55);

    return Catch::Session().run( argc, argv );
}