blob: 7637e167198685f1d421764265f0e8868a01f1a0 [file] [log] [blame]
Damien Miller05ec5c61999-12-07 17:03:33 +11001/*
Ben Lindstrom92a2e382001-03-05 06:59:27 +00002 * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved.
Damien Miller05ec5c61999-12-07 17:03:33 +11003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
Darren Tucker85f04c62003-08-03 00:05:58 +100027RCSID("$OpenBSD: atomicio.c,v 1.12 2003/07/31 15:50:16 avsm Exp $");
Damien Miller05ec5c61999-12-07 17:03:33 +110028
Ben Lindstrom226cfa02001-01-22 05:34:40 +000029#include "atomicio.h"
Damien Miller05ec5c61999-12-07 17:03:33 +110030
31/*
Darren Tucker85f04c62003-08-03 00:05:58 +100032 * ensure all of data on socket comes through. f==read || f==vwrite
Damien Miller05ec5c61999-12-07 17:03:33 +110033 */
Damien Miller98c7ad62000-03-09 21:27:49 +110034ssize_t
Damien Miller7684ee12000-03-17 23:40:15 +110035atomicio(f, fd, _s, n)
Darren Tucker9f63f222003-07-03 13:46:56 +100036 ssize_t (*f) (int, void *, size_t);
Damien Miller05ec5c61999-12-07 17:03:33 +110037 int fd;
Damien Miller7684ee12000-03-17 23:40:15 +110038 void *_s;
Damien Miller05ec5c61999-12-07 17:03:33 +110039 size_t n;
40{
Damien Miller7684ee12000-03-17 23:40:15 +110041 char *s = _s;
Damien Miller98c7ad62000-03-09 21:27:49 +110042 ssize_t res, pos = 0;
Damien Miller05ec5c61999-12-07 17:03:33 +110043
44 while (n > pos) {
Damien Miller5f056372000-04-16 12:31:48 +100045 res = (f) (fd, s + pos, n - pos);
Damien Miller05ec5c61999-12-07 17:03:33 +110046 switch (res) {
47 case -1:
Damien Millerf2fdbe52000-07-08 10:57:08 +100048#ifdef EWOULDBLOCK
49 if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK)
50#else
Damien Miller05ec5c61999-12-07 17:03:33 +110051 if (errno == EINTR || errno == EAGAIN)
Damien Millerf2fdbe52000-07-08 10:57:08 +100052#endif
Damien Miller05ec5c61999-12-07 17:03:33 +110053 continue;
54 case 0:
55 return (res);
56 default:
57 pos += res;
58 }
59 }
60 return (pos);
61}