| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: atomicio.c,v 1.3 2011/07/25 03:03:10 christos Exp $ */ | | 1 | /* $NetBSD: atomicio.c,v 1.4 2011/08/01 15:55:00 christos Exp $ */ |
2 | /* $OpenBSD: atomicio.c,v 1.26 2010/09/22 22:58:51 djm Exp $ */ | | 2 | /* $OpenBSD: atomicio.c,v 1.26 2010/09/22 22:58:51 djm Exp $ */ |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2006 Damien Miller. All rights reserved. | | 4 | * Copyright (c) 2006 Damien Miller. All rights reserved. |
5 | * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved. | | 5 | * Copyright (c) 2005 Anil Madhavapeddy. All rights reserved. |
6 | * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. | | 6 | * Copyright (c) 1995,1999 Theo de Raadt. All rights reserved. |
7 | * All rights reserved. | | 7 | * All rights reserved. |
8 | * | | 8 | * |
9 | * Redistribution and use in source and binary forms, with or without | | 9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions | | 10 | * modification, are permitted provided that the following conditions |
11 | * are met: | | 11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright | | 12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. | | 13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright | | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| @@ -18,51 +18,55 @@ | | | @@ -18,51 +18,55 @@ |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | | 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | | 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | | 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | | 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | | 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | | 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | | 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ | | 28 | */ |
29 | | | 29 | |
30 | #include "includes.h" | | 30 | #include "includes.h" |
31 | __RCSID("$NetBSD: atomicio.c,v 1.3 2011/07/25 03:03:10 christos Exp $"); | | 31 | __RCSID("$NetBSD: atomicio.c,v 1.4 2011/08/01 15:55:00 christos Exp $"); |
32 | #include <sys/param.h> | | 32 | #include <sys/param.h> |
33 | #include <sys/uio.h> | | 33 | #include <sys/uio.h> |
34 | | | 34 | |
35 | #include <errno.h> | | 35 | #include <errno.h> |
36 | #include <poll.h> | | 36 | #include <poll.h> |
37 | #include <string.h> | | 37 | #include <string.h> |
38 | #include <unistd.h> | | 38 | #include <unistd.h> |
39 | | | 39 | |
40 | #include "atomicio.h" | | 40 | #include "atomicio.h" |
41 | | | 41 | |
42 | /* | | 42 | /* |
43 | * ensure all of data on socket comes through. f==read || f==vwrite | | 43 | * ensure all of data on socket comes through. f==read || f==vwrite |
44 | */ | | 44 | */ |
45 | size_t | | 45 | size_t |
46 | atomicio6(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n, | | 46 | atomicio6(ssize_t (*f) (int, void *, size_t), int fd, void *_s, size_t n, |
47 | int (*cb)(void *, size_t), void *cb_arg) | | 47 | int (*cb)(void *, size_t), void *cb_arg) |
48 | { | | 48 | { |
49 | char *s = _s; | | 49 | char *s = _s; |
50 | size_t pos = 0; | | 50 | size_t pos = 0; |
51 | ssize_t res; | | 51 | ssize_t res; |
52 | struct pollfd pfd; | | 52 | struct pollfd pfd; |
53 | | | 53 | |
54 | pfd.fd = fd; | | 54 | pfd.fd = fd; |
55 | pfd.events = f == read ? POLLIN : POLLOUT; | | 55 | /* |
| | | 56 | * check for vwrite instead of read to avoid read being renamed |
| | | 57 | * by SSP issues |
| | | 58 | */ |
| | | 59 | pfd.events = f == vwrite ? POLLOUT : POLLIN; |
56 | while (n > pos) { | | 60 | while (n > pos) { |
57 | res = (f) (fd, s + pos, n - pos); | | 61 | res = (f) (fd, s + pos, n - pos); |
58 | switch (res) { | | 62 | switch (res) { |
59 | case -1: | | 63 | case -1: |
60 | if (errno == EINTR) | | 64 | if (errno == EINTR) |
61 | continue; | | 65 | continue; |
62 | if (errno == EAGAIN) { | | 66 | if (errno == EAGAIN) { |
63 | (void)poll(&pfd, 1, -1); | | 67 | (void)poll(&pfd, 1, -1); |
64 | continue; | | 68 | continue; |
65 | } | | 69 | } |
66 | return 0; | | 70 | return 0; |
67 | case 0: | | 71 | case 0: |
68 | errno = EPIPE; | | 72 | errno = EPIPE; |