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
59262566Sdes#ifndef BROKEN_READ_COMPARISON
60181111Sdes	pfd.fd = fd;
61181111Sdes	pfd.events = f == read ? POLLIN : POLLOUT;
62262566Sdes#endif
6357429Smarkm	while (n > pos) {
6457429Smarkm		res = (f) (fd, s + pos, n - pos);
6557429Smarkm		switch (res) {
6657429Smarkm		case -1:
67181111Sdes			if (errno == EINTR)
6857429Smarkm				continue;
69181111Sdes			if (errno == EAGAIN || errno == EWOULDBLOCK) {
70262566Sdes#ifndef BROKEN_READ_COMPARISON
71181111Sdes				(void)poll(&pfd, 1, -1);
72262566Sdes#endif
73181111Sdes				continue;
74181111Sdes			}
75149749Sdes			return 0;
7657429Smarkm		case 0:
77149749Sdes			errno = EPIPE;
78149749Sdes			return pos;
7957429Smarkm		default:
80162852Sdes			pos += (size_t)res;
81221420Sdes			if (cb != NULL && cb(cb_arg, (size_t)res) == -1) {
82221420Sdes				errno = EINTR;
83221420Sdes				return pos;
84221420Sdes			}
8557429Smarkm		}
8657429Smarkm	}
87221420Sdes	return pos;
8857429Smarkm}
89162852Sdes
90221420Sdessize_t
91221420Sdesatomicio(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n)
92221420Sdes{
93221420Sdes	return atomicio6(f, fd, _s, n, NULL, NULL);
94221420Sdes}
95221420Sdes
96162852Sdes/*
97162852Sdes * ensure all of data on socket comes through. f==readv || f==writev
98162852Sdes */
99162852Sdessize_t
100221420Sdesatomiciov6(ssize_t (*f) (int, const struct iovec *, int), int fd,
101221420Sdes    const struct iovec *_iov, int iovcnt,
102221420Sdes    int (*cb)(void *, size_t), void *cb_arg)
103162852Sdes{
104162852Sdes	size_t pos = 0, rem;
105162852Sdes	ssize_t res;
106162852Sdes	struct iovec iov_array[IOV_MAX], *iov = iov_array;
107181111Sdes	struct pollfd pfd;
108162852Sdes
109162852Sdes	if (iovcnt > IOV_MAX) {
110162852Sdes		errno = EINVAL;
111162852Sdes		return 0;
112162852Sdes	}
113162852Sdes	/* Make a copy of the iov array because we may modify it below */
114162852Sdes	memcpy(iov, _iov, iovcnt * sizeof(*_iov));
115162852Sdes
116181111Sdes#ifndef BROKEN_READV_COMPARISON
117181111Sdes	pfd.fd = fd;
118181111Sdes	pfd.events = f == readv ? POLLIN : POLLOUT;
119181111Sdes#endif
120162852Sdes	for (; iovcnt > 0 && iov[0].iov_len > 0;) {
121162852Sdes		res = (f) (fd, iov, iovcnt);
122162852Sdes		switch (res) {
123162852Sdes		case -1:
124181111Sdes			if (errno == EINTR)
125162852Sdes				continue;
126181111Sdes			if (errno == EAGAIN || errno == EWOULDBLOCK) {
127181111Sdes#ifndef BROKEN_READV_COMPARISON
128181111Sdes				(void)poll(&pfd, 1, -1);
129181111Sdes#endif
130181111Sdes				continue;
131181111Sdes			}
132162852Sdes			return 0;
133162852Sdes		case 0:
134162852Sdes			errno = EPIPE;
135162852Sdes			return pos;
136162852Sdes		default:
137162852Sdes			rem = (size_t)res;
138162852Sdes			pos += rem;
139162852Sdes			/* skip completed iov entries */
140162852Sdes			while (iovcnt > 0 && rem >= iov[0].iov_len) {
141162852Sdes				rem -= iov[0].iov_len;
142162852Sdes				iov++;
143162852Sdes				iovcnt--;
144162852Sdes			}
145162852Sdes			/* This shouldn't happen... */
146162852Sdes			if (rem > 0 && (iovcnt <= 0 || rem > iov[0].iov_len)) {
147162852Sdes				errno = EFAULT;
148162852Sdes				return 0;
149162852Sdes			}
150162852Sdes			if (iovcnt == 0)
151162852Sdes				break;
152162852Sdes			/* update pointer in partially complete iov */
153162852Sdes			iov[0].iov_base = ((char *)iov[0].iov_base) + rem;
154162852Sdes			iov[0].iov_len -= rem;
155162852Sdes		}
156221420Sdes		if (cb != NULL && cb(cb_arg, (size_t)res) == -1) {
157221420Sdes			errno = EINTR;
158221420Sdes			return pos;
159221420Sdes		}
160162852Sdes	}
161162852Sdes	return pos;
162162852Sdes}
163221420Sdes
164221420Sdessize_t
165221420Sdesatomiciov(ssize_t (*f) (int, const struct iovec *, int), int fd,
166221420Sdes    const struct iovec *_iov, int iovcnt)
167221420Sdes{
168221420Sdes	return atomiciov6(f, fd, _iov, iovcnt, NULL, NULL);
169221420Sdes}
170