xref: /freebsd/crypto/openssh/atomicio.c (revision d95e11bf7e5a59b5c3f81bd8dfc2918ee7d3bada)
1511b41d2SMark Murray /*
21e8db6e2SBrian Feldman  * Copyright (c) 1995,1999 Theo de Raadt.  All rights reserved.
3511b41d2SMark Murray  * All rights reserved.
4511b41d2SMark Murray  *
5511b41d2SMark Murray  * Redistribution and use in source and binary forms, with or without
6511b41d2SMark Murray  * modification, are permitted provided that the following conditions
7511b41d2SMark Murray  * are met:
8511b41d2SMark Murray  * 1. Redistributions of source code must retain the above copyright
9511b41d2SMark Murray  *    notice, this list of conditions and the following disclaimer.
10511b41d2SMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
11511b41d2SMark Murray  *    notice, this list of conditions and the following disclaimer in the
12511b41d2SMark Murray  *    documentation and/or other materials provided with the distribution.
13511b41d2SMark Murray  *
14511b41d2SMark Murray  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15511b41d2SMark Murray  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16511b41d2SMark Murray  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17511b41d2SMark Murray  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18511b41d2SMark Murray  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19511b41d2SMark Murray  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20511b41d2SMark Murray  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21511b41d2SMark Murray  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22511b41d2SMark Murray  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23511b41d2SMark Murray  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24511b41d2SMark Murray  */
25511b41d2SMark Murray 
26511b41d2SMark Murray #include "includes.h"
27d95e11bfSDag-Erling Smørgrav RCSID("$OpenBSD: atomicio.c,v 1.12 2003/07/31 15:50:16 avsm Exp $");
28511b41d2SMark Murray 
291e8db6e2SBrian Feldman #include "atomicio.h"
30511b41d2SMark Murray 
31511b41d2SMark Murray /*
32d95e11bfSDag-Erling Smørgrav  * ensure all of data on socket comes through. f==read || f==vwrite
33511b41d2SMark Murray  */
34511b41d2SMark Murray ssize_t
35a8f6863aSKris Kennaway atomicio(f, fd, _s, n)
36d95e11bfSDag-Erling Smørgrav 	ssize_t (*f) (int, void *, size_t);
37511b41d2SMark Murray 	int fd;
38a8f6863aSKris Kennaway 	void *_s;
39511b41d2SMark Murray 	size_t n;
40511b41d2SMark Murray {
41a8f6863aSKris Kennaway 	char *s = _s;
42511b41d2SMark Murray 	ssize_t res, pos = 0;
43511b41d2SMark Murray 
44511b41d2SMark Murray 	while (n > pos) {
45511b41d2SMark Murray 		res = (f) (fd, s + pos, n - pos);
46511b41d2SMark Murray 		switch (res) {
47511b41d2SMark Murray 		case -1:
4883d2307dSDag-Erling Smørgrav #ifdef EWOULDBLOCK
4983d2307dSDag-Erling Smørgrav 			if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
5083d2307dSDag-Erling Smørgrav #else
51511b41d2SMark Murray 			if (errno == EINTR || errno == EAGAIN)
5283d2307dSDag-Erling Smørgrav #endif
53511b41d2SMark Murray 				continue;
54511b41d2SMark Murray 		case 0:
55511b41d2SMark Murray 			return (res);
56511b41d2SMark Murray 		default:
57511b41d2SMark Murray 			pos += res;
58511b41d2SMark Murray 		}
59511b41d2SMark Murray 	}
60511b41d2SMark Murray 	return (pos);
61511b41d2SMark Murray }
62