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