Index: user/pho/stress2/misc/sendfile.sh =================================================================== --- user/pho/stress2/misc/sendfile.sh (revision 285624) +++ user/pho/stress2/misc/sendfile.sh (revision 285625) @@ -1,215 +1,215 @@ #!/bin/sh # # Copyright (c) 2009 Peter Holm # 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. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. # # $FreeBSD$ # # Test scenario for sendfile livelock seen on 7.2-STABLE for non SMP # Scenario by kib@ . ../default.cfg odir=`pwd` cd /tmp sed '1,/^EOF/d' < $odir/$0 > sendfile.c mycc -o sendfile -Wall sendfile.c -pthread rm -f sendfile.c [ -d "$RUNDIR" ] || mkdir -p $RUNDIR cd $RUNDIR -in=/tmp/inputFile -out=/tmp/outputFile +in=inputFile +out=outputFile for i in 1 2 3 4 8 16 1k 2k 3k 4k 5k 1m 2m 3m 4m 5m ; do rm -f $in $out dd if=/dev/random of=$in bs=$i count=1 2>&1 | \ egrep -v "records|transferred" /tmp/sendfile $in $out 12345 cmp $in $out || { echo FAIL; ls -l $in $out; } rm -f $in $out done rm -f /tmp/sendfile exit EOF #include #include #include #include #include #include #include #include #include #include #include #include int port; char *inputFile; char *outputFile; int bufsize = 4096; static void reader(void) { int tcpsock, msgsock; int on; socklen_t len; struct sockaddr_in inetaddr, inetpeer; int n, t, *buf, fd; on = 1; if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) err(1, "socket(), %s:%d", __FILE__, __LINE__); if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) err(1, "setsockopt(), %s:%d", __FILE__, __LINE__); inetaddr.sin_family = AF_INET; inetaddr.sin_addr.s_addr = INADDR_ANY; inetaddr.sin_port = htons(port); inetaddr.sin_len = sizeof(inetaddr); if (bind(tcpsock, (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) err(1, "bind(), %s:%d", __FILE__, __LINE__); if (listen(tcpsock, 5) < 0) err(1, "listen(), %s:%d", __FILE__, __LINE__); len = sizeof(inetpeer); if ((msgsock = accept(tcpsock, (struct sockaddr *)&inetpeer, &len)) < 0) err(1, "accept(), %s:%d", __FILE__, __LINE__); t = 0; if ((buf = malloc(bufsize)) == NULL) err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__); if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1) err(1, "open(%s)", outputFile); for (;;) { if ((n = read(msgsock, buf, bufsize)) < 0) err(1, "read(), %s:%d", __FILE__, __LINE__); t += n; if (n == 0) break; if ((write(fd, buf, n)) != n) err(1, "write"); } close(msgsock); close(fd); return; } static void writer(void) { int tcpsock, on; struct sockaddr_in inetaddr; struct hostent *hostent; struct stat statb; int i, r, fd; off_t off = 0; #if 1 size_t size; #endif on = 1; for (i = 1; i < 5; i++) { if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) err(1, "socket(), %s:%d", __FILE__, __LINE__); if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) err(1, "setsockopt(), %s:%d", __FILE__, __LINE__); #if 1 /* livelock trigger */ size = getpagesize() -4; if (setsockopt(tcpsock, SOL_SOCKET, SO_SNDBUF, (void *)&size, sizeof(size)) < 0) err(1, "setsockopt(SO_SNDBUF), %s:%d", __FILE__, __LINE__); #endif hostent = gethostbyname ("localhost"); memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr, sizeof (struct in_addr)); inetaddr.sin_family = AF_INET; inetaddr.sin_addr.s_addr = INADDR_ANY; inetaddr.sin_port = htons(port); inetaddr.sin_len = sizeof(inetaddr); r = connect(tcpsock, (struct sockaddr *) &inetaddr, sizeof(inetaddr)); if (r == 0) break; sleep(1); close(tcpsock); } if (r < 0) err(1, "connect(), %s:%d", __FILE__, __LINE__); if (stat(inputFile, &statb) != 0) err(1, "stat(%s)", inputFile); if ((fd = open(inputFile, O_RDONLY)) == -1) err(1, "open(%s)", inputFile); if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1) err(1, "sendfile"); return; } int main(int argc, char **argv) { pid_t pid; if (argc != 4) { fprintf(stderr, "Usage: %s 0) { reader(); kill(pid, SIGINT); } else err(1, "fork(), %s:%d", __FILE__, __LINE__); return (0); } Index: user/pho/stress2/misc/sendfile3.sh =================================================================== --- user/pho/stress2/misc/sendfile3.sh (revision 285624) +++ user/pho/stress2/misc/sendfile3.sh (revision 285625) @@ -1,217 +1,215 @@ #!/bin/sh # # Copyright (c) 2011 Peter Holm # 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. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. # # $FreeBSD$ # # Test scenario for sendfile deadlock (processes stuck in sfbufa) # Variation of sendfile.sh # kern.ipc.nsfbufs should be low for this test . ../default.cfg odir=`pwd` cd /tmp sed '1,/^EOF/d' < $odir/$0 > sendfile3.c mycc -o sendfile3 -Wall -Wextra sendfile3.c rm -f sendfile3.c [ -d "$RUNDIR" ] || mkdir -p $RUNDIR cd $RUNDIR -in=/tmp/inputFile -out=/tmp/outputFile +in=inputFile +out=outputFile parallel=20 for i in 50m 100m; do rm -f $in dd if=/dev/random of=$in bs=$i count=1 2>&1 | \ egrep -v "records|transferred" for j in `jot $parallel`; do rm -f ${out}$j /tmp/sendfile3 $in ${out}$j 1234$j & done - for j in `jot $parallel`; do - wait - done + wait for j in `jot $parallel`; do rm -f ${out}$j done done rm -f $in /tmp/sendfile3 exit EOF #include #include #include #include #include #include #include #include #include #include #include #include #include int port; char *inputFile; char *outputFile; int bufsize = 4096; static void reader(void) { int tcpsock, msgsock; int on; socklen_t len; struct sockaddr_in inetaddr, inetpeer; int n, t, *buf, fd; on = 1; if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) err(1, "socket(), %s:%d", __FILE__, __LINE__); if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) err(1, "setsockopt(), %s:%d", __FILE__, __LINE__); inetaddr.sin_family = AF_INET; inetaddr.sin_addr.s_addr = INADDR_ANY; inetaddr.sin_port = htons(port); inetaddr.sin_len = sizeof(inetaddr); if (bind(tcpsock, (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) err(1, "bind(), %s:%d", __FILE__, __LINE__); if (listen(tcpsock, 5) < 0) err(1, "listen(), %s:%d", __FILE__, __LINE__); len = sizeof(inetpeer); if ((msgsock = accept(tcpsock, (struct sockaddr *)&inetpeer, &len)) < 0) err(1, "accept(), %s:%d", __FILE__, __LINE__); t = 0; if ((buf = malloc(bufsize)) == NULL) err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__); if ((fd = open(outputFile, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1) err(1, "open(%s)", outputFile); for (;;) { if ((n = read(msgsock, buf, bufsize)) < 0) err(1, "read(), %s:%d", __FILE__, __LINE__); t += n; if (n == 0) break; if ((write(fd, buf, n)) != n) err(1, "write"); } close(msgsock); close(fd); return; } static void writer(void) { int tcpsock, on; struct sockaddr_in inetaddr; struct hostent *hostent; struct stat statb; int i, r, fd; off_t off = 0; on = 1; for (i = 0; i < 5; i++) { if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) err(1, "socket(), %s:%d", __FILE__, __LINE__); if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) err(1, "setsockopt(), %s:%d", __FILE__, __LINE__); bzero((char *) &inetaddr, sizeof(inetaddr)); hostent = gethostbyname ("localhost"); memcpy (&inetaddr.sin_addr.s_addr, hostent->h_addr, sizeof (struct in_addr)); inetaddr.sin_family = AF_INET; inetaddr.sin_addr.s_addr = INADDR_ANY; inetaddr.sin_port = htons(port); inetaddr.sin_len = sizeof(inetaddr); r = connect(tcpsock, (struct sockaddr *) &inetaddr, sizeof(inetaddr)); if (r == 0) break; sleep(1); close(tcpsock); } if (r < 0) err(1, "connect(), %s:%d", __FILE__, __LINE__); if (stat(inputFile, &statb) != 0) err(1, "stat(%s)", inputFile); if ((fd = open(inputFile, O_RDONLY)) == -1) err(1, "open(%s)", inputFile); if (sendfile(fd, tcpsock, 0, statb.st_size, NULL, &off, 0) == -1) err(1, "sendfile"); } int main(int argc, char **argv) { pid_t pid; if (argc != 4) { fprintf(stderr, "Usage: %s 0) { reader(); kill(pid, SIGINT); waitpid(pid, NULL, 0); } else err(1, "fork(), %s:%d", __FILE__, __LINE__); return (0); } Index: user/pho/stress2/misc/sendfile5.sh =================================================================== --- user/pho/stress2/misc/sendfile5.sh (revision 285624) +++ user/pho/stress2/misc/sendfile5.sh (revision 285625) @@ -1,159 +1,161 @@ #!/bin/sh # # Copyright (c) 2011 Peter Holm # 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. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. # # $FreeBSD$ # # sendfile(2) test by kib@ # Deadlock seen. [ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 . ../default.cfg here=`pwd` +file=`basename $diskimage` +dir=`dirname $diskimage` cd /tmp sed '1,/^EOF/d' < $here/$0 > sendfile5.c mycc -o sendfile5 -Wall -Wextra -O2 sendfile5.c rm -f sendfile5.c -dd if=/dev/zero of=/tmp/big bs=1m count=1k 2>&1 | egrep -v "records|transferred" +dd if=/dev/zero of=$diskimage bs=1m count=1k 2>&1 | egrep -v "records|transferred" cd $here mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart mount -t tmpfs tmpfs $mntpoint echo "Testing tmpfs(5)" -cp /tmp/big $mntpoint -/tmp/sendfile5 $mntpoint/big +cp $diskimage $mntpoint +/tmp/sendfile5 $mntpoint/$file umount $mntpoint mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart mdconfig -a -t swap -s 2g -u $mdstart || exit 1 bsdlabel -w md$mdstart auto newfs $newfs_flags md${mdstart}$part > /dev/null mount /dev/md${mdstart}$part $mntpoint echo "Testing FFS" -cp /tmp/big $mntpoint -/tmp/sendfile5 $mntpoint/big +cp $diskimage $mntpoint +/tmp/sendfile5 $mntpoint/$file umount $mntpoint -mount -t nullfs /tmp $mntpoint +mount -t nullfs $dir $mntpoint echo "Testing nullfs(5)" -/tmp/sendfile5 $mntpoint/big +/tmp/sendfile5 $mntpoint/$file umount $mntpoint -rm -f /tmp/sendfile5 /tmp/big +rm -f /tmp/sendfile5 $diskimage exit EOF #include #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { const char *from_name; char *buf; int sv[2]; struct stat st; off_t written, pos; int child, error, from, status; if (argc != 2) errx(1, "Usage: %s from", argv[0]); from_name = argv[1]; from = open(from_name, O_RDONLY); if (from == -1) err(1, "open read %s", from_name); error = fstat(from, &st); if (error == -1) err(1, "stat %s", from_name); error = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); if (error == -1) err(1, "socketpair"); child = fork(); if (child == -1) err(1, "fork"); else if (child != 0) { close(sv[1]); pos = 0; for (;;) { error = sendfile(from, sv[0], pos, st.st_size - pos, NULL, &written, 0); if (error == -1) { if (errno != EAGAIN) err(1, "sendfile"); } pos += written; if (pos == st.st_size) break; } close(sv[0]); waitpid(child, &status, 0); } else { close(sv[0]); buf = malloc(st.st_size); if (buf == NULL) err(1, "malloc %jd", st.st_size); pos = 0; for (;;) { written = 413; if (written > st.st_size - pos) written = st.st_size - pos; #if 0 written = st.st_size - pos; if (written > 1000) written = 1000; written = arc4random_uniform(written) + 1; #endif error = read(sv[1], buf + pos, written); if (error == -1) err(1, "read"); else if (error == 0) errx(1, "short read"); pos += error; if (pos == st.st_size) break; } close(sv[1]); _exit(0); } return (0); } Index: user/pho/stress2/misc/sendfile7.sh =================================================================== --- user/pho/stress2/misc/sendfile7.sh (revision 285624) +++ user/pho/stress2/misc/sendfile7.sh (revision 285625) @@ -1,41 +1,41 @@ #!/bin/sh # # Copyright (c) 2013 Peter Holm # 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. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. # # $FreeBSD$ # # Run sendfile5.sh with VM pressure added. # "panic: vm_page_requeue: page 0xc47fdcc0 is not queued" seen. [ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 ./sendfile5.sh & while kill -0 $! 2>/dev/null; do - ../testcases/swap/swap -t 2m -i 40 + ../testcases/swap/swap -t 2m -i 40 > /dev/null 2>&1 done wait Index: user/pho/stress2/misc/sendfile_shm.sh =================================================================== --- user/pho/stress2/misc/sendfile_shm.sh (revision 285624) +++ user/pho/stress2/misc/sendfile_shm.sh (revision 285625) @@ -1,202 +1,203 @@ #!/bin/sh # # Copyright (c) 2013 EMC Corp. # 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. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. # # $FreeBSD$ # # Sendfile(2) over posix shmfd # Test scenario by kib@ [ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 . ../default.cfg [ -r /boot/kernel/kernel ] || exit 0 here=`pwd` +dir=`dirname $diskimage` cd /tmp sed '1,/^EOF/d' < $here/$0 > sendfile_shm.c mycc -o sendfile_shm -Wall -Wextra -O2 sendfile_shm.c || exit 1 rm -f sendfile_shm.c cd $here daemon ../testcases/swap/swap -t 2m -i 20 > /dev/null 2>&1 sleep 5 for i in `jot 10`; do - /tmp/sendfile_shm /boot/kernel/kernel /tmp/sendfile_shm.$i > \ + /tmp/sendfile_shm /boot/kernel/kernel $dir/sendfile_shm.$i > \ /dev/null & done for i in `jot 10`; do wait done while pkill -9 swap; do sleep .5 done for i in `jot 10`; do - cmp -s /boot/kernel/kernel /tmp/sendfile_shm.$i 2>/dev/null || + cmp -s /boot/kernel/kernel $dir/sendfile_shm.$i 2>/dev/null || e=1 - rm -f /tmp/sendfile_shm.$i + rm -f $dir/sendfile_shm.$i done [ -n "$e" ] && echo FAIL wait rm -f /tmp/sendfile_shm exit EOF /* $Id: sendfile_shm1.c,v 1.2 2013/08/25 14:35:14 kostik Exp kostik $ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PAGE_SIZE 4096 static void load(int infd, int shmfd, off_t size) { off_t c, r; char buf[100]; for (c = 0; c < size; c += r) { r = read(infd, buf, sizeof(buf)); if (r == -1) err(1, "read disk"); else if (r == 0) errx(1, "too short"); write(shmfd, buf, r); } } static void sendfd(int s, int shmfd, off_t size) { off_t sbytes; int error; error = sendfile(shmfd, s, 0, size, NULL, &sbytes, 0); if (error == -1) err(1, "sendfile(%d, %d, %jd)", s, shmfd, size); printf("sent %jd bytes, requested %jd\n", (uintmax_t)sbytes, (uintmax_t)size); } static void receivefd(int s, int outfd, off_t size) { char buf[100]; off_t c, r, r1; for (c = 0; c < size; c += r) { r = read(s, buf, sizeof(buf)); if (r == -1) err(1, "read sock"); else if (r == 0) break; else { r1 = write(outfd, buf, r); if (r1 == -1) err(1, "write disk"); else if (r1 != r) { err(1, "short write %jd %jd", (uintmax_t)r, (uintmax_t)r1); } } } printf("received %jd bytes\n", (uintmax_t)c); } int main(int argc, char *argv[]) { struct stat st; struct sigaction sa; pid_t child; int s[2], infd, outfd, shmfd, error; if (argc != 3) { fprintf(stderr, "usage: sendfile_shm infile outfile\n"); exit(2); } memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_IGN; error = sigaction(SIGPIPE, &sa, NULL); if (error == -1) err(1, "sigaction SIGPIPE"); infd = open(argv[1], O_RDONLY); if (infd == -1) err(1, "open %s", argv[1]); error = fstat(infd, &st); if (error == -1) err(1, "stat"); outfd = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, 0666); if (outfd == -1) err(1, "open %s", argv[2]); shmfd = shm_open(SHM_ANON, O_CREAT | O_RDWR, 0600); if (shmfd == -1) err(1, "shm_open"); error = ftruncate(shmfd, st.st_size); if (error == -1) err(1, "ftruncate"); load(infd, shmfd, st.st_size); error = socketpair(AF_UNIX, SOCK_STREAM, 0, s); if (error == -1) err(1, "socketpair"); fflush(stdout); fflush(stderr); child = fork(); if (child == -1) err(1, "fork"); else if (child == 0) { close(s[1]); sendfd(s[0], shmfd, st.st_size); exit(0); } else { close(shmfd); close(s[0]); sleep(1); receivefd(s[1], outfd, st.st_size); } return (0); }