17a997a69SXin LI /* $OpenBSD: atomicio.c,v 1.8 2006/02/11 19:31:18 otto Exp $ */ 27a997a69SXin LI 38c384020SXin LI /* 47a997a69SXin LI * Copyright (c) 2005 Anil Madhavapeddy. All rights served. 58c384020SXin LI * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. 68c384020SXin LI * All rights reserved. 78c384020SXin LI * 88c384020SXin LI * Redistribution and use in source and binary forms, with or without 98c384020SXin LI * modification, are permitted provided that the following conditions 108c384020SXin LI * are met: 118c384020SXin LI * 1. Redistributions of source code must retain the above copyright 128c384020SXin LI * notice, this list of conditions and the following disclaimer. 138c384020SXin LI * 2. Redistributions in binary form must reproduce the above copyright 148c384020SXin LI * notice, this list of conditions and the following disclaimer in the 158c384020SXin LI * documentation and/or other materials provided with the distribution. 168c384020SXin LI * 178c384020SXin LI * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 188c384020SXin LI * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 198c384020SXin LI * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 208c384020SXin LI * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 218c384020SXin LI * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 228c384020SXin LI * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 238c384020SXin LI * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 248c384020SXin LI * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 258c384020SXin LI * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 268c384020SXin LI * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 278c384020SXin LI */ 288c384020SXin LI 298c384020SXin LI #include <sys/types.h> 308c384020SXin LI #include <sys/uio.h> 318c384020SXin LI #include <errno.h> 328c384020SXin LI #include <unistd.h> 337a997a69SXin LI #include "atomicio.h" 348c384020SXin LI 358c384020SXin LI /* 367a997a69SXin LI * ensure all of data on socket comes through. f==read || f==vwrite 378c384020SXin LI */ 387a997a69SXin LI size_t 397a997a69SXin LI atomicio(f, fd, _s, n) 407a997a69SXin LI ssize_t (*f) (int, void *, size_t); 417a997a69SXin LI int fd; 427a997a69SXin LI void *_s; 437a997a69SXin LI size_t n; 448c384020SXin LI { 458c384020SXin LI char *s = _s; 467a997a69SXin LI size_t pos = 0; 477a997a69SXin LI ssize_t res; 488c384020SXin LI 498c384020SXin LI while (n > pos) { 508c384020SXin LI res = (f) (fd, s + pos, n - pos); 518c384020SXin LI switch (res) { 528c384020SXin LI case -1: 538c384020SXin LI if (errno == EINTR || errno == EAGAIN) 548c384020SXin LI continue; 557a997a69SXin LI return 0; 568c384020SXin LI case 0: 577a997a69SXin LI errno = EPIPE; 587a997a69SXin LI return pos; 598c384020SXin LI default: 607a997a69SXin LI pos += (size_t)res; 618c384020SXin LI } 628c384020SXin LI } 637a997a69SXin LI return pos; 648c384020SXin LI } 65