xref: /netbsd-src/crypto/external/bsd/openssh/dist/atomicio.c (revision a24efa7dea9f1f56c3bdb15a927d3516792ace1c)
1 /*	$NetBSD: atomicio.c,v 1.6 2015/04/03 23:58:19 christos Exp $	*/
2 /* $OpenBSD: atomicio.c,v 1.27 2015/01/16 06:40:12 deraadt Exp $ */
3 /*
4  * Copyright (c) 2006 Damien Miller. All rights reserved.
5  * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved.
6  * Copyright (c) 1995,1999 Theo de Raadt.  All rights reserved.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "includes.h"
31 __RCSID("$NetBSD: atomicio.c,v 1.6 2015/04/03 23:58:19 christos Exp $");
32 #include <sys/param.h>
33 #include <sys/uio.h>
34 
35 #include <errno.h>
36 #include <poll.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <limits.h>
40 
41 #include "atomicio.h"
42 
43 /*
44  * ensure all of data on socket comes through. f==read || f==vwrite
45  */
46 size_t
47 atomicio6(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n,
48     int (*cb)(void *, size_t), void *cb_arg)
49 {
50 	char *s = _s;
51 	size_t pos = 0;
52 	ssize_t res;
53 	struct pollfd pfd;
54 
55 	pfd.fd = fd;
56 	/*
57 	 * check for vwrite instead of read to avoid read being renamed
58 	 * by SSP issues
59 	 */
60 	pfd.events = f == vwrite ? POLLOUT : POLLIN;
61 	while (n > pos) {
62 		res = (f) (fd, s + pos, n - pos);
63 		switch (res) {
64 		case -1:
65 			if (errno == EINTR)
66 				continue;
67 			if (errno == EAGAIN) {
68 				(void)poll(&pfd, 1, -1);
69 				continue;
70 			}
71 			return 0;
72 		case 0:
73 			errno = EPIPE;
74 			return pos;
75 		default:
76 			pos += (size_t)res;
77 			if (cb != NULL && cb(cb_arg, (size_t)res) == -1) {
78 				errno = EINTR;
79 				return pos;
80 			}
81 		}
82 	}
83 	return pos;
84 }
85 
86 size_t
87 atomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n)
88 {
89 	return atomicio6(f, fd, _s, n, NULL, NULL);
90 }
91 
92 /*
93  * ensure all of data on socket comes through. f==readv || f==writev
94  */
95 size_t
96 atomiciov6(ssize_t (*f) (int, const struct iovec *, int), int fd,
97     const struct iovec *_iov, int iovcnt,
98     int (*cb)(void *, size_t), void *cb_arg)
99 {
100 	size_t pos = 0, rem;
101 	ssize_t res;
102 	struct iovec iov_array[IOV_MAX], *iov = iov_array;
103 	struct pollfd pfd;
104 
105 	if (iovcnt > IOV_MAX) {
106 		errno = EINVAL;
107 		return 0;
108 	}
109 	/* Make a copy of the iov array because we may modify it below */
110 	memcpy(iov, _iov, iovcnt * sizeof(*_iov));
111 
112 	pfd.fd = fd;
113 	pfd.events = f == readv ? POLLIN : POLLOUT;
114 	for (; iovcnt > 0 && iov[0].iov_len > 0;) {
115 		res = (f) (fd, iov, iovcnt);
116 		switch (res) {
117 		case -1:
118 			if (errno == EINTR)
119 				continue;
120 			if (errno == EAGAIN) {
121 				(void)poll(&pfd, 1, -1);
122 				continue;
123 			}
124 			return 0;
125 		case 0:
126 			errno = EPIPE;
127 			return pos;
128 		default:
129 			rem = (size_t)res;
130 			pos += rem;
131 			/* skip completed iov entries */
132 			while (iovcnt > 0 && rem >= iov[0].iov_len) {
133 				rem -= iov[0].iov_len;
134 				iov++;
135 				iovcnt--;
136 			}
137 			/* This shouldn't happen... */
138 			if (rem > 0 && (iovcnt <= 0 || rem > iov[0].iov_len)) {
139 				errno = EFAULT;
140 				return 0;
141 			}
142 			if (iovcnt == 0)
143 				break;
144 			/* update pointer in partially complete iov */
145 			iov[0].iov_base = ((char *)iov[0].iov_base) + rem;
146 			iov[0].iov_len -= rem;
147 		}
148 		if (cb != NULL && cb(cb_arg, (size_t)res) == -1) {
149 			errno = EINTR;
150 			return pos;
151 		}
152 	}
153 	return pos;
154 }
155 
156 size_t
157 atomiciov(ssize_t (*f) (int, const struct iovec *, int), int fd,
158     const struct iovec *_iov, int iovcnt)
159 {
160 	return atomiciov6(f, fd, _iov, iovcnt, NULL, NULL);
161 }
162