commit 46976bcc1b9ced0489918b8896f548c1c30621eb
Author: Bogdano Arendartchuk <barendartchuk@suse.com>
Date:   Mon Sep 30 21:58:10 2013 +0200

    Added option to disable block device probing

Index: hal-0.5.12/hald/hald.c
===================================================================
--- hal-0.5.12.orig/hald/hald.c
+++ hal-0.5.12/hald/hald.c
@@ -305,6 +305,9 @@ usage (void)
  		 "        --use-syslog          Print out debug messages to syslog instead of\n"
 		 "                              stderr. Use this option to get debug messages\n"
 		 "                              if hald runs as a daemon.\n"
+		 "        --ignore-storage      Do not probe storage devices. Can be used to prevent\n"
+		 "                              high CPU usage in large systems.\n"
+		 "                              (overrides HALD_IGNORE_STORAGE)\n"
 		 "        --help                Show this information and exit\n"
 		 "        --version             Output version information and exit\n"
 		 "        --exit-after-probing  Exit when probing is complete. Useful only\n"
@@ -327,6 +330,7 @@ static dbus_bool_t opt_retain_privileges
 /** If #TRUE, we will spew out debug */
 dbus_bool_t hald_is_verbose = FALSE;
 dbus_bool_t hald_use_syslog = FALSE;
+dbus_bool_t hald_enable_storage_probing = TRUE;
 static dbus_bool_t hald_debug_exit_after_probing = FALSE;
 
 #ifdef HAVE_POLKIT
@@ -537,6 +541,7 @@ int
 main (int argc, char *argv[])
 {
 	GMainLoop *loop;
+	const char *value;
 	guint sigterm_iochn_listener_source_id;
 	guint opt_child_timeout;
 #ifdef HAVE_POLKIT
@@ -568,6 +573,12 @@ main (int argc, char *argv[])
 	else
 		hald_is_verbose = FALSE;
 
+	value = getenv ("HALD_IGNORE_STORAGE");
+	if (value) {
+		if ((strcasecmp(value, "yes") == 0) || (strcasecmp(value, "on") == 0))
+			hald_enable_storage_probing = FALSE;
+	}
+
 	/* our helpers are installed into libexec, so adjust out $PATH
 	 * to include this at the end (since we want to overide in
 	 * run-hald.sh and friends)
@@ -593,6 +604,7 @@ main (int argc, char *argv[])
 			{"exit-after-probing", 0, NULL, 0},
 			{"daemon", 1, NULL, 0},
 			{"verbose", 1, NULL, 0},
+			{"ignore-storage", 0, NULL, 0},
 			{"retain-privileges", 0, NULL, 0},
 			{"child-timeout", 1, NULL, 0},
 			{"use-syslog", 0, NULL, 0},
@@ -640,6 +652,8 @@ main (int argc, char *argv[])
 				}
 			} else if (strcmp (opt, "retain-privileges") == 0) {
                                 opt_retain_privileges = TRUE;
+			} else if (strcmp (opt, "ignore-storage") == 0) {
+				hald_enable_storage_probing = FALSE;
 			} else if (strcmp (opt, "use-syslog") == 0) {
                                 hald_use_syslog = TRUE;
 			}
@@ -672,7 +686,7 @@ main (int argc, char *argv[])
 
 	HAL_INFO ((PACKAGE_STRING));
 	HAL_INFO (("using child timeout %is", opt_child_timeout));
-	
+
 	if (opt_become_daemon) {
 		int child_pid;
 		int dev_null_fd;
@@ -774,8 +788,11 @@ main (int argc, char *argv[])
 		return 1;
 	}
 
-	/* initialize privileged operating system specific parts */
-	osspec_privileged_init ();
+	if (hald_enable_storage_probing)
+		/* initialize privileged operating system specific parts */
+		osspec_privileged_init ();
+	else
+		HAL_INFO (("block probing disabled by command-line or environment variable."));
 
 #ifdef HAVE_POLKIT
         p_error = NULL;
Index: hal-0.5.12/hald/hald.h
===================================================================
--- hal-0.5.12.orig/hald/hald.h
+++ hal-0.5.12/hald/hald.h
@@ -53,6 +53,7 @@ void property_atomic_update_begin (void)
 void property_atomic_update_end (void);
 
 extern dbus_bool_t hald_is_verbose;
+extern dbus_bool_t hald_enable_storage_probing;
 extern dbus_bool_t hald_use_syslog;
 extern dbus_bool_t hald_is_initialising;
 extern dbus_bool_t hald_is_shutting_down;
Index: hal-0.5.12/hald/linux/coldplug.c
===================================================================
--- hal-0.5.12.orig/hald/linux/coldplug.c
+++ hal-0.5.12/hald/linux/coldplug.c
@@ -514,22 +514,29 @@ static void scan_class(void)
 
 			g_strlcpy(dirname, "/sys/class/", sizeof(dirname));
 			g_strlcat(dirname, dent->d_name, sizeof(dirname));
-			dir2 = opendir(dirname);
-			if (dir2 != NULL) {
-				for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
-					char dirname2[HAL_PATH_MAX];
-
-					if (dent2->d_name[0] == '.')
-						continue;
-					if (!strcmp(dent2->d_name, "device"))
-						continue;
-
-					g_strlcpy(dirname2, dirname, sizeof(dirname2));
-					g_strlcat(dirname2, "/", sizeof(dirname2));
-					g_strlcat(dirname2, dent2->d_name, sizeof(dirname2));
-					device_list_insert(dirname2, dent->d_name, HOTPLUG_EVENT_SYSFS_DEVICE);
+
+			if (!hald_enable_storage_probing &&
+			    (strncmp(dirname, "scsi", 4) == 0 || strncmp(dirname, "block", 5) == 0))
+					continue;
+			else {
+				dir2 = opendir(dirname);
+				if (dir2 != NULL) {
+					for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
+						char dirname2[HAL_PATH_MAX];
+
+						if (dent2->d_name[0] == '.')
+							continue;
+						if (!strcmp(dent2->d_name, "device"))
+							continue;
+
+						g_strlcpy(dirname2, dirname, sizeof(dirname2));
+						g_strlcat(dirname2, "/", sizeof(dirname2));
+						g_strlcat(dirname2, dent2->d_name, sizeof(dirname2));
+
+						device_list_insert(dirname2, dent->d_name, HOTPLUG_EVENT_SYSFS_DEVICE);
+					}
+					closedir(dir2);
 				}
-				closedir(dir2);
 			}
 		}
 		closedir(dir);
@@ -599,15 +606,17 @@ coldplug_synthesize_events (void)
 		device_list = g_slist_sort (device_list, _device_order);
 		process_coldplug_events ();
 
-		/* scan /sys/block, if it isn't already a class */
-		if (stat("/sys/class/block", &statbuf) != 0) {
-			scan_block ();
-			device_list = g_slist_sort (device_list, _device_order);
-			process_coldplug_events ();
-		}
+		if (hald_enable_storage_probing) {
+			/* scan /sys/block, if it isn't already a class */
+			if (stat("/sys/class/block", &statbuf) != 0) {
+				scan_block ();
+				device_list = g_slist_sort (device_list, _device_order);
+				process_coldplug_events ();
+			}
 
-                /* add events from reading /proc/mdstat */
-                blockdev_process_mdstat ();
+                	/* add events from reading /proc/mdstat */
+                	blockdev_process_mdstat ();
+		}
 	}
 
 	g_hash_table_destroy (sysfs_to_udev_map);
Index: hal-0.5.12/hald/linux/hotplug.c
===================================================================
--- hal-0.5.12.orig/hald/linux/hotplug.c
+++ hal-0.5.12/hald/linux/hotplug.c
@@ -459,6 +459,7 @@ hotplug_event_process_queue (void)
 	HotplugEvent *hotplug_event;
 	GList *lp, *lp2;
 	static gboolean processing = FALSE;
+	static gboolean enabled_block_probing = TRUE;
 
 	if (G_UNLIKELY (hotplug_event_queue == NULL))
 		return;
@@ -472,6 +473,12 @@ hotplug_event_process_queue (void)
 	while (lp != NULL) {
 		hotplug_event = lp->data;
 
+		if (!hald_enable_storage_probing && (hotplug_event->type == HOTPLUG_EVENT_SYSFS_BLOCK ||
+		    strncmp(hotplug_event->sysfs.subsystem, "scsi", 4) == 0 || strncmp(hotplug_event->sysfs.subsystem, "block", 5) == 0)) {
+			enabled_block_probing = FALSE;
+			break;
+		}
+
 		if (hotplug_event->action == HOTPLUG_ACTION_ADD)
 			HAL_DEBUG (("checking ADD event %s", hotplug_event->sysfs.sysfs_path));
 		else if (hotplug_event->action == HOTPLUG_ACTION_REMOVE)
@@ -497,6 +504,10 @@ hotplug_event_process_queue (void)
 			lp = g_list_next (lp);
 		}
 	}
+
+	if (!enabled_block_probing && hotplug_event_queue->length > 0)
+		g_queue_remove(hotplug_event_queue, hotplug_event);
+
 	HAL_DEBUG (("events queued = %d, events in progress = %d", hotplug_event_queue->length, g_list_length (hotplug_events_in_progress)));
 
 	processing = FALSE;
@@ -518,7 +529,10 @@ hotplug_rescan_device (HalDevice *d)
 		break;
 
 	case HOTPLUG_EVENT_SYSFS_BLOCK:
-		ret = blockdev_rescan_device (d);
+		if (hald_enable_storage_probing)
+			ret = blockdev_rescan_device (d);
+		else
+			ret = FALSE;
 		break;
 
 	case HOTPLUG_EVENT_ACPI:
@@ -567,7 +581,10 @@ hotplug_reprobe_generate_remove_events (
 		break;
 
 	case HOTPLUG_EVENT_SYSFS_BLOCK:
-		e = blockdev_generate_remove_hotplug_event (d);
+		if (hald_enable_storage_probing)
+			e = blockdev_generate_remove_hotplug_event (d);
+		else
+			e = NULL;
 		break;
 
 	case HOTPLUG_EVENT_ACPI:
@@ -608,7 +625,10 @@ hotplug_reprobe_generate_add_events (Hal
 		break;
 
 	case HOTPLUG_EVENT_SYSFS_BLOCK:
-		e = blockdev_generate_add_hotplug_event (d);
+		if (hald_enable_storage_probing)
+			e = blockdev_generate_add_hotplug_event (d);
+		else
+			e = NULL;
 		break;
 
 	case HOTPLUG_EVENT_ACPI:
Index: hal-0.5.12/hald/linux/osspec.c
===================================================================
--- hal-0.5.12.orig/hald/linux/osspec.c
+++ hal-0.5.12/hald/linux/osspec.c
@@ -245,6 +245,12 @@ hald_udev_data (GIOChannel *source, GIOC
 		HAL_INFO (("Temporary workaround: ignoring temporary cryptsetup file"));
 		goto invalid;
 	}
+
+	if (!hald_enable_storage_probing &&
+	    (strncmp(hotplug_event->sysfs.subsystem, "scsi", 4) == 0 || strncmp(hotplug_event->sysfs.subsystem, "block", 5) == 0)) {
+		goto invalid;
+	}
+
 	if (strstr (hotplug_event->sysfs.device_file, "/dm-") != NULL) {
 		HAL_DEBUG (("Found a dm-device (%s), mark it", hotplug_event->sysfs.device_file));
 		hotplug_event->sysfs.is_dm_device = TRUE;
Index: hal-0.5.12/doc/man/hald.8.in
===================================================================
--- hal-0.5.12.orig/doc/man/hald.8.in
+++ hal-0.5.12/doc/man/hald.8.in
@@ -34,6 +34,10 @@ Enable verbose debug output.
 Enable logging of debug output to the syslog instead of stderr. Use 
 this option only together with --verbose.
 .TP
+.I "--ignore-storage"
+Do not probe storage devices. Can be used to prevent high CPU usage in
+large systems (overrides HALD_IGNORE_STORAGE environment variable).
+.TP
 .I "--help"
 Print out usage.
 .TP
