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 3efd8e31 2022-10-23 thomas #include <errno.h>
18 3efd8e31 2022-10-23 thomas #include <stdio.h>
19 3efd8e31 2022-10-23 thomas #include <signal.h>
20 3efd8e31 2022-10-23 thomas #include <poll.h>
21 3efd8e31 2022-10-23 thomas #include <time.h>
22 3efd8e31 2022-10-23 thomas #include <unistd.h>
23 3efd8e31 2022-10-23 thomas
24 3efd8e31 2022-10-23 thomas #include "got_error.h"
25 3efd8e31 2022-10-23 thomas
26 3efd8e31 2022-10-23 thomas #include "got_lib_poll.h"
27 3efd8e31 2022-10-23 thomas
28 3efd8e31 2022-10-23 thomas const struct got_error *
29 3efd8e31 2022-10-23 thomas got_poll_fd(int fd, int events, int timeout)
30 3efd8e31 2022-10-23 thomas {
31 3efd8e31 2022-10-23 thomas struct pollfd pfd[1];
32 3efd8e31 2022-10-23 thomas struct timespec ts;
33 3efd8e31 2022-10-23 thomas sigset_t sigset;
34 3efd8e31 2022-10-23 thomas int n;
35 3efd8e31 2022-10-23 thomas
36 3efd8e31 2022-10-23 thomas pfd[0].fd = fd;
37 3efd8e31 2022-10-23 thomas pfd[0].events = events;
38 3efd8e31 2022-10-23 thomas
39 3efd8e31 2022-10-23 thomas ts.tv_sec = timeout;
40 3efd8e31 2022-10-23 thomas ts.tv_nsec = 0;
41 3efd8e31 2022-10-23 thomas
42 3efd8e31 2022-10-23 thomas if (sigemptyset(&sigset) == -1)
43 3efd8e31 2022-10-23 thomas return got_error_from_errno("sigemptyset");
44 3efd8e31 2022-10-23 thomas if (sigaddset(&sigset, SIGWINCH) == -1)
45 3efd8e31 2022-10-23 thomas return got_error_from_errno("sigaddset");
46 3efd8e31 2022-10-23 thomas
47 3efd8e31 2022-10-23 thomas n = ppoll(pfd, 1, timeout == INFTIM ? NULL : &ts, &sigset);
48 3efd8e31 2022-10-23 thomas if (n == -1)
49 3efd8e31 2022-10-23 thomas return got_error_from_errno("ppoll");
50 3efd8e31 2022-10-23 thomas if (n == 0) {
51 3efd8e31 2022-10-23 thomas if (pfd[0].revents & POLLHUP)
52 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_EOF);
53 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_TIMEOUT);
54 3efd8e31 2022-10-23 thomas }
55 3efd8e31 2022-10-23 thomas if (pfd[0].revents & (POLLERR | POLLNVAL))
56 3efd8e31 2022-10-23 thomas return got_error_from_errno("poll error");
57 3efd8e31 2022-10-23 thomas if (pfd[0].revents & events)
58 3efd8e31 2022-10-23 thomas return NULL;
59 3efd8e31 2022-10-23 thomas if (pfd[0].revents & POLLHUP)
60 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_EOF);
61 3efd8e31 2022-10-23 thomas
62 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_INTERRUPT);
63 3efd8e31 2022-10-23 thomas }
64 3efd8e31 2022-10-23 thomas
65 3efd8e31 2022-10-23 thomas const struct got_error *
66 3efd8e31 2022-10-23 thomas got_poll_read_full(int fd, size_t *len, void *buf, size_t bufsize,
67 3efd8e31 2022-10-23 thomas size_t minbytes)
68 3efd8e31 2022-10-23 thomas {
69 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
70 3efd8e31 2022-10-23 thomas size_t have = 0;
71 3efd8e31 2022-10-23 thomas ssize_t r;
72 3efd8e31 2022-10-23 thomas
73 3efd8e31 2022-10-23 thomas if (minbytes > bufsize)
74 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
75 3efd8e31 2022-10-23 thomas
76 3efd8e31 2022-10-23 thomas while (have < minbytes) {
77 3efd8e31 2022-10-23 thomas err = got_poll_fd(fd, POLLIN, INFTIM);
78 3efd8e31 2022-10-23 thomas if (err)
79 3efd8e31 2022-10-23 thomas return err;
80 3efd8e31 2022-10-23 thomas r = read(fd, buf + have, bufsize - have);
81 3efd8e31 2022-10-23 thomas if (r == -1)
82 3efd8e31 2022-10-23 thomas return got_error_from_errno("read");
83 3efd8e31 2022-10-23 thomas if (r == 0)
84 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_EOF);
85 3efd8e31 2022-10-23 thomas have += r;
86 3efd8e31 2022-10-23 thomas }
87 3efd8e31 2022-10-23 thomas
88 3efd8e31 2022-10-23 thomas *len = have;
89 3efd8e31 2022-10-23 thomas return NULL;
90 3efd8e31 2022-10-23 thomas }
91 3efd8e31 2022-10-23 thomas
92 3efd8e31 2022-10-23 thomas const struct got_error *
93 3efd8e31 2022-10-23 thomas got_poll_write_full(int fd, const void *buf, off_t len)
94 3efd8e31 2022-10-23 thomas {
95 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
96 3efd8e31 2022-10-23 thomas off_t wlen = 0;
97 3efd8e31 2022-10-23 thomas ssize_t w = 0;
98 3efd8e31 2022-10-23 thomas
99 3efd8e31 2022-10-23 thomas while (wlen != len) {
100 3efd8e31 2022-10-23 thomas if (wlen > 0) {
101 3efd8e31 2022-10-23 thomas err = got_poll_fd(fd, POLLOUT, INFTIM);
102 3efd8e31 2022-10-23 thomas if (err)
103 3efd8e31 2022-10-23 thomas return err;
104 3efd8e31 2022-10-23 thomas }
105 3efd8e31 2022-10-23 thomas w = write(fd, buf + wlen, len - wlen);
106 3efd8e31 2022-10-23 thomas if (w == -1) {
107 3efd8e31 2022-10-23 thomas if (errno != EAGAIN)
108 3efd8e31 2022-10-23 thomas return got_error_from_errno("write");
109 3efd8e31 2022-10-23 thomas } else
110 3efd8e31 2022-10-23 thomas wlen += w;
111 3efd8e31 2022-10-23 thomas }
112 3efd8e31 2022-10-23 thomas
113 3efd8e31 2022-10-23 thomas return NULL;
114 3efd8e31 2022-10-23 thomas }