// Helper function. creates a CStorageDevice from a HAL udi bool CHALManager::DeviceFromVolumeUdi(const char *udi, CStorageDevice *device) { if (g_HalManager.m_Context == NULL) return false; LibHalVolume *tempVolume; LibHalDrive *tempDrive; bool Created = false; tempVolume = libhal_volume_from_udi(g_HalManager.m_Context, udi); if (tempVolume) { const char *DriveUdi = libhal_volume_get_storage_device_udi(tempVolume); tempDrive = libhal_drive_from_udi(g_HalManager.m_Context, DriveUdi); if (tempDrive) { char * FriendlyName = libhal_device_get_property_string(g_HalManager.m_Context, udi, "info.product", NULL); device->FriendlyName = FriendlyName; libhal_free_string(FriendlyName); char *block = libhal_device_get_property_string(g_HalManager.m_Context, udi, "block.device", NULL); device->DevID = block; libhal_free_string(block); device->HotPlugged = (bool)libhal_drive_is_hotpluggable(tempDrive); device->Type = libhal_drive_get_type(tempDrive); device->Mounted = (bool)libhal_volume_is_mounted(tempVolume); device->MountPoint = libhal_volume_get_mount_point(tempVolume); if (device->Mounted) URIUtils::AddSlashAtEnd(device->MountPoint); device->Label = libhal_volume_get_label(tempVolume); device->UUID = libhal_volume_get_uuid(tempVolume); device->FileSystem = libhal_volume_get_fstype(tempVolume); device->HalIgnore = libhal_device_get_property_bool(g_HalManager.m_Context, udi, "volume.ignore", NULL); ApproveDevice(device); libhal_drive_free(tempDrive); Created = true; } else CLog::Log(LOGERROR, "HAL: Couldn't create a Drive even if we had a volume - %s", udi); libhal_volume_free(tempVolume); } return Created; }
gchar* get_friendly_volume_name(LibHalContext* ctx, LibHalVolume* volume) { char* ret, *tmp; char* partition_name = NULL; int partition_num; ret = libhal_volume_get_label(volume); if(ret && ret[0] != 0) return g_strdup(ret); /* Try to describe the device */ const char* assoc_udi = libhal_volume_get_storage_device_udi(volume); LibHalDrive* assoc_drv = NULL; if(assoc_udi) assoc_drv = libhal_drive_from_udi(ctx, assoc_udi); if(assoc_drv) { partition_num = (libhal_volume_is_partition(volume) ? (int)libhal_volume_get_partition_number(volume) : -1); if(partition_num > 0) { /* Try to get the name */ tmp = libhal_volume_get_partition_label(volume); if(tmp && tmp[0] != 0) partition_name = g_strdup(tmp); else partition_name = g_strdup_printf(_("Partition %d"), partition_num); } /* if device is partition we can tell where it's actually mounted */ tmp = get_friendly_drive_name(assoc_drv); if(partition_name) { ret = g_strdup_printf(_("%s on %s"), partition_name, tmp); g_free(partition_name); } else { ret = g_strdup_printf(_("(Unknown Volume) on %s"), tmp); } g_free(tmp); libhal_drive_free(assoc_drv); return ret; } ret = _("(Unknown Volume)"); return g_strdup(ret); }
GSList* build_volume_list(LibHalContext* ctx, enum FormatVolumeType type, GHashTable* icon_cache, int icon_width, int icon_height) { const char* capability = ""; char** device_udis; int i, device_udi_count = 0; GSList* device_list = NULL; DBusError error; switch(type) { case FORMATVOLUMETYPE_VOLUME: capability = "volume"; break; case FORMATVOLUMETYPE_DRIVE: capability = "storage"; break; } /* Pull the storage (or volume) list from HAL */ dbus_error_init (&error); if ( (device_udis = libhal_find_device_by_capability (ctx, capability, &device_udi_count, &error) ) == NULL) { LIBHAL_FREE_DBUS_ERROR (&error); goto out; } /* if we use libhal_device_get-property() instead of * libhal_volume_get_mount_mount_point() we have to setup DBusError and * some other things, the problem is do we like to have (null) in the gui * when libhal-storage cannot discover where a partition is mounted? * if so we can remove the DBusError code and use the libhal-storage func * to retrive the mount point. * (or maybe we can check if is_partition == null and then don't strdup * it. * It returns null when a device is marked as swap or mounted via * cryptdisk or even if it's not listed in /etc/fstab */ /* Now we use libhal-storage to get the info */ FormatVolume* current; const char* icon_path; for(i=0; i < device_udi_count; i++) { current = g_new0(FormatVolume, 1); g_debug("udi: %s", device_udis[i]); current->udi = g_strdup(device_udis[i]); switch(type) { case FORMATVOLUMETYPE_VOLUME: current->volume = libhal_volume_from_udi(ctx, device_udis[i]); if(!current->volume) { g_free(current); continue; } /* FIXME: This tastes like wrong */ current->icon = NULL; current->friendly_name = get_friendly_volume_info(ctx, current->volume); current->drive_udi = g_strdup(libhal_volume_get_storage_device_udi(current->volume)); current->mountpoint = g_strdup(libhal_volume_get_mount_point(current->volume)); break; case FORMATVOLUMETYPE_DRIVE: current->drive = libhal_drive_from_udi(ctx, device_udis[i]); if(!current->drive) { g_free(current); continue; } g_debug("Icon drive: %s; Icon volume: %s", libhal_drive_get_dedicated_icon_drive(current->drive), libhal_drive_get_dedicated_icon_volume(current->drive)); icon_path = libhal_drive_get_dedicated_icon_drive(current->drive); current->icon = load_icon_from_cache(icon_path, icon_cache, icon_width, icon_height); current->friendly_name = get_friendly_drive_info(current->drive); break; } /* Do some last minute sanity checks */ if(!current->friendly_name) current->friendly_name = ""; device_list = g_slist_prepend(device_list, current); } if(device_udis) libhal_free_string_array(device_udis); out: return device_list; }
static void thunar_vfs_volume_manager_hal_device_added (LibHalContext *context, const gchar *udi) { ThunarVfsVolumeManagerHal *manager_hal = libhal_ctx_get_user_data (context); ThunarVfsVolumeHal *volume_hal; LibHalVolume *hv; LibHalDrive *hd; const gchar *drive_udi; _thunar_vfs_return_if_fail (THUNAR_VFS_IS_VOLUME_MANAGER_HAL (manager_hal)); _thunar_vfs_return_if_fail (manager_hal->context == context); /* check if we have a volume here */ hv = libhal_volume_from_udi (context, udi); /* HAL might want us to ignore this volume for some reason */ if (G_UNLIKELY (hv != NULL && libhal_volume_should_ignore (hv))) { libhal_volume_free (hv); return; } /* emit the "device-added" signal (to support thunar-volman) */ g_signal_emit_by_name (G_OBJECT (manager_hal), "device-added", udi); if (G_LIKELY (hv != NULL)) { /* check if we have a mountable file system here */ if (libhal_volume_get_fsusage (hv) == LIBHAL_VOLUME_USAGE_MOUNTABLE_FILESYSTEM) { /* determine the UDI of the drive to which this volume belongs */ drive_udi = libhal_volume_get_storage_device_udi (hv); if (G_LIKELY (drive_udi != NULL)) { /* determine the drive for the volume */ hd = libhal_drive_from_udi (context, drive_udi); if (G_LIKELY (hd != NULL)) { /* check if we already have a volume object for the UDI */ volume_hal = thunar_vfs_volume_manager_hal_get_volume_by_udi (manager_hal, udi); if (G_LIKELY (volume_hal == NULL)) { /* otherwise, we allocate a new volume object */ volume_hal = g_object_new (THUNAR_VFS_TYPE_VOLUME_HAL, NULL); volume_hal->udi = g_strdup (udi); } /* update the volume object with the new data from the HAL volume/drive */ thunar_vfs_volume_hal_update (volume_hal, context, hv, hd); /* add the volume object to our list if we allocated a new one */ if (g_list_find (THUNAR_VFS_VOLUME_MANAGER (manager_hal)->volumes, volume_hal) == NULL) { /* add the volume to the volume manager */ thunar_vfs_volume_manager_add (THUNAR_VFS_VOLUME_MANAGER (manager_hal), THUNAR_VFS_VOLUME (volume_hal)); /* release the reference on the volume */ g_object_unref (G_OBJECT (volume_hal)); } /* release the HAL drive */ libhal_drive_free (hd); } } } /* release the HAL volume */ libhal_volume_free (hv); } else { /* but maybe we have a floppy disk drive here */ hd = libhal_drive_from_udi (context, udi); if (G_UNLIKELY (hd == NULL)) return; /* check if we have a floppy disk drive */ if (G_LIKELY (libhal_drive_get_type (hd) == LIBHAL_DRIVE_TYPE_FLOPPY)) { /* check if we already have a volume object for the UDI */ volume_hal = thunar_vfs_volume_manager_hal_get_volume_by_udi (manager_hal, udi); if (G_LIKELY (volume_hal == NULL)) { /* otherwise, we allocate a new volume object */ volume_hal = g_object_new (THUNAR_VFS_TYPE_VOLUME_HAL, NULL); volume_hal->udi = g_strdup (udi); } /* update the volume object with the new data from the HAL volume/drive */ thunar_vfs_volume_hal_update (volume_hal, context, NULL, hd); /* add the volume object to our list if we allocated a new one */ if (g_list_find (THUNAR_VFS_VOLUME_MANAGER (manager_hal)->volumes, volume_hal) == NULL) { /* add the volume to the volume manager */ thunar_vfs_volume_manager_add (THUNAR_VFS_VOLUME_MANAGER (manager_hal), THUNAR_VFS_VOLUME (volume_hal)); /* release the reference on the volume */ g_object_unref (G_OBJECT (volume_hal)); } } /* release the HAL drive */ libhal_drive_free (hd); } }
static void thunar_vfs_volume_manager_hal_update_volume_by_udi (ThunarVfsVolumeManagerHal *manager_hal, const gchar *udi) { ThunarVfsVolumeHal *volume_hal; LibHalVolume *hv = NULL; LibHalDrive *hd = NULL; const gchar *drive_udi; /* check if we have a volume for the UDI */ volume_hal = thunar_vfs_volume_manager_hal_get_volume_by_udi (manager_hal, udi); if (G_UNLIKELY (volume_hal == NULL)) return; /* check if we have a volume here */ hv = libhal_volume_from_udi (manager_hal->context, udi); if (G_UNLIKELY (hv == NULL)) { /* check if we have a drive here */ hd = libhal_drive_from_udi (manager_hal->context, udi); if (G_UNLIKELY (hd == NULL)) { /* the device is no longer a drive or volume, so drop it */ thunar_vfs_volume_manager_hal_device_removed (manager_hal->context, udi); return; } /* update the drive with the new HAL drive/volume */ thunar_vfs_volume_hal_update (volume_hal, manager_hal->context, NULL, hd); /* release the drive */ libhal_drive_free (hd); } else { /* determine the UDI of the drive to which this volume belongs */ drive_udi = libhal_volume_get_storage_device_udi (hv); if (G_LIKELY (drive_udi != NULL)) { /* determine the drive for the volume */ hd = libhal_drive_from_udi (manager_hal->context, drive_udi); } /* check if we have the drive for the volume */ if (G_LIKELY (hd != NULL)) { /* update the volume with the new HAL drive/volume */ thunar_vfs_volume_hal_update (volume_hal, manager_hal->context, hv, hd); /* release the drive */ libhal_drive_free (hd); } else { /* unable to determine the drive, volume gone? */ thunar_vfs_volume_manager_hal_device_removed (manager_hal->context, udi); } /* release the volume */ libhal_volume_free (hv); } }
static void thunar_vfs_volume_manager_hal_init (ThunarVfsVolumeManagerHal *manager_hal) { LibHalDrive *hd; DBusError error; gchar **drive_udis; gchar **udis; gint n_drive_udis; gint n_udis; gint n, m; /* initialize the D-BUS error */ dbus_error_init (&error); /* allocate a HAL context */ manager_hal->context = libhal_ctx_new (); if (G_UNLIKELY (manager_hal->context == NULL)) return; /* try to connect to the system bus */ manager_hal->dbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error); if (G_UNLIKELY (manager_hal->dbus_connection == NULL)) goto failed; /* setup the D-BUS connection for the HAL context */ libhal_ctx_set_dbus_connection (manager_hal->context, manager_hal->dbus_connection); /* connect our manager object to the HAL context */ libhal_ctx_set_user_data (manager_hal->context, manager_hal); /* setup callbacks */ libhal_ctx_set_device_added (manager_hal->context, thunar_vfs_volume_manager_hal_device_added); libhal_ctx_set_device_removed (manager_hal->context, thunar_vfs_volume_manager_hal_device_removed); libhal_ctx_set_device_new_capability (manager_hal->context, thunar_vfs_volume_manager_hal_device_new_capability); libhal_ctx_set_device_lost_capability (manager_hal->context, thunar_vfs_volume_manager_hal_device_lost_capability); libhal_ctx_set_device_property_modified (manager_hal->context, thunar_vfs_volume_manager_hal_device_property_modified); libhal_ctx_set_device_condition (manager_hal->context, thunar_vfs_volume_manager_hal_device_condition); /* try to initialize the HAL context */ if (!libhal_ctx_init (manager_hal->context, &error)) goto failed; /* setup the D-BUS connection with the GLib main loop */ dbus_connection_setup_with_g_main (manager_hal->dbus_connection, NULL); /* lookup all drives currently known to HAL */ drive_udis = libhal_find_device_by_capability (manager_hal->context, "storage", &n_drive_udis, &error); if (G_LIKELY (drive_udis != NULL)) { /* process all drives UDIs */ for (m = 0; m < n_drive_udis; ++m) { /* determine the LibHalDrive for the drive UDI */ hd = libhal_drive_from_udi (manager_hal->context, drive_udis[m]); if (G_UNLIKELY (hd == NULL)) continue; /* check if we have a floppy disk here */ if (libhal_drive_get_type (hd) == LIBHAL_DRIVE_TYPE_FLOPPY) { /* add the drive based on the UDI */ thunar_vfs_volume_manager_hal_device_added (manager_hal->context, drive_udis[m]); } else { /* determine all volumes for the given drive */ udis = libhal_drive_find_all_volumes (manager_hal->context, hd, &n_udis); if (G_LIKELY (udis != NULL)) { /* add volumes for all given UDIs */ for (n = 0; n < n_udis; ++n) { /* add the volume based on the UDI */ thunar_vfs_volume_manager_hal_device_added (manager_hal->context, udis[n]); /* release the UDI (HAL bug #5279) */ free (udis[n]); } /* release the UDIs array (HAL bug #5279) */ free (udis); } } /* release the hal drive */ libhal_drive_free (hd); } /* release the drive UDIs */ libhal_free_string_array (drive_udis); } /* watch all devices for changes */ if (!libhal_device_property_watch_all (manager_hal->context, &error)) goto failed; return; failed: /* release the HAL context */ if (G_LIKELY (manager_hal->context != NULL)) { libhal_ctx_free (manager_hal->context); manager_hal->context = NULL; } /* print a warning message */ if (dbus_error_is_set (&error)) { g_warning (_("Failed to connect to the HAL daemon: %s"), error.message); dbus_error_free (&error); } }
int main (int argc, char *argv[]) { char *udi; char *device; const char *drive_udi; LibHalDrive *drive; LibHalVolume *volume; DBusError error; LibHalContext *hal_ctx = NULL; DBusConnection *system_bus = NULL; #ifdef HAVE_POLKIT LibPolKitContext *pol_ctx = NULL; #endif char *invoked_by_uid; char *invoked_by_syscon_name; device = getenv ("HAL_PROP_BLOCK_DEVICE"); if (device == NULL) usage (); udi = getenv ("HAL_PROP_INFO_UDI"); if (udi == NULL) usage (); invoked_by_uid = getenv ("HAL_METHOD_INVOKED_BY_UID"); invoked_by_syscon_name = getenv ("HAL_METHOD_INVOKED_BY_SYSTEMBUS_CONNECTION_NAME"); dbus_error_init (&error); if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) { printf ("Cannot connect to hald\n"); LIBHAL_FREE_DBUS_ERROR (&error); usage (); } dbus_error_init (&error); system_bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error); if (system_bus == NULL) { printf ("Cannot connect to the system bus\n"); LIBHAL_FREE_DBUS_ERROR (&error); usage (); } #ifdef HAVE_POLKIT pol_ctx = libpolkit_new_context (system_bus); if (pol_ctx == NULL) { printf ("Cannot get libpolkit context\n"); unknown_zpool_error ("Cannot get libpolkit context"); } #endif /* should be a volume */ if ((volume = libhal_volume_from_udi (hal_ctx, udi)) == NULL) { unknown_zpool_error ("Invalid volume"); } if ((drive_udi = libhal_volume_get_storage_device_udi (volume)) == NULL ) { unknown_zpool_error ("Cannot get drive udi"); } if ((drive = libhal_drive_from_udi (hal_ctx, drive_udi)) == NULL) { unknown_zpool_error ("Cannot get drive from udi"); } if ((libhal_volume_get_fstype (volume) == NULL) || (strcmp (libhal_volume_get_fstype (volume), "zfs") != 0)) { unknown_zpool_error ("Not a zpool"); } if ((libhal_volume_get_label (volume) == NULL) || (strlen (libhal_volume_get_label (volume)) == 0)) { unknown_zpool_error ("Invalid zpool name"); } handle_zpool (hal_ctx, #ifdef HAVE_POLKIT pol_ctx, #endif ZPOOL_SUBCMD, libhal_volume_get_label (volume), device, invoked_by_uid, invoked_by_syscon_name, system_bus); return 0; }
/* Callback function, called when a new device has been inserted. */ static void device_added(LibHalContext *context, const char *did) { const char *dudi, *fstype; char *dev, *mountp, *mountable, *label, *locked_reason; LibHalVolume *volume; LibHalDrive *drive; struct device_t *device; if (libhal_device_property_exists(context, did, "info.locked", (DBusError *)NULL) && libhal_device_get_property_bool(context, did, "info.locked", (DBusError *)NULL)) { if (debug_mode_flag) { locked_reason = libhal_device_get_property_string( context, did, "info.locked.reason", (DBusError *)NULL); if (locked_reason) { if (debug_mode_flag) printf("%s%d: %s\n", __FILE__, __LINE__, locked_reason); libhal_free_string(locked_reason); } } return; } if (!libhal_device_query_capability(context, did, "volume", (DBusError *)NULL)) return; label = libhal_device_get_property_string(context, did, "volume.label", (DBusError *)NULL); if (!(mountable = libhal_device_get_property_string( context, did, "volume.fsusage", (DBusError *)NULL)) || strcmp(mountable, "filesystem")) goto out; if (!(volume = libhal_volume_from_udi(context, did))) goto out; if (!(dudi = libhal_volume_get_storage_device_udi(volume))) goto out; if (!(drive = libhal_drive_from_udi(context, dudi))) goto out; if (!libhal_drive_is_hotpluggable(drive) && !libhal_drive_uses_removable_media(drive)) goto out; if (!(fstype = libhal_volume_get_fstype(volume))) goto out; if (!(dev = libhal_device_get_property_string(context, did, "block.device", (DBusError *)NULL))) goto out; mountp = get_mount_point(dev, label); if (!mountp) goto out; device = get_device(mountp, did, dev, label, fstype, volume, drive); if(!is_mounted(device)) { free_device(device); goto out; } if (!device) goto out; consider_fstab(device); device->hook = malloc(2*sizeof(char*)); if(!file_exists(HOOK_PATH)) { device->hook[0] = get_hook(device, "mount"); device->hook[1] = get_hook(device, "umount"); } else { device->hook[0] = NULL; device->hook[1] = NULL; } if (file_exists(device->mountp) < 0) mkdir(device->mountp, 0750); do_mount(device) < 0 ? free_device(device) : add_to_device_list(device); if (device) { if (!add_fstab_entry(device)) device->should_remove_entry = 1; if (debug_mode_flag) debug_dump_device(device); } if (device->hook[0]) run_hook(0, device); out: if (mountable) libhal_free_string(mountable); if (label) libhal_free_string(label); }