Blame


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