1221420Sdes/* $OpenBSD: atomicio.c,v 1.26 2010/09/22 22:58:51 djm 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>
44162852Sdes
4576259Sgreen#include "atomicio.h"
4657429Smarkm
4757429Smarkm/*
48124208Sdes * ensure all of data on socket comes through. f==read || f==vwrite
4957429Smarkm */
50149749Sdessize_t
51221420Sdesatomicio6(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n,
52221420Sdes    int (*cb)(void *, size_t), void *cb_arg)
5357429Smarkm{
5458582Skris	char *s = _s;
55149749Sdes	size_t pos = 0;
56149749Sdes	ssize_t res;
57181111Sdes	struct pollfd pfd;
5857429Smarkm
59181111Sdes	pfd.fd = fd;
60181111Sdes	pfd.events = f == read ? POLLIN : POLLOUT;
6157429Smarkm	while (n > pos) {
6257429Smarkm		res = (f) (fd, s + pos, n - pos);
6357429Smarkm		switch (res) {
6457429Smarkm		case -1:
65181111Sdes			if (errno == EINTR)
6657429Smarkm				continue;
67181111Sdes			if (errno == EAGAIN || errno == EWOULDBLOCK) {
68181111Sdes				(void)poll(&pfd, 1, -1);
69181111Sdes				continue;
70181111Sdes			}
71149749Sdes			return 0;
7257429Smarkm		case 0:
73149749Sdes			errno = EPIPE;
74149749Sdes			return pos;
7557429Smarkm		default:
76162852Sdes			pos += (size_t)res;
77221420Sdes			if (cb != NULL && cb(cb_arg, (size_t)res) == -1) {
78221420Sdes				errno = EINTR;
79221420Sdes				return pos;
80221420Sdes			}
8157429Smarkm		}
8257429Smarkm	}
83221420Sdes	return pos;
8457429Smarkm}
85162852Sdes
86221420Sdessize_t
87221420Sdesatomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n)
88221420Sdes{
89221420Sdes	return atomicio6(f, fd, _s, n, NULL, NULL);
90221420Sdes}
91221420Sdes
92162852Sdes/*
93162852Sdes * ensure all of data on socket comes through. f==readv || f==writev
94162852Sdes */
95162852Sdessize_t
96221420Sdesatomiciov6(ssize_t (*f) (int, const struct iovec *, int), int fd,
97221420Sdes    const struct iovec *_iov, int iovcnt,
98221420Sdes    int (*cb)(void *, size_t), void *cb_arg)
99162852Sdes{
100162852Sdes	size_t pos = 0, rem;
101162852Sdes	ssize_t res;
102162852Sdes	struct iovec iov_array[IOV_MAX], *iov = iov_array;
103181111Sdes	struct pollfd pfd;
104162852Sdes
105162852Sdes	if (iovcnt > IOV_MAX) {
106162852Sdes		errno = EINVAL;
107162852Sdes		return 0;
108162852Sdes	}
109162852Sdes	/* Make a copy of the iov array because we may modify it below */
110162852Sdes	memcpy(iov, _iov, iovcnt * sizeof(*_iov));
111162852Sdes
112181111Sdes#ifndef BROKEN_READV_COMPARISON
113181111Sdes	pfd.fd = fd;
114181111Sdes	pfd.events = f == readv ? POLLIN : POLLOUT;
115181111Sdes#endif
116162852Sdes	for (; iovcnt > 0 && iov[0].iov_len > 0;) {
117162852Sdes		res = (f) (fd, iov, iovcnt);
118162852Sdes		switch (res) {
119162852Sdes		case -1:
120181111Sdes			if (errno == EINTR)
121162852Sdes				continue;
122181111Sdes			if (errno == EAGAIN || errno == EWOULDBLOCK) {
123181111Sdes#ifndef BROKEN_READV_COMPARISON
124181111Sdes				(void)poll(&pfd, 1, -1);
125181111Sdes#endif
126181111Sdes				continue;
127181111Sdes			}
128162852Sdes			return 0;
129162852Sdes		case 0:
130162852Sdes			errno = EPIPE;
131162852Sdes			return pos;
132162852Sdes		default:
133162852Sdes			rem = (size_t)res;
134162852Sdes			pos += rem;
135162852Sdes			/* skip completed iov entries */
136162852Sdes			while (iovcnt > 0 && rem >= iov[0].iov_len) {
137162852Sdes				rem -= iov[0].iov_len;
138162852Sdes				iov++;
139162852Sdes				iovcnt--;
140162852Sdes			}
141162852Sdes			/* This shouldn't happen... */
142162852Sdes			if (rem > 0 && (iovcnt <= 0 || rem > iov[0].iov_len)) {
143162852Sdes				errno = EFAULT;
144162852Sdes				return 0;
145162852Sdes			}
146162852Sdes			if (iovcnt == 0)
147162852Sdes				break;
148162852Sdes			/* update pointer in partially complete iov */
149162852Sdes			iov[0].iov_base = ((char *)iov[0].iov_base) + rem;
150162852Sdes			iov[0].iov_len -= rem;
151162852Sdes		}
152221420Sdes		if (cb != NULL && cb(cb_arg, (size_t)res) == -1) {
153221420Sdes			errno = EINTR;
154221420Sdes			return pos;
155221420Sdes		}
156162852Sdes	}
157162852Sdes	return pos;
158162852Sdes}
159221420Sdes
160221420Sdessize_t
161221420Sdesatomiciov(ssize_t (*f) (int, const struct iovec *, int), int fd,
162221420Sdes    const struct iovec *_iov, int iovcnt)
163221420Sdes{
164221420Sdes	return atomiciov6(f, fd, _iov, iovcnt, NULL, NULL);
165221420Sdes}
166