Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / atf / dist / atf-c / signals.c
blobb64b67aaaf06995f375b14919fd2b1c6410396c4
1 /*
2 * Automated Testing Framework (atf)
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #if defined(HAVE_CONFIG_H)
31 #include "bconfig.h"
32 #endif
34 #include <sys/types.h>
36 #include <errno.h>
37 #include <stdbool.h>
38 #include <unistd.h>
40 #include "atf-c/error.h"
41 #include "atf-c/sanity.h"
42 #include "atf-c/signals.h"
44 const int atf_signals_last_signo = LAST_SIGNO;
46 /* ---------------------------------------------------------------------
47 * The "atf_signal_holder" type.
48 * --------------------------------------------------------------------- */
50 static bool happened[LAST_SIGNO + 1];
52 static
53 void
54 holder_handler(int signo)
56 happened[signo] = true;
60 * Constructors/destructors.
63 atf_error_t
64 atf_signal_holder_init(atf_signal_holder_t *sh, int signo)
66 atf_error_t err;
68 err = atf_signal_programmer_init(&sh->m_sp, signo, holder_handler);
69 if (atf_is_error(err))
70 goto out;
72 atf_object_init(&sh->m_object);
73 sh->m_signo = signo;
74 happened[signo] = false;
76 INV(!atf_is_error(err));
77 out:
78 return err;
81 void
82 atf_signal_holder_fini(atf_signal_holder_t *sh)
84 atf_signal_programmer_fini(&sh->m_sp);
86 if (happened[sh->m_signo])
87 kill(getpid(), sh->m_signo);
89 atf_object_fini(&sh->m_object);
93 * Getters.
96 void
97 atf_signal_holder_process(atf_signal_holder_t *sh)
99 if (happened[sh->m_signo]) {
100 int ret;
101 atf_error_t err;
103 atf_signal_programmer_fini(&sh->m_sp);
104 happened[sh->m_signo] = false;
106 ret = kill(getpid(), sh->m_signo);
107 INV(ret != -1);
109 err = atf_signal_programmer_init(&sh->m_sp, sh->m_signo,
110 holder_handler);
111 INV(!atf_is_error(err));
115 /* ---------------------------------------------------------------------
116 * The "atf_signal_programmer" type.
117 * --------------------------------------------------------------------- */
120 * Constructors/destructors.
123 atf_error_t
124 atf_signal_programmer_init(atf_signal_programmer_t *sp, int signo,
125 atf_signal_handler_t handler)
127 atf_error_t err;
128 struct sigaction sa;
130 sa.sa_handler = handler;
131 sigemptyset(&sa.sa_mask);
132 sa.sa_flags = 0;
134 if (sigaction(signo, &sa, &sp->m_oldsa) == -1)
135 err = atf_libc_error(errno, "Failed to program signal %d", signo);
136 else {
137 atf_object_init(&sp->m_object);
138 sp->m_signo = signo;
139 err = atf_no_error();
142 return err;
145 void
146 atf_signal_programmer_fini(atf_signal_programmer_t *sp)
148 if (sigaction(sp->m_signo, &sp->m_oldsa, NULL) == -1)
149 UNREACHABLE;
151 atf_object_fini(&sp->m_object);
154 /* ---------------------------------------------------------------------
155 * Free functions.
156 * --------------------------------------------------------------------- */
158 void
159 atf_signal_reset(int signo)
161 struct sigaction sa;
163 sa.sa_handler = SIG_DFL;
164 sigemptyset(&sa.sa_mask);
165 sa.sa_flags = 0;
167 (void)sigaction(signo, &sa, NULL);