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.
24 #include "got_error.h"
26 #include "got_lib_poll.h"
28 const struct got_error
*
29 got_poll_fd(int fd
, int events
, int timeout
)
37 pfd
[0].events
= events
;
42 if (sigemptyset(&sigset
) == -1)
43 return got_error_from_errno("sigemptyset");
44 if (sigaddset(&sigset
, SIGWINCH
) == -1)
45 return got_error_from_errno("sigaddset");
47 n
= ppoll(pfd
, 1, timeout
== INFTIM
? NULL
: &ts
, &sigset
);
49 return got_error_from_errno("ppoll");
51 if (pfd
[0].revents
& POLLHUP
)
52 return got_error(GOT_ERR_EOF
);
53 return got_error(GOT_ERR_TIMEOUT
);
55 if (pfd
[0].revents
& (POLLERR
| POLLNVAL
))
56 return got_error_from_errno("poll error");
57 if (pfd
[0].revents
& events
)
59 if (pfd
[0].revents
& POLLHUP
)
60 return got_error(GOT_ERR_EOF
);
62 return got_error(GOT_ERR_INTERRUPT
);
65 const struct got_error
*
66 got_poll_read_full(int fd
, size_t *len
, void *buf
, size_t bufsize
,
69 const struct got_error
*err
= NULL
;
73 if (minbytes
> bufsize
)
74 return got_error(GOT_ERR_NO_SPACE
);
76 while (have
< minbytes
) {
77 err
= got_poll_fd(fd
, POLLIN
, INFTIM
);
80 r
= read(fd
, buf
+ have
, bufsize
- have
);
82 return got_error_from_errno("read");
84 return got_error(GOT_ERR_EOF
);
92 const struct got_error
*
93 got_poll_write_full(int fd
, const void *buf
, off_t len
)
95 const struct got_error
*err
= NULL
;
101 err
= got_poll_fd(fd
, POLLOUT
, INFTIM
);
105 w
= write(fd
, buf
+ wlen
, len
- wlen
);
108 return got_error_from_errno("write");