File: sigresend.c

package info (click to toggle)
valgrind 1%3A3.16.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 158,568 kB
  • sloc: ansic: 746,130; exp: 26,134; xml: 22,708; asm: 13,570; cpp: 7,691; makefile: 6,177; perl: 5,965; sh: 5,665; javascript: 929
file content (73 lines) | stat: -rw-r--r-- 1,734 bytes parent folder | download | duplicates (6)
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
/* Test to check that the mask parameter of the sigresend syscall is handled
   correctly. */

#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>

static void signal_handler(int signo, siginfo_t *info, void *uc)
{
   ssize_t written;
   const char str[] = "Signal caught.\n";
   size_t len = sizeof(str) - 1;
   sigset_t current;

   written = write(STDOUT_FILENO, str, len);
   assert(written == len);

   /* Check that SIGUSR1 is already blocked in the signal handler. */
   assert(!sigprocmask(SIG_BLOCK, NULL, &current));
   assert(sigismember(&current, SIGUSR1));
}

int main(void)
{
   sigset_t block, current;
   struct sigaction sa;

   /* Check that SIGUSR1 is unblocked. */
   if (sigprocmask(0, NULL, &current)) {
      perror("sigprocmask");
      return 1;
   }
   assert(!sigismember(&current, SIGUSR1));

   /* Establish a SIGINT handler. */
   sa.sa_sigaction = signal_handler;
   sa.sa_flags = SA_RESTART | SA_SIGINFO;
   if (sigfillset(&sa.sa_mask)) {
      perror("sigfillset");
      return 1;
   }
   if (sigaction(SIGINT, &sa, NULL)) {
      perror("sigaction");
      return 1;
   }

   /* Send us a signal to handle and install a new sigmask. */
   if (sigemptyset(&block)) {
      perror("sigemptyset");
      return 1;
   }
   if (sigaddset(&block, SIGUSR1)) {
      perror("sigaddset");
      return 1;
   }
   if (syscall(SYS_sigresend, SIGINT, NULL, &block)) {
      fprintf(stderr, "Sigresend failed.\n");
      return 1;
   }

   /* Check that SIGUSR1 is now blocked. */
   if (sigprocmask(SIG_BLOCK, NULL, &current)) {
      perror("sigprocmask");
      return 1;
   }
   assert(sigismember(&current, SIGUSR1));

   return 0;
}