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