Postfix3.3.1
killme_after.c
[詳解]
1 /*++
2 /* NAME
3 /* killme_after 3
4 /* SUMMARY
5 /* programmed death
6 /* SYNOPSIS
7 /* #include <killme_after.h>
8 /*
9 /* void killme_after(seconds)
10 /* unsigned int seconds;
11 /* DESCRIPTION
12 /* The killme_after() function does a best effort to terminate
13 /* the process after the specified time, should it still exist.
14 /* It is meant to be used in a signal handler, as an insurance
15 /* against getting stuck somewhere while preparing for exit.
16 /* DIAGNOSTICS
17 /* None. This routine does a best effort, damn the torpedoes.
18 /* LICENSE
19 /* .ad
20 /* .fi
21 /* The Secure Mailer license must be distributed with this software.
22 /* AUTHOR(S)
23 /* Wietse Venema
24 /* IBM T.J. Watson Research
25 /* P.O. Box 704
26 /* Yorktown Heights, NY 10598, USA
27 /*--*/
28 
29 /* System library. */
30 
31 #include <sys_defs.h>
32 #include <signal.h>
33 #include <unistd.h>
34 
35 /* Utility library. */
36 
37 #include <killme_after.h>
38 
39 /* killme_after - self-assured death */
40 
41 void killme_after(unsigned int seconds)
42 {
43  struct sigaction sig_action;
44 
45  /*
46  * Schedule an ALARM signal, and make sure the signal will be delivered
47  * even if we are being called from a signal handler and SIGALRM delivery
48  * is blocked.
49  *
50  * Undocumented: when a process runs with PID 1, Linux won't deliver a
51  * signal unless the process specifies a handler (i.e. SIG_DFL is treated
52  * as SIG_IGN). Conveniently, _exit() can be used directly as a signal
53  * handler. This changes the wait status that a parent would see, but in
54  * the case of "init" mode on Linux, no-one would care.
55  */
56  alarm(0);
57  sigemptyset(&sig_action.sa_mask);
58  sig_action.sa_flags = 0;
59  sig_action.sa_handler = (getpid() == 1 ? _exit : SIG_DFL);
60  sigaction(SIGALRM, &sig_action, (struct sigaction *) 0);
61  alarm(seconds);
62  sigaddset(&sig_action.sa_mask, SIGALRM);
63  sigprocmask(SIG_UNBLOCK, &sig_action.sa_mask, (sigset_t *) 0);
64 }
void killme_after(unsigned int seconds)
Definition: killme_after.c:41