1*0772266eSXin LI /* $OpenBSD: atomicio.c,v 1.11 2012/12/04 02:24:47 deraadt Exp $ */
28c384020SXin LI /*
3c7c53f7aSXin LI * Copyright (c) 2006 Damien Miller. All rights reserved.
4c7c53f7aSXin LI * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved.
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 <errno.h>
30c7c53f7aSXin LI #include <poll.h>
318c384020SXin LI #include <unistd.h>
32c7c53f7aSXin LI
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
atomicio(ssize_t (* f)(int,void *,size_t),int fd,void * _s,size_t n)39c7c53f7aSXin LI atomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n)
408c384020SXin LI {
418c384020SXin LI char *s = _s;
427a997a69SXin LI size_t pos = 0;
437a997a69SXin LI ssize_t res;
44c7c53f7aSXin LI struct pollfd pfd;
458c384020SXin LI
46c7c53f7aSXin LI pfd.fd = fd;
47c7c53f7aSXin LI pfd.events = f == read ? POLLIN : POLLOUT;
488c384020SXin LI while (n > pos) {
498c384020SXin LI res = (f) (fd, s + pos, n - pos);
508c384020SXin LI switch (res) {
518c384020SXin LI case -1:
52c7c53f7aSXin LI if (errno == EINTR)
538c384020SXin LI continue;
5459c7ad52SXin LI if ((errno == EAGAIN) || (errno == ENOBUFS)) {
55c7c53f7aSXin LI (void)poll(&pfd, 1, -1);
56c7c53f7aSXin LI continue;
57c7c53f7aSXin LI }
587a997a69SXin LI return 0;
598c384020SXin LI case 0:
607a997a69SXin LI errno = EPIPE;
617a997a69SXin LI return pos;
628c384020SXin LI default:
637a997a69SXin LI pos += (size_t)res;
648c384020SXin LI }
658c384020SXin LI }
66c7c53f7aSXin LI return (pos);
678c384020SXin LI }
68