summaryrefslogtreecommitdiffstats
path: root/src/util/sigdelay.c
blob: 6f07a22acfc606f6d866aeacf71f249e8d4804f7 (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*++
/* NAME
/*	sigdelay 3
/* SUMMARY
/*	delay/resume signal delivery
/* SYNOPSIS
/*	#include <sigdelay.h>
/*
/*	void	sigdelay()
/*
/*	void	sigresume()
/* DESCRIPTION
/*	sigdelay() delays delivery of signals. Signals that
/*	arrive in the mean time will be queued.
/*
/*	sigresume() resumes delivery of signals. Signals that have
/*	arrived in the mean time will be delivered.
/* DIAGNOSTICS
/*	All errors are fatal.
/* BUGS
/*	The signal queue may be really short (as in: one per signal type).
/*
/*	Some signals such as SIGKILL cannot be blocked.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include <sys_defs.h>
#include <signal.h>

/* Utility library. */

#include "msg.h"
#include "posix_signals.h"
#include "sigdelay.h"

/* Application-specific. */

static sigset_t saved_sigmask;
static sigset_t block_sigmask;
static int suspending;
static int siginit_done;

/* siginit - compute signal mask only once */

static void siginit(void)
{
    int     sig;

    siginit_done = 1;
    sigemptyset(&block_sigmask);
    for (sig = 1; sig < NSIG; sig++)
	sigaddset(&block_sigmask, sig);
}

/* sigresume - deliver delayed signals and disable signal delay */

void    sigresume(void)
{
    if (suspending != 0) {
	suspending = 0;
	if (sigprocmask(SIG_SETMASK, &saved_sigmask, (sigset_t *) 0) < 0)
	    msg_fatal("sigresume: sigprocmask: %m");
    }
}

/* sigdelay - save signal mask and block all signals */

void    sigdelay(void)
{
    if (siginit_done == 0)
	siginit();
    if (suspending == 0) {
	suspending = 1;
	if (sigprocmask(SIG_BLOCK, &block_sigmask, &saved_sigmask) < 0)
	    msg_fatal("sigdelay: sigprocmask: %m");
    }
}

#ifdef TEST

 /*
  * Test program - press Ctrl-C twice while signal delivery is delayed, and
  * see how many signals are delivered when signal delivery is resumed.
  */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

static void gotsig(int sig)
{
    printf("Got signal %d\n", sig);
}

int     main(int unused_argc, char **unused_argv)
{
    signal(SIGINT, gotsig);
    signal(SIGQUIT, gotsig);

    printf("Delaying signal delivery\n");
    sigdelay();
    sleep(5);
    printf("Resuming signal delivery\n");
    sigresume();
    exit(0);
}

#endif