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