1295367Sdes/* $OpenBSD: atomicio.c,v 1.27 2015/01/16 06:40:12 deraadt Exp $ */
257429Smarkm/*
3162852Sdes * Copyright (c) 2006 Damien Miller. All rights reserved.
4149749Sdes * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved.
576259Sgreen * Copyright (c) 1995,1999 Theo de Raadt.  All rights reserved.
657429Smarkm * All rights reserved.
757429Smarkm *
857429Smarkm * Redistribution and use in source and binary forms, with or without
957429Smarkm * modification, are permitted provided that the following conditions
1057429Smarkm * are met:
1157429Smarkm * 1. Redistributions of source code must retain the above copyright
1257429Smarkm *    notice, this list of conditions and the following disclaimer.
1357429Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1457429Smarkm *    notice, this list of conditions and the following disclaimer in the
1557429Smarkm *    documentation and/or other materials provided with the distribution.
1657429Smarkm *
1757429Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1857429Smarkm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1957429Smarkm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2057429Smarkm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2157429Smarkm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2257429Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2357429Smarkm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2457429Smarkm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2557429Smarkm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2657429Smarkm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2757429Smarkm */
2857429Smarkm
2957429Smarkm#include "includes.h"
3057429Smarkm
31162852Sdes#include <sys/param.h>
32162852Sdes#include <sys/uio.h>
33162852Sdes
34162852Sdes#include <errno.h>
35181111Sdes#ifdef HAVE_POLL_H
36181111Sdes#include <poll.h>
37181111Sdes#else
38181111Sdes# ifdef HAVE_SYS_POLL_H
39181111Sdes#  include <sys/poll.h>
40181111Sdes# endif
41181111Sdes#endif
42162852Sdes#include <string.h>
43181111Sdes#include <unistd.h>
44295367Sdes#include <limits.h>
45162852Sdes
4676259Sgreen#include "atomicio.h"
4757429Smarkm
4857429Smarkm/*
49124208Sdes * ensure all of data on socket comes through. f==read || f==vwrite
5057429Smarkm */
51149749Sdessize_t
52221420Sdesatomicio6(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n,
53221420Sdes    int (*cb)(void *, size_t), void *cb_arg)
5457429Smarkm{
5558582Skris	char *s = _s;
56149749Sdes	size_t pos = 0;
57149749Sdes	ssize_t res;
58181111Sdes	struct pollfd pfd;
5957429Smarkm
60262566Sdes#ifndef BROKEN_READ_COMPARISON
61181111Sdes	pfd.fd = fd;
62181111Sdes	pfd.events = f == read ? POLLIN : POLLOUT;
63262566Sdes#endif
6457429Smarkm	while (n > pos) {
6557429Smarkm		res = (f) (fd, s + pos, n - pos);
6657429Smarkm		switch (res) {
6757429Smarkm		case -1:
68181111Sdes			if (errno == EINTR)
6957429Smarkm				continue;
70181111Sdes			if (errno == EAGAIN || errno == EWOULDBLOCK) {
71262566Sdes#ifndef BROKEN_READ_COMPARISON
72181111Sdes				(void)poll(&pfd, 1, -1);
73262566Sdes#endif
74181111Sdes				continue;
75181111Sdes			}
76149749Sdes			return 0;
7757429Smarkm		case 0:
78149749Sdes			errno = EPIPE;
79149749Sdes			return pos;
8057429Smarkm		default:
81162852Sdes			pos += (size_t)res;
82221420Sdes			if (cb != NULL && cb(cb_arg, (size_t)res) == -1) {
83221420Sdes				errno = EINTR;
84221420Sdes				return pos;
85221420Sdes			}
8657429Smarkm		}
8757429Smarkm	}
88221420Sdes	return pos;
8957429Smarkm}
90162852Sdes
91221420Sdessize_t
92221420Sdesatomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n)
93221420Sdes{
94221420Sdes	return atomicio6(f, fd, _s, n, NULL, NULL);
95221420Sdes}
96221420Sdes
97162852Sdes/*
98162852Sdes * ensure all of data on socket comes through. f==readv || f==writev
99162852Sdes */
100162852Sdessize_t
101221420Sdesatomiciov6(ssize_t (*f) (int, const struct iovec *, int), int fd,
102221420Sdes    const struct iovec *_iov, int iovcnt,
103221420Sdes    int (*cb)(void *, size_t), void *cb_arg)
104162852Sdes{
105162852Sdes	size_t pos = 0, rem;
106162852Sdes	ssize_t res;
107162852Sdes	struct iovec iov_array[IOV_MAX], *iov = iov_array;
108181111Sdes	struct pollfd pfd;
109162852Sdes
110162852Sdes	if (iovcnt > IOV_MAX) {
111162852Sdes		errno = EINVAL;
112162852Sdes		return 0;
113162852Sdes	}
114162852Sdes	/* Make a copy of the iov array because we may modify it below */
115162852Sdes	memcpy(iov, _iov, iovcnt * sizeof(*_iov));
116162852Sdes
117181111Sdes#ifndef BROKEN_READV_COMPARISON
118181111Sdes	pfd.fd = fd;
119181111Sdes	pfd.events = f == readv ? POLLIN : POLLOUT;
120181111Sdes#endif
121162852Sdes	for (; iovcnt > 0 && iov[0].iov_len > 0;) {
122162852Sdes		res = (f) (fd, iov, iovcnt);
123162852Sdes		switch (res) {
124162852Sdes		case -1:
125181111Sdes			if (errno == EINTR)
126162852Sdes				continue;
127181111Sdes			if (errno == EAGAIN || errno == EWOULDBLOCK) {
128181111Sdes#ifndef BROKEN_READV_COMPARISON
129181111Sdes				(void)poll(&pfd, 1, -1);
130181111Sdes#endif
131181111Sdes				continue;
132181111Sdes			}
133162852Sdes			return 0;
134162852Sdes		case 0:
135162852Sdes			errno = EPIPE;
136162852Sdes			return pos;
137162852Sdes		default:
138162852Sdes			rem = (size_t)res;
139162852Sdes			pos += rem;
140162852Sdes			/* skip completed iov entries */
141162852Sdes			while (iovcnt > 0 && rem >= iov[0].iov_len) {
142162852Sdes				rem -= iov[0].iov_len;
143162852Sdes				iov++;
144162852Sdes				iovcnt--;
145162852Sdes			}
146162852Sdes			/* This shouldn't happen... */
147162852Sdes			if (rem > 0 && (iovcnt <= 0 || rem > iov[0].iov_len)) {
148162852Sdes				errno = EFAULT;
149162852Sdes				return 0;
150162852Sdes			}
151162852Sdes			if (iovcnt == 0)
152162852Sdes				break;
153162852Sdes			/* update pointer in partially complete iov */
154162852Sdes			iov[0].iov_base = ((char *)iov[0].iov_base) + rem;
155162852Sdes			iov[0].iov_len -= rem;
156162852Sdes		}
157221420Sdes		if (cb != NULL && cb(cb_arg, (size_t)res) == -1) {
158221420Sdes			errno = EINTR;
159221420Sdes			return pos;
160221420Sdes		}
161162852Sdes	}
162162852Sdes	return pos;
163162852Sdes}
164221420Sdes
165221420Sdessize_t
166221420Sdesatomiciov(ssize_t (*f) (int, const struct iovec *, int), int fd,
167221420Sdes    const struct iovec *_iov, int iovcnt)
168221420Sdes{
169221420Sdes	return atomiciov6(f, fd, _iov, iovcnt, NULL, NULL);
170221420Sdes}
171