Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 1 | /* 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 Vlasenko | d18f52b | 2008-03-02 12:53:15 +0000 | [diff] [blame] | 7 | * Copyright (C) 2006 Denys Vlasenko |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 10 | */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
| 12 | |
Denis Vlasenko | 08ea11a | 2008-09-11 19:51:11 +0000 | [diff] [blame] | 13 | /* All known arches use small ints for signals */ |
| 14 | smallint bb_got_signal; |
| 15 | |
| 16 | void record_signo(int signo) |
| 17 | { |
| 18 | bb_got_signal = signo; |
| 19 | } |
| 20 | |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 21 | /* Saves 2 bytes on x86! Oh my... */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 22 | int FAST_FUNC sigaction_set(int signum, const struct sigaction *act) |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 23 | { |
| 24 | return sigaction(signum, act, NULL); |
| 25 | } |
| 26 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 27 | int FAST_FUNC sigprocmask_allsigs(int how) |
Denis Vlasenko | 3f165fa | 2008-03-17 08:29:08 +0000 | [diff] [blame] | 28 | { |
| 29 | sigset_t set; |
| 30 | sigfillset(&set); |
| 31 | return sigprocmask(how, &set, NULL); |
| 32 | } |
| 33 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 34 | void FAST_FUNC bb_signals(int sigs, void (*f)(int)) |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 35 | { |
| 36 | int sig_no = 0; |
| 37 | int bit = 1; |
| 38 | |
| 39 | while (sigs) { |
| 40 | if (sigs & bit) { |
Denys Vlasenko | c4f2d99 | 2012-09-27 13:20:34 +0200 | [diff] [blame] | 41 | sigs -= bit; |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 42 | signal(sig_no, f); |
| 43 | } |
| 44 | sig_no++; |
| 45 | bit <<= 1; |
| 46 | } |
| 47 | } |
| 48 | |
Denis Vlasenko | cab28aa | 2009-01-31 01:02:07 +0000 | [diff] [blame] | 49 | void FAST_FUNC bb_signals_recursive_norestart(int sigs, void (*f)(int)) |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 50 | { |
| 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 Vlasenko | c4f2d99 | 2012-09-27 13:20:34 +0200 | [diff] [blame] | 62 | sigs -= bit; |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 63 | sigaction_set(sig_no, &sa); |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 64 | } |
| 65 | sig_no++; |
| 66 | bit <<= 1; |
| 67 | } |
| 68 | } |
| 69 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 70 | void FAST_FUNC sig_block(int sig) |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 71 | { |
| 72 | sigset_t ss; |
| 73 | sigemptyset(&ss); |
| 74 | sigaddset(&ss, sig); |
| 75 | sigprocmask(SIG_BLOCK, &ss, NULL); |
| 76 | } |
| 77 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 78 | void FAST_FUNC sig_unblock(int sig) |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 79 | { |
| 80 | sigset_t ss; |
| 81 | sigemptyset(&ss); |
| 82 | sigaddset(&ss, sig); |
| 83 | sigprocmask(SIG_UNBLOCK, &ss, NULL); |
| 84 | } |
| 85 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 86 | void FAST_FUNC wait_for_any_sig(void) |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 87 | { |
| 88 | sigset_t ss; |
| 89 | sigemptyset(&ss); |
| 90 | sigsuspend(&ss); |
| 91 | } |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 92 | |
| 93 | /* Assuming the sig is fatal */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 94 | void FAST_FUNC kill_myself_with_sig(int sig) |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 95 | { |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 96 | signal(sig, SIG_DFL); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 97 | sig_unblock(sig); |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 98 | raise(sig); |
Denys Vlasenko | c4f2d99 | 2012-09-27 13:20:34 +0200 | [diff] [blame] | 99 | _exit(sig | 128); /* Should not reach it */ |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 100 | } |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 101 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 102 | void FAST_FUNC signal_SA_RESTART_empty_mask(int sig, void (*handler)(int)) |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 103 | { |
| 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 Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 112 | void FAST_FUNC signal_no_SA_RESTART_empty_mask(int sig, void (*handler)(int)) |
Denis Vlasenko | 8e2cfec | 2008-03-12 23:19:35 +0000 | [diff] [blame] | 113 | { |
| 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 | } |