hal/hald/linux2 coldplug.c, 1.14, 1.15 osspec.c, 1.23, 1.24 osspec_linux.h, 1.1, 1.2

David Zeuthen david at freedesktop.org
Wed Apr 27 11:53:41 PDT 2005


Update of /cvs/hal/hal/hald/linux2
In directory gabe:/tmp/cvs-serv10203/hald/linux2

Modified Files:
	coldplug.c osspec.c osspec_linux.h 
Log Message:
2005-04-27  David Zeuthen  <davidz at redhat.com>

	* hald/linux2/osspec_linux.h: Remove prototype for the function
	hal_util_get_device_file().

	* hald/linux2/osspec.c (hal_util_get_udevinfo_path): Remove 
	(hal_util_get_device_file): Remove 

	* hald/linux2/coldplug.c (hal_util_get_udevinfo_path): Move from 
	osspec.c 
	(hal_util_get_sysfs_to_dev_map): New function
	(coldplug_synthesize_events): Get the sysfs->dev map in one go using
	'udevinfo -d' available in udev since at least version 057.

	* configure.in: Bump dbus requirement to 0.33 since the new dbus is
	out.

2005-04-27  David Zeuthen  <davidz at redhat.com>

	* libhal/libhal.c (libhal_device_set_property_helper): Don't leak the
	DBusMessage objects

	* hald/hald_dbus.c (sender_has_privileges): Don't leak the error and
	actually return FALSE if sender is unprivileged. Also fix up spelling.



Index: coldplug.c
===================================================================
RCS file: /cvs/hal/hal/hald/linux2/coldplug.c,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -d -r1.14 -r1.15
--- coldplug.c	16 Mar 2005 20:09:47 -0000	1.14
+++ coldplug.c	27 Apr 2005 18:53:39 -0000	1.15
@@ -52,13 +52,164 @@
 
 #define DMPREFIX "dm-"
 
+
+
+/* Returns the path of the udevinfo program 
+ *
+ * @return                      Path or NULL if udevinfo program is not found
+ */
+static const gchar *
+hal_util_get_udevinfo_path (void)
+{
+	guint i;
+	struct stat s;
+	static gchar *path = NULL;
+	gchar *possible_paths[] = { 
+		"/sbin/udevinfo",
+		"/usr/bin/udevinfo",
+		"/usr/sbin/udevinfo",
+		"/usr/local/sbin/udevinfo"
+	};
+
+	if (path != NULL)
+		return path;
+
+	for (i = 0; i < sizeof (possible_paths) / sizeof (char *); i++) {
+		if (stat (possible_paths[i], &s) == 0 && S_ISREG (s.st_mode)) {
+			path = possible_paths[i];
+			break;
+		}
+	}
+	return path;
+}
+
+static GHashTable *
+hal_util_get_sysfs_to_dev_map (void)
+{
+	GHashTable *sysfs_to_dev_map;
+	char *udevinfo_argv[7] = {NULL, "-d", NULL};
+	char *udevinfo_stdout;
+	char *udevinfo_stderr;
+	int udevinfo_exitcode;
+        char *p;
+	char *q;
+	char *r;
+	int len;
+	char sysfs_path[PATH_MAX + 1];
+	char device_file[PATH_MAX + 1];
+	const char *sysfs_mount_path;
+	gboolean has_more_lines;
+
+	sysfs_mount_path = get_hal_sysfs_path ();
+
+	sysfs_to_dev_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+
+	/* get path to udevinfo */
+	udevinfo_argv[0] = (char *) hal_util_get_udevinfo_path ();
+	if (udevinfo_argv[0] == NULL)
+		goto error;
+
+
+	/* Invoke udevinfo */
+	if (udevinfo_argv[0] == NULL || g_spawn_sync ("/",
+						  udevinfo_argv,
+						  NULL,
+						  0,
+						  NULL,
+						  NULL,
+						  &udevinfo_stdout,
+						  &udevinfo_stderr,
+						  &udevinfo_exitcode,
+						  NULL) != TRUE) {
+		HAL_ERROR (("Couldn't invoke %s", udevinfo_argv[0]));
+		goto error;
+	}
+
+	if (udevinfo_exitcode != 0) {
+		HAL_ERROR (("%s returned %d", udevinfo_argv[0], udevinfo_exitcode));
+		goto error;
+	}
+
+	has_more_lines = TRUE;
+
+	p = udevinfo_stdout;
+
+	do {
+		if (*p == '\0') {
+			has_more_lines = FALSE;
+			break;
+		}
+
+		for (q = p; *q != '\n' && *q != '\0' && *q != '='; q++)
+			;
+		
+		len = q - p;
+		switch (*q) {
+		case '=':
+			strncpy (sysfs_path, p, len > PATH_MAX ? PATH_MAX : len);
+			sysfs_path [len > PATH_MAX ? PATH_MAX : len] = '\0';
+			break;
+			
+		case '\n':
+			HAL_ERROR (("Expected '=', not '\\n' in line '%s'", p));
+			goto error;
+			
+		case '\0':
+			HAL_ERROR (("Expected '=', not '\\0' in line '%s'", p));
+			goto error;
+			
+		default:
+			HAL_ERROR (("Expected '=', not '%c' in line '%s'", *q, p));
+			goto error;
+		}
+		
+		q++;
+		r = q;
+		for ( ; *q != '\n' && *q != '\0'; q++)
+			;
+		
+		len = q - r;
+		switch (*q) {
+		case '\0':
+			has_more_lines = FALSE;
+			/* explicit fallthrough */
+
+		case '\n':
+			strncpy (device_file, r, len > PATH_MAX ? PATH_MAX : len);
+			device_file [len > PATH_MAX ? PATH_MAX : len] = '\0';
+			break;
+			
+		default:
+			HAL_ERROR (("Expected '\\n' or '\\0', not '%c' in line '%s'", *q, p));
+			goto error;
+		}
+
+		g_hash_table_insert (sysfs_to_dev_map, g_strdup_printf ("%s%s", sysfs_mount_path, sysfs_path), 
+				     g_strdup(device_file));
+		
+#ifdef HAL_COLDPLUG_VERBOSE
+		printf ("Got '%s' -> '%s'\n", sysfs_path, device_file);
+#endif
+		p = q + 1;
+
+	} while (has_more_lines);
+
+	return sysfs_to_dev_map;
+
+error:
+	g_hash_table_destroy (sysfs_to_dev_map);
+	return NULL;
+}
+
+
 static gboolean
-coldplug_synthesize_block_event(const gchar *f);
+coldplug_synthesize_block_event(const gchar *f, GHashTable *sysfs_to_dev_map);
 
 static void
 coldplug_compute_visit_device (const gchar *path, 
 			       GHashTable *sysfs_to_bus_map, 
-			       GHashTable *sysfs_to_class_in_devices_map);
+			       GHashTable *sysfs_to_class_in_devices_map,
+			       GHashTable *sysfs_to_dev_map);
 
 /* For debugging */
 /*#define HAL_COLDPLUG_VERBOSE*/
@@ -146,6 +297,15 @@
 	 */
 	GSList *sysfs_dm_dev = NULL;
 
+	GHashTable *sysfs_to_dev_map = NULL;
+
+	
+	if ((sysfs_to_dev_map = hal_util_get_sysfs_to_dev_map ()) == NULL) {
+		HAL_ERROR (("Unable to get sysfs to dev map"));
+		goto error;
+	}
+
+
 	/* build bus map */
 	sysfs_to_bus_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 	g_snprintf (path, HAL_PATH_MAX, "%s/bus", get_hal_sysfs_path ());
@@ -271,7 +431,7 @@
 		while ((f1 = g_dir_read_name (dir1)) != NULL) {
 
 			g_snprintf (path, HAL_PATH_MAX, "%s/devices/%s/%s", get_hal_sysfs_path (), f, f1);
-			coldplug_compute_visit_device (path, sysfs_to_bus_map, sysfs_to_class_in_devices_map);
+			coldplug_compute_visit_device (path, sysfs_to_bus_map, sysfs_to_class_in_devices_map, sysfs_to_dev_map);
 		}
 		g_dir_close (dir1);
 	}
@@ -287,6 +447,7 @@
 		gchar *sysfs_path;
 		gchar *subsystem;
 		HotplugEvent *hotplug_event;
+		gchar *device_file;
 
 		sysfs_path = (gchar *) li->data;
 		subsystem = (gchar *) li->next->data;
@@ -299,7 +460,11 @@
 		hotplug_event->type = HOTPLUG_EVENT_SYSFS;
 		g_strlcpy (hotplug_event->sysfs.subsystem, subsystem, sizeof (hotplug_event->sysfs.subsystem));
 		g_strlcpy (hotplug_event->sysfs.sysfs_path, sysfs_path, sizeof (hotplug_event->sysfs.sysfs_path));
-		hal_util_get_device_file (sysfs_path, hotplug_event->sysfs.device_file, sizeof (hotplug_event->sysfs.device_file));
+
+		device_file = (gchar *) g_hash_table_lookup (sysfs_to_dev_map, sysfs_path);
+		if (device_file != NULL) {
+			strncpy (hotplug_event->sysfs.device_file, device_file, sizeof (hotplug_event->sysfs.device_file));
+		}
 		hotplug_event->sysfs.net_ifindex = -1;
 		
 		hotplug_event_enqueue (hotplug_event);
@@ -322,17 +487,19 @@
 			sysfs_dm_dev = g_slist_append(sysfs_dm_dev, g_strdup(f));
 			continue;
 		}
-		if (coldplug_synthesize_block_event(f) == FALSE)
+		if (coldplug_synthesize_block_event(f, sysfs_to_dev_map) == FALSE)
 			goto error;
 	}
 	/* process all dm devices last so that their backing devices exist */
 	for (li = sysfs_dm_dev; li != NULL; li = g_slist_next (g_slist_next (li))) {
-		if (coldplug_synthesize_block_event(li->data) == FALSE)
+		if (coldplug_synthesize_block_event (li->data, sysfs_to_dev_map) == FALSE)
 			goto error;
 		g_free (li->data);
 	}
 	g_slist_free (sysfs_dm_dev);
 	g_dir_close (dir);
+
+	g_hash_table_destroy (sysfs_to_dev_map);
        
 	return TRUE;
 error:
@@ -341,7 +508,7 @@
 }
 
 static gboolean
-coldplug_synthesize_block_event(const gchar *f)
+coldplug_synthesize_block_event(const gchar *f, GHashTable *sysfs_to_dev_map)
 {
 	GDir *dir1;
 	gsize flen;
@@ -352,6 +519,7 @@
 	gchar path[HAL_PATH_MAX];
 	gchar path1[HAL_PATH_MAX];
 	const gchar *f1;
+	gchar *device_file;
 
 	g_snprintf (path, HAL_PATH_MAX, "%s/block/%s", get_hal_sysfs_path (), f);
 #ifdef HAL_COLDPLUG_VERBOSE
@@ -371,7 +539,12 @@
 	hotplug_event->type = HOTPLUG_EVENT_SYSFS;
 	g_strlcpy (hotplug_event->sysfs.subsystem, "block", sizeof (hotplug_event->sysfs.subsystem));
 	g_strlcpy (hotplug_event->sysfs.sysfs_path, path, sizeof (hotplug_event->sysfs.sysfs_path));
-	hal_util_get_device_file (path, hotplug_event->sysfs.device_file, sizeof (hotplug_event->sysfs.device_file));
+
+	device_file = (gchar *) g_hash_table_lookup (sysfs_to_dev_map, path);
+	if (device_file != NULL) {
+		strncpy (hotplug_event->sysfs.device_file, device_file, sizeof (hotplug_event->sysfs.device_file));
+	}
+
 	if (normalized_target != NULL)
 		g_strlcpy (hotplug_event->sysfs.wait_for_sysfs_path, normalized_target, sizeof (hotplug_event->sysfs.wait_for_sysfs_path));
 	else
@@ -400,7 +573,10 @@
 			g_strlcpy (hotplug_event->sysfs.subsystem, "block", sizeof (hotplug_event->sysfs.subsystem));
 			g_strlcpy (hotplug_event->sysfs.sysfs_path, path1, sizeof (hotplug_event->sysfs.sysfs_path));
 			g_strlcpy (hotplug_event->sysfs.wait_for_sysfs_path, path, sizeof (hotplug_event->sysfs.wait_for_sysfs_path));
-			hal_util_get_device_file (path1, hotplug_event->sysfs.device_file, sizeof (hotplug_event->sysfs.device_file));
+			device_file = (gchar *) g_hash_table_lookup (sysfs_to_dev_map, path1);
+			if (device_file != NULL) {
+				strncpy (hotplug_event->sysfs.device_file, device_file, sizeof (hotplug_event->sysfs.device_file));
+			}
 			hotplug_event->sysfs.net_ifindex = -1;
 			hotplug_event_enqueue (hotplug_event);
 		}
@@ -416,7 +592,8 @@
 static void
 coldplug_compute_visit_device (const gchar *path, 
 			       GHashTable *sysfs_to_bus_map, 
-			       GHashTable *sysfs_to_class_in_devices_map)
+			       GHashTable *sysfs_to_class_in_devices_map,
+			       GHashTable *sysfs_to_dev_map)
 {
 	gchar *bus;
 	GError *err = NULL;
@@ -455,6 +632,7 @@
 	for (i = class_devs; i != NULL; i = g_slist_next (g_slist_next (i))) {
 		gchar *sysfs_path;
 		gchar *subsystem;
+		gchar *device_file;
 		HotplugEvent *hotplug_event;
 
 		sysfs_path = (gchar *) i->data;
@@ -468,7 +646,11 @@
 		hotplug_event->type = HOTPLUG_EVENT_SYSFS;
 		g_strlcpy (hotplug_event->sysfs.subsystem, subsystem, sizeof (hotplug_event->sysfs.subsystem));
 		g_strlcpy (hotplug_event->sysfs.sysfs_path, sysfs_path, sizeof (hotplug_event->sysfs.sysfs_path));
-		hal_util_get_device_file (sysfs_path, hotplug_event->sysfs.device_file, sizeof (hotplug_event->sysfs.device_file));
+
+		device_file = (gchar *) g_hash_table_lookup (sysfs_to_dev_map, sysfs_path);
+		if (device_file != NULL) {
+			strncpy (hotplug_event->sysfs.device_file, device_file, sizeof (hotplug_event->sysfs.device_file));
+		}
 		if (path != NULL)
 			g_strlcpy (hotplug_event->sysfs.wait_for_sysfs_path, path, sizeof (hotplug_event->sysfs.wait_for_sysfs_path));
 		else
@@ -496,7 +678,8 @@
 				/* recursion fun */
 				coldplug_compute_visit_device (path_child, 
 							       sysfs_to_bus_map, 
-							       sysfs_to_class_in_devices_map);
+							       sysfs_to_class_in_devices_map,
+							       sysfs_to_dev_map);
 			}
 		}
 	}

Index: osspec.c
===================================================================
RCS file: /cvs/hal/hal/hald/linux2/osspec.c,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -d -r1.23 -r1.24
--- osspec.c	16 Mar 2005 20:09:47 -0000	1.23
+++ osspec.c	27 Apr 2005 18:53:39 -0000	1.24
@@ -522,110 +522,6 @@
 	return hotplug_reprobe_tree (d);
 }
 
-/* Returns the path of the udevinfo program 
- *
- * @return                      Path or NULL if udevinfo program is not found
- */
-static const gchar *
-hal_util_get_udevinfo_path (void)
-{
-	guint i;
-	struct stat s;
-	static gchar *path = NULL;
-	gchar *possible_paths[] = { 
-		"/sbin/udevinfo",
-		"/usr/bin/udevinfo",
-		"/usr/sbin/udevinfo",
-		"/usr/local/sbin/udevinfo"
-	};
-
-	if (path != NULL)
-		return path;
-
-	for (i = 0; i < sizeof (possible_paths) / sizeof (char *); i++) {
-		if (stat (possible_paths[i], &s) == 0 && S_ISREG (s.st_mode)) {
-			path = possible_paths[i];
-			break;
-		}
-	}
-	return path;
-}
-
-/** Get the name of the special device file given the sysfs path.
- *
- *  @param  sysfs_path          Path to class device in sysfs
- *  @param  dev_file            Where the special device file name should be stored
- *  @param  dev_file_length     Size of dev_file character array
- *  @return                     TRUE only if the device file could be found
- */
-gboolean
-hal_util_get_device_file (const gchar *sysfs_path, gchar *dev_file, gsize dev_file_length)
-{
-	int i;
-	gsize sysfs_path_len;
-	gsize sysfs_mount_path_len;
-	gchar sysfs_path_trunc[HAL_PATH_MAX];
-	gchar sysfs_path_dev_trunc[HAL_PATH_MAX + 4];
-	char *udev_argv[7] = { NULL, 
-			       "-r", "-q", "name", "-p",
-			       sysfs_path_trunc, NULL };
-	char *udev_stdout;
-	char *udev_stderr;
-	int udev_exitcode;
-	struct stat statbuf;
-
-	/* check for dev file in sysfs path */
-	sysfs_path_len = strlen (sysfs_path);
-	strncpy (sysfs_path_dev_trunc, sysfs_path, HAL_PATH_MAX);
-	strncat (sysfs_path_dev_trunc + sysfs_path_len, "/dev", 4);
-	if (stat (sysfs_path_dev_trunc, &statbuf) != 0)
-		return FALSE;
-
-	/* get path to udevinfo */
-	udev_argv[0] = (char *) hal_util_get_udevinfo_path ();
-	if (udev_argv[0] == NULL)
-		return FALSE;
-
-	/* compute truncated sysfs path as udevinfo doesn't want the sysfs_mount_path (e.g. /sys) prefix */
-	sysfs_mount_path_len = strlen (hal_sysfs_path);
-	if (strlen (sysfs_path) > sysfs_mount_path_len) {
-		strncpy (sysfs_path_trunc, sysfs_path + sysfs_mount_path_len, HAL_PATH_MAX - sysfs_mount_path_len);
-	}
-
-	/* Now invoke udevinfo */
-	if (udev_argv[0] == NULL || g_spawn_sync ("/",
-						  udev_argv,
-						  NULL,
-						  0,
-						  NULL,
-						  NULL,
-						  &udev_stdout,
-						  &udev_stderr,
-						  &udev_exitcode,
-						  NULL) != TRUE) {
-		HAL_ERROR (("Couldn't invoke %s", udev_argv[0]));
-		return FALSE;
-	}
-
-	if (udev_exitcode != 0) {
-		HAL_ERROR (("%s returned %d for %s", udev_argv[0], udev_exitcode, sysfs_path_trunc));
-		return FALSE;
-	}
-
-	/* sanitize string returned by udev */
-	for (i = 0; udev_stdout[i] != 0; i++) {
-		if (udev_stdout[i] == '\r' || udev_stdout[i] == '\n') {
-			udev_stdout[i] = 0;
-			break;
-		}
-	}
-
-	/*HAL_INFO (("got device file %s for %s", udev_stdout, sysfs_path));*/
-
-	strncpy (dev_file, udev_stdout, dev_file_length);
-	return TRUE;
-}
-
 gboolean
 hal_util_set_driver (HalDevice *d, const char *property_name, const char *sysfs_path)
 {

Index: osspec_linux.h
===================================================================
RCS file: /cvs/hal/hal/hald/linux2/osspec_linux.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- osspec_linux.h	8 Feb 2005 22:36:58 -0000	1.1
+++ osspec_linux.h	27 Apr 2005 18:53:39 -0000	1.2
@@ -33,8 +33,6 @@
 
 const gchar *get_hal_proc_path (void);
 
-gboolean hal_util_get_device_file (const gchar *sysfs_path, gchar *dev_file, gsize dev_file_length);
-
 gboolean hal_util_set_driver (HalDevice *d, const char *property_name, const char *sysfs_path);
 
 HalDevice *hal_util_find_closest_ancestor (const gchar *sysfs_path);




More information about the hal-commit mailing list