include/clamav/clamd_compat.h

filter: Statementstmt 93.0% 40 / 43 Branchbran 84.2% 29 / 38 (7 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.8% 69 / 81 (7 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« t/abi_consumer/Makefile.PL ↑ Summary blib/arch/ClamAV/Clamd/Install/Files.pm »
1 /* clamd_compat.h - the platform bits, probed rather than assumed.
2 *
3 * The rule here is the one the FreeBSD 9 / gcc 4.2.1 smoker taught:
4 * test for the feature, never for the compiler or its version.
5 */
6 #ifndef CLAMD_COMPAT_H
7 #define CLAMD_COMPAT_H
8  
9 #ifdef _WIN32
10 # include <winsock2.h>
11 # include <ws2tcpip.h>
12 # include <windows.h>
13 # include <io.h>
14 # include <fcntl.h>
15 # include <errno.h>
16 # include <sys/types.h>
17 # include <sys/stat.h>
18 typedef int cc_socklen_t;
19 /* A socket is held in an int everywhere in this dist, so the invalid
20 * one is -1 here as well and not INVALID_SOCKET, which is unsigned and
21 * 64 bits wide. Kernel handles fit in 32 bits on Win64. */
22 # define CC_INVALID_SOCK (-1)
23 # define cc_close_sock(s) closesocket((SOCKET)(s))
24 # define cc_sock_errno() WSAGetLastError()
25 /* Windows has AF_UNIX from Win10 1803, but it has no SCM_RIGHTS and
26 * no descriptor passing at all - so FILDES can never work there. That
27 * is a phase 2 decision; what matters here is that it is a platform
28 * fact and not a build accident. */
29 # define CC_HAVE_FD_PASSING 0
30 #else
31 # include <sys/types.h>
32 # include <sys/socket.h>
33 # include <sys/un.h>
34 # include <netinet/in.h>
35 # include <netinet/tcp.h>
36 # include <arpa/inet.h>
37 # include <netdb.h>
38 # include <unistd.h>
39 # include <fcntl.h>
40 # include <errno.h>
41 typedef socklen_t cc_socklen_t;
42 # define CC_INVALID_SOCK (-1)
43 # define cc_close_sock(s) close(s)
44 # define cc_sock_errno() errno
45 /* Descriptor passing is a fact about the headers in front of us, not
46 * about the operating system. Solaris and illumos keep the 4.3BSD
47 * msghdr - the one with msg_accrights and no msg_control - and hide
48 * the CMSG_* macros until _XPG4_2 is defined, and perl.h has already
49 * included <sys/socket.h> by the time this file is read, so the define
50 * has to arrive on the command line. Makefile.PL probes for the
51 * spelling that works there.
52 *
53 * Where the macros stay hidden, SCM_RIGHTS cannot be spoken at all:
54 * FILDES compiles out and cc_scan_fd falls back to INSTREAM, which is
55 * slower and correct. Testing the macros rather than the platform is
56 * also what catches the case where the probe never ran.
57 */
58 # if defined(CC_NO_FD_PASSING) || !defined(SCM_RIGHTS) \
59 || !defined(CMSG_SPACE) || !defined(CMSG_LEN) \
60 || !defined(CMSG_FIRSTHDR) || !defined(CMSG_DATA)
61 # define CC_HAVE_FD_PASSING 0
62 # else
63 # define CC_HAVE_FD_PASSING 1
64 # endif
65 #endif
66  
67 #include <string.h>
68 #include <stdlib.h>
69 #include <time.h>
70  
71 #ifndef _WIN32
72 # include <sys/time.h>
73 # include <sys/stat.h>
74 # include <poll.h>
75 #endif
76  
77 /* The file side. The underscore names on Windows are deliberate: perl
78 * renames the plain ones (read, open, close, fstat) there, and its fstat
79 * wants perl's own stat struct. The descriptor is opened binary, or the
80 * C runtime rewrites line endings and stops at ^Z - in a scanner, that
81 * is a file clamd never saw the whole of. */
82 #ifdef _WIN32
83 typedef struct _stat64 cc_stat_t;
84 # define cc_fstat(fd, st) _fstat64((fd), (st))
85 # define cc_open_ro(path) _open((path), _O_RDONLY | _O_BINARY)
86 # define cc_read_fd(fd, b, n) _read((fd), (b), (unsigned int)(n))
87 # define cc_close_fd(fd) _close(fd)
88 # define CC_S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
89 #else
90 typedef struct stat cc_stat_t;
91 # define cc_fstat(fd, st) fstat((fd), (st))
92 # define cc_open_ro(path) open((path), O_RDONLY)
93 # define cc_read_fd(fd, b, n) read((fd), (b), (n))
94 # define cc_close_fd(fd) close(fd)
95 # define CC_S_ISREG(m) S_ISREG(m)
96 #endif
97  
98 /* Writing to a socket the peer has closed raises SIGPIPE, whose default
99 * disposition kills the process. A library that takes down its caller
100 * because clamd hung up is not usable inside a server, and clamd hangs
101 * up routinely - it does exactly that when a stream exceeds
102 * StreamMaxLength.
103 *
104 * There is no single portable answer, so both are used:
105 * MSG_NOSIGNAL per-call, Linux and modern POSIX
106 * SO_NOSIGPIPE per-socket, macOS and the BSDs
107 * Neither exists everywhere; where neither does, every send still
108 * reports EPIPE and the caller's own disposition governs.
109 *
110 * This is also why nothing below uses write() or writev(): they take no
111 * flags. sendmsg() carries an iovec and takes flags, so it does both
112 * jobs at once.
113 */
114 #ifdef MSG_NOSIGNAL
115 # define CC_MSG_NOSIGNAL MSG_NOSIGNAL
116 #else
117 # define CC_MSG_NOSIGNAL 0
118 #endif
119  
120 /* "The peer has gone" is spelled differently per platform, and getting
121 * the set wrong turns a stream clamd deliberately closed into a generic
122 * IO error - which loses the distinction between "no verdict" and
123 * "something broke". macOS in particular answers ENOTCONN where Linux
124 * answers EPIPE. */
125 10134 static int cc_errno_is_closed(int e) {
126 #ifdef _WIN32
127 return e == EPIPE;
128 #else
129 10134 ▶ if (e == EPIPE || e == ECONNRESET || e == ENOTCONN) return 1; /* # uncoverable condition left count:3 */
Branch: 3 branches
BranchTrueFalseTotal
if (e == EPIPE || e == ECONNRESET || e == ENOTCONN) return 1; /* # uncoverable condition left count:3 */ 10131 3 10134
if (e == EPIPE || e == ECONNRESET || e == ENOTCONN) return 1; /* # uncoverable condition left count:3 */ 10120 11 10131
if (e == EPIPE || e == ECONNRESET || e == ENOTCONN) return 1; /* # uncoverable condition left count:3 */ 0 10120 10120
130 # ifdef ESHUTDOWN
131 /*
132 # uncoverable branch true
133 # uncoverable condition left count:1 */
134 10120 ▶ if (e == ESHUTDOWN) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (e == ESHUTDOWN) return 1; 0 10120 10120
135 # endif
136 # ifdef ECONNABORTED
137 /*
138 # uncoverable branch true
139 # uncoverable condition left count:1 */
140 10120 ▶ if (e == ECONNABORTED) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (e == ECONNABORTED) return 1; 0 10120 10120
141 # endif
142 10120 return 0;
143 #endif
144 }
145  
146 /* Winsock reports through WSAGetLastError and never touches errno, and
147 * every caller below reads errno. So the socket calls go through these,
148 * which translate once: would-block to EAGAIN, every spelling of "the
149 * peer has gone" to EPIPE, the rest to EIO. Elsewhere they are the plain
150 * calls. */
151 #ifdef _WIN32
152 static void cc_wsa_to_errno(void) {
153 switch (WSAGetLastError()) {
154 case WSAEWOULDBLOCK:
155 case WSAEINPROGRESS:
156 case WSAEALREADY: errno = EAGAIN; break;
157 case WSAEINTR: errno = EINTR; break;
158 case WSAECONNRESET:
159 case WSAECONNABORTED:
160 case WSAENOTCONN:
161 case WSAESHUTDOWN:
162 case WSAENETRESET: errno = EPIPE; break;
163 default: errno = EIO; break;
164 }
165 }
166  
167 static ssize_t cc_send(int fd, const char *buf, size_t len, int flags) {
168 int n;
169 do {
170 n = send((SOCKET)fd, buf, (int)len, flags);
171 if (n == SOCKET_ERROR) cc_wsa_to_errno();
172 } while (n == SOCKET_ERROR && errno == EINTR);
173 return n == SOCKET_ERROR ? -1 : (ssize_t)n;
174 }
175  
176 static ssize_t cc_recv(int fd, char *buf, size_t len) {
177 int n;
178 do {
179 n = recv((SOCKET)fd, buf, (int)len, 0);
180 if (n == SOCKET_ERROR) cc_wsa_to_errno();
181 } while (n == SOCKET_ERROR && errno == EINTR);
182 return n == SOCKET_ERROR ? -1 : (ssize_t)n;
183 }
184  
185 /* Winsock has to be started before the first call into it. perl starts
186 * it lazily inside its own socket wrappers, which this dist does not go
187 * through, so it cannot be assumed. WSAStartup is counted, and the one
188 * reference taken here is held for the life of the process. */
189 static int cc_net_init(void) {
190 static int started = 0;
191 WSADATA wsa;
192 if (started) return 0;
193 if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) return -1;
194 started = 1;
195 return 0;
196 }
197 #else
198 /* A signal landing during a call on a non-blocking socket cannot
199 * interrupt it, since nothing sleeps; the retry is for a platform that
200 * reports one anyway, and it lives here so no caller has to. */
201 49636 static ssize_t cc_send(int fd, const char *buf, size_t len, int flags) {
202 ssize_t n;
203 do {
204 49636 n = send(fd, buf, len, flags);
205 /*
206 # uncoverable branch true
207 # uncoverable condition left count:1
208 # uncoverable condition left count:2 */
209 49636 ▶ } while (n < 0 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
} while (n < 0 && errno == EINTR); 16 49620 49636
} while (n < 0 && errno == EINTR); 0 16 16
210 49636 return n;
211 }
212  
213 10258 static ssize_t cc_recv(int fd, char *buf, size_t len) {
214 ssize_t n;
215 do {
216 10258 n = recv(fd, buf, len, 0);
217 /*
218 # uncoverable branch true
219 # uncoverable condition left count:1
220 # uncoverable condition left count:2 */
221 10258 ▶ } while (n < 0 && errno == EINTR);
Branch: 2 branches
BranchTrueFalseTotal
} while (n < 0 && errno == EINTR); 10131 127 10258
} while (n < 0 && errno == EINTR); 0 10131 10131
222 10258 return n;
223 }
224 # define cc_net_init() 0
225 #endif
226  
227 /* EAGAIN and EWOULDBLOCK are one value on every platform this has met,
228 * which is why the second test is never the one that answers. */
229 10136 static int cc_would_block(int e) {
230 10136 ▶ return e == EAGAIN || e == EWOULDBLOCK; /* # uncoverable condition left count:2 */
Branch: 2 branches
BranchTrueFalseTotal
return e == EAGAIN || e == EWOULDBLOCK; /* # uncoverable condition left count:2 */ 3 10133 10136
return e == EAGAIN || e == EWOULDBLOCK; /* # uncoverable condition left count:2 */ 0 3 3
231 }
232  
233 168 static void cc_suppress_sigpipe(int fd) {
234 #ifdef SO_NOSIGPIPE
235 int one = 1;
236 (void)setsockopt(fd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&one, sizeof one);
237 #else
238 (void)fd;
239 #endif
240 168 }
241  
242 /* Monotonic where it exists, wall clock where it does not. A deadline
243 * computed off a clock that can step backwards is a timeout that can
244 * fail to fire, so prefer CLOCK_MONOTONIC and say so. */
245 10752 static double cc_now(void) {
246 #ifdef _WIN32
247 LARGE_INTEGER freq, count;
248 QueryPerformanceFrequency(&freq);
249 QueryPerformanceCounter(&count);
250 return (double)count.QuadPart / (double)freq.QuadPart;
251 #else
252 # if defined(CLOCK_MONOTONIC)
253 struct timespec ts;
254 /*
255 # uncoverable branch false
256 # uncoverable condition right count:1 */
257 10752 ▶ if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) 10752 0 10752
258 10752 return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9;
259 # endif
260 {
261 struct timeval tv;
262 0 gettimeofday(&tv, NULL); /* # uncoverable statement */
263 0 return (double)tv.tv_sec + (double)tv.tv_usec / 1e6; /* # uncoverable statement */
264 }
265 #endif
266 }
267  
268 /* The largest path a UNIX socket address can hold, including its NUL.
269 * 104 on the BSDs and macOS, 108 on Linux. This is not a suggestion:
270 * the kernel copies a fixed-size array, so anything longer is silently
271 * truncated by whoever writes it. */
272 #ifndef _WIN32
273 # define CC_SUN_PATH_MAX (sizeof(((struct sockaddr_un *)0)->sun_path))
274 #endif
275  
276 /* Every socket here is non-blocking from the moment it exists, so there
277 * is only one direction to set. 0 on success, -1 on failure. */
278 168 static int cc_set_nonblock(int fd) {
279 #ifdef _WIN32
280 u_long v = 1;
281 return ioctlsocket((SOCKET)fd, FIONBIO, &v) == 0 ? 0 : -1;
282 #else
283 168 int fl = fcntl(fd, F_GETFL, 0);
284 /*
285 # uncoverable branch true
286 # uncoverable condition left count:1 */
287 168 ▶ if (fl < 0) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (fl < 0) return -1; 0 168 168
288 168 return fcntl(fd, F_SETFL, fl | O_NONBLOCK);
289 #endif
290 }
291  
292 /* Wait for readiness with a deadline. Returns 1 ready, 0 timed out,
293 * -1 error. EINTR restarts against the deadline rather than the original
294 * timeout, or a stream of signals turns a 2 second wait into forever.
295 *
296 * Windows waits with select() and not WSAPoll(): before Windows 10 2004
297 * WSAPoll never reports a connect that failed, so a refused connection
298 * sat there until the timeout. select() reports it in the except set,
299 * which counts as ready here - the caller's SO_ERROR or next call then
300 * says what happened. */
301 #ifdef _WIN32
302 static int cc_select(int fd, int for_write, long sec, long usec) {
303 fd_set rd, wr, ex;
304 struct timeval tv;
305 int r;
306 FD_ZERO(&rd); FD_ZERO(&wr); FD_ZERO(&ex);
307 if (for_write) FD_SET((SOCKET)fd, &wr); else FD_SET((SOCKET)fd, &rd);
308 FD_SET((SOCKET)fd, &ex);
309 tv.tv_sec = sec; tv.tv_usec = usec;
310 r = select(0, &rd, &wr, &ex, &tv);
311 if (r == SOCKET_ERROR) { cc_wsa_to_errno(); return -1; }
312 return r > 0 ? 1 : 0;
313 }
314 #endif
315  
316 127 static int cc_wait(int fd, int for_write, double deadline) {
317 24 for (;;) {
318 int ms, r;
319 151 double left = deadline - cc_now();
320 151 ▶ if (left <= 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (left <= 0) return 0; 1 150 151
321 150 ms = (int)(left * 1000);
322 150 ▶ if (ms < 1) ms = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (ms < 1) ms = 1; 1 149 150
323 #ifdef _WIN32
324 r = cc_select(fd, for_write, ms / 1000, (ms % 1000) * 1000);
325 #else
326 {
327 struct pollfd p;
328 150 p.fd = fd;
329 150 ▶ p.events = for_write ? POLLOUT : POLLIN;
Branch: 1 branch
BranchTrueFalseTotal
p.events = for_write ? POLLOUT : POLLIN; 19 131 150
330 150 p.revents = 0;
331 150 r = poll(&p, 1, ms);
332 }
333 #endif
334 150 ▶ if (r > 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (r > 0) return 1; 118 32 150
335 32 ▶ if (r == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (r == 0) return 0; 8 24 32
336 /*
337 # uncoverable branch false
338 # uncoverable condition right count:1 */
339 24 ▶ if (errno == EINTR) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (errno == EINTR) continue; 24 0 24
340 0 return -1; /* # uncoverable statement */
341 }
342 }
343  
344 /* Is there something to read right now? Never waits. A hangup or an
345 * error counts: either way the next read has an answer, and with POLLIN
346 * the only event asked for, those are the only things poll can report. */
347 24804 static int cc_readable_now(int fd) {
348 #ifdef _WIN32
349 return cc_select(fd, 0, 0, 0) > 0 ? 1 : 0;
350 #else
351 struct pollfd p;
352 24804 p.fd = fd; p.events = POLLIN; p.revents = 0;
353 24804 return poll(&p, 1, 0) > 0 ? 1 : 0;
354 #endif
355 }
356  
357 #endif /* CLAMD_COMPAT_H */
« t/abi_consumer/Makefile.PL ↑ Summary blib/arch/ClamAV/Clamd/Install/Files.pm »