Magellan Linux

Contents of /trunk/hal/patches/hal-0.5.10-rework-handling-of-data-from-udevinfo-for-more-effic.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 597 - (show annotations) (download)
Mon May 19 19:05:19 2008 UTC (15 years, 11 months ago) by niro
File size: 9791 byte(s)
-gentoo patches

1 From 2fb4d7282f73972294d7d4f1b83a3dc35f7d6f5c Mon Sep 17 00:00:00 2001
2 From: Rob Taylor <rob.taylor@codethink.co.uk>
3 Date: Mon, 19 Nov 2007 16:18:32 +0000
4 Subject: [PATCH] rework handling of data from udevinfo for more efficient memory usage
5
6 This patch reworks the handling of the output of udevinfo so that the
7 output is parsed into an intermediatary form (with a hash table of
8 sysfpath to structures pointing directly into the output as captured).
9 This is converted into HotplugEvents one at a time when needed by
10 coldplug_get_hotplug_event.
11 ---
12 hald/linux/coldplug.c | 175 ++++++++++++++++++++++++++++++++-----------------
13 1 files changed, 115 insertions(+), 60 deletions(-)
14
15 diff --git a/hald/linux/coldplug.c b/hald/linux/coldplug.c
16 index 3ad7199..dbc1830 100644
17 --- a/hald/linux/coldplug.c
18 +++ b/hald/linux/coldplug.c
19 @@ -55,23 +55,107 @@ struct sysfs_device {
20 static GHashTable *sysfs_to_udev_map;
21 static GSList *device_list;
22 static char dev_root[HAL_PATH_MAX];
23 +static gchar *udevinfo_stdout = NULL;
24
25 -static void hotplug_event_free (HotplugEvent *ev)
26 +typedef struct _UdevInfo UdevInfo;
27 +
28 +struct _UdevInfo
29 +{
30 + const char *sysfs_path;
31 + const char *device_file;
32 + const char *vendor;
33 + const char *model;
34 + const char *revision;
35 + const char *serial;
36 + const char *fsusage;
37 + const char *fstype;
38 + const char *fsversion;
39 + const char *fsuuid;
40 + const char *fslabel;
41 +};
42 +
43 +static void udev_info_free (gpointer data)
44 +{
45 + g_slice_free(UdevInfo, data);
46 +}
47 +
48 +
49 +static HotplugEvent*
50 +udev_info_to_hotplug_event (const UdevInfo *info)
51 {
52 - g_slice_free(HotplugEvent, ev);
53 + HotplugEvent *hotplug_event;
54 + gchar *str;
55 +
56 + hotplug_event = g_slice_new0 (HotplugEvent);
57 + g_strlcpy (hotplug_event->sysfs.sysfs_path, "/sys", sizeof(hotplug_event->sysfs.sysfs_path));
58 + g_strlcat (hotplug_event->sysfs.sysfs_path, info->sysfs_path, sizeof(hotplug_event->sysfs.sysfs_path));
59 +
60 + HAL_INFO(("creating HotplugEvent for %s", hotplug_event->sysfs.sysfs_path));
61 +
62 + if (info->device_file) {
63 + g_snprintf (hotplug_event->sysfs.device_file, sizeof(hotplug_event->sysfs.device_file),
64 + "%s/%s", dev_root, info->device_file);
65 + HAL_INFO(("with device file %s", hotplug_event->sysfs.device_file));
66 + }
67 + if ((str = hal_util_strdup_valid_utf8(info->vendor)) != NULL) {
68 + g_strlcpy (hotplug_event->sysfs.vendor, str, sizeof(hotplug_event->sysfs.vendor));
69 + g_free (str);
70 + }
71 +
72 + if ((str = hal_util_strdup_valid_utf8(info->model)) != NULL) {
73 + g_strlcpy (hotplug_event->sysfs.model, str, sizeof(hotplug_event->sysfs.model));
74 + g_free (str);
75 + }
76 +
77 + if ((str = hal_util_strdup_valid_utf8(info->revision)) != NULL) {
78 + g_strlcpy (hotplug_event->sysfs.revision, str, sizeof(hotplug_event->sysfs.revision));
79 + g_free (str);
80 + }
81 +
82 + if ((str = hal_util_strdup_valid_utf8(info->serial)) != NULL) {
83 + g_strlcpy (hotplug_event->sysfs.serial, str, sizeof(hotplug_event->sysfs.serial));
84 + g_free (str);
85 + }
86 +
87 + if ((str = hal_util_strdup_valid_utf8(info->fsusage)) != NULL) {
88 + g_strlcpy (hotplug_event->sysfs.fsusage, str, sizeof(hotplug_event->sysfs.fsusage));
89 + g_free (str);
90 + }
91 +
92 + if ((str = hal_util_strdup_valid_utf8(info->fstype)) != NULL) {
93 + g_strlcpy (hotplug_event->sysfs.fstype, str, sizeof(hotplug_event->sysfs.fstype));
94 + g_free (str);
95 + }
96 +
97 + if ((str = hal_util_strdup_valid_utf8(info->fsversion)) != NULL) {
98 + g_strlcpy (hotplug_event->sysfs.fsversion, str, sizeof(hotplug_event->sysfs.fsversion));
99 + g_free (str);
100 + }
101 +
102 + if ((str = hal_util_strdup_valid_utf8(info->fsuuid)) != NULL) {
103 + g_strlcpy (hotplug_event->sysfs.fsuuid, str, sizeof(hotplug_event->sysfs.fsuuid));
104 + g_free (str);
105 + }
106 +
107 + if ((str = hal_util_strdup_valid_utf8(info->fslabel)) != NULL) {
108 + g_strlcpy (hotplug_event->sysfs.fslabel, str, sizeof(hotplug_event->sysfs.fslabel));
109 + g_free (str);
110 + }
111 +
112 + return hotplug_event;
113 }
114
115 +
116 static gboolean
117 hal_util_init_sysfs_to_udev_map (void)
118 {
119 char *udevdb_export_argv[] = { "/usr/bin/udevinfo", "-e", NULL };
120 char *udevroot_argv[] = { "/usr/bin/udevinfo", "-r", NULL };
121 - char *udevinfo_stdout;
122 int udevinfo_exitcode;
123 - HotplugEvent *hotplug_event = NULL;
124 + UdevInfo *info = NULL;
125 char *p;
126
127 - sysfs_to_udev_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, hotplug_event_free);
128 + sysfs_to_udev_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, udev_info_free);
129
130 /* get udevroot */
131 if (g_spawn_sync ("/", udevroot_argv, NULL, 0, NULL, NULL,
132 @@ -114,7 +198,6 @@ hal_util_init_sysfs_to_udev_map (void)
133 p = udevinfo_stdout;
134 while (p[0] != '\0') {
135 char *line, *end;
136 - gchar *str;
137
138 /* get line, terminate and move to next line */
139 line = p;
140 @@ -126,79 +209,49 @@ hal_util_init_sysfs_to_udev_map (void)
141
142 /* insert device */
143 if (line[0] == '\0') {
144 - if (hotplug_event != NULL) {
145 - g_hash_table_insert (sysfs_to_udev_map, g_strdup (hotplug_event->sysfs.sysfs_path), hotplug_event);
146 - HAL_INFO (("found (udevdb export) '%s' -> '%s'",
147 - hotplug_event->sysfs.sysfs_path, hotplug_event->sysfs.device_file));
148 - hotplug_event = NULL;
149 + if (info != NULL) {
150 + g_hash_table_insert (sysfs_to_udev_map, g_strdup_printf ("/sys%s", info->sysfs_path), info);
151 + HAL_INFO (("found (udevdb export) '/sys%s' -> '%s/%s'",
152 + info->sysfs_path, dev_root, info->device_file));
153 + info = NULL;
154 }
155 continue;
156 }
157
158 /* new device */
159 if (strncmp(line, "P: ", 3) == 0) {
160 - hotplug_event = g_slice_new0 (HotplugEvent);
161 - g_strlcpy (hotplug_event->sysfs.sysfs_path, "/sys", sizeof(hotplug_event->sysfs.sysfs_path));
162 - g_strlcat (hotplug_event->sysfs.sysfs_path, &line[3], sizeof(hotplug_event->sysfs.sysfs_path));
163 + info = g_slice_new0 (UdevInfo);
164 + info->sysfs_path = &line[3];
165 continue;
166 }
167
168 /* only valid if we have an actual device */
169 - if (hotplug_event == NULL)
170 + if (info == NULL)
171 continue;
172
173 if (strncmp(line, "N: ", 3) == 0) {
174 - g_snprintf (hotplug_event->sysfs.device_file, sizeof(hotplug_event->sysfs.device_file),
175 - "%s/%s", dev_root, &line[3]);
176 + info->device_file = &line[3];
177 } else if (strncmp(line, "E: ID_VENDOR=", 13) == 0) {
178 - if ((str = hal_util_strdup_valid_utf8(&line[13])) != NULL) {
179 - g_strlcpy (hotplug_event->sysfs.vendor, str, sizeof(hotplug_event->sysfs.vendor));
180 - g_free (str);
181 - }
182 + info->vendor = &line[13];
183 } else if (strncmp(line, "E: ID_MODEL=", 12) == 0) {
184 - if ((str = hal_util_strdup_valid_utf8(&line[12])) != NULL) {
185 - g_strlcpy (hotplug_event->sysfs.model, str, sizeof(hotplug_event->sysfs.model));
186 - g_free (str);
187 - }
188 + info->model = &line[12];
189 } else if (strncmp(line, "E: ID_REVISION=", 15) == 0) {
190 - if ((str = hal_util_strdup_valid_utf8(&line[15])) != NULL) {
191 - g_strlcpy (hotplug_event->sysfs.revision, str, sizeof(hotplug_event->sysfs.revision));
192 - g_free (str);
193 - }
194 + info->revision = &line[15];
195 } else if (strncmp(line, "E: ID_SERIAL=", 13) == 0) {
196 - if ((str = hal_util_strdup_valid_utf8(&line[13])) != NULL) {
197 - g_strlcpy (hotplug_event->sysfs.serial, str, sizeof(hotplug_event->sysfs.serial));
198 - g_free (str);
199 - }
200 + info->serial = &line[13];
201 } else if (strncmp(line, "E: ID_FS_USAGE=", 15) == 0) {
202 - if ((str = hal_util_strdup_valid_utf8(&line[15])) != NULL) {
203 - g_strlcpy (hotplug_event->sysfs.fsusage, str, sizeof(hotplug_event->sysfs.fsusage));
204 - g_free (str);
205 - }
206 + info->fsusage = &line[15];
207 } else if (strncmp(line, "E: ID_FS_TYPE=", 14) == 0) {
208 - if ((str = hal_util_strdup_valid_utf8(&line[14])) != NULL) {
209 - g_strlcpy (hotplug_event->sysfs.fstype, str, sizeof(hotplug_event->sysfs.fstype));
210 - g_free (str);
211 - }
212 + info->fstype = &line[14];
213 } else if (strncmp(line, "E: ID_FS_VERSION=", 17) == 0) {
214 - if ((str = hal_util_strdup_valid_utf8(&line[17])) != NULL) {
215 - g_strlcpy (hotplug_event->sysfs.fsversion, str, sizeof(hotplug_event->sysfs.fsversion));
216 - g_free (str);
217 - }
218 + info->fsversion = &line[17];
219 } else if (strncmp(line, "E: ID_FS_UUID=", 14) == 0) {
220 - if ((str = hal_util_strdup_valid_utf8(&line[14])) != NULL) {
221 - g_strlcpy (hotplug_event->sysfs.fsuuid, str, sizeof(hotplug_event->sysfs.fsuuid));
222 - g_free (str);
223 - }
224 + info->fsuuid = &line[14];
225 } else if (strncmp(line, "E: ID_FS_LABEL=", 15) == 0) {
226 - if ((str = hal_util_strdup_valid_utf8(&line[15])) != NULL) {
227 - g_strlcpy (hotplug_event->sysfs.fslabel, str, sizeof(hotplug_event->sysfs.fslabel));
228 - g_free (str);
229 - }
230 + info->fslabel = &line[15];
231 }
232 }
233
234 - g_free(udevinfo_stdout);
235 return TRUE;
236
237 error:
238 @@ -211,19 +264,20 @@ error:
239 static HotplugEvent
240 *coldplug_get_hotplug_event(const gchar *sysfs_path, const gchar *subsystem, HotplugEventType type)
241 {
242 - HotplugEvent *hotplug_event, *hotplug_event_udev;
243 + HotplugEvent *hotplug_event;
244 const char *pos;
245 gchar path[HAL_PATH_MAX];
246 struct stat statbuf;
247 -
248 - hotplug_event = g_slice_new0 (HotplugEvent);
249 + UdevInfo *info;
250
251 /* lookup if udev has something stored in its database */
252 - hotplug_event_udev = (HotplugEvent *) g_hash_table_lookup (sysfs_to_udev_map, sysfs_path);
253 - if (hotplug_event_udev != NULL) {
254 - memcpy(hotplug_event, hotplug_event_udev, sizeof(HotplugEvent));
255 + info = (UdevInfo*) g_hash_table_lookup (sysfs_to_udev_map, sysfs_path);
256 + if (info) {
257 + hotplug_event = udev_info_to_hotplug_event (info);
258 HAL_INFO (("new event (dev node from udev) '%s' '%s'", hotplug_event->sysfs.sysfs_path, hotplug_event->sysfs.device_file));
259 } else {
260 + hotplug_event = g_slice_new0 (HotplugEvent);
261 +
262 /* device is not in udev database */
263 g_strlcpy(hotplug_event->sysfs.sysfs_path, sysfs_path, sizeof(hotplug_event->sysfs.sysfs_path));
264
265 @@ -543,6 +597,7 @@ coldplug_synthesize_events (void)
266 }
267
268 g_hash_table_destroy (sysfs_to_udev_map);
269 + g_free (udevinfo_stdout);
270 return TRUE;
271
272 error:
273 --
274 1.5.3.7
275