1314818Sngie/* $NetBSD: t_sig.c,v 1.3 2017/01/13 21:30:41 christos Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Luke Mewburn and Jaromir Dolecek. 9272343Sngie * 10272343Sngie * Redistribution and use in source and binary forms, with or without 11272343Sngie * modification, are permitted provided that the following conditions 12272343Sngie * are met: 13272343Sngie * 1. Redistributions of source code must retain the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer. 15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 16272343Sngie * notice, this list of conditions and the following disclaimer in the 17272343Sngie * documentation and/or other materials provided with the distribution. 18272343Sngie * 19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29272343Sngie * POSSIBILITY OF SUCH DAMAGE. 30272343Sngie */ 31272343Sngie 32272343Sngie#include <sys/cdefs.h> 33272343Sngie__COPYRIGHT("@(#) Copyright (c) 2008\ 34272343Sngie The NetBSD Foundation, inc. All rights reserved."); 35314818Sngie__RCSID("$NetBSD: t_sig.c,v 1.3 2017/01/13 21:30:41 christos Exp $"); 36272343Sngie 37272343Sngie#include <sys/event.h> 38272343Sngie#include <sys/ioctl.h> 39272343Sngie#include <sys/param.h> 40272343Sngie#include <sys/time.h> 41272343Sngie#include <sys/wait.h> 42272343Sngie 43272343Sngie#include <inttypes.h> 44272343Sngie#include <signal.h> 45272343Sngie#include <stdio.h> 46272343Sngie#include <stdlib.h> 47272343Sngie#include <unistd.h> 48272343Sngie 49272343Sngie#include <atf-c.h> 50272343Sngie 51314818Sngie#include "h_macros.h" 52272343Sngie 53272343Sngie#define NSIGNALS 5 54272343Sngie 55272343SngieATF_TC(sig); 56272343SngieATF_TC_HEAD(sig, tc) 57272343Sngie{ 58272343Sngie atf_tc_set_md_var(tc, "descr", "Checks EVFILT_SIGNAL"); 59272343Sngie} 60272343SngieATF_TC_BODY(sig, tc) 61272343Sngie{ 62272343Sngie struct timespec timeout; 63309469Sngie#ifdef __NetBSD__ 64272343Sngie struct kfilter_mapping km; 65309469Sngie#endif 66272343Sngie struct kevent event[1]; 67309469Sngie#ifdef __NetBSD__ 68272343Sngie char namebuf[32]; 69309469Sngie#endif 70272343Sngie pid_t pid, child; 71272343Sngie int kq, n, num, status; 72272343Sngie 73272343Sngie pid = getpid(); 74272343Sngie (void)printf("my pid: %d\n", pid); 75272343Sngie 76272343Sngie /* fork a child to send signals */ 77272343Sngie RL(child = fork()); 78272343Sngie if (child == 0) { 79272343Sngie int i; 80272343Sngie (void)sleep(2); 81272343Sngie for(i = 0; i < NSIGNALS; ++i) { 82272343Sngie (void)kill(pid, SIGUSR1); 83272343Sngie (void)sleep(2); 84272343Sngie } 85272343Sngie _exit(0); 86272343Sngie /* NOTREACHED */ 87272343Sngie } 88272343Sngie 89272343Sngie RL(kq = kqueue()); 90272343Sngie 91309469Sngie#ifdef __NetBSD__ 92272343Sngie (void)strlcpy(namebuf, "EVFILT_SIGNAL", sizeof(namebuf)); 93272343Sngie km.name = namebuf; 94272343Sngie RL(ioctl(kq, KFILTER_BYNAME, &km)); 95272343Sngie (void)printf("got %d as filter number for `%s'.\n", km.filter, km.name); 96309469Sngie#endif 97272343Sngie 98272343Sngie /* ignore the signal to avoid taking it for real */ 99272343Sngie REQUIRE_LIBC(signal(SIGUSR1, SIG_IGN), SIG_ERR); 100272343Sngie 101272343Sngie event[0].ident = SIGUSR1; 102309469Sngie#ifdef __NetBSD__ 103272343Sngie event[0].filter = km.filter; 104309469Sngie#else 105309469Sngie event[0].filter = EVFILT_SIGNAL; 106309469Sngie#endif 107272343Sngie event[0].flags = EV_ADD | EV_ENABLE; 108272343Sngie 109272343Sngie RL(kevent(kq, event, 1, NULL, 0, NULL)); 110272343Sngie 111272343Sngie (void)sleep(1); 112272343Sngie 113272343Sngie timeout.tv_sec = 1; 114272343Sngie timeout.tv_nsec = 0; 115272343Sngie 116272343Sngie for (num = 0; num < NSIGNALS; num += n) { 117272343Sngie struct timeval then, now, diff; 118272343Sngie 119272343Sngie RL(gettimeofday(&then, NULL)); 120272343Sngie RL(n = kevent(kq, NULL, 0, event, 1, &timeout)); 121272343Sngie RL(gettimeofday(&now, NULL)); 122272343Sngie timersub(&now, &then, &diff); 123272343Sngie 124272343Sngie (void)printf("sig: kevent returned %d in %lld.%06ld\n", 125272343Sngie n, (long long)diff.tv_sec, (long)diff.tv_usec); 126272343Sngie 127272343Sngie if (n == 0) 128272343Sngie continue; 129272343Sngie 130309469Sngie#ifdef __FreeBSD__ 131309469Sngie (void)printf("sig: kevent flags: 0x%x, data: %" PRIdPTR " (# " 132309469Sngie#else 133272343Sngie (void)printf("sig: kevent flags: 0x%x, data: %" PRId64 " (# " 134309469Sngie#endif 135272343Sngie "times signal posted)\n", event[0].flags, event[0].data); 136272343Sngie } 137272343Sngie 138272343Sngie (void)waitpid(child, &status, 0); 139272343Sngie (void)printf("sig: finished successfully\n"); 140272343Sngie} 141272343Sngie 142272343SngieATF_TP_ADD_TCS(tp) 143272343Sngie{ 144272343Sngie ATF_TP_ADD_TC(tp, sig); 145272343Sngie 146272343Sngie return atf_no_error(); 147272343Sngie} 148