xref: /freebsd/tools/test/stress2/misc/sendfile_shm.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1*8a272653SPeter Holm#!/bin/sh
2*8a272653SPeter Holm
3*8a272653SPeter Holm#
4*8a272653SPeter Holm# Copyright (c) 2013 EMC Corp.
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) over posix shmfd
30*8a272653SPeter Holm# Test scenario by kib@
31*8a272653SPeter Holm
32*8a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
33*8a272653SPeter Holm
34*8a272653SPeter Holm. ../default.cfg
35*8a272653SPeter Holm
36*8a272653SPeter Holm[ -r /boot/kernel/kernel ] || exit 0
37*8a272653SPeter Holmhere=`pwd`
38*8a272653SPeter Holmdir=`dirname $diskimage`
39*8a272653SPeter Holmcd /tmp
40*8a272653SPeter Holmsed '1,/^EOF/d' < $here/$0 > sendfile_shm.c
41*8a272653SPeter Holmmycc -o sendfile_shm -Wall -Wextra -O2 sendfile_shm.c || exit 1
42*8a272653SPeter Holmrm -f sendfile_shm.c
43*8a272653SPeter Holmcd $here
44*8a272653SPeter Holm
45*8a272653SPeter Holmdaemon ../testcases/swap/swap -t 2m -i 20 > /dev/null 2>&1
46*8a272653SPeter Holmsleep 5
47*8a272653SPeter Holmfor i in `jot 10`; do
48*8a272653SPeter Holm	/tmp/sendfile_shm /boot/kernel/kernel $dir/sendfile_shm.$i > \
49*8a272653SPeter Holm	    /dev/null &
50*8a272653SPeter Holmdone
51*8a272653SPeter Holmwait
52*8a272653SPeter Holmwhile pkill -9 swap; do
53*8a272653SPeter Holm	sleep .5
54*8a272653SPeter Holmdone
55*8a272653SPeter Holmfor i in `jot 10`; do
56*8a272653SPeter Holm	cmp -s /boot/kernel/kernel $dir/sendfile_shm.$i 2>/dev/null ||
57*8a272653SPeter Holm	    { e=1; ls -al $dir/sendfile_shm.* /boot/kernel/kernel; }
58*8a272653SPeter Holm	rm -f $dir/sendfile_shm.$i
59*8a272653SPeter Holm	[ $e ] && break
60*8a272653SPeter Holmdone
61*8a272653SPeter Holm[ -n "$e" ] && echo FAIL
62*8a272653SPeter Holmwait
63*8a272653SPeter Holm
64*8a272653SPeter Holmrm -f /tmp/sendfile_shm
65*8a272653SPeter Holmexit
66*8a272653SPeter HolmEOF
67*8a272653SPeter Holm/* $Id: sendfile_shm1.c,v 1.2 2013/08/25 14:35:14 kostik Exp kostik $ */
68*8a272653SPeter Holm
69*8a272653SPeter Holm#include <sys/types.h>
70*8a272653SPeter Holm#include <sys/event.h>
71*8a272653SPeter Holm#include <sys/mman.h>
72*8a272653SPeter Holm#include <sys/stat.h>
73*8a272653SPeter Holm#include <sys/socket.h>
74*8a272653SPeter Holm#include <sys/time.h>
75*8a272653SPeter Holm#include <sys/uio.h>
76*8a272653SPeter Holm#include <err.h>
77*8a272653SPeter Holm#include <fcntl.h>
78*8a272653SPeter Holm#include <signal.h>
79*8a272653SPeter Holm#include <stdint.h>
80*8a272653SPeter Holm#include <stdio.h>
81*8a272653SPeter Holm#include <stdlib.h>
82*8a272653SPeter Holm#include <string.h>
83*8a272653SPeter Holm#include <unistd.h>
84*8a272653SPeter Holm
85*8a272653SPeter Holm#define	PAGE_SIZE 4096
86*8a272653SPeter Holm
87*8a272653SPeter Holmstatic void
88*8a272653SPeter Holmload(int infd, int shmfd, off_t size)
89*8a272653SPeter Holm{
90*8a272653SPeter Holm	off_t c, r;
91*8a272653SPeter Holm	char buf[100];
92*8a272653SPeter Holm
93*8a272653SPeter Holm	for (c = 0; c < size; c += r) {
94*8a272653SPeter Holm		r = read(infd, buf, sizeof(buf));
95*8a272653SPeter Holm		if (r == -1)
96*8a272653SPeter Holm			err(1, "read disk");
97*8a272653SPeter Holm		else if (r == 0)
98*8a272653SPeter Holm			errx(1, "too short");
99*8a272653SPeter Holm		write(shmfd, buf, r);
100*8a272653SPeter Holm	}
101*8a272653SPeter Holm}
102*8a272653SPeter Holm
103*8a272653SPeter Holmstatic void
104*8a272653SPeter Holmsendfd(int s, int shmfd, off_t size)
105*8a272653SPeter Holm{
106*8a272653SPeter Holm	off_t sbytes;
107*8a272653SPeter Holm	int error;
108*8a272653SPeter Holm
109*8a272653SPeter Holm	error = sendfile(shmfd, s, 0, size, NULL, &sbytes, 0);
110*8a272653SPeter Holm	if (error == -1)
111*8a272653SPeter Holm		err(1, "sendfile(%d, %d, %jd)", s, shmfd, size);
112*8a272653SPeter Holm	printf("sent %jd bytes, requested %jd\n", (uintmax_t)sbytes,
113*8a272653SPeter Holm	    (uintmax_t)size);
114*8a272653SPeter Holm}
115*8a272653SPeter Holm
116*8a272653SPeter Holmstatic void
117*8a272653SPeter Holmreceivefd(int s, int outfd, off_t size)
118*8a272653SPeter Holm{
119*8a272653SPeter Holm	char buf[100];
120*8a272653SPeter Holm	off_t c, r, r1;
121*8a272653SPeter Holm
122*8a272653SPeter Holm	for (c = 0; c < size; c += r) {
123*8a272653SPeter Holm		r = read(s, buf, sizeof(buf));
124*8a272653SPeter Holm		if (r == -1)
125*8a272653SPeter Holm			err(1, "read sock");
126*8a272653SPeter Holm		else if (r == 0)
127*8a272653SPeter Holm			break;
128*8a272653SPeter Holm		else {
129*8a272653SPeter Holm			r1 = write(outfd, buf, r);
130*8a272653SPeter Holm			if (r1 == -1)
131*8a272653SPeter Holm				err(1, "write disk");
132*8a272653SPeter Holm			else if (r1 != r) {
133*8a272653SPeter Holm				err(1, "short write %jd %jd",
134*8a272653SPeter Holm				    (uintmax_t)r, (uintmax_t)r1);
135*8a272653SPeter Holm			}
136*8a272653SPeter Holm		}
137*8a272653SPeter Holm	}
138*8a272653SPeter Holm	printf("received %jd bytes\n", (uintmax_t)c);
139*8a272653SPeter Holm}
140*8a272653SPeter Holm
141*8a272653SPeter Holmint
142*8a272653SPeter Holmmain(int argc, char *argv[])
143*8a272653SPeter Holm{
144*8a272653SPeter Holm	struct stat st;
145*8a272653SPeter Holm	struct sigaction sa;
146*8a272653SPeter Holm	pid_t child;
147*8a272653SPeter Holm	int s[2], infd, outfd, shmfd, error;
148*8a272653SPeter Holm
149*8a272653SPeter Holm	if (argc != 3) {
150*8a272653SPeter Holm		fprintf(stderr, "usage: sendfile_shm infile outfile\n");
151*8a272653SPeter Holm		exit(2);
152*8a272653SPeter Holm	}
153*8a272653SPeter Holm
154*8a272653SPeter Holm	memset(&sa, 0, sizeof(sa));
155*8a272653SPeter Holm	sa.sa_handler = SIG_IGN;
156*8a272653SPeter Holm	error = sigaction(SIGPIPE, &sa, NULL);
157*8a272653SPeter Holm	if (error == -1)
158*8a272653SPeter Holm		err(1, "sigaction SIGPIPE");
159*8a272653SPeter Holm
160*8a272653SPeter Holm	infd = open(argv[1], O_RDONLY);
161*8a272653SPeter Holm	if (infd == -1)
162*8a272653SPeter Holm		err(1, "open %s", argv[1]);
163*8a272653SPeter Holm	error = fstat(infd, &st);
164*8a272653SPeter Holm	if (error == -1)
165*8a272653SPeter Holm		err(1, "stat");
166*8a272653SPeter Holm
167*8a272653SPeter Holm	outfd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0666);
168*8a272653SPeter Holm	if (outfd == -1)
169*8a272653SPeter Holm		err(1, "open %s", argv[2]);
170*8a272653SPeter Holm
171*8a272653SPeter Holm	shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600);
172*8a272653SPeter Holm	if (shmfd == -1)
173*8a272653SPeter Holm		err(1, "shm_open");
174*8a272653SPeter Holm	error = ftruncate(shmfd, st.st_size);
175*8a272653SPeter Holm	if (error == -1)
176*8a272653SPeter Holm		err(1, "ftruncate");
177*8a272653SPeter Holm	load(infd, shmfd, st.st_size);
178*8a272653SPeter Holm
179*8a272653SPeter Holm	error = socketpair(AF_UNIX, SOCK_STREAM, 0, s);
180*8a272653SPeter Holm	if (error == -1)
181*8a272653SPeter Holm		err(1, "socketpair");
182*8a272653SPeter Holm
183*8a272653SPeter Holm	fflush(stdout);
184*8a272653SPeter Holm	fflush(stderr);
185*8a272653SPeter Holm	child = fork();
186*8a272653SPeter Holm	if (child == -1)
187*8a272653SPeter Holm		err(1, "fork");
188*8a272653SPeter Holm	else if (child == 0) {
189*8a272653SPeter Holm		close(s[1]);
190*8a272653SPeter Holm		sendfd(s[0], shmfd, st.st_size);
191*8a272653SPeter Holm		exit(0);
192*8a272653SPeter Holm	} else {
193*8a272653SPeter Holm		close(shmfd);
194*8a272653SPeter Holm		close(s[0]);
195*8a272653SPeter Holm		sleep(1);
196*8a272653SPeter Holm		receivefd(s[1], outfd, st.st_size);
197*8a272653SPeter Holm	}
198*8a272653SPeter Holm
199*8a272653SPeter Holm	return (0);
200*8a272653SPeter Holm}
201