Skip to content

Commit

Permalink
lib/iobroker: Add output polling to the iobroker's repertoire
Browse files Browse the repository at this point in the history
For some reason, I actually forgot about it, and then in my
hazed daze of tiredness realized that without it it would only
be an "iBroker", and before you know it we'd have to rename
Nagios to something feline and retarded because we happened to
prefix a noun with an I. Now that we've amended the prefix with
an O, and thus completed it, I feel better already!

It's also useful, since it means we can poll (efficiently) to
see which workers are ready to receive input, in case one of
them happens to be a slow reader, suffer from dyslexia or just
doesn't feel like reading when we want it to.

Signed-off-by: Andreas Ericsson <[email protected]>

git-svn-id: https://nagios.svn.sourceforge.net/svnroot/nagios/nagioscore/trunk@2446 5f96b256-904b-4d8d-8c98-d829582c6739
  • Loading branch information
ageric committed Oct 30, 2012
1 parent ab349fb commit fb35578
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
27 changes: 22 additions & 5 deletions lib/iobroker.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

typedef struct {
int fd; /* the file descriptor */
int flags; /* various flags for the buffer */
int events; /* events the caller is interested in */
int (*handler)(int, int, void *); /* where we send data */
void *arg; /* the argument we send to the input handler */
} iobroker_fd;
Expand Down Expand Up @@ -183,8 +183,7 @@ struct iobroker_set *iobroker_create(void)
return NULL;
}


int iobroker_register(iobroker_set *iobs, int fd, void *arg, int (*handler)(int, int, void *))
static int reg_one(iobroker_set *iobs, int fd, int events, void *arg, int (*handler)(int, int, void *))
{
iobroker_fd *s;

Expand All @@ -204,7 +203,7 @@ int iobroker_register(iobroker_set *iobs, int fd, void *arg, int (*handler)(int,
#ifdef IOBROKER_USES_EPOLL
{
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLRDHUP;
ev.events = events;
ev.data.ptr = arg;
ev.data.fd = fd;
if (epoll_ctl(iobs->epfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
Expand All @@ -215,15 +214,33 @@ int iobroker_register(iobroker_set *iobs, int fd, void *arg, int (*handler)(int,

s = calloc(1, sizeof(iobroker_fd));
s->handler = handler;
s->flags = s->flags;
s->fd = fd;
s->arg = arg;
s->events = events;
iobs->iobroker_fds[fd] = s;
iobs->num_fds++;

return 0;
}

int iobroker_register(iobroker_set *iobs, int fd, void *arg, int (*handler)(int, int, void *))
{
#ifdef IOBROKER_USES_EPOLL
return reg_one(iobs, fd, EPOLLIN | EPOLLRDHUP, arg, handler);
#else
return reg_one(iobs, fd, POLLIN, arg, handler);
#endif
}

int iobroker_register_out(iobroker_set *iobs, int fd, void *arg, int (*handler)(int, int, void *))
{
#ifdef IOBROKER_USES_EPOLL
return reg_one(iobs, fd, EPOLLOUT, arg, handler);
#else
return reg_one(iobs, fd, POLLOUT, arg, handler);
#endif
}

int iobroker_is_registered(iobroker_set *iobs, int fd)
{
if (!iobs || fd < 0 || fd > iobs->max_fds || !iobs->iobroker_fds[fd])
Expand Down
14 changes: 14 additions & 0 deletions lib/iobroker.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ extern int iobroker_max_usable_fds(void);
extern int iobroker_register(iobroker_set *iobs, int sd, void *arg, int (*handler)(int, int, void *));


/**
* Register a socket for output polling with the broker
* @note There's no guarantee that *ALL* data is writable just
* because the socket won't block you completely.
*
* @param iobs The socket set to add the socket to.
* @param sd The socket descriptor to add
* @param arg Argument passed to output handler on ready-to-write
* @param handler The function to call when output won't block
*
* @return 0 on success. < 0 on errors
*/
extern int iobroker_register_out(iobroker_set *iobs, int sd, void *arg, int (*handler)(int, int, void *));

/**
* Check if a particular filedescriptor is registered with the iobroker set
* @param[in] iobs The iobroker set the filedescriptor should be member of
Expand Down

0 comments on commit fb35578

Please sign in to comment.