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
| | /*
* Copyright (C) 2012-2013, Eric Wong <normalperson@yhbt.net>
* License: GPLv3 or later (see COPYING for details)
*/
#include "cmogstored.h"
/*
* check for POLLERR/POLLHUP on the socket to see if it died while
* we were in the queue. This can save us from expensive processing.
*/
bool mog_socket_alive(struct mog_fd *mfd)
{
static const short dead_events = POLLERR | POLLHUP;
struct pollfd fds = { .fd = mfd->fd, .events = POLLIN };
int rc;
int nbytes;
do {
rc = poll(&fds, 1, 0);
} while (rc < 0 && errno == EINTR);
if (rc == 1) {
if (fds.revents & POLLIN) {
CHECK(int, 0, ioctl(mfd->fd, FIONREAD, &nbytes));
return (nbytes <= 0) ? false : true;
}
return (fds.revents & dead_events) ? false : true;
}
if (rc == 0)
return true;
assert(rc < 0 && "poll returned unexpected value");
if (errno == ENOMEM || errno == EAGAIN)
return false; /* kernel is in trouble, abort */
assert(0 && "poll usage bug?");
return false;
}
|