80 lines · cpp
1// RUN: %clangxx -std=c++11 -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s2 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(&s);18 va_list va;19 va_start(va, n);20 while (n--) {21 res |= sigaddset(&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(&out, &first, &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(&out, &first, &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, SIGUSR1);45 kill(pid, SIGUSR2);46 int child_stat;47 wait(&child_stat);48 return !WIFEXITED(child_stat);49 } else {50 int sig;51 int res = sigwait(&s, &sig);52 assert(!res);53 fprintf(stderr, "died with sig %d\n", sig);54 _exit(0);55 }56}57 58void test_sigwait() {59 // test sigorset... s should now contain SIGUSR1 | SIGUSR260 sigset_t s = sigset_or(mkset(1, SIGUSR1), mkset(1, SIGUSR2));61 sigprocmask(SIG_BLOCK, &s, 0);62 int res;63 res = fork_and_signal(s);64 fprintf(stderr, "fork_and_signal with SIGUSR1,2: %d\n", res);65 // CHECK: died with sig {{10|30}}66 // CHECK: fork_and_signal with SIGUSR1,2: 067 68 // test sigandset... s should only have SIGUSR2 now69 s = sigset_and(s, mkset(1, SIGUSR2));70 res = fork_and_signal(s);71 fprintf(stderr, "fork_and_signal with SIGUSR2: %d\n", res);72 // CHECK: died with sig {{12|31}}73 // CHECK: fork_and_signal with SIGUSR2: 074}75 76int main(void) {77 test_sigwait();78 return 0;79}80