blob: 959114679e25d2b73a608c3413779d43354fc995 [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... */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000015int FAST_FUNC sigaction_set(int signum, const struct sigaction *act)
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000016{
17 return sigaction(signum, act, NULL);
18}
19
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000020int FAST_FUNC sigprocmask_allsigs(int how)
Denis Vlasenko3f165fa2008-03-17 08:29:08 +000021{
22 sigset_t set;
23 sigfillset(&set);
24 return sigprocmask(how, &set, NULL);
25}
26
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000027void FAST_FUNC bb_signals(int sigs, void (*f)(int))
Denis Vlasenko25591c32008-02-16 22:58:56 +000028{
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
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000042void FAST_FUNC bb_signals_recursive(int sigs, void (*f)(int))
Denis Vlasenko25591c32008-02-16 22:58:56 +000043{
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
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000063void FAST_FUNC sig_block(int sig)
Denis Vlasenko25591c32008-02-16 22:58:56 +000064{
65 sigset_t ss;
66 sigemptyset(&ss);
67 sigaddset(&ss, sig);
68 sigprocmask(SIG_BLOCK, &ss, NULL);
69}
70
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000071void FAST_FUNC sig_unblock(int sig)
Denis Vlasenko25591c32008-02-16 22:58:56 +000072{
73 sigset_t ss;
74 sigemptyset(&ss);
75 sigaddset(&ss, sig);
76 sigprocmask(SIG_UNBLOCK, &ss, NULL);
77}
78
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000079void FAST_FUNC 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 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000087void FAST_FUNC kill_myself_with_sig(int sig)
Denis Vlasenko400d8bb2008-02-24 13:36:01 +000088{
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);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +000092 _exit(EXIT_FAILURE); /* Should not reach it */
Denis Vlasenko400d8bb2008-02-24 13:36:01 +000093}
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000094
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000095void FAST_FUNC signal_SA_RESTART_empty_mask(int sig, void (*handler)(int))
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000096{
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
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000105void FAST_FUNC signal_no_SA_RESTART_empty_mask(int sig, void (*handler)(int))
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000106{
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}