blob: 3f589321c1d82a3d3be520873d019b9b714ab66d [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko25591c32008-02-16 22:58:56 +000010 */
Denis Vlasenko25591c32008-02-16 22:58:56 +000011#include "libbb.h"
12
Denis Vlasenko08ea11a2008-09-11 19:51:11 +000013/* All known arches use small ints for signals */
14smallint bb_got_signal;
15
16void record_signo(int signo)
17{
18 bb_got_signal = signo;
19}
20
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000021/* Saves 2 bytes on x86! Oh my... */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000022int FAST_FUNC sigaction_set(int signum, const struct sigaction *act)
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000023{
24 return sigaction(signum, act, NULL);
25}
26
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000027int FAST_FUNC sigprocmask_allsigs(int how)
Denis Vlasenko3f165fa2008-03-17 08:29:08 +000028{
29 sigset_t set;
30 sigfillset(&set);
31 return sigprocmask(how, &set, NULL);
32}
33
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000034void FAST_FUNC bb_signals(int sigs, void (*f)(int))
Denis Vlasenko25591c32008-02-16 22:58:56 +000035{
36 int sig_no = 0;
37 int bit = 1;
38
39 while (sigs) {
40 if (sigs & bit) {
Denys Vlasenkoc4f2d992012-09-27 13:20:34 +020041 sigs -= bit;
Denis Vlasenko25591c32008-02-16 22:58:56 +000042 signal(sig_no, f);
43 }
44 sig_no++;
45 bit <<= 1;
46 }
47}
48
Denis Vlasenkocab28aa2009-01-31 01:02:07 +000049void FAST_FUNC bb_signals_recursive_norestart(int sigs, void (*f)(int))
Denis Vlasenko25591c32008-02-16 22:58:56 +000050{
51 int sig_no = 0;
52 int bit = 1;
53 struct sigaction sa;
54
55 memset(&sa, 0, sizeof(sa));
56 sa.sa_handler = f;
57 /*sa.sa_flags = 0;*/
58 /*sigemptyset(&sa.sa_mask); - hope memset did it*/
59
60 while (sigs) {
61 if (sigs & bit) {
Denys Vlasenkoc4f2d992012-09-27 13:20:34 +020062 sigs -= bit;
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +000063 sigaction_set(sig_no, &sa);
Denis Vlasenko25591c32008-02-16 22:58:56 +000064 }
65 sig_no++;
66 bit <<= 1;
67 }
68}
69
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000070void FAST_FUNC sig_block(int sig)
Denis Vlasenko25591c32008-02-16 22:58:56 +000071{
72 sigset_t ss;
73 sigemptyset(&ss);
74 sigaddset(&ss, sig);
75 sigprocmask(SIG_BLOCK, &ss, NULL);
76}
77
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000078void FAST_FUNC sig_unblock(int sig)
Denis Vlasenko25591c32008-02-16 22:58:56 +000079{
80 sigset_t ss;
81 sigemptyset(&ss);
82 sigaddset(&ss, sig);
83 sigprocmask(SIG_UNBLOCK, &ss, NULL);
84}
85
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000086void FAST_FUNC wait_for_any_sig(void)
Denis Vlasenko25591c32008-02-16 22:58:56 +000087{
88 sigset_t ss;
89 sigemptyset(&ss);
90 sigsuspend(&ss);
91}
Denis Vlasenko400d8bb2008-02-24 13:36:01 +000092
93/* Assuming the sig is fatal */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000094void FAST_FUNC kill_myself_with_sig(int sig)
Denis Vlasenko400d8bb2008-02-24 13:36:01 +000095{
Denis Vlasenko400d8bb2008-02-24 13:36:01 +000096 signal(sig, SIG_DFL);
Denis Vlasenko4cf1d082008-03-12 23:13:50 +000097 sig_unblock(sig);
Denis Vlasenko400d8bb2008-02-24 13:36:01 +000098 raise(sig);
Denys Vlasenkoc4f2d992012-09-27 13:20:34 +020099 _exit(sig | 128); /* Should not reach it */
Denis Vlasenko400d8bb2008-02-24 13:36:01 +0000100}
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000101
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000102void FAST_FUNC signal_SA_RESTART_empty_mask(int sig, void (*handler)(int))
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000103{
104 struct sigaction sa;
105 memset(&sa, 0, sizeof(sa));
106 /*sigemptyset(&sa.sa_mask);*/
107 sa.sa_flags = SA_RESTART;
108 sa.sa_handler = handler;
109 sigaction_set(sig, &sa);
110}
111
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000112void FAST_FUNC signal_no_SA_RESTART_empty_mask(int sig, void (*handler)(int))
Denis Vlasenko8e2cfec2008-03-12 23:19:35 +0000113{
114 struct sigaction sa;
115 memset(&sa, 0, sizeof(sa));
116 /*sigemptyset(&sa.sa_mask);*/
117 /*sa.sa_flags = 0;*/
118 sa.sa_handler = handler;
119 sigaction_set(sig, &sa);
120}