blob: 685c5523085b3a24f6255163e336a6d9740ef951 [file] [log] [blame]
Denis Vlasenko25591c32008-02-16 22:58:56 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * Copyright (C) 2006 Rob Landley
Denis Vlasenkod18f52b2008-03-02 12:53:15 +00007 * Copyright (C) 2006 Denys Vlasenko
Denis Vlasenko25591c32008-02-16 22:58:56 +00008 *
9 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
10 */
11
12#include "libbb.h"
13
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000014/* Saves 2 bytes on x86! Oh my... */
15int sigaction_set(int signum, const struct sigaction *act)
16{
17 return sigaction(signum, act, NULL);
18}
19
Denis Vlasenko3f165fa2008-03-17 08:29:08 +000020int sigprocmask_allsigs(int how)
21{
22 sigset_t set;
23 sigfillset(&set);
24 return sigprocmask(how, &set, NULL);
25}
26
Denis Vlasenko25591c32008-02-16 22:58:56 +000027void bb_signals(int sigs, void (*f)(int))
28{
29 int sig_no = 0;
30 int bit = 1;
31
32 while (sigs) {
33 if (sigs & bit) {
34 sigs &= ~bit;
35 signal(sig_no, f);
36 }
37 sig_no++;
38 bit <<= 1;
39 }
40}
41
42void bb_signals_recursive(int sigs, void (*f)(int))
43{
44 int sig_no = 0;
45 int bit = 1;
46 struct sigaction sa;
47
48 memset(&sa, 0, sizeof(sa));
49 sa.sa_handler = f;
50 /*sa.sa_flags = 0;*/
51 /*sigemptyset(&sa.sa_mask); - hope memset did it*/
52
53 while (sigs) {
54 if (sigs & bit) {
55 sigs &= ~bit;
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000056 sigaction_set(sig_no, &sa);
Denis Vlasenko25591c32008-02-16 22:58:56 +000057 }
58 sig_no++;
59 bit <<= 1;
60 }
61}
62
63void sig_block(int sig)
64{
65 sigset_t ss;
66 sigemptyset(&ss);
67 sigaddset(&ss, sig);
68 sigprocmask(SIG_BLOCK, &ss, NULL);
69}
70
71void sig_unblock(int sig)
72{
73 sigset_t ss;
74 sigemptyset(&ss);
75 sigaddset(&ss, sig);
76 sigprocmask(SIG_UNBLOCK, &ss, NULL);
77}
78
Denis Vlasenko3f165fa2008-03-17 08:29:08 +000079void wait_for_any_sig(void)
Denis Vlasenko25591c32008-02-16 22:58:56 +000080{
81 sigset_t ss;
82 sigemptyset(&ss);
83 sigsuspend(&ss);
84}
Denis Vlasenko400d8bb2008-02-24 13:36:01 +000085
86/* Assuming the sig is fatal */
87void kill_myself_with_sig(int sig)
88{
Denis Vlasenko400d8bb2008-02-24 13:36:01 +000089 signal(sig, SIG_DFL);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000090 sig_unblock(sig);
Denis Vlasenko400d8bb2008-02-24 13:36:01 +000091 raise(sig);
92 _exit(1); /* Should not reach it */
93}
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000094
95void signal_SA_RESTART_empty_mask(int sig, void (*handler)(int))
96{
97 struct sigaction sa;
98 memset(&sa, 0, sizeof(sa));
99 /*sigemptyset(&sa.sa_mask);*/
100 sa.sa_flags = SA_RESTART;
101 sa.sa_handler = handler;
102 sigaction_set(sig, &sa);
103}
104
105void signal_no_SA_RESTART_empty_mask(int sig, void (*handler)(int))
106{
107 struct sigaction sa;
108 memset(&sa, 0, sizeof(sa));
109 /*sigemptyset(&sa.sa_mask);*/
110 /*sa.sa_flags = 0;*/
111 sa.sa_handler = handler;
112 sigaction_set(sig, &sa);
113}