xref: /dragonfly/crypto/openssh/atomicio.c (revision ee116499)
1664f4763Szrj /* $OpenBSD: atomicio.c,v 1.30 2019/01/24 02:42:23 dtucker Exp $ */
218de8d7fSPeter Avalos /*
318de8d7fSPeter Avalos  * Copyright (c) 2006 Damien Miller. All rights reserved.
418de8d7fSPeter Avalos  * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved.
518de8d7fSPeter Avalos  * Copyright (c) 1995,1999 Theo de Raadt.  All rights reserved.
618de8d7fSPeter Avalos  * All rights reserved.
718de8d7fSPeter Avalos  *
818de8d7fSPeter Avalos  * Redistribution and use in source and binary forms, with or without
918de8d7fSPeter Avalos  * modification, are permitted provided that the following conditions
1018de8d7fSPeter Avalos  * are met:
1118de8d7fSPeter Avalos  * 1. Redistributions of source code must retain the above copyright
1218de8d7fSPeter Avalos  *    notice, this list of conditions and the following disclaimer.
1318de8d7fSPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
1418de8d7fSPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
1518de8d7fSPeter Avalos  *    documentation and/or other materials provided with the distribution.
1618de8d7fSPeter Avalos  *
1718de8d7fSPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1818de8d7fSPeter Avalos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1918de8d7fSPeter Avalos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2018de8d7fSPeter Avalos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2118de8d7fSPeter Avalos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2218de8d7fSPeter Avalos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2318de8d7fSPeter Avalos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2418de8d7fSPeter Avalos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2518de8d7fSPeter Avalos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2618de8d7fSPeter Avalos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2718de8d7fSPeter Avalos  */
2818de8d7fSPeter Avalos 
2918de8d7fSPeter Avalos #include "includes.h"
3018de8d7fSPeter Avalos 
3118de8d7fSPeter Avalos #include <sys/uio.h>
3218de8d7fSPeter Avalos 
3318de8d7fSPeter Avalos #include <errno.h>
3418de8d7fSPeter Avalos #ifdef HAVE_POLL_H
3518de8d7fSPeter Avalos #include <poll.h>
3618de8d7fSPeter Avalos #else
3718de8d7fSPeter Avalos # ifdef HAVE_SYS_POLL_H
3818de8d7fSPeter Avalos #  include <sys/poll.h>
3918de8d7fSPeter Avalos # endif
4018de8d7fSPeter Avalos #endif
4118de8d7fSPeter Avalos #include <string.h>
4218de8d7fSPeter Avalos #include <unistd.h>
43e9778795SPeter Avalos #include <limits.h>
4418de8d7fSPeter Avalos 
4518de8d7fSPeter Avalos #include "atomicio.h"
4618de8d7fSPeter Avalos 
4718de8d7fSPeter Avalos /*
4818de8d7fSPeter Avalos  * ensure all of data on socket comes through. f==read || f==vwrite
4918de8d7fSPeter Avalos  */
5018de8d7fSPeter Avalos size_t
atomicio6(ssize_t (* f)(int,void *,size_t),int fd,void * _s,size_t n,int (* cb)(void *,size_t),void * cb_arg)519f304aafSPeter Avalos atomicio6(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n,
529f304aafSPeter Avalos     int (*cb)(void *, size_t), void *cb_arg)
5318de8d7fSPeter Avalos {
5418de8d7fSPeter Avalos 	char *s = _s;
5518de8d7fSPeter Avalos 	size_t pos = 0;
5618de8d7fSPeter Avalos 	ssize_t res;
5718de8d7fSPeter Avalos 	struct pollfd pfd;
5818de8d7fSPeter Avalos 
5918de8d7fSPeter Avalos 	pfd.fd = fd;
60664f4763Szrj #ifndef BROKEN_READ_COMPARISON
6118de8d7fSPeter Avalos 	pfd.events = f == read ? POLLIN : POLLOUT;
62664f4763Szrj #else
63664f4763Szrj 	pfd.events = POLLIN|POLLOUT;
6436e94dc5SPeter Avalos #endif
6518de8d7fSPeter Avalos 	while (n > pos) {
6618de8d7fSPeter Avalos 		res = (f) (fd, s + pos, n - pos);
6718de8d7fSPeter Avalos 		switch (res) {
6818de8d7fSPeter Avalos 		case -1:
69664f4763Szrj 			if (errno == EINTR) {
70664f4763Szrj 				/* possible SIGALARM, update callback */
71664f4763Szrj 				if (cb != NULL && cb(cb_arg, 0) == -1) {
72664f4763Szrj 					errno = EINTR;
73664f4763Szrj 					return pos;
74664f4763Szrj 				}
7518de8d7fSPeter Avalos 				continue;
76664f4763Szrj 			} else if (errno == EAGAIN || errno == EWOULDBLOCK) {
7718de8d7fSPeter Avalos 				(void)poll(&pfd, 1, -1);
7818de8d7fSPeter Avalos 				continue;
7918de8d7fSPeter Avalos 			}
8018de8d7fSPeter Avalos 			return 0;
8118de8d7fSPeter Avalos 		case 0:
8218de8d7fSPeter Avalos 			errno = EPIPE;
8318de8d7fSPeter Avalos 			return pos;
8418de8d7fSPeter Avalos 		default:
8518de8d7fSPeter Avalos 			pos += (size_t)res;
869f304aafSPeter Avalos 			if (cb != NULL && cb(cb_arg, (size_t)res) == -1) {
879f304aafSPeter Avalos 				errno = EINTR;
889f304aafSPeter Avalos 				return pos;
8918de8d7fSPeter Avalos 			}
9018de8d7fSPeter Avalos 		}
919f304aafSPeter Avalos 	}
929f304aafSPeter Avalos 	return pos;
939f304aafSPeter Avalos }
949f304aafSPeter Avalos 
959f304aafSPeter Avalos size_t
atomicio(ssize_t (* f)(int,void *,size_t),int fd,void * _s,size_t n)969f304aafSPeter Avalos atomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n)
979f304aafSPeter Avalos {
989f304aafSPeter Avalos 	return atomicio6(f, fd, _s, n, NULL, NULL);
9918de8d7fSPeter Avalos }
10018de8d7fSPeter Avalos 
10118de8d7fSPeter Avalos /*
10218de8d7fSPeter Avalos  * ensure all of data on socket comes through. f==readv || f==writev
10318de8d7fSPeter Avalos  */
10418de8d7fSPeter Avalos size_t
atomiciov6(ssize_t (* f)(int,const struct iovec *,int),int fd,const struct iovec * _iov,int iovcnt,int (* cb)(void *,size_t),void * cb_arg)1059f304aafSPeter Avalos atomiciov6(ssize_t (*f) (int, const struct iovec *, int), int fd,
1069f304aafSPeter Avalos     const struct iovec *_iov, int iovcnt,
1079f304aafSPeter Avalos     int (*cb)(void *, size_t), void *cb_arg)
10818de8d7fSPeter Avalos {
10918de8d7fSPeter Avalos 	size_t pos = 0, rem;
11018de8d7fSPeter Avalos 	ssize_t res;
11118de8d7fSPeter Avalos 	struct iovec iov_array[IOV_MAX], *iov = iov_array;
11218de8d7fSPeter Avalos 	struct pollfd pfd;
11318de8d7fSPeter Avalos 
114ce74bacaSMatthew Dillon 	if (iovcnt < 0 || iovcnt > IOV_MAX) {
11518de8d7fSPeter Avalos 		errno = EINVAL;
11618de8d7fSPeter Avalos 		return 0;
11718de8d7fSPeter Avalos 	}
11818de8d7fSPeter Avalos 	/* Make a copy of the iov array because we may modify it below */
119ce74bacaSMatthew Dillon 	memcpy(iov, _iov, (size_t)iovcnt * sizeof(*_iov));
12018de8d7fSPeter Avalos 
12118de8d7fSPeter Avalos 	pfd.fd = fd;
122664f4763Szrj #ifndef BROKEN_READV_COMPARISON
12318de8d7fSPeter Avalos 	pfd.events = f == readv ? POLLIN : POLLOUT;
124664f4763Szrj #else
125664f4763Szrj 	pfd.events = POLLIN|POLLOUT;
12618de8d7fSPeter Avalos #endif
12718de8d7fSPeter Avalos 	for (; iovcnt > 0 && iov[0].iov_len > 0;) {
12818de8d7fSPeter Avalos 		res = (f) (fd, iov, iovcnt);
12918de8d7fSPeter Avalos 		switch (res) {
13018de8d7fSPeter Avalos 		case -1:
131664f4763Szrj 			if (errno == EINTR) {
132664f4763Szrj 				/* possible SIGALARM, update callback */
133664f4763Szrj 				if (cb != NULL && cb(cb_arg, 0) == -1) {
134664f4763Szrj 					errno = EINTR;
135664f4763Szrj 					return pos;
136664f4763Szrj 				}
13718de8d7fSPeter Avalos 				continue;
138664f4763Szrj 			} else if (errno == EAGAIN || errno == EWOULDBLOCK) {
13918de8d7fSPeter Avalos 				(void)poll(&pfd, 1, -1);
14018de8d7fSPeter Avalos 				continue;
14118de8d7fSPeter Avalos 			}
14218de8d7fSPeter Avalos 			return 0;
14318de8d7fSPeter Avalos 		case 0:
14418de8d7fSPeter Avalos 			errno = EPIPE;
14518de8d7fSPeter Avalos 			return pos;
14618de8d7fSPeter Avalos 		default:
14718de8d7fSPeter Avalos 			rem = (size_t)res;
14818de8d7fSPeter Avalos 			pos += rem;
14918de8d7fSPeter Avalos 			/* skip completed iov entries */
15018de8d7fSPeter Avalos 			while (iovcnt > 0 && rem >= iov[0].iov_len) {
15118de8d7fSPeter Avalos 				rem -= iov[0].iov_len;
15218de8d7fSPeter Avalos 				iov++;
15318de8d7fSPeter Avalos 				iovcnt--;
15418de8d7fSPeter Avalos 			}
15518de8d7fSPeter Avalos 			/* This shouldn't happen... */
15618de8d7fSPeter Avalos 			if (rem > 0 && (iovcnt <= 0 || rem > iov[0].iov_len)) {
15718de8d7fSPeter Avalos 				errno = EFAULT;
15818de8d7fSPeter Avalos 				return 0;
15918de8d7fSPeter Avalos 			}
16018de8d7fSPeter Avalos 			if (iovcnt == 0)
16118de8d7fSPeter Avalos 				break;
16218de8d7fSPeter Avalos 			/* update pointer in partially complete iov */
16318de8d7fSPeter Avalos 			iov[0].iov_base = ((char *)iov[0].iov_base) + rem;
16418de8d7fSPeter Avalos 			iov[0].iov_len -= rem;
16518de8d7fSPeter Avalos 		}
1669f304aafSPeter Avalos 		if (cb != NULL && cb(cb_arg, (size_t)res) == -1) {
1679f304aafSPeter Avalos 			errno = EINTR;
1689f304aafSPeter Avalos 			return pos;
1699f304aafSPeter Avalos 		}
17018de8d7fSPeter Avalos 	}
17118de8d7fSPeter Avalos 	return pos;
17218de8d7fSPeter Avalos }
1739f304aafSPeter Avalos 
1749f304aafSPeter Avalos size_t
atomiciov(ssize_t (* f)(int,const struct iovec *,int),int fd,const struct iovec * _iov,int iovcnt)1759f304aafSPeter Avalos atomiciov(ssize_t (*f) (int, const struct iovec *, int), int fd,
1769f304aafSPeter Avalos     const struct iovec *_iov, int iovcnt)
1779f304aafSPeter Avalos {
1789f304aafSPeter Avalos 	return atomiciov6(f, fd, _iov, iovcnt, NULL, NULL);
1799f304aafSPeter Avalos }
180