portable: set next version
[got-portable.git] / lib / pollfd.c
blob32efcd7f1a512fe6f1444aaba4ff7e8aa6d051b1
1 /*
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 <errno.h>
18 #include <stdio.h>
19 #include <signal.h>
20 #include <poll.h>
21 #include <time.h>
22 #include <unistd.h>
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)
31 struct pollfd pfd[1];
32 struct timespec ts;
33 sigset_t sigset;
34 int n;
36 pfd[0].fd = fd;
37 pfd[0].events = events;
39 ts.tv_sec = timeout;
40 ts.tv_nsec = 0;
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);
48 if (n == -1)
49 return got_error_from_errno("ppoll");
50 if (n == 0) {
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)
58 return NULL;
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,
67 size_t minbytes)
69 const struct got_error *err = NULL;
70 size_t have = 0;
71 ssize_t r;
73 if (minbytes > bufsize)
74 return got_error(GOT_ERR_NO_SPACE);
76 while (have < minbytes) {
77 err = got_poll_fd(fd, POLLIN, INFTIM);
78 if (err)
79 return err;
80 r = read(fd, buf + have, bufsize - have);
81 if (r == -1)
82 return got_error_from_errno("read");
83 if (r == 0)
84 return got_error(GOT_ERR_EOF);
85 have += r;
88 *len = have;
89 return NULL;
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;
96 off_t wlen = 0;
97 ssize_t w = 0;
99 while (wlen != len) {
100 if (wlen > 0) {
101 err = got_poll_fd(fd, POLLOUT, INFTIM);
102 if (err)
103 return err;
105 w = write(fd, buf + wlen, len - wlen);
106 if (w == -1) {
107 if (errno != EAGAIN)
108 return got_error_from_errno("write");
109 } else
110 wlen += w;
113 return NULL;