2 * Copyright (c) 2018, 2022 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "got_compat.h"
26 #include "got_error.h"
28 #include "got_lib_poll.h"
30 const struct got_error
*
31 got_poll_fd(int fd
, int events
, int timeout
)
39 pfd
[0].events
= events
;
44 if (sigemptyset(&sigset
) == -1)
45 return got_error_from_errno("sigemptyset");
46 if (sigaddset(&sigset
, SIGWINCH
) == -1)
47 return got_error_from_errno("sigaddset");
49 n
= ppoll(pfd
, 1, timeout
== INFTIM
? NULL
: &ts
, &sigset
);
51 return got_error_from_errno("ppoll");
53 if (pfd
[0].revents
& POLLHUP
)
54 return got_error(GOT_ERR_EOF
);
55 return got_error(GOT_ERR_TIMEOUT
);
57 if (pfd
[0].revents
& (POLLERR
| POLLNVAL
))
58 return got_error_from_errno("poll error");
59 if (pfd
[0].revents
& events
)
61 if (pfd
[0].revents
& POLLHUP
)
62 return got_error(GOT_ERR_EOF
);
64 return got_error(GOT_ERR_INTERRUPT
);
67 const struct got_error
*
68 got_poll_read_full(int fd
, size_t *len
, void *buf
, size_t bufsize
,
71 const struct got_error
*err
= NULL
;
75 if (minbytes
> bufsize
)
76 return got_error(GOT_ERR_NO_SPACE
);
78 while (have
< minbytes
) {
79 err
= got_poll_fd(fd
, POLLIN
, INFTIM
);
82 r
= read(fd
, buf
+ have
, bufsize
- have
);
84 return got_error_from_errno("read");
86 return got_error(GOT_ERR_EOF
);
94 const struct got_error
*
95 got_poll_write_full(int fd
, const void *buf
, off_t len
)
97 const struct got_error
*err
= NULL
;
101 while (wlen
!= len
) {
103 err
= got_poll_fd(fd
, POLLOUT
, INFTIM
);
107 w
= write(fd
, buf
+ wlen
, len
- wlen
);
110 return got_error_from_errno("write");