1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
/*
* connection_list.c - ACPI daemon connection list
*
* Copyright (C) 2008, Ted Felix (www.tedfelix.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Tabs at 4
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "acpid.h"
#include "log.h"
#include "connection_list.h"
#define max(a, b) (((a)>(b))?(a):(b))
/*---------------------------------------------------------------*/
/* private objects */
#define MAX_CONNECTIONS 20
static struct connection connection_list[MAX_CONNECTIONS];
static int nconnections = 0;
/* fd_set containing all the fd's that come in */
static fd_set allfds;
/* highest fd that is opened */
/* (-2 + 1) causes select() to return immediately */
static int highestfd = -2;
/*---------------------------------------------------------------*/
/* public functions */
void
add_connection(struct connection *p)
{
if (nconnections < 0)
return;
if (nconnections >= MAX_CONNECTIONS) {
acpid_log(LOG_ERR, "Too many connections.");
/* ??? This routine should return -1 in this situation so that */
/* callers can clean up any open fds and whatnot. */
return;
}
if (nconnections == 0)
FD_ZERO(&allfds);
/* add the connection to the connection list */
connection_list[nconnections] = *p;
++nconnections;
/* add to the fd set */
FD_SET(p->fd, &allfds);
highestfd = max(highestfd, p->fd);
}
/*---------------------------------------------------------------*/
void
delete_connection(int fd)
{
int i;
close(fd);
/* remove from the fd set */
FD_CLR(fd, &allfds);
for (i = 0; i < nconnections; ++i) {
/* if the file descriptors match, delete the connection */
if (connection_list[i].fd == fd) {
free(connection_list[i].pathname);
--nconnections;
connection_list[i] = connection_list[nconnections];
break;
}
}
/* prepare for recalculation of highestfd */
highestfd = -2;
/* recalculate highestfd */
for (i = 0; i < nconnections; ++i) {
highestfd = max(highestfd, connection_list[i].fd);
}
}
/*---------------------------------------------------------------*/
struct connection *
find_connection(int fd)
{
int i;
/* for each connection */
for (i = 0; i < nconnections; ++i) {
/* if the file descriptors match, return the connection */
if (connection_list[i].fd == fd)
return &connection_list[i];
}
return NULL;
}
/*---------------------------------------------------------------*/
struct connection *
find_connection_name(char *pathname)
{
int i;
/* for each connection */
for (i = 0; i < nconnections; ++i) {
/* skip null pathnames */
if (connection_list[i].pathname == NULL)
continue;
/* if the pathname matches, return the connection */
if (strcmp(connection_list[i].pathname, pathname) == 0)
return &connection_list[i];
}
return NULL;
}
/*---------------------------------------------------------------*/
int
get_number_of_connections()
{
return nconnections;
}
/*---------------------------------------------------------------*/
struct connection *
get_connection(int i)
{
if (i < 0 || i >= nconnections)
return NULL;
return &connection_list[i];
}
/*---------------------------------------------------------------*/
const fd_set *
get_fdset()
{
return &allfds;
}
/*---------------------------------------------------------------*/
int
get_highestfd()
{
return highestfd;
}
|