NetBSD/regress/sys/kern/pipe/pipe1.c

135 lines
3.7 KiB
C

/* $NetBSD: pipe1.c,v 1.3 2002/02/21 07:38:21 itojun Exp $ */
/*-
* Copyright (c) 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <poll.h>
#include <sched.h>
#include <signal.h>
#include <sys/wait.h>
/*
* Test sanity ws. interrupted & restarted write(2) calls.
*/
pid_t pid;
/*
* This is used for both parent and child. Handle parent's SIGALRM,
* the childs SIGINFO doesn't need anything.
*/
void
sighand(int sig)
{
if (sig == SIGALRM)
kill(pid, SIGINFO);
}
int
main()
{
int pp[2], st;
ssize_t sz, todo, done;
char *f;
todo = 2 * 1024 * 1024;
f = (char *) malloc(todo);
pipe(pp);
switch((pid = fork())) {
case 0: /* child */
close(pp[1]);
signal(SIGINFO, sighand);
/* Do inital write. This should succeed, make
* the other side do partial write and wait for us to pick
* rest up.
*/
done = read(pp[0], f, 128 * 1024);
/* Wait until parent is alarmed and awakens us */
pause();
/* Read all what parent wants to give us */
while((sz = read(pp[0], f, 1024 * 1024)) > 0)
done += sz;
/*
* Exit with 1 if number of bytes read doesn't match
* number of expected bytes
*/
exit(done != todo);
break;
case -1: /* error */
perror("fork");
_exit(1);
/* NOTREACHED */
default:
signal(SIGALRM, sighand);
close(pp[0]);
/*
* Arrange for alarm after two seconds. Since we have
* handler setup for SIGARLM, the write(2) call should
* be restarted internally by kernel.
*/
alarm(2);
/* We write exactly 'todo' bytes. The very first write(2)
* should partially succeed, block and eventually
* be restarted by kernel
*/
while(todo > 0 && ((sz = write(pp[1], f, todo)) > 0))
todo -= sz;
/* Close the pipe, so that child would stop reading */
close(pp[1]);
/* And pickup child's exit status */
waitpid(pid, &st, 0);
exit(WEXITSTATUS(st) != 0);
/* NOTREACHED */
}
return (2);
}