File: sigwait.c

package info (click to toggle)
valgrind 1%3A3.24.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 176,332 kB
  • sloc: ansic: 795,029; exp: 26,134; xml: 23,472; asm: 14,393; cpp: 9,397; makefile: 7,464; sh: 6,122; perl: 5,446; python: 1,498; javascript: 981; awk: 166; csh: 1
file content (68 lines) | stat: -rw-r--r-- 1,524 bytes parent folder | download | duplicates (3)
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
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

// from stack overflow
// https://stackoverflow.com/questions/6326290/about-the-ambiguous-description-of-sigwait
// except the deliberate errors

static int sig_count;

void on_sigusr1(int sig)
{
    ++sig_count;
}

int main(void)
{
  // Set a signal handler for SIGUSR1
  signal(SIGUSR1, &on_sigusr1);

  // At program startup, SIGUSR1 is neither blocked nor pending, so raising it
  // will call the signal handler
  raise(SIGUSR1);

  // Now let's block SIGUSR1
  sigset_t* psigset = malloc(sizeof(sigset_t));
  sigemptyset(psigset);
  sigaddset(psigset, SIGUSR1);
  sigprocmask(SIG_BLOCK, psigset, NULL);

  // SIGUSR1 is now blocked, raising it will not call the signal handler
  raise(SIGUSR1);

  // SIGUSR1 is now blocked and pending -- this call to sigwait will return
  // immediately
  int sig;
  int result = sigwait(psigset, &sig);
  if(result == 0)
    printf("sigwait got signal: %d\n", sig);

  // SIGUSR1 is now no longer pending (but still blocked).  Raise it again and
  // unblock it
  raise(SIGUSR1);
  printf("About to unblock SIGUSR1\n");
  sigprocmask(SIG_UNBLOCK, psigset, NULL);
  printf("Unblocked SIGUSR1\n");

  assert(sig_count == 2);

  // now a couple of bad params
  // reblock
  sigprocmask(SIG_BLOCK, psigset, NULL);
  raise(SIGUSR1);

  int* psig = malloc(sizeof(int));
  free(psig);
  result = sigwait(psigset, psig);

  free(psigset);

  raise(SIGUSR1);

  result = sigwait(psigset, &sig);

  return 0;
}