1ac264ff5Schristos /* $OpenBSD: atomicio.c,v 1.11 2012/12/04 02:24:47 deraadt Exp $ */
2ac264ff5Schristos /*
3ac264ff5Schristos * Copyright (c) 2006 Damien Miller. All rights reserved.
4ac264ff5Schristos * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved.
5ac264ff5Schristos * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
6ac264ff5Schristos * All rights reserved.
7ac264ff5Schristos *
8ac264ff5Schristos * Redistribution and use in source and binary forms, with or without
9ac264ff5Schristos * modification, are permitted provided that the following conditions
10ac264ff5Schristos * are met:
11ac264ff5Schristos * 1. Redistributions of source code must retain the above copyright
12ac264ff5Schristos * notice, this list of conditions and the following disclaimer.
13ac264ff5Schristos * 2. Redistributions in binary form must reproduce the above copyright
14ac264ff5Schristos * notice, this list of conditions and the following disclaimer in the
15ac264ff5Schristos * documentation and/or other materials provided with the distribution.
16ac264ff5Schristos *
17ac264ff5Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18ac264ff5Schristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19ac264ff5Schristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20ac264ff5Schristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21ac264ff5Schristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22ac264ff5Schristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23ac264ff5Schristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24ac264ff5Schristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25ac264ff5Schristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26ac264ff5Schristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27ac264ff5Schristos */
28*d71fee0fSchristos #include <sys/cdefs.h>
29*d71fee0fSchristos __RCSID("$NetBSD: atomicio.c,v 1.2 2017/02/06 16:03:40 christos Exp $");
30ac264ff5Schristos
31ac264ff5Schristos #include <errno.h>
32ac264ff5Schristos #include <poll.h>
33ac264ff5Schristos #include <unistd.h>
34ac264ff5Schristos
35ac264ff5Schristos #include "atomicio.h"
36ac264ff5Schristos
37ac264ff5Schristos /*
38ac264ff5Schristos * ensure all of data on socket comes through. f==read || f==vwrite
39ac264ff5Schristos */
40ac264ff5Schristos size_t
atomicio(ssize_t (* f)(int,void *,size_t),int fd,void * _s,size_t n)41ac264ff5Schristos atomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n)
42ac264ff5Schristos {
43ac264ff5Schristos char *s = _s;
44ac264ff5Schristos size_t pos = 0;
45ac264ff5Schristos ssize_t res;
46ac264ff5Schristos struct pollfd pfd;
47ac264ff5Schristos
48ac264ff5Schristos pfd.fd = fd;
49ac264ff5Schristos pfd.events = f == read ? POLLIN : POLLOUT;
50ac264ff5Schristos while (n > pos) {
51ac264ff5Schristos res = (f) (fd, s + pos, n - pos);
52ac264ff5Schristos switch (res) {
53ac264ff5Schristos case -1:
54ac264ff5Schristos if (errno == EINTR)
55ac264ff5Schristos continue;
56ac264ff5Schristos if ((errno == EAGAIN) || (errno == ENOBUFS)) {
57ac264ff5Schristos (void)poll(&pfd, 1, -1);
58ac264ff5Schristos continue;
59ac264ff5Schristos }
60ac264ff5Schristos return 0;
61ac264ff5Schristos case 0:
62ac264ff5Schristos errno = EPIPE;
63ac264ff5Schristos return pos;
64ac264ff5Schristos default:
65ac264ff5Schristos pos += (size_t)res;
66ac264ff5Schristos }
67ac264ff5Schristos }
68ac264ff5Schristos return (pos);
69ac264ff5Schristos }
70