2 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
4 * This copyrighted material is made available to anyone wishing to use,
5 * modify, copy, or redistribute it subject to the terms and conditions
6 * of the GNU Lesser General Public License v.2.1.
8 * You should have received a copy of the GNU Lesser General Public License
9 * along with this program; if not, write to the Free Software Foundation,
10 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 struct link_callback {
23 int (*callback)(void *data);
25 struct link_callback *next;
28 static unsigned used_pfds = 0;
29 static unsigned free_pfds = 0;
30 static struct pollfd *pfds = NULL;
31 static struct link_callback *callbacks = NULL;
33 int links_register(int fd, const char *name, int (*callback)(void *data), void *data)
36 struct link_callback *lc;
38 for (i = 0; i < used_pfds; i++) {
39 if (fd == pfds[i].fd) {
40 LOG_ERROR("links_register: Duplicate file descriptor");
45 lc = malloc(sizeof(*lc));
52 lc->callback = callback;
56 tmp = realloc(pfds, sizeof(struct pollfd) * ((used_pfds*2) + 1));
63 free_pfds = used_pfds + 1;
67 pfds[used_pfds].fd = fd;
68 pfds[used_pfds].events = POLLIN;
69 pfds[used_pfds].revents = 0;
74 LOG_DBG("Adding %s/%d", lc->name, lc->fd);
75 LOG_DBG(" used_pfds = %u, free_pfds = %u",
76 used_pfds, free_pfds);
81 int links_unregister(int fd)
84 struct link_callback *p, *c;
86 for (i = 0; i < used_pfds; i++)
87 if (fd == pfds[i].fd) {
88 /* entire struct is copied (overwritten) */
89 pfds[i] = pfds[used_pfds - 1];
94 for (p = NULL, c = callbacks; c; p = c, c = c->next)
96 LOG_DBG("Freeing up %s/%d", c->name, c->fd);
97 LOG_DBG(" used_pfds = %u, free_pfds = %u",
98 used_pfds, free_pfds);
110 int links_monitor(void)
115 for (i = 0; i < used_pfds; i++) {
119 r = poll(pfds, used_pfds, -1);
124 /* FIXME: handle POLLHUP */
125 for (i = 0; i < used_pfds; i++)
126 if (pfds[i].revents & POLLIN) {
127 LOG_DBG("Data ready on %d", pfds[i].fd);
129 /* FIXME: Add this back return 1;*/
136 int links_issue_callbacks(void)
139 struct link_callback *lc;
141 for (i = 0; i < used_pfds; i++)
142 if (pfds[i].revents & POLLIN)
143 for (lc = callbacks; lc; lc = lc->next)
144 if (pfds[i].fd == lc->fd) {
145 LOG_DBG("Issuing callback on %s/%d",
147 lc->callback(lc->data);