1// RUN: %clangxx -std=c++11 -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
2
3// sigandset is glibc specific.
4// UNSUPPORTED: android, target={{.*(freebsd|netbsd).*}}
5
6#include <assert.h>
7#include <signal.h>
8#include <stdarg.h>
9#include <stdio.h>
10#include <sys/time.h>
11#include <sys/wait.h>
12#include <unistd.h>
13
14sigset_t mkset(int n, ...) {
15 sigset_t s;
16 int res = 0;
17 res |= sigemptyset(set: &s);
18 va_list va;
19 va_start(va, n);
20 while (n--) {
21 res |= sigaddset(set: &s, va_arg(va, int));
22 }
23 va_end(va);
24 assert(!res);
25 return s;
26}
27
28sigset_t sigset_or(sigset_t first, sigset_t second) {
29 sigset_t out;
30 int res = sigorset(set: &out, left: &first, right: &second);
31 assert(!res);
32 return out;
33}
34
35sigset_t sigset_and(sigset_t first, sigset_t second) {
36 sigset_t out;
37 int res = sigandset(set: &out, left: &first, right: &second);
38 assert(!res);
39 return out;
40}
41
42int fork_and_signal(sigset_t s) {
43 if (pid_t pid = fork()) {
44 kill(pid: pid, SIGUSR1);
45 kill(pid: pid, SIGUSR2);
46 int child_stat;
47 wait(stat_loc: &child_stat);
48 return !WIFEXITED(child_stat);
49 } else {
50 int sig;
51 int res = sigwait(set: &s, sig: &sig);
52 assert(!res);
53 fprintf(stderr, format: "died with sig %d\n", sig);
54 _exit(status: 0);
55 }
56}
57
58void test_sigwait() {
59 // test sigorset... s should now contain SIGUSR1 | SIGUSR2
60 sigset_t s = sigset_or(first: mkset(n: 1, SIGUSR1), second: mkset(n: 1, SIGUSR2));
61 sigprocmask(SIG_BLOCK, set: &s, oset: 0);
62 int res;
63 res = fork_and_signal(s);
64 fprintf(stderr, format: "fork_and_signal with SIGUSR1,2: %d\n", res);
65 // CHECK: died with sig 10
66 // CHECK: fork_and_signal with SIGUSR1,2: 0
67
68 // test sigandset... s should only have SIGUSR2 now
69 s = sigset_and(first: s, second: mkset(n: 1, SIGUSR2));
70 res = fork_and_signal(s);
71 fprintf(stderr, format: "fork_and_signal with SIGUSR2: %d\n", res);
72 // CHECK: died with sig 12
73 // CHECK: fork_and_signal with SIGUSR2: 0
74}
75
76int main(void) {
77 test_sigwait();
78 return 0;
79}
80

source code of compiler-rt/test/sanitizer_common/TestCases/Linux/signal_send.cpp