1272343Sngie/* $NetBSD: t_pipe.c,v 1.3 2011/10/31 15:41:31 christos Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2001, 2008 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie#include <sys/cdefs.h>
30272343Sngie__COPYRIGHT("@(#) Copyright (c) 2008\
31272343Sngie The NetBSD Foundation, inc. All rights reserved.");
32272343Sngie__RCSID("$NetBSD: t_pipe.c,v 1.3 2011/10/31 15:41:31 christos Exp $");
33272343Sngie
34272343Sngie#include <sys/types.h>
35272343Sngie#include <sys/wait.h>
36272343Sngie
37272343Sngie#include <errno.h>
38272343Sngie#include <fcntl.h>
39272343Sngie#include <poll.h>
40272343Sngie#include <sched.h>
41272343Sngie#include <signal.h>
42272343Sngie#include <stdio.h>
43272343Sngie#include <stdlib.h>
44272343Sngie#include <unistd.h>
45272343Sngie
46272343Sngie#include <atf-c.h>
47272343Sngie
48272343Sngie#include "../../../h_macros.h"
49272343Sngie
50272343Sngiestatic pid_t pid;
51272343Sngiestatic int nsiginfo = 0;
52272343Sngie
53272343Sngie/*
54272343Sngie * This is used for both parent and child. Handle parent's SIGALRM,
55272343Sngie * the childs SIGINFO doesn't need anything.
56272343Sngie */
57272343Sngiestatic void
58272343Sngiesighand(int sig)
59272343Sngie{
60272343Sngie	if (sig == SIGALRM) {
61272343Sngie		kill(pid, SIGINFO);
62272343Sngie	}
63272343Sngie	if (sig == SIGINFO) {
64272343Sngie		nsiginfo++;
65272343Sngie	}
66272343Sngie}
67272343Sngie
68272343SngieATF_TC(pipe_restart);
69272343SngieATF_TC_HEAD(pipe_restart, tc)
70272343Sngie{
71272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks that writing to pipe "
72272343Sngie	    "works correctly after being interrupted and restarted "
73272343Sngie	    "(kern/14087)");
74272343Sngie}
75272343Sngie
76272343SngieATF_TC_BODY(pipe_restart, tc)
77272343Sngie{
78272343Sngie	int pp[2], st;
79272343Sngie	ssize_t sz, todo, done;
80272343Sngie	char *f;
81272343Sngie	sigset_t asigset, osigset, emptysigset;
82272343Sngie
83272343Sngie	/* Initialise signal masks */
84272343Sngie	RL(sigemptyset(&emptysigset));
85272343Sngie	RL(sigemptyset(&asigset));
86272343Sngie	RL(sigaddset(&asigset, SIGINFO));
87272343Sngie
88272343Sngie	/* Register signal handlers for both read and writer */
89272343Sngie	REQUIRE_LIBC(signal(SIGINFO, sighand), SIG_ERR);
90272343Sngie	REQUIRE_LIBC(signal(SIGALRM, sighand), SIG_ERR);
91272343Sngie
92272343Sngie	todo = 2 * 1024 * 1024;
93272343Sngie	REQUIRE_LIBC(f = malloc(todo), NULL);
94272343Sngie
95272343Sngie	RL(pipe(pp));
96272343Sngie
97272343Sngie	RL(pid = fork());
98272343Sngie	if (pid == 0) {
99272343Sngie		/* child */
100272343Sngie		RL(close(pp[1]));
101272343Sngie
102272343Sngie		/* Do inital write. This should succeed, make
103272343Sngie		 * the other side do partial write and wait for us to pick
104272343Sngie		 * rest up.
105272343Sngie		 */
106272343Sngie		RL(done = read(pp[0], f, 128 * 1024));
107272343Sngie
108272343Sngie		/* Wait until parent is alarmed and awakens us */
109272343Sngie		RL(sigprocmask(SIG_BLOCK, &asigset, &osigset));
110272343Sngie		while (nsiginfo == 0) {
111272343Sngie			if (sigsuspend(&emptysigset) != -1 || errno != EINTR)
112272343Sngie				atf_tc_fail("sigsuspend(&emptysigset): %s",
113272343Sngie				    strerror(errno));
114272343Sngie		}
115272343Sngie		RL(sigprocmask(SIG_SETMASK, &osigset, NULL));
116272343Sngie
117272343Sngie		/* Read all what parent wants to give us */
118272343Sngie		while((sz = read(pp[0], f, 1024 * 1024)) > 0)
119272343Sngie			done += sz;
120272343Sngie
121272343Sngie		/*
122272343Sngie		 * Exit with 1 if number of bytes read doesn't match
123272343Sngie		 * number of expected bytes
124272343Sngie		 */
125272343Sngie		printf("Read:     %#zx\n", (size_t)done);
126272343Sngie		printf("Expected: %#zx\n", (size_t)todo);
127272343Sngie
128272343Sngie		exit(done != todo);
129272343Sngie
130272343Sngie		/* NOTREACHED */
131272343Sngie	} else {
132272343Sngie		RL(close(pp[0]));
133272343Sngie
134272343Sngie		/*
135272343Sngie		 * Arrange for alarm after two seconds. Since we have
136272343Sngie		 * handler setup for SIGARLM, the write(2) call should
137272343Sngie		 * be restarted internally by kernel.
138272343Sngie		 */
139272343Sngie		(void)alarm(2);
140272343Sngie
141272343Sngie		/* We write exactly 'todo' bytes. The very first write(2)
142272343Sngie		 * should partially succeed, block and eventually
143272343Sngie		 * be restarted by kernel
144272343Sngie		 */
145272343Sngie		while(todo > 0 && ((sz = write(pp[1], f, todo)) > 0))
146272343Sngie			todo -= sz;
147272343Sngie
148272343Sngie		/* Close the pipe, so that child would stop reading */
149272343Sngie		RL(close(pp[1]));
150272343Sngie
151272343Sngie		/* And pickup child's exit status */
152272343Sngie		RL(waitpid(pid, &st, 0));
153272343Sngie
154272343Sngie		ATF_REQUIRE_EQ(WEXITSTATUS(st), 0);
155272343Sngie	}
156272343Sngie}
157272343Sngie
158272343SngieATF_TP_ADD_TCS(tp)
159272343Sngie{
160272343Sngie	ATF_TP_ADD_TC(tp, pipe_restart);
161272343Sngie
162272343Sngie	return atf_no_error();
163272343Sngie}
164