1314818Sngie/* $NetBSD: t_select.c,v 1.4 2017/01/13 21:18:33 christos Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundatiom 8272343Sngie * by Christos Zoulas. 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 <assert.h> 33272343Sngie#include <sys/types.h> 34272343Sngie#include <sys/select.h> 35272343Sngie#include <sys/wait.h> 36272343Sngie#include <err.h> 37272343Sngie#include <stdio.h> 38272343Sngie#include <string.h> 39272343Sngie#include <signal.h> 40272343Sngie#include <stdlib.h> 41272343Sngie#include <unistd.h> 42272343Sngie#include <errno.h> 43272343Sngie#include <fcntl.h> 44272343Sngie 45272343Sngie#include <atf-c.h> 46272343Sngie 47272343Sngiestatic sig_atomic_t keep_going = 1; 48272343Sngie 49272343Sngiestatic void 50273539Sngiesig_handler(int signum __unused) 51272343Sngie{ 52272343Sngie keep_going = 0; 53272343Sngie} 54272343Sngie 55272343Sngiestatic void 56273539Sngiesigchld(int signum __unused) 57272343Sngie{ 58272343Sngie} 59272343Sngie 60272343Sngiestatic char 61272343Sngiextoa(uint8_t n) 62272343Sngie{ 63272343Sngie static const char xarray[] = "0123456789abcdef"; 64272343Sngie assert(n < sizeof(xarray)); 65272343Sngie return xarray[n]; 66272343Sngie} 67272343Sngie 68272343Sngiestatic const char * 69272343Sngieprmask(const sigset_t *m, char *buf, size_t len) 70272343Sngie{ 71272343Sngie size_t j = 2; 72272343Sngie assert(len >= 3 + sizeof(*m)); 73272343Sngie buf[0] = '0'; 74272343Sngie buf[1] = 'x'; 75272343Sngie#define N(p, a) (((p) >> ((a) * 4)) & 0xf) 76272343Sngie for (size_t i = __arraycount(m->__bits); i > 0; i--) { 77272343Sngie uint32_t p = m->__bits[i - 1]; 78272343Sngie for (size_t k = sizeof(p); k > 0; k--) 79272343Sngie buf[j++] = xtoa(N(p, k - 1)); 80272343Sngie } 81272343Sngie buf[j] = '\0'; 82272343Sngie return buf; 83272343Sngie} 84272343Sngie 85314818Sngiestatic __dead void 86272343Sngiechild(const struct timespec *ts) 87272343Sngie{ 88272343Sngie struct sigaction sa; 89272343Sngie sigset_t set, oset, nset; 90272343Sngie char obuf[sizeof(oset) + 3], nbuf[sizeof(nset) + 3]; 91272343Sngie int fd; 92272343Sngie 93272343Sngie memset(&sa, 0, sizeof(sa)); 94272343Sngie sa.sa_handler = sig_handler; 95272343Sngie if ((fd = open("/dev/null", O_RDONLY)) == -1) 96272343Sngie err(1, "open"); 97272343Sngie 98272343Sngie if (sigaction(SIGTERM, &sa, NULL) == -1) 99272343Sngie err(1, "sigaction"); 100272343Sngie 101272343Sngie sigfillset(&set); 102272343Sngie if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) 103272343Sngie err(1, "sigprocmask"); 104272343Sngie 105272343Sngie if (sigprocmask(SIG_BLOCK, NULL, &oset) == -1) 106272343Sngie err(1, "sigprocmask"); 107272343Sngie 108272343Sngie sigemptyset(&set); 109272343Sngie 110272343Sngie for (;;) { 111272343Sngie fd_set rset; 112272343Sngie FD_ZERO(&rset); 113272343Sngie FD_SET(fd, &rset); 114272343Sngie if (pselect(1, &rset, NULL, NULL, ts, &set) == -1) { 115272343Sngie if(errno == EINTR) { 116272343Sngie if (!keep_going) 117272343Sngie break; 118272343Sngie } 119272343Sngie } 120272343Sngie if (ts) 121272343Sngie break; 122272343Sngie } 123272343Sngie if (sigprocmask(SIG_BLOCK, NULL, &nset) == -1) 124272343Sngie err(1, "sigprocmask"); 125272343Sngie if (memcmp(&oset, &nset, sizeof(oset)) != 0) 126272343Sngie atf_tc_fail("pselect() masks don't match " 127272343Sngie "after timeout %s != %s", 128272343Sngie prmask(&nset, nbuf, sizeof(nbuf)), 129272343Sngie prmask(&oset, obuf, sizeof(obuf))); 130312040Sngie _exit(0); 131272343Sngie} 132272343Sngie 133272343SngieATF_TC(pselect_sigmask); 134272343SngieATF_TC_HEAD(pselect_sigmask, tc) 135272343Sngie{ 136272343Sngie atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask " 137272343Sngie "setting when a signal is received (PR lib/43625)"); 138272343Sngie} 139272343Sngie 140272343SngieATF_TC_BODY(pselect_sigmask, tc) 141272343Sngie{ 142272343Sngie pid_t pid; 143272343Sngie int status; 144272343Sngie 145272343Sngie signal(SIGCHLD, sigchld); 146272343Sngie 147272343Sngie switch (pid = fork()) { 148272343Sngie case 0: 149272343Sngie child(NULL); 150314818Sngie /*NOTREACHED*/ 151272343Sngie case -1: 152272343Sngie err(1, "fork"); 153272343Sngie default: 154272343Sngie sleep(1); 155272343Sngie if (kill(pid, SIGTERM) == -1) 156272343Sngie err(1, "kill"); 157272343Sngie sleep(1); 158272343Sngie switch (waitpid(pid, &status, WNOHANG)) { 159272343Sngie case -1: 160272343Sngie err(1, "wait"); 161272343Sngie case 0: 162272343Sngie if (kill(pid, SIGKILL) == -1) 163272343Sngie err(1, "kill"); 164272343Sngie atf_tc_fail("pselect() did not receive signal"); 165272343Sngie break; 166272343Sngie default: 167272343Sngie break; 168272343Sngie } 169272343Sngie } 170272343Sngie} 171272343Sngie 172272343SngieATF_TC(pselect_timeout); 173272343SngieATF_TC_HEAD(pselect_timeout, tc) 174272343Sngie{ 175272343Sngie 176272343Sngie atf_tc_set_md_var(tc, "descr", "Checks pselect's temporary mask " 177272343Sngie "setting when a timeout occurs"); 178272343Sngie} 179272343Sngie 180272343SngieATF_TC_BODY(pselect_timeout, tc) 181272343Sngie{ 182272343Sngie pid_t pid; 183272343Sngie int status; 184272343Sngie static const struct timespec zero = { 0, 0 }; 185272343Sngie 186272343Sngie signal(SIGCHLD, sigchld); 187272343Sngie 188272343Sngie switch (pid = fork()) { 189272343Sngie case 0: 190272343Sngie child(&zero); 191272343Sngie break; 192272343Sngie case -1: 193272343Sngie err(1, "fork"); 194272343Sngie default: 195272343Sngie sleep(1); 196272343Sngie switch (waitpid(pid, &status, WNOHANG)) { 197272343Sngie case -1: 198272343Sngie err(1, "wait"); 199272343Sngie case 0: 200272343Sngie if (kill(pid, SIGKILL) == -1) 201272343Sngie err(1, "kill"); 202272343Sngie atf_tc_fail("pselect() did not receive signal"); 203272343Sngie break; 204272343Sngie default: 205272343Sngie break; 206272343Sngie } 207272343Sngie } 208272343Sngie} 209272343Sngie 210272343SngieATF_TP_ADD_TCS(tp) 211272343Sngie{ 212272343Sngie 213272343Sngie ATF_TP_ADD_TC(tp, pselect_sigmask); 214272343Sngie ATF_TP_ADD_TC(tp, pselect_timeout); 215272343Sngie 216272343Sngie return atf_no_error(); 217272343Sngie} 218