Home | History | Annotate | Line # | Download | only in sys
t_pipe.c revision 1.5.14.1
      1  1.5.14.1    martin /* $NetBSD: t_pipe.c,v 1.5.14.1 2020/04/08 14:09:09 martin Exp $ */
      2       1.1    jruoho 
      3       1.1    jruoho /*-
      4       1.1    jruoho  * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
      5       1.1    jruoho  * All rights reserved.
      6       1.1    jruoho  *
      7       1.1    jruoho  * Redistribution and use in source and binary forms, with or without
      8       1.1    jruoho  * modification, are permitted provided that the following conditions
      9       1.1    jruoho  * are met:
     10       1.1    jruoho  * 1. Redistributions of source code must retain the above copyright
     11       1.1    jruoho  *    notice, this list of conditions and the following disclaimer.
     12       1.1    jruoho  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1    jruoho  *    notice, this list of conditions and the following disclaimer in the
     14       1.1    jruoho  *    documentation and/or other materials provided with the distribution.
     15       1.1    jruoho  *
     16       1.1    jruoho  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17       1.1    jruoho  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18       1.1    jruoho  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19       1.1    jruoho  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20       1.1    jruoho  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21       1.1    jruoho  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22       1.1    jruoho  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23       1.1    jruoho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24       1.1    jruoho  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25       1.1    jruoho  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1    jruoho  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1    jruoho  */
     28       1.1    jruoho 
     29       1.1    jruoho #include <sys/cdefs.h>
     30       1.1    jruoho __COPYRIGHT("@(#) Copyright (c) 2008\
     31       1.1    jruoho  The NetBSD Foundation, inc. All rights reserved.");
     32  1.5.14.1    martin __RCSID("$NetBSD: t_pipe.c,v 1.5.14.1 2020/04/08 14:09:09 martin Exp $");
     33       1.1    jruoho 
     34       1.1    jruoho #include <sys/types.h>
     35       1.1    jruoho #include <sys/wait.h>
     36       1.1    jruoho 
     37       1.1    jruoho #include <errno.h>
     38       1.2     njoly #include <fcntl.h>
     39       1.1    jruoho #include <poll.h>
     40       1.1    jruoho #include <sched.h>
     41       1.1    jruoho #include <signal.h>
     42       1.1    jruoho #include <stdio.h>
     43       1.1    jruoho #include <stdlib.h>
     44       1.1    jruoho #include <unistd.h>
     45       1.1    jruoho 
     46       1.1    jruoho #include <atf-c.h>
     47       1.1    jruoho 
     48       1.5  christos #include "h_macros.h"
     49       1.1    jruoho 
     50       1.1    jruoho static pid_t pid;
     51       1.1    jruoho static int nsiginfo = 0;
     52       1.1    jruoho 
     53       1.1    jruoho /*
     54       1.1    jruoho  * This is used for both parent and child. Handle parent's SIGALRM,
     55       1.1    jruoho  * the childs SIGINFO doesn't need anything.
     56       1.1    jruoho  */
     57       1.1    jruoho static void
     58       1.1    jruoho sighand(int sig)
     59       1.1    jruoho {
     60       1.1    jruoho 	if (sig == SIGALRM) {
     61       1.1    jruoho 		kill(pid, SIGINFO);
     62       1.1    jruoho 	}
     63       1.1    jruoho 	if (sig == SIGINFO) {
     64       1.1    jruoho 		nsiginfo++;
     65       1.1    jruoho 	}
     66       1.1    jruoho }
     67       1.1    jruoho 
     68       1.1    jruoho ATF_TC(pipe_restart);
     69       1.1    jruoho ATF_TC_HEAD(pipe_restart, tc)
     70       1.1    jruoho {
     71       1.1    jruoho 	atf_tc_set_md_var(tc, "descr", "Checks that writing to pipe "
     72       1.1    jruoho 	    "works correctly after being interrupted and restarted "
     73       1.1    jruoho 	    "(kern/14087)");
     74       1.1    jruoho }
     75       1.1    jruoho 
     76       1.1    jruoho ATF_TC_BODY(pipe_restart, tc)
     77       1.1    jruoho {
     78       1.1    jruoho 	int pp[2], st;
     79       1.1    jruoho 	ssize_t sz, todo, done;
     80       1.1    jruoho 	char *f;
     81       1.1    jruoho 	sigset_t asigset, osigset, emptysigset;
     82       1.1    jruoho 
     83       1.1    jruoho 	/* Initialise signal masks */
     84       1.1    jruoho 	RL(sigemptyset(&emptysigset));
     85       1.1    jruoho 	RL(sigemptyset(&asigset));
     86       1.1    jruoho 	RL(sigaddset(&asigset, SIGINFO));
     87       1.1    jruoho 
     88       1.1    jruoho 	/* Register signal handlers for both read and writer */
     89       1.1    jruoho 	REQUIRE_LIBC(signal(SIGINFO, sighand), SIG_ERR);
     90       1.1    jruoho 	REQUIRE_LIBC(signal(SIGALRM, sighand), SIG_ERR);
     91       1.1    jruoho 
     92       1.1    jruoho 	todo = 2 * 1024 * 1024;
     93       1.1    jruoho 	REQUIRE_LIBC(f = malloc(todo), NULL);
     94       1.1    jruoho 
     95       1.1    jruoho 	RL(pipe(pp));
     96       1.1    jruoho 
     97       1.1    jruoho 	RL(pid = fork());
     98       1.1    jruoho 	if (pid == 0) {
     99       1.1    jruoho 		/* child */
    100       1.1    jruoho 		RL(close(pp[1]));
    101       1.1    jruoho 
    102  1.5.14.1    martin 		/* Do initial write. This should succeed, make
    103       1.1    jruoho 		 * the other side do partial write and wait for us to pick
    104       1.1    jruoho 		 * rest up.
    105       1.1    jruoho 		 */
    106       1.1    jruoho 		RL(done = read(pp[0], f, 128 * 1024));
    107       1.1    jruoho 
    108       1.1    jruoho 		/* Wait until parent is alarmed and awakens us */
    109       1.1    jruoho 		RL(sigprocmask(SIG_BLOCK, &asigset, &osigset));
    110       1.1    jruoho 		while (nsiginfo == 0) {
    111       1.1    jruoho 			if (sigsuspend(&emptysigset) != -1 || errno != EINTR)
    112       1.1    jruoho 				atf_tc_fail("sigsuspend(&emptysigset): %s",
    113       1.1    jruoho 				    strerror(errno));
    114       1.1    jruoho 		}
    115       1.1    jruoho 		RL(sigprocmask(SIG_SETMASK, &osigset, NULL));
    116       1.1    jruoho 
    117       1.1    jruoho 		/* Read all what parent wants to give us */
    118       1.1    jruoho 		while((sz = read(pp[0], f, 1024 * 1024)) > 0)
    119       1.1    jruoho 			done += sz;
    120       1.1    jruoho 
    121       1.1    jruoho 		/*
    122       1.1    jruoho 		 * Exit with 1 if number of bytes read doesn't match
    123       1.1    jruoho 		 * number of expected bytes
    124       1.1    jruoho 		 */
    125       1.1    jruoho 		printf("Read:     %#zx\n", (size_t)done);
    126       1.1    jruoho 		printf("Expected: %#zx\n", (size_t)todo);
    127       1.1    jruoho 
    128       1.1    jruoho 		exit(done != todo);
    129       1.1    jruoho 
    130       1.1    jruoho 		/* NOTREACHED */
    131       1.1    jruoho 	} else {
    132       1.1    jruoho 		RL(close(pp[0]));
    133       1.1    jruoho 
    134       1.1    jruoho 		/*
    135       1.1    jruoho 		 * Arrange for alarm after two seconds. Since we have
    136       1.1    jruoho 		 * handler setup for SIGARLM, the write(2) call should
    137       1.1    jruoho 		 * be restarted internally by kernel.
    138       1.1    jruoho 		 */
    139       1.1    jruoho 		(void)alarm(2);
    140       1.1    jruoho 
    141       1.1    jruoho 		/* We write exactly 'todo' bytes. The very first write(2)
    142       1.1    jruoho 		 * should partially succeed, block and eventually
    143       1.1    jruoho 		 * be restarted by kernel
    144       1.1    jruoho 		 */
    145       1.1    jruoho 		while(todo > 0 && ((sz = write(pp[1], f, todo)) > 0))
    146       1.1    jruoho 			todo -= sz;
    147       1.1    jruoho 
    148       1.1    jruoho 		/* Close the pipe, so that child would stop reading */
    149       1.1    jruoho 		RL(close(pp[1]));
    150       1.1    jruoho 
    151       1.1    jruoho 		/* And pickup child's exit status */
    152       1.1    jruoho 		RL(waitpid(pid, &st, 0));
    153       1.1    jruoho 
    154       1.1    jruoho 		ATF_REQUIRE_EQ(WEXITSTATUS(st), 0);
    155       1.1    jruoho 	}
    156       1.4  christos 	free(f);
    157       1.1    jruoho }
    158       1.1    jruoho 
    159       1.1    jruoho ATF_TP_ADD_TCS(tp)
    160       1.1    jruoho {
    161       1.1    jruoho 	ATF_TP_ADD_TC(tp, pipe_restart);
    162       1.1    jruoho 
    163       1.1    jruoho 	return atf_no_error();
    164       1.1    jruoho }
    165