Nagios
4.5.4
Dev docs for Nagios core and neb-module hackers
Loading...
Searching...
No Matches
lib
nwrite.h
Go to the documentation of this file.
1
#ifndef LIBNAGIOS_NWRITE_H_INCLUDED
2
#define LIBNAGIOS_NWRITE_H_INCLUDED
3
4
/**
5
* @file nwrite.h
6
* @brief Functions that properly handle incomplete write()'s
7
*
8
* Some functions simply use write() to send data through a socket.
9
* These calls are sometimes interrupted, especially in the case of
10
* an overly large buffer. Even though the write() _could_ finish,
11
* the incomplete write is treated as an error. The functions here
12
* properly handle those cases.
13
*
14
* @{
15
*/
16
17
/**
18
* Send data through a socket
19
* This function will send data through a socket and return
20
* the number of bytes written.
21
* @param sock The socket to write to
22
* @param data The data to write
23
* @param lth The length of the data
24
* @param sent The number of bytes written (can be NULL)
25
* @return The number of bytes written or -1 if error
26
*/
27
static
inline
ssize_t nwrite(
int
fd,
const
void
*buf,
size_t
count, ssize_t *written)
28
{
29
/*
30
* Given the API we have to assume (unsigned) size_t 'count' fits into
31
* a (signed) ssize_t because we can't return a larger value.
32
* https://stackoverflow.com/questions/29722999/will-write2-always-write-less-than-or-equal-to-ssize-max
33
*/
34
ssize_t out, tot = 0;
35
36
if
(!buf || count == 0)
37
return
0;
38
39
while
((
size_t
) tot < count) {
40
out = write(fd, (
const
char
*) buf + tot, count - tot);
41
if
(out > 0)
42
tot += out;
43
else
if
(errno == EAGAIN || errno == EINTR)
44
continue
;
45
else
{
46
if
(written)
47
*written = tot;
48
return
out;
49
}
50
}
51
if
(written)
52
*written = tot;
53
return
tot;
54
}
55
56
/** @} */
57
#endif
/* LIBNAGIOS_NWRITE_H_INCLUDED */
Generated by
1.12.0