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