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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/***************************************************************************
*
* up-devd.c : process devd events
*
* Copyright (C) 2006, 2007 Joe Marcus Clarke <marcus@FreeBSD.org>
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
**************************************************************************/
#include "config.h"
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "up-backend.h"
#include "up-backend-acpi.h"
#include "up-devd.h"
#define UP_DEVD_SOCK_PATH "/var/run/devd.pipe"
#define UP_DEVD_EVENT_NOTIFY '!'
#define UP_DEVD_EVENT_ADD '+'
#define UP_DEVD_EVENT_REMOVE '-'
#define UP_DEVD_EVENT_NOMATCH '?'
static UpDevdHandler *handlers[] = {
&up_backend_acpi_devd_handler,
};
static gboolean up_devd_inited = FALSE;
#if 0
static GHashTable *
up_devd_parse_params (const gchar *str)
{
GHashTable *params;
gchar **pairs;
gint i;
g_return_val_if_fail (str != NULL, FALSE);
params = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
pairs = g_strsplit(str, " ", 0);
for (i = 0; pairs[i]; i++) {
gchar *equal;
equal = strchr (pairs[i], '=');
g_hash_table_insert (params,
equal ? g_strndup(pairs[i], equal - pairs[i]) : g_strdup(pairs[i]),
equal ? g_strdup(equal + 1) : NULL);
}
g_strfreev (pairs);
return params;
}
#endif
static gboolean
up_devd_parse_notify (const gchar *event,
gchar **system,
gchar **subsystem,
gchar **type,
gchar **data)
{
gchar **items;
gboolean status = FALSE;
/*
!system=IFNET subsystem=bge0 type=LINK_DOWN
*/
g_return_val_if_fail(event != NULL, FALSE);
g_return_val_if_fail(system != NULL, FALSE);
g_return_val_if_fail(subsystem != NULL, FALSE);
g_return_val_if_fail(type != NULL, FALSE);
g_return_val_if_fail(data != NULL, FALSE);
items = g_strsplit(event, " ", 0);
if (g_strv_length(items) < 3)
goto end;
if (! g_str_has_prefix(items[0], "system=") ||
! g_str_has_prefix(items[1], "subsystem=") ||
! g_str_has_prefix(items[2], "type="))
goto end;
*system = g_strdup(items[0] + 7);
*subsystem = g_strdup(items[1] + 10);
*type = g_strdup(items[2] + 5);
*data = g_strdup(items[3]); /* may be NULL */
status = TRUE;
end:
g_strfreev(items);
return status;
}
static void
up_devd_process_notify_event (UpBackend *backend,
const gchar *system,
const gchar *subsystem,
const gchar *type,
const gchar *data)
{
gint i;
g_return_if_fail(backend != NULL);
g_return_if_fail(system != NULL);
g_return_if_fail(subsystem != NULL);
g_return_if_fail(type != NULL);
g_return_if_fail(data != NULL);
for (i = 0; i < (int) G_N_ELEMENTS(handlers); i++)
if (handlers[i]->notify && handlers[i]->notify (backend, system, subsystem, type, data))
return;
/* no default action */
}
static void
up_devd_process_event (const gchar *event, gpointer user_data)
{
UpBackend *backend;
g_return_if_fail(event != NULL);
backend = UP_BACKEND(user_data);
g_debug("received devd event: '%s'", event);
switch (event[0]) {
case UP_DEVD_EVENT_ADD:
case UP_DEVD_EVENT_REMOVE:
/* Do nothing as we don't currently hotplug ACPI devices. */
return;
case UP_DEVD_EVENT_NOTIFY: {
gchar *system;
gchar *subsystem;
gchar *type;
gchar *data;
if (!up_devd_parse_notify (event + 1, &system, &subsystem, &type, &data))
goto malformed;
up_devd_process_notify_event (backend, system, subsystem, type, data);
g_free (system);
g_free (subsystem);
g_free (type);
g_free (data);
}
return;
case UP_DEVD_EVENT_NOMATCH:
/* Do nothing. */
return;
}
malformed:
g_warning("malformed devd event: %s", event);
}
static gboolean
up_devd_event_cb (GIOChannel *source, GIOCondition condition,
gpointer user_data)
{
gchar *event;
gsize terminator;
GIOStatus status;
status = g_io_channel_read_line(source, &event, NULL, &terminator, NULL);
if (status == G_IO_STATUS_NORMAL) {
event[terminator] = 0;
up_devd_process_event(event, user_data);
g_free(event);
} else if (status == G_IO_STATUS_AGAIN) {
up_devd_init (UP_BACKEND(user_data));
if (up_devd_inited) {
int fd;
fd = g_io_channel_unix_get_fd (source);
g_io_channel_shutdown (source, FALSE, NULL);
close (fd);
return FALSE;
}
}
return TRUE;
}
void
up_devd_init (UpBackend *backend)
{
int event_fd;
struct sockaddr_un addr;
event_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if (event_fd < 0) {
g_warning("failed to create event socket: '%s'", g_strerror(errno));
up_devd_inited = FALSE;
return;
}
addr.sun_family = AF_UNIX;
strncpy (addr.sun_path, UP_DEVD_SOCK_PATH, sizeof(addr.sun_path));
if (connect (event_fd, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
GIOChannel *channel;
channel = g_io_channel_unix_new (event_fd);
g_io_add_watch (channel, G_IO_IN, up_devd_event_cb, backend);
g_io_channel_unref (channel);
up_devd_inited = TRUE;
} else {
g_warning ("failed to connect to %s: '%s'", UP_DEVD_SOCK_PATH,
g_strerror(errno));
close (event_fd);
up_devd_inited = FALSE;
}
}
|