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
|
diff --git a/kacpimon/connection_list.c b/kacpimon/connection_list.c
index 9b0b0a8..f228186 100644
--- a/kacpimon/connection_list.c
+++ b/kacpimon/connection_list.c
@@ -22,6 +22,7 @@
#include <unistd.h>
#include <stdio.h>
+#include <stdlib.h>
#include "connection_list.h"
@@ -30,9 +31,9 @@
/*---------------------------------------------------------------*/
/* private objects */
-#define MAX_CONNECTIONS 100
+static int capacity = 0;
-static struct connection connection_list[MAX_CONNECTIONS];
+static struct connection *connection_list = NULL;
static int nconnections = 0;
@@ -51,9 +52,19 @@ add_connection(struct connection *p)
{
if (nconnections < 0)
return;
- if (nconnections >= MAX_CONNECTIONS) {
- printf("add_connection(): Too many connections.\n");
- return;
+
+ /* if the list is full, allocate more space */
+ if (nconnections >= capacity) {
+ /* no more than 1024 */
+ if (capacity > 1024) {
+ printf("add_connection(): Too many connections.\n");
+ return;
+ }
+
+ /* another 20 */
+ capacity += 20;
+ connection_list =
+ realloc(connection_list, sizeof(struct connection) * capacity);
}
if (nconnections == 0)
@@ -70,6 +81,30 @@ add_connection(struct connection *p)
/*---------------------------------------------------------------*/
+void
+delete_all_connections(void)
+{
+ int i = 0;
+
+ /* For each connection */
+ for (i = 0; i <= get_number_of_connections(); ++i)
+ {
+ struct connection *p;
+
+ p = get_connection(i);
+
+ /* If this connection is invalid, try the next. */
+ if (p == 0)
+ continue;
+
+ close(p -> fd);
+ }
+ free(connection_list);
+ connection_list = NULL;
+}
+
+/*---------------------------------------------------------------*/
+
struct connection *
find_connection(int fd)
{
diff --git a/kacpimon/connection_list.h b/kacpimon/connection_list.h
index 1d037cf..a787637 100644
--- a/kacpimon/connection_list.h
+++ b/kacpimon/connection_list.h
@@ -56,4 +56,7 @@ extern const fd_set *get_fdset(void);
/* get the highest fd that was added to the list */
extern int get_highestfd(void);
+/* delete all connections, closing the fds */
+extern void delete_all_connections(void);
+
#endif /* CONNECTION_LIST_H__ */
diff --git a/kacpimon/kacpimon.c b/kacpimon/kacpimon.c
index 1ddb9aa..253d270 100644
--- a/kacpimon/kacpimon.c
+++ b/kacpimon/kacpimon.c
@@ -164,27 +164,6 @@ static void monitor(void)
// ---------------------------------------------------------------
-static void close_all(void)
-{
- int i = 0;
-
- /* For each connection */
- for (i = 0; i <= get_number_of_connections(); ++i)
- {
- struct connection *p;
-
- p = get_connection(i);
-
- /* If this connection is invalid, try the next. */
- if (p == 0)
- continue;
-
- close(p -> fd);
- }
-}
-
-// ---------------------------------------------------------------
-
int main(void)
{
printf("Kernel ACPI Event Monitor...\n");
@@ -199,7 +178,7 @@ int main(void)
printf("Closing files...\n");
- close_all();
+ delete_all_connections();
printf("Goodbye\n");
|