Bump to version 2.0.22
[platform/upstream/acpid.git] / kacpimon / connection_list.c
1 /*
2  *  connection_list.c - ACPI daemon connection list
3  *
4  *  Copyright (C) 2008, Ted Felix (www.tedfelix.com)
5  *
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.
10  *
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.
15  *
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
19  *
20  *  Tabs at 4
21  */
22
23 #include <unistd.h>
24 #include <stdio.h>
25
26 #include "connection_list.h"
27
28 #define max(a, b)  (((a)>(b))?(a):(b))
29
30 /*---------------------------------------------------------------*/
31 /* private objects */
32
33 #define MAX_CONNECTIONS 20
34
35 static struct connection connection_list[MAX_CONNECTIONS];
36
37 static int nconnections = 0;
38
39 /* fd_set containing all the fd's that come in */
40 static fd_set allfds;
41
42 /* highest fd that is opened */
43 /* (-2 + 1) causes select() to return immediately */
44 static int highestfd = -2;
45
46 /*---------------------------------------------------------------*/
47 /* public functions */
48
49 void
50 add_connection(struct connection *p)
51 {
52         if (nconnections < 0)
53                 return;
54         if (nconnections >= MAX_CONNECTIONS) {
55                 printf("add_connection(): Too many connections.\n");
56                 return;
57         }
58
59         if (nconnections == 0)
60                 FD_ZERO(&allfds);
61         
62         /* add the connection to the connection list */
63         connection_list[nconnections] = *p;
64         ++nconnections;
65         
66         /* add to the fd set */
67         FD_SET(p->fd, &allfds);
68         highestfd = max(highestfd, p->fd);
69 }
70
71 /*---------------------------------------------------------------*/
72
73 struct connection *
74 find_connection(int fd)
75 {
76         int i;
77
78         /* for each connection */
79         for (i = 0; i < nconnections; ++i) {
80                 /* if the file descriptors match, return the connection */
81                 if (connection_list[i].fd == fd)
82                         return &connection_list[i];
83         }
84
85         return NULL;
86 }
87
88 /*---------------------------------------------------------------*/
89
90 int 
91 get_number_of_connections(void)
92 {
93         return nconnections;
94 }
95
96 /*---------------------------------------------------------------*/
97
98 struct connection *
99 get_connection(int i)
100 {
101         if (i < 0  ||  i >= nconnections)
102                 return NULL;
103
104         return &connection_list[i];
105 }
106
107 /*---------------------------------------------------------------*/
108
109 const fd_set *
110 get_fdset(void)
111 {
112         return &allfds;
113 }
114
115 /*---------------------------------------------------------------*/
116
117 int
118 get_highestfd(void)
119 {
120         return highestfd;
121 }