1*11be35a1SLionel Sambuc /* $NetBSD: t_sig.c,v 1.2 2010/11/03 16:10:20 christos Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8*11be35a1SLionel Sambuc * by Luke Mewburn and Jaromir Dolecek.
9*11be35a1SLionel Sambuc *
10*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
12*11be35a1SLionel Sambuc * are met:
13*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
18*11be35a1SLionel Sambuc *
19*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30*11be35a1SLionel Sambuc */
31*11be35a1SLionel Sambuc
32*11be35a1SLionel Sambuc #include <sys/cdefs.h>
33*11be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008\
34*11be35a1SLionel Sambuc The NetBSD Foundation, inc. All rights reserved.");
35*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_sig.c,v 1.2 2010/11/03 16:10:20 christos Exp $");
36*11be35a1SLionel Sambuc
37*11be35a1SLionel Sambuc #include <sys/event.h>
38*11be35a1SLionel Sambuc #include <sys/ioctl.h>
39*11be35a1SLionel Sambuc #include <sys/param.h>
40*11be35a1SLionel Sambuc #include <sys/time.h>
41*11be35a1SLionel Sambuc #include <sys/wait.h>
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc #include <inttypes.h>
44*11be35a1SLionel Sambuc #include <signal.h>
45*11be35a1SLionel Sambuc #include <stdio.h>
46*11be35a1SLionel Sambuc #include <stdlib.h>
47*11be35a1SLionel Sambuc #include <unistd.h>
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc #include <atf-c.h>
50*11be35a1SLionel Sambuc
51*11be35a1SLionel Sambuc #include "../../h_macros.h"
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc #define NSIGNALS 5
54*11be35a1SLionel Sambuc
55*11be35a1SLionel Sambuc ATF_TC(sig);
ATF_TC_HEAD(sig,tc)56*11be35a1SLionel Sambuc ATF_TC_HEAD(sig, tc)
57*11be35a1SLionel Sambuc {
58*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks EVFILT_SIGNAL");
59*11be35a1SLionel Sambuc }
ATF_TC_BODY(sig,tc)60*11be35a1SLionel Sambuc ATF_TC_BODY(sig, tc)
61*11be35a1SLionel Sambuc {
62*11be35a1SLionel Sambuc struct timespec timeout;
63*11be35a1SLionel Sambuc struct kfilter_mapping km;
64*11be35a1SLionel Sambuc struct kevent event[1];
65*11be35a1SLionel Sambuc char namebuf[32];
66*11be35a1SLionel Sambuc pid_t pid, child;
67*11be35a1SLionel Sambuc int kq, n, num, status;
68*11be35a1SLionel Sambuc
69*11be35a1SLionel Sambuc pid = getpid();
70*11be35a1SLionel Sambuc (void)printf("my pid: %d\n", pid);
71*11be35a1SLionel Sambuc
72*11be35a1SLionel Sambuc /* fork a child to send signals */
73*11be35a1SLionel Sambuc RL(child = fork());
74*11be35a1SLionel Sambuc if (child == 0) {
75*11be35a1SLionel Sambuc int i;
76*11be35a1SLionel Sambuc (void)sleep(2);
77*11be35a1SLionel Sambuc for(i = 0; i < NSIGNALS; ++i) {
78*11be35a1SLionel Sambuc (void)kill(pid, SIGUSR1);
79*11be35a1SLionel Sambuc (void)sleep(2);
80*11be35a1SLionel Sambuc }
81*11be35a1SLionel Sambuc _exit(0);
82*11be35a1SLionel Sambuc /* NOTREACHED */
83*11be35a1SLionel Sambuc }
84*11be35a1SLionel Sambuc
85*11be35a1SLionel Sambuc RL(kq = kqueue());
86*11be35a1SLionel Sambuc
87*11be35a1SLionel Sambuc (void)strlcpy(namebuf, "EVFILT_SIGNAL", sizeof(namebuf));
88*11be35a1SLionel Sambuc km.name = namebuf;
89*11be35a1SLionel Sambuc RL(ioctl(kq, KFILTER_BYNAME, &km));
90*11be35a1SLionel Sambuc (void)printf("got %d as filter number for `%s'.\n", km.filter, km.name);
91*11be35a1SLionel Sambuc
92*11be35a1SLionel Sambuc /* ignore the signal to avoid taking it for real */
93*11be35a1SLionel Sambuc REQUIRE_LIBC(signal(SIGUSR1, SIG_IGN), SIG_ERR);
94*11be35a1SLionel Sambuc
95*11be35a1SLionel Sambuc event[0].ident = SIGUSR1;
96*11be35a1SLionel Sambuc event[0].filter = km.filter;
97*11be35a1SLionel Sambuc event[0].flags = EV_ADD | EV_ENABLE;
98*11be35a1SLionel Sambuc
99*11be35a1SLionel Sambuc RL(kevent(kq, event, 1, NULL, 0, NULL));
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc (void)sleep(1);
102*11be35a1SLionel Sambuc
103*11be35a1SLionel Sambuc timeout.tv_sec = 1;
104*11be35a1SLionel Sambuc timeout.tv_nsec = 0;
105*11be35a1SLionel Sambuc
106*11be35a1SLionel Sambuc for (num = 0; num < NSIGNALS; num += n) {
107*11be35a1SLionel Sambuc struct timeval then, now, diff;
108*11be35a1SLionel Sambuc
109*11be35a1SLionel Sambuc RL(gettimeofday(&then, NULL));
110*11be35a1SLionel Sambuc RL(n = kevent(kq, NULL, 0, event, 1, &timeout));
111*11be35a1SLionel Sambuc RL(gettimeofday(&now, NULL));
112*11be35a1SLionel Sambuc timersub(&now, &then, &diff);
113*11be35a1SLionel Sambuc
114*11be35a1SLionel Sambuc (void)printf("sig: kevent returned %d in %lld.%06ld\n",
115*11be35a1SLionel Sambuc n, (long long)diff.tv_sec, (long)diff.tv_usec);
116*11be35a1SLionel Sambuc
117*11be35a1SLionel Sambuc if (n == 0)
118*11be35a1SLionel Sambuc continue;
119*11be35a1SLionel Sambuc
120*11be35a1SLionel Sambuc (void)printf("sig: kevent flags: 0x%x, data: %" PRId64 " (# "
121*11be35a1SLionel Sambuc "times signal posted)\n", event[0].flags, event[0].data);
122*11be35a1SLionel Sambuc }
123*11be35a1SLionel Sambuc
124*11be35a1SLionel Sambuc (void)waitpid(child, &status, 0);
125*11be35a1SLionel Sambuc (void)printf("sig: finished successfully\n");
126*11be35a1SLionel Sambuc }
127*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)128*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
129*11be35a1SLionel Sambuc {
130*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, sig);
131*11be35a1SLionel Sambuc
132*11be35a1SLionel Sambuc return atf_no_error();
133*11be35a1SLionel Sambuc }
134