xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/killme_after.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: killme_after.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	killme_after 3
6 /* SUMMARY
7 /*	programmed death
8 /* SYNOPSIS
9 /*	#include <killme_after.h>
10 /*
11 /*	void	killme_after(seconds)
12 /*	unsigned int seconds;
13 /* DESCRIPTION
14 /*	The killme_after() function does a best effort to terminate
15 /*	the process after the specified time, should it still exist.
16 /*	It is meant to be used in a signal handler, as an insurance
17 /*	against getting stuck somewhere while preparing for exit.
18 /* DIAGNOSTICS
19 /*	None. This routine does a best effort, damn the torpedoes.
20 /* LICENSE
21 /* .ad
22 /* .fi
23 /*	The Secure Mailer license must be distributed with this software.
24 /* AUTHOR(S)
25 /*	Wietse Venema
26 /*	IBM T.J. Watson Research
27 /*	P.O. Box 704
28 /*	Yorktown Heights, NY 10598, USA
29 /*--*/
30 
31 /* System library. */
32 
33 #include <sys_defs.h>
34 #include <signal.h>
35 #include <unistd.h>
36 
37 /* Utility library. */
38 
39 #include <killme_after.h>
40 
41 /* killme_after - self-assured death */
42 
43 void    killme_after(unsigned int seconds)
44 {
45     struct sigaction sig_action;
46 
47     /*
48      * Schedule an ALARM signal, and make sure the signal will be delivered
49      * even if we are being called from a signal handler and SIGALRM delivery
50      * is blocked.
51      */
52     alarm(0);
53     sigemptyset(&sig_action.sa_mask);
54     sig_action.sa_flags = 0;
55     sig_action.sa_handler = SIG_DFL;
56     sigaction(SIGALRM, &sig_action, (struct sigaction *) 0);
57     alarm(seconds);
58     sigaddset(&sig_action.sa_mask, SIGALRM);
59     sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, (sigset_t *) 0);
60 }
61