-
Notifications
You must be signed in to change notification settings - Fork 256
/
bpoll.cc
184 lines (164 loc) · 4.07 KB
/
bpoll.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
// Marco van Wieringen, May 2012
#include "include/bareos.h"
#ifdef HAVE_POLL_H
# include <poll.h>
#elif HAVE_SYS_POLL_H
# include <sys/poll.h>
#endif
#ifdef HAVE_POLL
/*
* Returns: 1 if data available
* 0 if timeout
* -1 if error
*/
int WaitForReadableFd(int fd, int msec, bool ignore_interupts)
{
struct pollfd pfds[1];
int events;
events = POLLIN;
# if defined(POLLRDNORM)
events |= POLLRDNORM;
# endif
# if defined(POLLRDBAND)
events |= POLLRDBAND;
# endif
# if defined(POLLPRI)
events |= POLLPRI;
# endif
memset(pfds, 0, sizeof(pfds));
pfds[0].fd = fd;
pfds[0].events = events;
for (;;) {
switch (poll(pfds, 1, msec)) {
case 0: /* timeout */
return 0;
case -1:
if (ignore_interupts && (errno == EINTR || errno == EAGAIN)) {
continue;
}
return -1; /* error return */
default:
if (pfds[0].revents & events) {
return 1;
} else {
return 0;
}
}
}
}
/*
* Returns: 1 if data available
* 0 if timeout
* -1 if error
*/
int WaitForWritableFd(int fd, int msec, bool ignore_interupts)
{
struct pollfd pfds[1];
int events;
events = POLLOUT;
# if defined(POLLWRNORM)
events |= POLLWRNORM;
# endif
# if defined POLLWRBAND
events |= POLLWRBAND;
# endif
memset(pfds, 0, sizeof(pfds));
pfds[0].fd = fd;
pfds[0].events = events;
for (;;) {
switch (poll(pfds, 1, msec)) {
case 0: /* timeout */
return 0;
case -1:
if (ignore_interupts && (errno == EINTR || errno == EAGAIN)) {
continue;
}
return -1; /* error return */
default:
if (pfds[0].revents & events) {
return 1;
} else {
return 0;
}
}
}
}
#else
/*
* Returns: 1 if data available
* 0 if timeout
* -1 if error
*/
int WaitForReadableFd(int fd, int msec, bool ignore_interupts)
{
fd_set fdset;
struct timeval tv;
tv.tv_sec = msec / 1000;
tv.tv_usec = (msec % 1000) * 1000;
for (;;) {
FD_ZERO(&fdset);
FD_SET((unsigned)fd, &fdset);
switch (select(fd + 1, &fdset, NULL, NULL, &tv)) {
case 0: /* timeout */
return 0;
case -1:
if (ignore_interupts && (errno == EINTR || errno == EAGAIN)) {
continue;
}
return -1; /* error return */
default:
return 1;
}
}
}
/*
* Returns: 1 if data available
* 0 if timeout
* -1 if error
*/
int WaitForWritableFd([[maybe_unused]] int fd,
[[maybe_unused]] int msec,
[[maybe_unused]] bool ignore_interupts)
{
# if defined(HAVE_WIN32)
return 1;
# else
fd_set fdset;
struct timeval tv;
tv.tv_sec = msec / 1000;
tv.tv_usec = (msec % 1000) * 1000;
for (;;) {
FD_ZERO(&fdset);
FD_SET((unsigned)fd, &fdset);
switch (select(fd + 1, NULL, &fdset, NULL, &tv)) {
case 0: /* timeout */
return 0;
case -1:
if (ignore_interupts && (errno == EINTR || errno == EAGAIN)) {
continue;
}
return -1; /* error return */
default:
return 1;
}
}
# endif /* defined(HAVE_WIN32) */
}
#endif /* HAVE_POLL */