summaryrefslogtreecommitdiff
path: root/sysutils/hal/files/patch-hald_freebsd_addons_addon-storage.c
blob: ebeeb072c7afc991388fbc4e3bb7fd1cbbeba6dd (plain) (blame)
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
--- hald/freebsd/addons/addon-storage.c.orig	2008-03-17 17:25:16.000000000 -0400
+++ hald/freebsd/addons/addon-storage.c	2008-03-22 03:25:19.000000000 -0400
@@ -150,6 +150,34 @@ hf_addon_storage_update (void)
   return has_media;
 }
 
+static boolean
+poll_for_media (void)
+{
+  boolean has_media;
+
+  has_media = hf_addon_storage_update();
+  if (has_media != addon.had_media)
+    {
+      /*
+       * FIXME: if the media was removed, we should force-unmount
+       * all its child volumes (see linux2/addons/addon-storage.c).
+       * However, currently (FreeBSD 6.0) umount -f is broken and
+       * can cause kernel panics. When I tried to umount -f a
+       * flash card after removing it, it failed with EAGAIN. It
+       * continued to fail after I inserted the card. The system
+       * then hung while rebooting and did not unmount my other
+       * filesystems.
+       */
+
+      libhal_device_rescan(hfp_ctx, hfp_udi, &hfp_error);
+      dbus_error_free(&hfp_error);
+      addon.had_media = has_media;
+
+      return TRUE;
+    }
+  return FALSE;
+}
+
 static void
 update_proc_title (const char *device, boolean polling_enabled)
 {
@@ -159,6 +187,29 @@ update_proc_title (const char *device, b
     setproctitle("no polling on %s because it is explicitly disabled", device);
 }
 
+static DBusHandlerResult
+filter_function (DBusConnection *connection, DBusMessage *message, void *user_data)
+{
+  if (dbus_message_is_method_call(message,
+			  	  "org.freedesktop.Hal.Device.Storage.Removable",
+				  "CheckForMedia"))
+    {
+      DBusMessage *reply;
+      dbus_bool_t had_effect;
+
+      hfp_info("Forcing poll for media becusse CheckForMedia() was called");
+
+      had_effect = poll_for_media();
+
+      reply = dbus_message_new_method_return (message);
+      dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &had_effect, DBUS_TYPE_INVALID);
+      dbus_connection_send(connection, reply, NULL);
+      dbus_message_unref(reply);
+    }
+
+  return DBUS_HANDLER_RESULT_HANDLED;
+}
+
 int
 main (int argc, char **argv)
 {
@@ -202,13 +253,30 @@ main (int argc, char **argv)
     ! strcmp(driver, "cd")))) && ! strcmp(removable, "true");
   addon.had_media = hf_addon_storage_update();
 
+  if (! libhal_device_addon_is_ready(hfp_ctx, hfp_udi, &hfp_error))
+    goto end;
+  dbus_error_free(&hfp_error);
+
   connection = libhal_ctx_get_dbus_connection(hfp_ctx);
   assert(connection != NULL);
+  dbus_connection_set_exit_on_disconnect(connection, 0);
+  dbus_connection_add_filter(connection, filter_function, NULL, NULL);
 
-  while (TRUE)
+  if (! libhal_device_claim_interface(hfp_ctx,
+			 	      hfp_udi,
+				      "org.freedesktop.Hal.Device.Storage.Removable",
+				      "    <method name=\"CheckForMedia\">\n"
+				      "      <arg name=\"call_had_sideeffect\" direction=\"out\" type=\"b\"/>\n"
+				      "    </method>\n",
+				      &hfp_error))
     {
-      boolean has_media;
+      hfp_critical("Cannot claim interface 'org.freedesktop.Hal.Device.Storage.Removable'");
+      goto end;
+    }
+  dbus_error_free(&hfp_error);
 
+  while (TRUE)
+    {
       /* process dbus traffic until update interval has elapsed */
       while (TRUE)
 	{
@@ -225,7 +293,7 @@ main (int argc, char **argv)
 	      if (timeout.tv_sec < 0) /* current time went backwards */
 		timeout = addon.update_interval;
 
-	      dbus_connection_read_write(connection, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
+	      dbus_connection_read_write_dispatch(connection, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
 	      if (! dbus_connection_get_is_connected(connection))
 		goto end;
 	    }
@@ -239,24 +307,7 @@ main (int argc, char **argv)
 
       if (should_poll)
         {
-          has_media = hf_addon_storage_update();
-          if (has_media != addon.had_media)
-	    {
-	      /*
-	       * FIXME: if the media was removed, we should force-unmount
-	       * all its child volumes (see linux2/addons/addon-storage.c).
-	       * However, currently (FreeBSD 6.0) umount -f is broken and
-	       * can cause kernel panics. When I tried to umount -f a
-	       * flash card after removing it, it failed with EAGAIN. It
-	       * continued to fail after I inserted the card. The system
-	       * then hung while rebooting and did not unmount my other
-	       * filesystems.
-	       */
-
-	      libhal_device_rescan(hfp_ctx, hfp_udi, &hfp_error);
-	      dbus_error_free(&hfp_error);
-	      addon.had_media = has_media;
-	    }
+          poll_for_media();
         }
       else
         {