Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / busybox / libbb / signals.c
blob56512473a2469dbd6f8ed6c4d06663d051d03b8b
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Utility routines.
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * Copyright (C) 2006 Rob Landley
7 * Copyright (C) 2006 Denys Vlasenko
9 * Licensed under GPLv2, see file LICENSE in this source tree.
12 #include "libbb.h"
14 /* All known arches use small ints for signals */
15 smallint bb_got_signal;
17 void record_signo(int signo)
19 bb_got_signal = signo;
22 /* Saves 2 bytes on x86! Oh my... */
23 int FAST_FUNC sigaction_set(int signum, const struct sigaction *act)
25 return sigaction(signum, act, NULL);
28 int FAST_FUNC sigprocmask_allsigs(int how)
30 sigset_t set;
31 sigfillset(&set);
32 return sigprocmask(how, &set, NULL);
35 void FAST_FUNC bb_signals(int sigs, void (*f)(int))
37 int sig_no = 0;
38 int bit = 1;
40 while (sigs) {
41 if (sigs & bit) {
42 sigs -= bit;
43 signal(sig_no, f);
45 sig_no++;
46 bit <<= 1;
50 void FAST_FUNC bb_signals_recursive_norestart(int sigs, void (*f)(int))
52 int sig_no = 0;
53 int bit = 1;
54 struct sigaction sa;
56 memset(&sa, 0, sizeof(sa));
57 sa.sa_handler = f;
58 /*sa.sa_flags = 0;*/
59 /*sigemptyset(&sa.sa_mask); - hope memset did it*/
61 while (sigs) {
62 if (sigs & bit) {
63 sigs -= bit;
64 sigaction_set(sig_no, &sa);
66 sig_no++;
67 bit <<= 1;
71 void FAST_FUNC sig_block(int sig)
73 sigset_t ss;
74 sigemptyset(&ss);
75 sigaddset(&ss, sig);
76 sigprocmask(SIG_BLOCK, &ss, NULL);
79 void FAST_FUNC sig_unblock(int sig)
81 sigset_t ss;
82 sigemptyset(&ss);
83 sigaddset(&ss, sig);
84 sigprocmask(SIG_UNBLOCK, &ss, NULL);
87 void FAST_FUNC wait_for_any_sig(void)
89 sigset_t ss;
90 sigemptyset(&ss);
91 sigsuspend(&ss);
94 /* Assuming the sig is fatal */
95 void FAST_FUNC kill_myself_with_sig(int sig)
97 signal(sig, SIG_DFL);
98 sig_unblock(sig);
99 raise(sig);
100 _exit(sig | 128); /* Should not reach it */
103 void FAST_FUNC signal_SA_RESTART_empty_mask(int sig, void (*handler)(int))
105 struct sigaction sa;
106 memset(&sa, 0, sizeof(sa));
107 /*sigemptyset(&sa.sa_mask);*/
108 sa.sa_flags = SA_RESTART;
109 sa.sa_handler = handler;
110 sigaction_set(sig, &sa);
113 void FAST_FUNC signal_no_SA_RESTART_empty_mask(int sig, void (*handler)(int))
115 struct sigaction sa;
116 memset(&sa, 0, sizeof(sa));
117 /*sigemptyset(&sa.sa_mask);*/
118 /*sa.sa_flags = 0;*/
119 sa.sa_handler = handler;
120 sigaction_set(sig, &sa);