Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / daemons / cmirrord / link_mon.c
blob6f10c7e5226b31d0d367924f4b4dc3bd70082b65
1 /* $NetBSD$ */
3 /*
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
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <poll.h>
18 #include "logging.h"
20 struct link_callback {
21 int fd;
22 char *name;
23 void *data;
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)
36 int i;
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");
42 return -EINVAL;
46 lc = malloc(sizeof(*lc));
47 if (!lc)
48 return -ENOMEM;
50 lc->fd = fd;
51 lc->name = name;
52 lc->data = data;
53 lc->callback = callback;
55 if (!free_pfds) {
56 struct pollfd *tmp;
57 tmp = realloc(pfds, sizeof(struct pollfd) * ((used_pfds*2) + 1));
58 if (!tmp) {
59 free(lc);
60 return -ENOMEM;
63 pfds = tmp;
64 free_pfds = used_pfds + 1;
67 free_pfds--;
68 pfds[used_pfds].fd = fd;
69 pfds[used_pfds].events = POLLIN;
70 pfds[used_pfds].revents = 0;
71 used_pfds++;
73 lc->next = callbacks;
74 callbacks = lc;
75 LOG_DBG("Adding %s/%d", lc->name, lc->fd);
76 LOG_DBG(" used_pfds = %d, free_pfds = %d",
77 used_pfds, free_pfds);
79 return 0;
82 int links_unregister(int fd)
84 int i;
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];
91 used_pfds--;
92 free_pfds++;
95 for (p = NULL, c = callbacks; c; p = c, c = c->next)
96 if (fd == c->fd) {
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);
100 if (p)
101 p->next = c->next;
102 else
103 callbacks = c->next;
104 free(c);
105 break;
108 return 0;
111 int links_monitor(void)
113 int i, r;
115 for (i = 0; i < used_pfds; i++) {
116 pfds[i].revents = 0;
119 r = poll(pfds, used_pfds, -1);
120 if (r <= 0)
121 return r;
123 r = 0;
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;*/
130 r++;
133 return r;
136 int links_issue_callbacks(void)
138 int i;
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",
146 lc->name, lc->fd);
147 lc->callback(lc->data);
148 break;
150 return 0;