4 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU Lesser General Public License v.2.1.
10 * You should have received a copy of the GNU Lesser General Public License
11 * along with this program; if not, write to the Free Software Foundation,
12 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 struct link_callback
{
24 int (*callback
)(void *data
);
26 struct link_callback
*next
;
29 static int used_pfds
= 0;
30 static int free_pfds
= 0;
31 static struct pollfd
*pfds
= NULL
;
32 static struct link_callback
*callbacks
= NULL
;
34 int links_register(int fd
, char *name
, int (*callback
)(void *data
), void *data
)
37 struct link_callback
*lc
;
39 for (i
= 0; i
< used_pfds
; i
++) {
40 if (fd
== pfds
[i
].fd
) {
41 LOG_ERROR("links_register: Duplicate file descriptor");
46 lc
= malloc(sizeof(*lc
));
53 lc
->callback
= callback
;
57 tmp
= realloc(pfds
, sizeof(struct pollfd
) * ((used_pfds
*2) + 1));
64 free_pfds
= used_pfds
+ 1;
68 pfds
[used_pfds
].fd
= fd
;
69 pfds
[used_pfds
].events
= POLLIN
;
70 pfds
[used_pfds
].revents
= 0;
75 LOG_DBG("Adding %s/%d", lc
->name
, lc
->fd
);
76 LOG_DBG(" used_pfds = %d, free_pfds = %d",
77 used_pfds
, free_pfds
);
82 int links_unregister(int fd
)
85 struct link_callback
*p
, *c
;
87 for (i
= 0; i
< used_pfds
; i
++)
88 if (fd
== pfds
[i
].fd
) {
89 /* entire struct is copied (overwritten) */
90 pfds
[i
] = pfds
[used_pfds
- 1];
95 for (p
= NULL
, c
= callbacks
; c
; p
= c
, c
= c
->next
)
97 LOG_DBG("Freeing up %s/%d", c
->name
, c
->fd
);
98 LOG_DBG(" used_pfds = %d, free_pfds = %d",
99 used_pfds
, free_pfds
);
111 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
);