xref: /freebsd/tools/test/stress2/misc/sendfile4.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1*8a272653SPeter Holm#!/bin/sh
2*8a272653SPeter Holm
3*8a272653SPeter Holm#
4*8a272653SPeter Holm# Copyright (c) 2011 Peter Holm <pho@FreeBSD.org>
5*8a272653SPeter Holm# All rights reserved.
6*8a272653SPeter Holm#
7*8a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
8*8a272653SPeter Holm# modification, are permitted provided that the following conditions
9*8a272653SPeter Holm# are met:
10*8a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
11*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
12*8a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
13*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
14*8a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
15*8a272653SPeter Holm#
16*8a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*8a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*8a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8a272653SPeter Holm# SUCH DAMAGE.
27*8a272653SPeter Holm#
28*8a272653SPeter Holm
29*8a272653SPeter Holm# sendfile(2) test by kib@
30*8a272653SPeter Holm# Both processes will sit in the sbwait state forever:
31*8a272653SPeter Holm
32*8a272653SPeter Holm. ../default.cfg
33*8a272653SPeter Holm
34*8a272653SPeter Holmhere=`pwd`
35*8a272653SPeter Holmcd /tmp
36*8a272653SPeter Holmsed '1,/^EOF/d' < $here/$0 > sendfile4.c
37*8a272653SPeter Holmmycc -o sendfile4 -Wall -Wextra -O2 sendfile4.c || exit
38*8a272653SPeter Holmrm -f sendfile4.c
39*8a272653SPeter Holmcd $here
40*8a272653SPeter Holm
41*8a272653SPeter Holmdd if=/dev/zero of=$diskimage bs=1m count=6 status=none
42*8a272653SPeter Holm/tmp/sendfile4 $diskimage || echo FAIL
43*8a272653SPeter Holm
44*8a272653SPeter Holmrm -f /tmp/sendfile4 $diskimage
45*8a272653SPeter Holmexit
46*8a272653SPeter HolmEOF
47*8a272653SPeter Holm#include <sys/types.h>
48*8a272653SPeter Holm#include <sys/fcntl.h>
49*8a272653SPeter Holm#include <sys/socket.h>
50*8a272653SPeter Holm#include <sys/stat.h>
51*8a272653SPeter Holm#include <sys/uio.h>
52*8a272653SPeter Holm#include <sys/wait.h>
53*8a272653SPeter Holm#include <err.h>
54*8a272653SPeter Holm#include <errno.h>
55*8a272653SPeter Holm#include <signal.h>
56*8a272653SPeter Holm#include <stdlib.h>
57*8a272653SPeter Holm#include <unistd.h>
58*8a272653SPeter Holm
59*8a272653SPeter Holmstatic void
60*8a272653SPeter Holmhandler(int i __unused) {
61*8a272653SPeter Holm	_exit(1);
62*8a272653SPeter Holm}
63*8a272653SPeter Holm
64*8a272653SPeter Holmint
65*8a272653SPeter Holmmain(int argc, char *argv[])
66*8a272653SPeter Holm{
67*8a272653SPeter Holm	const char *from_name;
68*8a272653SPeter Holm	char *buf;
69*8a272653SPeter Holm	int sv[2];
70*8a272653SPeter Holm	struct stat st;
71*8a272653SPeter Holm	off_t written, pos;
72*8a272653SPeter Holm	int child, error, from, status;
73*8a272653SPeter Holm
74*8a272653SPeter Holm	if (argc != 2)
75*8a272653SPeter Holm		errx(1, "Usage: %s from", argv[0]);
76*8a272653SPeter Holm	from_name = argv[1];
77*8a272653SPeter Holm
78*8a272653SPeter Holm	from = open(from_name, O_RDONLY);
79*8a272653SPeter Holm	if (from == -1)
80*8a272653SPeter Holm		err(1, "open read %s", from_name);
81*8a272653SPeter Holm
82*8a272653SPeter Holm	error = fstat(from, &st);
83*8a272653SPeter Holm	if (error == -1)
84*8a272653SPeter Holm		err(1, "stat %s", from_name);
85*8a272653SPeter Holm
86*8a272653SPeter Holm	error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
87*8a272653SPeter Holm	if (error == -1)
88*8a272653SPeter Holm		err(1, "socketpair");
89*8a272653SPeter Holm
90*8a272653SPeter Holm	signal(SIGALRM, handler);
91*8a272653SPeter Holm	alarm(120);
92*8a272653SPeter Holm	child = fork();
93*8a272653SPeter Holm	if (child == -1)
94*8a272653SPeter Holm		err(1, "fork");
95*8a272653SPeter Holm	else if (child != 0) {
96*8a272653SPeter Holm		close(sv[1]);
97*8a272653SPeter Holm		pos = 0;
98*8a272653SPeter Holm		for (;;) {
99*8a272653SPeter Holm			error = sendfile(from, sv[0], pos, st.st_size - pos,
100*8a272653SPeter Holm			    NULL, &written, 0);
101*8a272653SPeter Holm			if (error == -1) {
102*8a272653SPeter Holm				if (errno != EAGAIN)
103*8a272653SPeter Holm					err(1, "sendfile");
104*8a272653SPeter Holm			}
105*8a272653SPeter Holm			pos += written;
106*8a272653SPeter Holm			if (pos == st.st_size)
107*8a272653SPeter Holm				break;
108*8a272653SPeter Holm		}
109*8a272653SPeter Holm		close(sv[0]);
110*8a272653SPeter Holm		waitpid(child, &status, 0);
111*8a272653SPeter Holm	} else {
112*8a272653SPeter Holm		close(sv[0]);
113*8a272653SPeter Holm		buf = malloc(st.st_size);
114*8a272653SPeter Holm		if (buf == NULL)
115*8a272653SPeter Holm			err(1, "malloc %jd", st.st_size);
116*8a272653SPeter Holm		pos = 0;
117*8a272653SPeter Holm		for (;;) {
118*8a272653SPeter Holm			written = 413;
119*8a272653SPeter Holm			if (written > st.st_size - pos)
120*8a272653SPeter Holm				written = st.st_size - pos;
121*8a272653SPeter Holm#if 0
122*8a272653SPeter Holm			written = st.st_size - pos;
123*8a272653SPeter Holm			if (written > 1000)
124*8a272653SPeter Holm				written = 1000;
125*8a272653SPeter Holm			written = arc4random_uniform(written) + 1;
126*8a272653SPeter Holm#endif
127*8a272653SPeter Holm			error = read(sv[1], buf + pos, written);
128*8a272653SPeter Holm			if (error == -1)
129*8a272653SPeter Holm				err(1, "read");
130*8a272653SPeter Holm			else if (error == 0)
131*8a272653SPeter Holm				errx(1, "short read");
132*8a272653SPeter Holm			pos += error;
133*8a272653SPeter Holm			if (pos == st.st_size)
134*8a272653SPeter Holm				break;
135*8a272653SPeter Holm		}
136*8a272653SPeter Holm		close(sv[1]);
137*8a272653SPeter Holm		_exit(0);
138*8a272653SPeter Holm	}
139*8a272653SPeter Holm
140*8a272653SPeter Holm	return (0);
141*8a272653SPeter Holm}
142