hal/tools/linux Makefile.am,1.8,1.9 hal_hotplug_map.c,NONE,1.1

David Zeuthen david at freedesktop.org
Wed Aug 25 13:55:15 PDT 2004


Update of /cvs/hal/hal/tools/linux
In directory gabe:/tmp/cvs-serv31962/tools/linux

Modified Files:
	Makefile.am 
Added Files:
	hal_hotplug_map.c 
Log Message:
2004-08-25  David Zeuthen  <david at fubar.dk>

	* configure.in: Add option for new callout hal-hotplug-map
	
	* hald/device.[ch]: Add the "cancelled" signal and hal_device_cancel
	method

	* hald/hald.c (main): Avoid daemonization for now; will make hald
	hang if anything fails up-until device probing (RH bug 130849)

	* hald/linux/class_device.c (class_device_got_device_file):
	Cancel the device if don't get the device file

	* hald/linux/common.h: Increase timeout to 60 secs. Make note
	about we really need the SEQNUM support in udev

	* hald/linux/osspec.c: 
	(process_coldplug_list_device_cancelled): New function
	(process_coldplug_list): Add support for cancelling
	(reenable_hotplug_proc_on_device_cancel): New function
	(hald_helper_hotplug): Add support for cancelling
	(hotplug_sem_up): Add some helpful debug
	(hotplug_sem_down): Add some helpful debug
	(hald_helper_data): Add some helpful debug

	* tools/Makefile.am: Also remove callout on uninstall

	* tools/fstab-sync.c:
	(fs_table_remove_volume): Only require device_file not complete Volume
	object
	(remove_udi): Don't require storage device to be present (fixes some
	problems when unplugging unpartitioned media as we don't get hotplug
	event and things are thus not serialised)

	* tools/linux/Makefile.am: Build and, optionally, install 
	hal-hotplug-map callout

	* tools/linux/hal_hotplug_map.c: New program; a tad Red Hat specific
	wrt. file locations; patches are welcome. The single purpose of
	this callout is to detect gphoto2 supported cameras and libsane
	supported scanners using usermap files in /etc/hotplug.



Index: Makefile.am
===================================================================
RCS file: /cvs/hal/hal/tools/linux/Makefile.am,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- Makefile.am	15 Aug 2004 18:54:57 -0000	1.8
+++ Makefile.am	25 Aug 2004 20:55:13 -0000	1.9
@@ -5,14 +5,19 @@
 	-DPACKAGE_DATA_DIR=\""$(datadir)"\" \
 	-DPACKAGE_BIN_DIR=\""$(bindir)"\" \
 	-DPACKAGE_LOCALE_DIR=\""$(prefix)/$(DATADIRNAME)/locale"\" \
-	-DPACKAGE_LOCALSTATEDIR=\""$(localstatedir)"\"
+	-DPACKAGE_LOCALSTATEDIR=\""$(localstatedir)"\" \
+	-I$(top_srcdir) \
+	@PACKAGE_CFLAGS@
 
-libexec_PROGRAMS = hal.hotplug hal.dev
+libexec_PROGRAMS = hal.hotplug hal.dev hal-hotplug-map
 
 hal_hotplug_SOURCES = hal_hotplug.c
 
 hal_dev_SOURCES = hal_dev.c
 
+hal_hotplug_map_SOURCES = hal_hotplug_map.c
+hal_hotplug_map_LDADD = @DBUS_LIBS@ $(top_builddir)/libhal/libhal.la
+
 clean-local :
 	rm -f *~
 
@@ -21,7 +26,16 @@
 	- cd $(DESTDIR)$(LINUX_HOTPLUG_DIR)/default && $(LN_S) $(libexecdir)/hal.hotplug hal.hotplug
 	$(mkinstalldirs) $(DESTDIR)/etc/dev.d/default
 	- cd $(DESTDIR)/etc/dev.d/default && $(LN_S) $(libexecdir)/hal.dev hal.dev
+if HOTPLUG_MAP_ENABLED
+	$(mkinstalldirs) $(DESTDIR)$(SYSCONFDIR)/hal/device.d
+	- cd $(DESTDIR)$(SYSCONFDIR)/hal/device.d && $(LN_S) $(libexecdir)/hal-hotplug-map 40-hal-hotplug-map.hal
+endif
 
 uninstall-local :
 	- rm -f $(DESTDIR)$(LINUX_HOTPLUG_DIR)/default/hal.hotplug
 	- rm -f $(DESTDIR)/etc/dev.d/default/hal.dev
+if HOTPLUG_MAP_ENABLED
+	- rm -f $(DESTDIR)$(SYSCONFDIR)/hal/device.d/40-hal-hotplug-map.hal
+endif
+
+

--- NEW FILE: hal_hotplug_map.c ---

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DBUS_API_SUBJECT_TO_CHANGE
#include "libhal/libhal.h"

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE !TRUE
#endif

static int verbose = FALSE;

#define dbg(...) do {if (verbose) fprintf (stderr, __VA_ARGS__);} while (0)

/* @todo FIXME HACK: it's a hack to hardcode this */
static const char *usbmapfiles[] = {"/etc/hotplug/usb.usermap", "/etc/hotplug/usb/libsane.usermap", NULL};

static int
handle_usb_found (const char *module)
{
	char *udi;
	LibHalContext *hal_context = NULL;

	udi = getenv ("UDI");
	if (udi == NULL)
		return FALSE;

	hal_context = hal_initialize (NULL, 0);
	if (hal_context == NULL)
		return FALSE;

	if (strcmp (module, "usbcam") == 0) {
		hal_device_add_capability (hal_context, udi, "camera");
		hal_device_set_property_string (hal_context, udi, "info.category", "camera");
		hal_device_set_property_string (hal_context, udi, "camera.access_method", "user");
		hal_device_set_property_bool (hal_context, udi, "camera.libgphoto2_support", TRUE);
	} else if (strcmp (module, "libusbscanner") == 0) {
		hal_device_add_capability (hal_context, udi, "scanner");
		hal_device_set_property_string (hal_context, udi, "info.category", "scanner");
		hal_device_set_property_string (hal_context, udi, "scanner.access_method", "user");
		hal_device_set_property_bool (hal_context, udi, "scanner.libsane_support", TRUE);
	}

	hal_shutdown (hal_context);
	return TRUE;
}

static int 
handle_usb_mapfile (const char *mapfile, int target_vendor_id, int target_product_id)
{
	int found;
	FILE *f;
	char buf[256];
	char module[256];
	int match, vendor_id, product_id;

	found = FALSE;
	f = fopen (mapfile, "r");
	if (f == NULL)
		goto out;

	while (fgets (buf, sizeof (buf), f) != NULL) {
		if (buf[0] == '#')
			continue;
		if (sscanf (buf, "%s 0x%x 0x%x 0x%x", module, &match, &vendor_id, &product_id) != 4)
			continue;

		if (match != 0x03)
			continue;

		/*dbg ("entry: %s 0x%04x 0x%04x\n", module, vendor_id, product_id);*/

		if (target_vendor_id == vendor_id && target_product_id == product_id) {
			dbg ("FOUND! %s\n", module);
			handle_usb_found (module);
			found = TRUE;
			goto out;
		}
	}

out:
	if (f != NULL)
		fclose (f);
	return found;
}

int 
main (int argc, char *argv[])
{
	int i;
	char *bus;
	char *vendor_id_str;
	char *product_id_str;
	int vendor_id;
	int product_id;

	if (argc != 2)
		return 1;

	if (strcmp (argv[1], "add") != 0)
		return 0;

	if (getenv ("HALD_VERBOSE") != NULL)
		verbose = TRUE;

	bus = getenv ("HAL_PROP_INFO_BUS");
	if (bus == NULL || strcmp (bus, "usb") != 0)
		return 0;

	vendor_id_str = getenv ("HAL_PROP_USB_VENDOR_ID");
	if (vendor_id_str == NULL)
		return 1;
	vendor_id = atoi (vendor_id_str);

	product_id_str = getenv ("HAL_PROP_USB_PRODUCT_ID");
	if (product_id_str == NULL)
		return 1;
	product_id = atoi (product_id_str);

	/* root hubs */
	if (vendor_id == 0 && product_id == 0)
		return 0;

	dbg ("hal_hotplug_map: Checking usermaps for USB device vid=0x%04x pid=0x%04x\n", vendor_id, product_id);

	for (i = 0; usbmapfiles[i] != NULL; i++) {
		if (handle_usb_mapfile (usbmapfiles[i], vendor_id, product_id))
			return 0;
	}

	return 0;
}




More information about the hal-commit mailing list