Blame


1 3efd8e31 2022-10-23 thomas /*
2 3efd8e31 2022-10-23 thomas * Copyright (c) 2018, 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas *
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
7 3efd8e31 2022-10-23 thomas *
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3efd8e31 2022-10-23 thomas */
16 3efd8e31 2022-10-23 thomas
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
18 4fccd2fe 2023-03-08 thomas
19 3efd8e31 2022-10-23 thomas #include <errno.h>
20 3efd8e31 2022-10-23 thomas #include <stdio.h>
21 3efd8e31 2022-10-23 thomas #include <signal.h>
22 3efd8e31 2022-10-23 thomas #include <poll.h>
23 3efd8e31 2022-10-23 thomas #include <time.h>
24 3efd8e31 2022-10-23 thomas #include <unistd.h>
25 3efd8e31 2022-10-23 thomas
26 3efd8e31 2022-10-23 thomas #include "got_error.h"
27 3efd8e31 2022-10-23 thomas
28 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
29 3efd8e31 2022-10-23 thomas
30 3efd8e31 2022-10-23 thomas const struct got_error *
31 3efd8e31 2022-10-23 thomas got_poll_fd(int fd, int events, int timeout)
32 3efd8e31 2022-10-23 thomas {
33 3efd8e31 2022-10-23 thomas struct pollfd pfd[1];
34 3efd8e31 2022-10-23 thomas struct timespec ts;
35 3efd8e31 2022-10-23 thomas sigset_t sigset;
36 3efd8e31 2022-10-23 thomas int n;
37 3efd8e31 2022-10-23 thomas
38 3efd8e31 2022-10-23 thomas pfd[0].fd = fd;
39 3efd8e31 2022-10-23 thomas pfd[0].events = events;
40 3efd8e31 2022-10-23 thomas
41 3efd8e31 2022-10-23 thomas ts.tv_sec = timeout;
42 3efd8e31 2022-10-23 thomas ts.tv_nsec = 0;
43 3efd8e31 2022-10-23 thomas
44 3efd8e31 2022-10-23 thomas if (sigemptyset(&sigset) == -1)
45 3efd8e31 2022-10-23 thomas return got_error_from_errno("sigemptyset");
46 3efd8e31 2022-10-23 thomas if (sigaddset(&sigset, SIGWINCH) == -1)
47 3efd8e31 2022-10-23 thomas return got_error_from_errno("sigaddset");
48 3efd8e31 2022-10-23 thomas
49 3efd8e31 2022-10-23 thomas n = ppoll(pfd, 1, timeout == INFTIM ? NULL : &ts, &sigset);
50 3efd8e31 2022-10-23 thomas if (n == -1)
51 3efd8e31 2022-10-23 thomas return got_error_from_errno("ppoll");
52 3efd8e31 2022-10-23 thomas if (n == 0) {
53 3efd8e31 2022-10-23 thomas if (pfd[0].revents & POLLHUP)
54 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_EOF);
55 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_TIMEOUT);
56 3efd8e31 2022-10-23 thomas }
57 3efd8e31 2022-10-23 thomas if (pfd[0].revents & (POLLERR | POLLNVAL))
58 3efd8e31 2022-10-23 thomas return got_error_from_errno("poll error");
59 3efd8e31 2022-10-23 thomas if (pfd[0].revents & events)
60 3efd8e31 2022-10-23 thomas return NULL;
61 3efd8e31 2022-10-23 thomas if (pfd[0].revents & POLLHUP)
62 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_EOF);
63 3efd8e31 2022-10-23 thomas
64 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_INTERRUPT);
65 3efd8e31 2022-10-23 thomas }
66 3efd8e31 2022-10-23 thomas
67 3efd8e31 2022-10-23 thomas const struct got_error *
68 c1c94802 2024-03-30 thomas got_poll_read_full_timeout(int fd, size_t *len, void *buf, size_t bufsize,
69 c1c94802 2024-03-30 thomas size_t minbytes, int timeout)
70 3efd8e31 2022-10-23 thomas {
71 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
72 3efd8e31 2022-10-23 thomas size_t have = 0;
73 3efd8e31 2022-10-23 thomas ssize_t r;
74 3efd8e31 2022-10-23 thomas
75 3efd8e31 2022-10-23 thomas if (minbytes > bufsize)
76 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
77 3efd8e31 2022-10-23 thomas
78 3efd8e31 2022-10-23 thomas while (have < minbytes) {
79 c1c94802 2024-03-30 thomas err = got_poll_fd(fd, POLLIN, timeout);
80 3efd8e31 2022-10-23 thomas if (err)
81 3efd8e31 2022-10-23 thomas return err;
82 3efd8e31 2022-10-23 thomas r = read(fd, buf + have, bufsize - have);
83 3efd8e31 2022-10-23 thomas if (r == -1)
84 3efd8e31 2022-10-23 thomas return got_error_from_errno("read");
85 3efd8e31 2022-10-23 thomas if (r == 0)
86 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_EOF);
87 3efd8e31 2022-10-23 thomas have += r;
88 3efd8e31 2022-10-23 thomas }
89 3efd8e31 2022-10-23 thomas
90 3efd8e31 2022-10-23 thomas *len = have;
91 3efd8e31 2022-10-23 thomas return NULL;
92 3efd8e31 2022-10-23 thomas }
93 3efd8e31 2022-10-23 thomas
94 3efd8e31 2022-10-23 thomas const struct got_error *
95 c1c94802 2024-03-30 thomas got_poll_read_full(int fd, size_t *len, void *buf, size_t bufsize,
96 c1c94802 2024-03-30 thomas size_t minbytes)
97 c1c94802 2024-03-30 thomas {
98 c1c94802 2024-03-30 thomas return got_poll_read_full_timeout(fd, len, buf, bufsize,
99 c1c94802 2024-03-30 thomas minbytes, INFTIM);
100 c1c94802 2024-03-30 thomas }
101 c1c94802 2024-03-30 thomas
102 c1c94802 2024-03-30 thomas const struct got_error *
103 3efd8e31 2022-10-23 thomas got_poll_write_full(int fd, const void *buf, off_t len)
104 3efd8e31 2022-10-23 thomas {
105 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
106 3efd8e31 2022-10-23 thomas off_t wlen = 0;
107 3efd8e31 2022-10-23 thomas ssize_t w = 0;
108 3efd8e31 2022-10-23 thomas
109 3efd8e31 2022-10-23 thomas while (wlen != len) {
110 3efd8e31 2022-10-23 thomas if (wlen > 0) {
111 3efd8e31 2022-10-23 thomas err = got_poll_fd(fd, POLLOUT, INFTIM);
112 3efd8e31 2022-10-23 thomas if (err)
113 3efd8e31 2022-10-23 thomas return err;
114 3efd8e31 2022-10-23 thomas }
115 3efd8e31 2022-10-23 thomas w = write(fd, buf + wlen, len - wlen);
116 3efd8e31 2022-10-23 thomas if (w == -1) {
117 3efd8e31 2022-10-23 thomas if (errno != EAGAIN)
118 3efd8e31 2022-10-23 thomas return got_error_from_errno("write");
119 3efd8e31 2022-10-23 thomas } else
120 3efd8e31 2022-10-23 thomas wlen += w;
121 3efd8e31 2022-10-23 thomas }
122 3efd8e31 2022-10-23 thomas
123 3efd8e31 2022-10-23 thomas return NULL;
124 3efd8e31 2022-10-23 thomas }