2 * connection_list.c - ACPI daemon connection list
4 * Copyright (C) 2008, Ted Felix (www.tedfelix.com)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "connection_list.h"
33 #define max(a, b) (((a)>(b))?(a):(b))
35 /*---------------------------------------------------------------*/
38 #define MAX_CONNECTIONS 20
40 static struct connection connection_list[MAX_CONNECTIONS];
42 static int nconnections = 0;
44 /* fd_set containing all the fd's that come in */
47 /* highest fd that is opened */
48 /* (-2 + 1) causes select() to return immediately */
49 static int highestfd = -2;
51 /*---------------------------------------------------------------*/
52 /* public functions */
55 add_connection(struct connection *p)
59 if (nconnections >= MAX_CONNECTIONS) {
60 acpid_log(LOG_ERR, "Too many connections.");
61 /* ??? This routine should return -1 in this situation so that */
62 /* callers can clean up any open fds and whatnot. */
66 if (nconnections == 0)
69 /* add the connection to the connection list */
70 connection_list[nconnections] = *p;
73 /* add to the fd set */
74 FD_SET(p->fd, &allfds);
75 highestfd = max(highestfd, p->fd);
78 /*---------------------------------------------------------------*/
81 delete_connection(int fd)
87 /* remove from the fd set */
90 for (i = 0; i < nconnections; ++i) {
91 /* if the file descriptors match, delete the connection */
92 if (connection_list[i].fd == fd) {
93 free(connection_list[i].pathname);
96 connection_list[i] = connection_list[nconnections];
102 /* prepare for recalculation of highestfd */
105 /* recalculate highestfd */
106 for (i = 0; i < nconnections; ++i) {
107 highestfd = max(highestfd, connection_list[i].fd);
111 /*---------------------------------------------------------------*/
114 find_connection(int fd)
118 /* for each connection */
119 for (i = 0; i < nconnections; ++i) {
120 /* if the file descriptors match, return the connection */
121 if (connection_list[i].fd == fd)
122 return &connection_list[i];
128 /*---------------------------------------------------------------*/
131 find_connection_name(char *pathname)
135 /* for each connection */
136 for (i = 0; i < nconnections; ++i) {
137 /* skip null pathnames */
138 if (connection_list[i].pathname == NULL)
141 /* if the pathname matches, return the connection */
142 if (strcmp(connection_list[i].pathname, pathname) == 0)
143 return &connection_list[i];
149 /*---------------------------------------------------------------*/
152 get_number_of_connections()
157 /*---------------------------------------------------------------*/
160 get_connection(int i)
162 if (i < 0 || i >= nconnections)
165 return &connection_list[i];
168 /*---------------------------------------------------------------*/
176 /*---------------------------------------------------------------*/