t_sig.c revision 272343
133965Sjdp/* $NetBSD: t_sig.c,v 1.2 2010/11/03 16:10:20 christos Exp $ */ 277298Sobrien 333965Sjdp/*- 433965Sjdp * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc. 533965Sjdp * All rights reserved. 633965Sjdp * 733965Sjdp * This code is derived from software contributed to The NetBSD Foundation 833965Sjdp * by Luke Mewburn and Jaromir Dolecek. 933965Sjdp * 1033965Sjdp * Redistribution and use in source and binary forms, with or without 1133965Sjdp * modification, are permitted provided that the following conditions 1233965Sjdp * are met: 1333965Sjdp * 1. Redistributions of source code must retain the above copyright 1433965Sjdp * notice, this list of conditions and the following disclaimer. 1533965Sjdp * 2. Redistributions in binary form must reproduce the above copyright 1633965Sjdp * notice, this list of conditions and the following disclaimer in the 1733965Sjdp * documentation and/or other materials provided with the distribution. 1833965Sjdp * 1933965Sjdp * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2077298Sobrien * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2133965Sjdp * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2277298Sobrien * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2333965Sjdp * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2433965Sjdp * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2533965Sjdp * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2633965Sjdp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2733965Sjdp * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2833965Sjdp * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2933965Sjdp * POSSIBILITY OF SUCH DAMAGE. 3033965Sjdp */ 3133965Sjdp 3233965Sjdp#include <sys/cdefs.h> 3333965Sjdp__COPYRIGHT("@(#) Copyright (c) 2008\ 3433965Sjdp The NetBSD Foundation, inc. All rights reserved."); 3533965Sjdp__RCSID("$NetBSD: t_sig.c,v 1.2 2010/11/03 16:10:20 christos Exp $"); 3633965Sjdp 3733965Sjdp#include <sys/event.h> 3833965Sjdp#include <sys/ioctl.h> 3933965Sjdp#include <sys/param.h> 4033965Sjdp#include <sys/time.h> 4133965Sjdp#include <sys/wait.h> 4233965Sjdp 4377298Sobrien#include <inttypes.h> 4460484Sobrien#include <signal.h> 4560484Sobrien#include <stdio.h> 4660484Sobrien#include <stdlib.h> 4760484Sobrien#include <unistd.h> 4833965Sjdp 4933965Sjdp#include <atf-c.h> 5033965Sjdp 5133965Sjdp#include "../../h_macros.h" 5233965Sjdp 5333965Sjdp#define NSIGNALS 5 5433965Sjdp 5533965SjdpATF_TC(sig); 5633965SjdpATF_TC_HEAD(sig, tc) 5733965Sjdp{ 5833965Sjdp atf_tc_set_md_var(tc, "descr", "Checks EVFILT_SIGNAL"); 5933965Sjdp} 6033965SjdpATF_TC_BODY(sig, tc) 6133965Sjdp{ 6233965Sjdp struct timespec timeout; 6333965Sjdp struct kfilter_mapping km; 6477298Sobrien struct kevent event[1]; 6577298Sobrien char namebuf[32]; 6633965Sjdp pid_t pid, child; 6777298Sobrien int kq, n, num, status; 6877298Sobrien 6933965Sjdp pid = getpid(); 7038889Sjdp (void)printf("my pid: %d\n", pid); 7138889Sjdp 7277298Sobrien /* fork a child to send signals */ 7338889Sjdp RL(child = fork()); 7433965Sjdp if (child == 0) { 7533965Sjdp int i; 7633965Sjdp (void)sleep(2); 7777298Sobrien for(i = 0; i < NSIGNALS; ++i) { 7877298Sobrien (void)kill(pid, SIGUSR1); 7933965Sjdp (void)sleep(2); 8033965Sjdp } 8133965Sjdp _exit(0); 8233965Sjdp /* NOTREACHED */ 8333965Sjdp } 8438889Sjdp 8538889Sjdp RL(kq = kqueue()); 8638889Sjdp 8733965Sjdp (void)strlcpy(namebuf, "EVFILT_SIGNAL", sizeof(namebuf)); 8833965Sjdp km.name = namebuf; 8933965Sjdp RL(ioctl(kq, KFILTER_BYNAME, &km)); 9033965Sjdp (void)printf("got %d as filter number for `%s'.\n", km.filter, km.name); 9133965Sjdp 9233965Sjdp /* ignore the signal to avoid taking it for real */ 9333965Sjdp REQUIRE_LIBC(signal(SIGUSR1, SIG_IGN), SIG_ERR); 9433965Sjdp 9577298Sobrien event[0].ident = SIGUSR1; 9633965Sjdp event[0].filter = km.filter; 9733965Sjdp event[0].flags = EV_ADD | EV_ENABLE; 9833965Sjdp 9933965Sjdp RL(kevent(kq, event, 1, NULL, 0, NULL)); 10033965Sjdp 10133965Sjdp (void)sleep(1); 10233965Sjdp 10377298Sobrien timeout.tv_sec = 1; 10433965Sjdp timeout.tv_nsec = 0; 10577298Sobrien 10633965Sjdp for (num = 0; num < NSIGNALS; num += n) { 10733965Sjdp struct timeval then, now, diff; 10833965Sjdp 10933965Sjdp RL(gettimeofday(&then, NULL)); 11033965Sjdp RL(n = kevent(kq, NULL, 0, event, 1, &timeout)); 11133965Sjdp RL(gettimeofday(&now, NULL)); 11233965Sjdp timersub(&now, &then, &diff); 11333965Sjdp 11433965Sjdp (void)printf("sig: kevent returned %d in %lld.%06ld\n", 11533965Sjdp n, (long long)diff.tv_sec, (long)diff.tv_usec); 11633965Sjdp 11760484Sobrien if (n == 0) 11877298Sobrien continue; 11933965Sjdp 12033965Sjdp (void)printf("sig: kevent flags: 0x%x, data: %" PRId64 " (# " 12133965Sjdp "times signal posted)\n", event[0].flags, event[0].data); 12233965Sjdp } 12333965Sjdp 12433965Sjdp (void)waitpid(child, &status, 0); 12533965Sjdp (void)printf("sig: finished successfully\n"); 12633965Sjdp} 12733965Sjdp 12833965SjdpATF_TP_ADD_TCS(tp) 12933965Sjdp{ 13033965Sjdp ATF_TP_ADD_TC(tp, sig); 13133965Sjdp 13233965Sjdp return atf_no_error(); 13333965Sjdp} 13433965Sjdp