Improve debug output and support for the detach helper
authorDavid Zeuthen <davidz@redhat.com>
Tue, 23 Feb 2010 20:56:12 +0000 (15:56 -0500)
committerDavid Zeuthen <davidz@redhat.com>
Tue, 23 Feb 2010 20:56:12 +0000 (15:56 -0500)
This allows easily invoking the tool from a shell, like this

 $ sudo /usr/libexec/udisks-helper-drive-detach /dev/sdb
 Detaching device: /dev/sdb
 SYNCHRONIZE CACHE: FAILED: No such file or directory
 (Continuing despite SYNCHRONIZE CACHE failure.)
 STOP UNIT: OK
 Unbind USB interface driver: OK
 Suspend USB Device: OK
 $ echo $?
 0

src/device.c
src/helpers/job-drive-detach.c

index 78458bd..e23241d 100644 (file)
@@ -6933,7 +6933,6 @@ device_drive_detach_authorized_cb (Daemon *daemon,
   n = 0;
   argv[n++] = "udisks-helper-drive-detach";
   argv[n++] = device->priv->device_file;
-  argv[n++] = device->priv->native_path;
   argv[n++] = NULL;
 
   if (!job_new (context, "DriveDetach", FALSE, device, argv, NULL, drive_detach_completed_cb, FALSE, NULL, NULL))
index 25d9079..3f34324 100644 (file)
@@ -45,7 +45,6 @@ main (int argc,
   int ret;
   int sg_fd;
   const gchar *device;
-  const gchar *sysfs_path;
   struct udev *udev;
   struct udev_device *udevice;
   struct udev_device *udevice_usb_interface;
@@ -55,6 +54,7 @@ main (int argc,
   gchar *usb_interface_name;
   size_t usb_interface_name_len;
   FILE *f;
+  struct stat statbuf;
 
   udev = NULL;
   udevice = NULL;
@@ -67,14 +67,24 @@ main (int argc,
   ret = 1;
   sg_fd = -1;
 
-  if (argc != 3)
+  if (argc != 2)
     {
       usage ();
       goto out;
     }
 
   device = argv[1];
-  sysfs_path = argv[2];
+
+  if (stat (device, &statbuf) != 0)
+    {
+      g_printerr ("Error statting %s: %m\n", device);
+      goto out;
+    }
+  if (statbuf.st_rdev == 0)
+    {
+      g_printerr ("%s is not a special device file\n", device);
+      goto out;
+    }
 
   sg_fd = sg_cmds_open_device (device, 1 /* read_only */, 1);
   if (sg_fd < 0)
@@ -83,6 +93,9 @@ main (int argc,
       goto out;
     }
 
+  g_printerr ("Detaching device: %s\n", device);
+
+  g_printerr ("SYNCHRONIZE CACHE: ");
   if (sg_ll_sync_cache_10 (sg_fd, 0, /* sync_nv */
                            0, /* immed */
                            0, /* group */
@@ -92,10 +105,16 @@ main (int argc,
                            0 /* verbose */
                            ) != 0)
     {
-      g_printerr ("Error SYNCHRONIZE CACHE for %s: %m\n", device);
+      g_printerr ("FAILED: %m\n");
       /* this is not a catastrophe, carry on */
+      g_printerr ("(Continuing despite SYNCHRONIZE CACHE failure.)\n");
+    }
+  else
+    {
+      g_printerr ("OK\n");
     }
 
+  g_printerr ("STOP UNIT: ");
   if (sg_ll_start_stop_unit (sg_fd, 0, /* immed */
                              0, /* pc_mod__fl_num */
                              0, /* power_cond */
@@ -106,9 +125,13 @@ main (int argc,
                              0 /* verbose */
                              ) != 0)
     {
-      g_printerr ("Error STOP UNIT for %s: %m\n", device);
+      g_printerr ("FAILED: %m\n");
       goto out;
     }
+  else
+    {
+      g_printerr ("OK\n");
+    }
 
   /* OK, close the device */
   sg_cmds_close_device (sg_fd);
@@ -122,10 +145,10 @@ main (int argc,
       goto out;
     }
 
-  udevice = udev_device_new_from_syspath (udev, sysfs_path);
+  udevice = udev_device_new_from_devnum (udev, 'b', statbuf.st_rdev);
   if (udevice == NULL)
     {
-      g_printerr ("No udev device for %s: %m\n", sysfs_path);
+      g_printerr ("No udev device for device %s (devnum 0x%08x): %m\n", device, (gint) statbuf.st_rdev);
       goto out;
     }
 
@@ -133,13 +156,14 @@ main (int argc,
   udevice_usb_interface = udev_device_get_parent_with_subsystem_devtype (udevice, "usb", "usb_interface");
   if (udevice_usb_interface == NULL)
     {
-      g_printerr ("No usb parent interface for %s: %m\n", sysfs_path);
+      g_printerr ("No usb parent interface for %s: %m\n", device);
       goto out;
     }
 
   usb_interface_name = g_path_get_basename (udev_device_get_devpath (udevice_usb_interface));
   usb_interface_name_len = strlen (usb_interface_name);
 
+  g_printerr ("Unbind USB interface driver: ");
   unbind_path = g_strdup_printf ("%s/driver/unbind", udev_device_get_syspath (udevice_usb_interface));
   f = fopen (unbind_path, "w");
   if (f == NULL)
@@ -154,6 +178,8 @@ main (int argc,
       goto out;
     }
   fclose (f);
+  g_printerr ("OK\n");
+
 
   /* If this is the only USB interface on the device, also suspend the
    * USB device to e.g.  make the lights on the device power off.
@@ -163,6 +189,8 @@ main (int argc,
    */
   ret = 0;
 
+  g_printerr ("Suspend USB Device: ");
+
   udevice_usb_device = udev_device_get_parent_with_subsystem_devtype (udevice, "usb", "usb_device");
   if (udevice_usb_device != NULL)
     {
@@ -180,17 +208,25 @@ main (int argc,
           f = fopen (power_level_path, "w");
           if (f == NULL)
             {
-              g_printerr ("Cannot open %s for writing: %m\n", unbind_path);
+              g_printerr ("FAILED: Cannot open %s for writing: %m\n", unbind_path);
             }
           else
             {
               if (fwrite (suspend_str, sizeof(char), strlen (suspend_str), f) < strlen (suspend_str))
                 {
-                  g_printerr ("Error writing %s to %s: %m\n", power_level_path, suspend_str);
+                  g_printerr ("FAILED: Error writing %s to %s: %m\n", power_level_path, suspend_str);
+                }
+              else
+                {
+                  g_printerr ("OK\n");
                 }
               fclose (f);
             }
         }
+      else
+        {
+          g_printerr ("Skipping since multiple USB interfaces exist.\n");
+        }
     }
 
  out: