Home | History | Annotate | Line # | Download | only in sys
      1 /* $NetBSD: t_sigqueue.c,v 1.7 2017/01/13 20:44:10 christos Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __RCSID("$NetBSD: t_sigqueue.c,v 1.7 2017/01/13 20:44:10 christos Exp $");
     34 
     35 #include <atf-c.h>
     36 #include <err.h>
     37 #include <errno.h>
     38 #include <signal.h>
     39 #include <stdio.h>
     40 #include <stdlib.h>
     41 #include <sched.h>
     42 #include <unistd.h>
     43 
     44 static void	handler(int, siginfo_t *, void *);
     45 
     46 #define VALUE (int)0xc001dad1
     47 static int value;
     48 
     49 static void
     50 handler(int signo __unused, siginfo_t *info, void *data __unused)
     51 {
     52 	value = info->si_value.sival_int;
     53 	kill(0, SIGINFO);
     54 }
     55 
     56 ATF_TC(sigqueue_basic);
     57 ATF_TC_HEAD(sigqueue_basic, tc)
     58 {
     59 	atf_tc_set_md_var(tc, "descr", "Checks sigqueue(3) sigval delivery");
     60 }
     61 
     62 ATF_TC_BODY(sigqueue_basic, tc)
     63 {
     64 	struct sigaction sa;
     65 	union sigval sv;
     66 
     67 	sa.sa_sigaction = handler;
     68 	sigemptyset(&sa.sa_mask);
     69 	sa.sa_flags = SA_SIGINFO;
     70 
     71 	if (sigaction(SIGUSR1, &sa, NULL) != 0)
     72 		atf_tc_fail("sigaction failed");
     73 
     74 	sv.sival_int = VALUE;
     75 
     76 	if (sigqueue(0, SIGUSR1, sv) != 0)
     77 		atf_tc_fail("sigqueue failed");
     78 
     79 	sched_yield();
     80 	ATF_REQUIRE_EQ(sv.sival_int, value);
     81 }
     82 
     83 ATF_TC(sigqueue_err);
     84 ATF_TC_HEAD(sigqueue_err, tc)
     85 {
     86 	atf_tc_set_md_var(tc, "descr", "Test errors from sigqueue(3)");
     87 }
     88 
     89 ATF_TC_BODY(sigqueue_err, tc)
     90 {
     91 	static union sigval sv;
     92 
     93 	errno = 0;
     94 	ATF_REQUIRE_ERRNO(EINVAL, sigqueue(getpid(), -1, sv) == -1);
     95 }
     96 
     97 static int signals[] = {
     98 	SIGINT, SIGRTMIN + 1, SIGINT, SIGRTMIN + 0, SIGRTMIN + 2,
     99 	SIGQUIT, SIGRTMIN + 1
    100 };
    101 #ifdef __arraycount
    102 #define CNT	__arraycount(signals)
    103 #else
    104 #define CNT	(sizeof(signals) / sizeof(signals[0]))
    105 #endif
    106 
    107 static sig_atomic_t count = 0;
    108 static int delivered[CNT];
    109 
    110 static void
    111 myhandler(int signo, siginfo_t *info, void *context __unused)
    112 {
    113 	delivered[count++] = signo;
    114 	printf("Signal #%zu: signo: %d\n", (size_t)count, signo);
    115 }
    116 
    117 static int
    118 asc(const void *a, const void *b)
    119 {
    120 	const int *ia = a, *ib = b;
    121 	return *ib - *ia;
    122 }
    123 
    124 /*
    125  * given a array of signals to be delivered in tosend of size len
    126  * place in ordered the signals to be delivered in delivery order
    127  * and return the number of signals that should be delivered
    128  */
    129 static size_t
    130 sigorder(int *ordered, const int *tosend, size_t len)
    131 {
    132 	memcpy(ordered, tosend, len * sizeof(*tosend));
    133 	qsort(ordered, len, sizeof(*ordered), asc);
    134 	if (len == 1)
    135 		return len;
    136 
    137 	size_t i, j;
    138 	for (i = 0, j = 0; i < len - 1; i++) {
    139 		if (ordered[i] >= SIGRTMIN)
    140 			continue;
    141 		if (j == 0)
    142 			j = i + 1;
    143 		while (ordered[i] == ordered[j] && j < len)
    144 			j++;
    145 		if (j == len)
    146 			break;
    147 		ordered[i + 1] = ordered[j];
    148 	}
    149 	return i + 1;
    150 }
    151 
    152 ATF_TC(sigqueue_rt);
    153 ATF_TC_HEAD(sigqueue_rt, tc)
    154 {
    155 	atf_tc_set_md_var(tc, "descr", "Test queuing of real-time signals");
    156 }
    157 
    158 ATF_TC_BODY(sigqueue_rt, tc)
    159 {
    160 	pid_t pid;
    161 	union sigval val;
    162 	struct sigaction act;
    163 	int ordered[CNT];
    164 	struct sigaction oact[CNT];
    165 	size_t ndelivered;
    166 
    167 	ndelivered = sigorder(ordered, signals, CNT);
    168 
    169 	act.sa_flags = SA_SIGINFO;
    170 	act.sa_sigaction = myhandler;
    171 	sigemptyset(&act.sa_mask);
    172 	for (size_t i = 0; i < ndelivered; i++)
    173 		ATF_REQUIRE(sigaction(ordered[i], &act, &oact[i]) != -1);
    174 
    175 	val.sival_int = 0;
    176 	pid = getpid();
    177 
    178 	sigset_t mask, orig;
    179 	sigemptyset(&mask);
    180 	for (size_t i = 0; i < CNT; i++)
    181 		if (sigaddset(&mask, signals[i]) == -1)
    182 			warn("sigaddset");
    183 
    184 	ATF_REQUIRE(sigprocmask(SIG_BLOCK, &mask, &orig) != -1);
    185 
    186 	for (size_t i = 0; i < CNT; i++)
    187 		ATF_REQUIRE(sigqueue(pid, signals[i], val) != -1);
    188 
    189 	ATF_REQUIRE(sigprocmask(SIG_UNBLOCK, &mask, &orig) != -1);
    190 	sleep(1);
    191 	ATF_CHECK_MSG((size_t)count == ndelivered,
    192 	    "count %zu != ndelivered %zu", (size_t)count, ndelivered);
    193 	for (size_t i = 0; i < ndelivered; i++)
    194 		ATF_REQUIRE_MSG(ordered[i] == delivered[i],
    195 		    "%zu: ordered %d != delivered %d",
    196 		    i, ordered[i], delivered[i]);
    197 
    198 	if ((size_t)count > ndelivered)
    199 		for (size_t i = ndelivered; i < (size_t)count; i++)
    200 			printf("Undelivered signal #%zu: %d\n", i, ordered[i]);
    201 
    202 	for (size_t i = 0; i < ndelivered; i++)
    203 		ATF_REQUIRE(sigaction(signals[i], &oact[i], NULL) != -1);
    204 }
    205 
    206 ATF_TP_ADD_TCS(tp)
    207 {
    208 
    209 	ATF_TP_ADD_TC(tp, sigqueue_basic);
    210 	ATF_TP_ADD_TC(tp, sigqueue_err);
    211 	ATF_TP_ADD_TC(tp, sigqueue_rt);
    212 
    213 	return atf_no_error();
    214 }
    215