sockstream.c revision 302408
14Srgrimes/*- 24Srgrimes * Copyright (c) 2015 Nahanni Systems, Inc. 34Srgrimes * All rights reserved. 44Srgrimes * 54Srgrimes * Redistribution and use in source and binary forms, with or without 64Srgrimes * modification, are permitted provided that the following conditions 74Srgrimes * are met: 84Srgrimes * 1. Redistributions of source code must retain the above copyright 94Srgrimes * notice, this list of conditions and the following disclaimer. 104Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 114Srgrimes * notice, this list of conditions and the following disclaimer in the 124Srgrimes * documentation and/or other materials provided with the distribution. 134Srgrimes * 144Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 154Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 164Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 174Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 184Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 194Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 204Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 214Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 224Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 234Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 244Srgrimes * SUCH DAMAGE. 254Srgrimes * 264Srgrimes * $FreeBSD: stable/11/usr.sbin/bhyve/sockstream.c 302408 2016-07-08 00:04:57Z gjb $ 274Srgrimes */ 284Srgrimes 294Srgrimes#include <sys/cdefs.h> 304Srgrimes__FBSDID("$FreeBSD: stable/11/usr.sbin/bhyve/sockstream.c 302408 2016-07-08 00:04:57Z gjb $"); 314Srgrimes 324Srgrimes#include <sys/types.h> 334Srgrimes#include <unistd.h> 344Srgrimes 354Srgrimes#include <errno.h> 36620Srgrimes 3712848Sbde#include "sockstream.h" 384Srgrimes 394Srgrimesssize_t 404Srgrimesstream_read(int fd, void *buf, ssize_t nbytes) 414Srgrimes{ 424Srgrimes uint8_t *p; 438876Srgrimes ssize_t len = 0; 444Srgrimes ssize_t n; 454Srgrimes 464Srgrimes p = buf; 474Srgrimes 482056Swollman while (len < nbytes) { 492056Swollman n = read(fd, p + len, nbytes - len); 502056Swollman if (n == 0) 512056Swollman break; 522056Swollman 532056Swollman if (n < 0) { 542056Swollman if (errno == EINTR || errno == EAGAIN) 552056Swollman continue; 5612604Sbde return (n); 5712649Speter } 584Srgrimes len += n; 5910665Sbde } 607090Sbde return (len); 612056Swollman} 6212791Sgibbs 634Srgrimesssize_t 6412604Sbdestream_write(int fd, const void *buf, ssize_t nbytes) 6512604Sbde{ 6612604Sbde const uint8_t *p; 6712604Sbde ssize_t len = 0; 6810665Sbde ssize_t n; 6912604Sbde 7012604Sbde p = buf; 7112604Sbde 7212604Sbde while (len < nbytes) { 7310665Sbde n = write(fd, p + len, nbytes - len); 7412604Sbde if (n == 0) 7512604Sbde break; 7612604Sbde if (n < 0) { 7712604Sbde if (errno == EINTR || errno == EAGAIN) 78798Swollman continue; 7912604Sbde return (n); 8012604Sbde } 8112604Sbde len += n; 8212604Sbde } 834Srgrimes return (len); 8412604Sbde} 8512604Sbde 8612604Sbde 8712604Sbde