tog: group state assignments and zap dup repo assignment
[got-portable.git] / lib / pollfd.c
blob0d2d79bde766ba0de787feb2149ccba209ae2a4a
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 "got_compat.h"
19 #include <errno.h>
20 #include <stdio.h>
21 #include <signal.h>
22 #include <poll.h>
23 #include <time.h>
24 #include <unistd.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)
33 struct pollfd pfd[1];
34 struct timespec ts;
35 sigset_t sigset;
36 int n;
38 pfd[0].fd = fd;
39 pfd[0].events = events;
41 ts.tv_sec = timeout;
42 ts.tv_nsec = 0;
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);
50 if (n == -1)
51 return got_error_from_errno("ppoll");
52 if (n == 0) {
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)
60 return NULL;
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_timeout(int fd, size_t *len, void *buf, size_t bufsize,
69 size_t minbytes, int timeout)
71 const struct got_error *err = NULL;
72 size_t have = 0;
73 ssize_t r;
75 if (minbytes > bufsize)
76 return got_error(GOT_ERR_NO_SPACE);
78 while (have < minbytes) {
79 err = got_poll_fd(fd, POLLIN, timeout);
80 if (err)
81 return err;
82 r = read(fd, buf + have, bufsize - have);
83 if (r == -1)
84 return got_error_from_errno("read");
85 if (r == 0)
86 return got_error(GOT_ERR_EOF);
87 have += r;
90 *len = have;
91 return NULL;
94 const struct got_error *
95 got_poll_read_full(int fd, size_t *len, void *buf, size_t bufsize,
96 size_t minbytes)
98 return got_poll_read_full_timeout(fd, len, buf, bufsize,
99 minbytes, INFTIM);
102 const struct got_error *
103 got_poll_write_full(int fd, const void *buf, off_t len)
105 const struct got_error *err = NULL;
106 off_t wlen = 0;
107 ssize_t w = 0;
109 while (wlen != len) {
110 if (wlen > 0) {
111 err = got_poll_fd(fd, POLLOUT, INFTIM);
112 if (err)
113 return err;
115 w = write(fd, buf + wlen, len - wlen);
116 if (w == -1) {
117 if (errno != EAGAIN)
118 return got_error_from_errno("write");
119 } else
120 wlen += w;
123 return NULL;