2008-02-16 22:58:56 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
|
|
|
* Copyright (C) 2006 Rob Landley
|
2008-03-02 12:53:15 +00:00
|
|
|
* Copyright (C) 2006 Denys Vlasenko
|
2008-02-16 22:58:56 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2008-02-16 22:58:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-09-11 19:51:11 +00:00
|
|
|
/* All known arches use small ints for signals */
|
|
|
|
smallint bb_got_signal;
|
|
|
|
|
|
|
|
void record_signo(int signo)
|
|
|
|
{
|
|
|
|
bb_got_signal = signo;
|
|
|
|
}
|
|
|
|
|
2008-03-12 23:19:35 +00:00
|
|
|
/* Saves 2 bytes on x86! Oh my... */
|
2008-06-27 02:52:20 +00:00
|
|
|
int FAST_FUNC sigaction_set(int signum, const struct sigaction *act)
|
2008-03-12 23:19:35 +00:00
|
|
|
{
|
|
|
|
return sigaction(signum, act, NULL);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
int FAST_FUNC sigprocmask_allsigs(int how)
|
2008-03-17 08:29:08 +00:00
|
|
|
{
|
|
|
|
sigset_t set;
|
|
|
|
sigfillset(&set);
|
|
|
|
return sigprocmask(how, &set, NULL);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC bb_signals(int sigs, void (*f)(int))
|
2008-02-16 22:58:56 +00:00
|
|
|
{
|
|
|
|
int sig_no = 0;
|
|
|
|
int bit = 1;
|
|
|
|
|
|
|
|
while (sigs) {
|
|
|
|
if (sigs & bit) {
|
2012-09-27 11:20:34 +00:00
|
|
|
sigs -= bit;
|
2008-02-16 22:58:56 +00:00
|
|
|
signal(sig_no, f);
|
|
|
|
}
|
|
|
|
sig_no++;
|
|
|
|
bit <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-31 01:02:07 +00:00
|
|
|
void FAST_FUNC bb_signals_recursive_norestart(int sigs, void (*f)(int))
|
2008-02-16 22:58:56 +00:00
|
|
|
{
|
|
|
|
int sig_no = 0;
|
|
|
|
int bit = 1;
|
|
|
|
struct sigaction sa;
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = f;
|
|
|
|
/*sa.sa_flags = 0;*/
|
|
|
|
/*sigemptyset(&sa.sa_mask); - hope memset did it*/
|
|
|
|
|
|
|
|
while (sigs) {
|
|
|
|
if (sigs & bit) {
|
2012-09-27 11:20:34 +00:00
|
|
|
sigs -= bit;
|
2008-03-12 23:19:35 +00:00
|
|
|
sigaction_set(sig_no, &sa);
|
2008-02-16 22:58:56 +00:00
|
|
|
}
|
|
|
|
sig_no++;
|
|
|
|
bit <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC sig_block(int sig)
|
2008-02-16 22:58:56 +00:00
|
|
|
{
|
|
|
|
sigset_t ss;
|
|
|
|
sigemptyset(&ss);
|
|
|
|
sigaddset(&ss, sig);
|
|
|
|
sigprocmask(SIG_BLOCK, &ss, NULL);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC sig_unblock(int sig)
|
2008-02-16 22:58:56 +00:00
|
|
|
{
|
|
|
|
sigset_t ss;
|
|
|
|
sigemptyset(&ss);
|
|
|
|
sigaddset(&ss, sig);
|
|
|
|
sigprocmask(SIG_UNBLOCK, &ss, NULL);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC wait_for_any_sig(void)
|
2008-02-16 22:58:56 +00:00
|
|
|
{
|
|
|
|
sigset_t ss;
|
|
|
|
sigemptyset(&ss);
|
|
|
|
sigsuspend(&ss);
|
|
|
|
}
|
2008-02-24 13:36:01 +00:00
|
|
|
|
|
|
|
/* Assuming the sig is fatal */
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC kill_myself_with_sig(int sig)
|
2008-02-24 13:36:01 +00:00
|
|
|
{
|
|
|
|
signal(sig, SIG_DFL);
|
2008-03-12 23:13:50 +00:00
|
|
|
sig_unblock(sig);
|
2008-02-24 13:36:01 +00:00
|
|
|
raise(sig);
|
2012-09-27 11:20:34 +00:00
|
|
|
_exit(sig | 128); /* Should not reach it */
|
2008-02-24 13:36:01 +00:00
|
|
|
}
|
2008-03-12 23:19:35 +00:00
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC signal_SA_RESTART_empty_mask(int sig, void (*handler)(int))
|
2008-03-12 23:19:35 +00:00
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
/*sigemptyset(&sa.sa_mask);*/
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
sa.sa_handler = handler;
|
|
|
|
sigaction_set(sig, &sa);
|
|
|
|
}
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
void FAST_FUNC signal_no_SA_RESTART_empty_mask(int sig, void (*handler)(int))
|
2008-03-12 23:19:35 +00:00
|
|
|
{
|
|
|
|
struct sigaction sa;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
/*sigemptyset(&sa.sa_mask);*/
|
|
|
|
/*sa.sa_flags = 0;*/
|
|
|
|
sa.sa_handler = handler;
|
|
|
|
sigaction_set(sig, &sa);
|
|
|
|
}
|