static void unmount_childs (LibHalContext *ctx, const char *udi) { DBusError error; int num_volumes; char **volumes; dbus_error_init (&error); /* need to force unmount all partitions */ if ((volumes = libhal_manager_find_device_string_match ( ctx, "block.storage_device", udi, &num_volumes, &error)) != NULL) { dbus_error_free (&error); int i; for (i = 0; i < num_volumes; i++) { char *vol_udi; vol_udi = volumes[i]; if (libhal_device_get_property_bool (ctx, vol_udi, "block.is_volume", &error)) { dbus_error_free (&error); if (libhal_device_get_property_bool (ctx, vol_udi, "volume.is_mounted", &error)) { dbus_error_free (&error); HAL_DEBUG (("Forcing unmount of child '%s'", vol_udi)); force_unmount (ctx, vol_udi); } } } libhal_free_string_array (volumes); } my_dbus_error_free (&error); }
static gboolean battery_update_property( LibHalContext *ctx, struct battery_info *battery, const char *key ) { DBusError error; gboolean ret; ret = TRUE; dbus_error_init( &error ); if( !strcmp( key, "battery.present" ) ) battery->status.present = libhal_device_get_property_bool( ctx, battery->udi, key, &error ); if( !strcmp( key, "battery.charge_level.current" ) ) battery->status.current_level = libhal_device_get_property_int( ctx, battery->udi, key, &error ); if( !strcmp( key, "battery.charge_level.rate" ) ) battery->status.rate = libhal_device_get_property_int( ctx, battery->udi, key, &error ); else if( !strcmp( key, "battery.charge_level.design" ) ) battery->status.design_level = libhal_device_get_property_int( ctx, battery->udi, key, &error ); else if( !strcmp( key, "battery.charge_level.last_full" ) ) battery->status.full_level = libhal_device_get_property_int( ctx, battery->udi, key, &error ); else if( !strcmp( key, "battery.remaining_time" ) ) battery->status.remaining_time = libhal_device_get_property_int( ctx, battery->udi, key, &error ); else if( !strcmp( key, "battery.rechargeable.is_charging" ) ) battery->status.charging = libhal_device_get_property_bool( ctx, battery->udi, key, &error ); else if( !strcmp( key, "battery.rechargeable.is_discharging" ) ) battery->status.discharging = libhal_device_get_property_bool( ctx, battery->udi, key, &error ); else ret = FALSE; dbus_error_free( &error ); return ret; }
int list_battery_devices(LibHalContext *hal_ctx) { int i; int num_devices; char **device_names; DBusError error; dbus_error_init(&error); if (!(device_names = libhal_manager_find_device_string_match(hal_ctx, "info.category", "battery", &num_devices, &error))) { fprintf(stderr, "Empty HAL device list.\n"); LIBHAL_FREE_DBUS_ERROR(&error); return (31); } for (i = 0; i < num_devices; i++) { if (libhal_device_get_property_bool(hal_ctx, device_names[i], "battery.present", &error)) { printf("%s\n", device_names[i]); } } libhal_free_string_array(device_names); dbus_error_free(&error); return (0); }
static gboolean battery_update_all(LibHalContext *ctx) { int i; int num_devices; char **battery_devices; int fd; DBusError error; HAL_DEBUG(("battery_update_all() enter")); dbus_error_init(&error); if ((battery_devices = libhal_manager_find_device_string_match (ctx, "info.category", "battery", &num_devices, &error)) != NULL) { for (i = 0; i < num_devices; i++) { my_dbus_error_free(&error); if (libhal_device_get_property_bool(ctx, battery_devices[i], "battery.present", &error)) { if ((fd = open_device(ctx, battery_devices[i])) == -1) { continue; } battery_dynamic_update(ctx, battery_devices[i], fd); close(fd); } } libhal_free_string_array(battery_devices); } my_dbus_error_free(&error); HAL_DEBUG(("battery_update_all() exit")); return (TRUE); }
gboolean battery_update(LibHalContext *ctx, const char *udi, int fd) { acpi_bst_t bst; DBusError error; HAL_DEBUG(("battery_update() enter")); dbus_error_init(&error); libhal_device_set_property_string(ctx, udi, "info.product", "Battery Bay", &error); my_dbus_error_free(&error); libhal_device_set_property_string(ctx, udi, "info.category", "battery", &error); bzero(&bst, sizeof (bst)); if (ioctl(fd, BATT_IOC_STATUS, &bst) < 0) { if (errno == ENXIO) { my_dbus_error_free(&error); libhal_device_set_property_bool(ctx, udi, "battery.present", FALSE, &error); } else { my_dbus_error_free(&error); return (FALSE); } } else { my_dbus_error_free(&error); libhal_device_set_property_bool(ctx, udi, "battery.present", TRUE, &error); } my_dbus_error_free(&error); if (!libhal_device_get_property_bool(ctx, udi, "battery.present", &error)) { HAL_DEBUG(("battery_update(): battery is NOT present")); battery_remove(ctx, udi); } else { HAL_DEBUG(("battery_update(): battery is present")); my_dbus_error_free(&error); libhal_device_set_property_string(ctx, udi, "battery.type", "primary", &error); my_dbus_error_free(&error); libhal_device_add_capability(ctx, udi, "battery", &error); my_dbus_error_free(&error); if (libhal_device_get_property_type(ctx, udi, "battery.vendor", &error) == LIBHAL_PROPERTY_TYPE_INVALID) { battery_static_update(ctx, udi, fd); } battery_dynamic_update(ctx, udi, fd); } my_dbus_error_free(&error); HAL_DEBUG(("battery_update() exit")); return (TRUE); }
static boolean_t volume_should_mount(const char *udi) { char *storage_device = NULL; int ret = B_FALSE; if (libhal_device_get_property_bool(hal_ctx, udi, "volume.ignore", NULL)) { goto out; } /* get the backing storage device */ if (!(storage_device = libhal_device_get_property_string(hal_ctx, udi, "block.storage_device", NULL))) { dprintf("cannot get block.storage_device\n"); goto out; } /* we handle either removable or hotpluggable */ if (!libhal_device_get_property_bool(hal_ctx, storage_device, "storage.removable", NULL) && !libhal_device_get_property_bool(hal_ctx, storage_device, "storage.hotpluggable", NULL)) { goto out; } /* ignore if claimed by another volume manager */ if (libhal_device_get_property_bool(hal_ctx, storage_device, "info.claimed", NULL)) { goto out; } ret = B_TRUE; out: libhal_free_string(storage_device); return (ret); }
static PropertyCacheItem* property_cache_item_get (const char *hal_device_udi) { PropertyCacheItem * pci = g_new0 (PropertyCacheItem,1); DBusError err; dbus_error_init (&err); pci->bus_no_present = libhal_device_property_exists (halctx, hal_device_udi, "usb_device.bus_number", &err); if (dbus_error_is_set (&err)) { HAL_ERROR (("Error: [%s]/[%s]", err.name, err.message)); dbus_error_free (&err); } if (pci->bus_no_present) pci->bus_no = libhal_device_get_property_int (halctx, hal_device_udi, "usb_device.bus_number", &err); LIBHAL_FREE_DBUS_ERROR (&err); pci->port_no_present = libhal_device_property_exists (halctx, hal_device_udi, "usb_device.linux.device_number", &err); LIBHAL_FREE_DBUS_ERROR (&err); if (pci->port_no_present) pci->port_no = libhal_device_get_property_int (halctx, hal_device_udi, "usb_device.linux.device_number", &err); LIBHAL_FREE_DBUS_ERROR (&err); pci->csr_is_dual_present = libhal_device_property_exists (halctx, hal_device_udi, "battery.csr.is_dual", &err); LIBHAL_FREE_DBUS_ERROR (&err); if (pci->csr_is_dual_present) pci->csr_is_dual = libhal_device_get_property_bool (halctx, hal_device_udi, "battery.csr.is_dual", &err); LIBHAL_FREE_DBUS_ERROR (&err); pci->current_charge_present = libhal_device_property_exists (halctx, hal_device_udi, "battery.charge_level.current", &err); LIBHAL_FREE_DBUS_ERROR (&err); if (pci->current_charge_present) pci->current_charge = libhal_device_get_property_int (halctx, hal_device_udi, "battery.charge_level.current", &err); LIBHAL_FREE_DBUS_ERROR (&err); return pci; }
/* ARGSUSED */ static void rmm_property_modified(LibHalContext *ctx, const char *udi, const char *key, dbus_bool_t is_removed, dbus_bool_t is_added) { DBusError error; GSList *l; managed_volume_t *v; boolean_t is_mounted; if (strcmp(key, "volume.is_mounted") != 0) { return; } is_mounted = libhal_device_get_property_bool(hal_ctx, udi, key, NULL); l = g_slist_find_custom(managed_volumes, udi, rmm_managed_compare_udi); v = (l != NULL) ? l->data : NULL; if (is_mounted) { dprintf("Mounted: %s\n", udi); if (v != NULL) { /* volume mounted by us is already taken care of */ if (v->my) { return; } } else { if ((v = rmm_managed_alloc(ctx, udi)) == NULL) { return; } managed_volumes = g_slist_prepend(managed_volumes, v); } v->aa.aa_action = INSERT; (void) vold_postprocess(hal_ctx, udi, &v->aa); } else { dprintf("Unmounted: %s\n", udi); if (v == NULL) { return; } v->aa.aa_action = EJECT; (void) vold_postprocess(hal_ctx, udi, &v->aa); rmm_managed_free(v); managed_volumes = g_slist_delete_link(managed_volumes, l); } }
static void storage_eject_pressed(const char *udi) { DBusError error; /* ignore if disabled via SMF or claimed by another volume manager */ if (!rmm_prop_eject_button || libhal_device_get_property_bool(hal_ctx, udi, "info.claimed", NULL)) { return; } dbus_error_init(&error); (void) rmm_hal_eject(hal_ctx, udi, &error); rmm_dbus_error_free(&error); }
static int get_bool_prop(LibHalContext *ctxt, const char *udi, const char *prop, int *val_p) { DBusError err; int val; int rv; dbus_error_init(&err); val = libhal_device_get_property_bool(ctxt, udi, prop, &err); rv = dbus_error_is_set(&err); dbus_error_free(&err); if (rv == 0) *val_p = val; return rv; }
// 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; }
/* * Mount all mountable volumes */ static void rmm_mount_all() { DBusError error; char **udis = NULL; int num_udis; int i; managed_volume_t *v; dbus_error_init(&error); /* get all volumes */ if ((udis = libhal_find_device_by_capability(hal_ctx, "volume", &num_udis, &error)) == NULL) { dprintf("mount_all: no volumes found\n"); goto out; } for (i = 0; i < num_udis; i++) { /* skip if already mounted */ if (libhal_device_get_property_bool(hal_ctx, udis[i], "volume.is_mounted", NULL)) { dprintf("mount_all: %s already mounted\n", udis[i]); continue; } if (!volume_should_mount(udis[i])) { continue; } if ((v = rmm_managed_alloc(hal_ctx, udis[i])) == NULL) { continue; } if (rmm_action(hal_ctx, udis[i], INSERT, &v->aa, 0, 0, 0)) { v->my = B_TRUE; managed_volumes = g_slist_prepend(managed_volumes, v); } else { rmm_managed_free(v); } } out: if (udis != NULL) { libhal_free_string_array(udis); } rmm_dbus_error_free(&error); }
/* * Mount all volumes mounted by this program */ static void rmm_unmount_all() { GSList *i; managed_volume_t *v; for (i = managed_volumes; i != NULL; i = managed_volumes) { v = (managed_volume_t *)i->data; if (v->my && libhal_device_get_property_bool(hal_ctx, v->udi, "volume.is_mounted", NULL)) { (void) rmm_action(hal_ctx, v->udi, UNMOUNT, &v->aa, 0, 0, 0); } managed_volumes = g_slist_remove(managed_volumes, v); rmm_managed_free(v); } }
static gboolean adaptor_update_property( LibHalContext *ctx, struct adaptor_info *adaptor, const char *key ) { DBusError error; gboolean ret; ret = TRUE; dbus_error_init( &error ); if( !strcmp( key, "ac_adapter.present" ) ) adaptor->present = libhal_device_get_property_bool( ctx, adaptor->udi, key, &error ); else ret = FALSE; dbus_error_free( &error ); return ret; }
static gboolean poll_for_media (gpointer user_data) { if (check_lock_state) { DBusError error; dbus_bool_t should_poll; check_lock_state = FALSE; HAL_INFO (("Checking whether device %s is locked on HAL", device_file)); dbus_error_init (&error); if (libhal_device_is_locked_by_others (ctx, udi, "org.freedesktop.Hal.Device.Storage", &error)) { HAL_INFO (("... device %s is locked on HAL", device_file)); is_locked_by_hal = TRUE; update_proc_title (); LIBHAL_FREE_DBUS_ERROR (&error); goto skip_check; } else { HAL_INFO (("... device %s is not locked on HAL", device_file)); is_locked_by_hal = FALSE; } LIBHAL_FREE_DBUS_ERROR (&error); should_poll = libhal_device_get_property_bool (ctx, udi, "storage.media_check_enabled", &error); LIBHAL_FREE_DBUS_ERROR (&error); polling_disabled = !should_poll; update_proc_title (); } /* TODO: we could remove the timeout completely... */ if (is_locked_by_hal || polling_disabled) goto skip_check; poll_for_media_force (); skip_check: return TRUE; }
static void hal_property_modified(LibHalContext *ctx, const char *udi, const char *key, dbus_bool_t is_removed, dbus_bool_t is_added) { if (!strcmp(key, "volume.is_mounted")) { if (libhal_device_get_property_bool(ctx, udi, "volume.is_mounted", NULL)) { char *mountpoint = libhal_device_get_property_string( ctx, udi, "volume.mount_point", NULL); if (!strncmp(MEDIA_DIR, mountpoint, strlen(MEDIA_DIR))) { add_mount(udi, mountpoint); } if (mountpoint) libhal_free_string(mountpoint); } else { remove_mount(udi); } update_status(); } }
static dbus_bool_t unmount_cleartext_devices (LibHalContext *ctx, const char *udi) { DBusError error; char **clear_devices; int num_clear_devices; dbus_bool_t ret; ret = FALSE; /* check if the volume we back is mounted.. if it is.. unmount it */ dbus_error_init (&error); clear_devices = libhal_manager_find_device_string_match (ctx, "volume.crypto_luks.clear.backing_volume", udi, &num_clear_devices, &error); if (clear_devices != NULL && num_clear_devices > 0) { int i; ret = TRUE; for (i = 0; i < num_clear_devices; i++) { char *clear_udi; clear_udi = clear_devices[i]; LIBHAL_FREE_DBUS_ERROR (&error); if (libhal_device_get_property_bool (ctx, clear_udi, "volume.is_mounted", &error)) { HAL_DEBUG (("Forcing unmount of child '%s' (crypto)", clear_udi)); force_unmount (ctx, clear_udi); } } libhal_free_string_array (clear_devices); } LIBHAL_FREE_DBUS_ERROR (&error); return ret; }
signed int get_hal_bool(const char *udi, const char *key, int optional) { int ret; DBusError error; if (!hal_ready()) return -1; dbus_error_init(&error); ret = libhal_device_get_property_bool(hal_ctx, udi, key, &error); if (!dbus_error_is_set(&error)) { return ret; } else { if (!optional) { fprintf(stderr, "error: libhal_device_get_property_bool: %s: %s\n", error.name, error.message); } dbus_error_free(&error); return -1; } }
static int init_hal(void) { LibHalContext *ctx; DBusError error; DBusConnection *dbus_connection; char **devices, **volumes; int i, num; if (!(ctx = libhal_ctx_new())) { fprintf(stderr, "can't initialize\n"); return -1; } dbus_error_init(&error); dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error); if (dbus_error_is_set(&error)) { fprintf(stderr, "%s\n", error.message); dbus_error_free(&error); return -1; } dbus_connection_setup_with_g_main(dbus_connection, NULL); libhal_ctx_set_dbus_connection(ctx, dbus_connection); libhal_ctx_set_device_property_modified(ctx, hal_property_modified); if (!libhal_device_property_watch_all(ctx, &error)) { fprintf(stderr, "%s\n", error.message); dbus_error_free(&error); libhal_ctx_free(ctx); return -1; } if (!libhal_ctx_init(ctx, &error)) { fprintf(stderr, "%s\n", error.message); dbus_error_free(&error); libhal_ctx_free(ctx); return -1; } if (!(devices = libhal_get_all_devices(ctx, &num, &error))) { fprintf(stderr, "%s\n", error.message); dbus_error_free(&error); libhal_ctx_shutdown(ctx, NULL); libhal_ctx_free(ctx); return -1; } libhal_free_string_array(devices); volumes = libhal_find_device_by_capability(ctx, "volume", &num, &error); if (dbus_error_is_set(&error)) { printf("can't find volume devices: %s\n", error.message); dbus_error_free(&error); libhal_ctx_shutdown(ctx, NULL); libhal_ctx_free(ctx); return -1; } for (i = 0; i < num; i++) { char *udi = volumes[i]; if (libhal_device_property_exists(ctx, udi, "volume.is_mounted", NULL) && libhal_device_get_property_bool(ctx, udi, "volume.is_mounted", NULL)) { hal_property_modified(ctx, udi, "volume.is_mounted", FALSE, FALSE); hal_property_modified(ctx, udi, "volume.mount_point", FALSE, FALSE); } } libhal_free_string_array(volumes); 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); }
gboolean check_libhal (const char *server_addr) { pid_t child_pid; child_pid = fork (); if (child_pid == -1) { printf ("Cannot fork\n"); exit (1); } else if (child_pid == 0) { DBusError error; DBusConnection *conn; LibHalContext *ctx; dbus_bool_t passed; printf ("server address='%s'\n", server_addr); dbus_error_init (&error); if ((conn = dbus_connection_open (server_addr, &error)) == NULL) { printf ("Error connecting to server: %s\n", error.message); goto fail; } dbus_connection_setup_with_g_main (conn, NULL); if ((ctx = libhal_ctx_new ()) == NULL) { printf ("Error getting libhal context\n"); goto fail; } libhal_ctx_set_dbus_connection (ctx, conn); libhal_ctx_init (ctx, &error); if (dbus_error_is_set (&error)) { printf ("FAILED98: %s\n", error.message); goto fail; } printf ("SUCCESS98\n"); libhal_device_print (ctx, "/org/freedesktop/Hal/devices/testobj1", &error); if (dbus_error_is_set (&error)) { printf ("FAILED99: %s\n", error.message); goto fail; } printf ("SUCCESS99\n"); passed = FALSE; { char *val; val = libhal_device_get_property_string (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.string", &error); if (val == NULL || strcmp (val, "fooooobar22") != 0 || dbus_error_is_set (&error)) { libhal_free_string (val); printf ("FAILED100\n"); goto fail; } printf ("SUCCESS100\n"); libhal_free_string (val); } { char *val; val = libhal_device_get_property_string (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.string2", &error); if (val == NULL || strcmp (val, "fooøةמ") != 0 || dbus_error_is_set (&error)) { libhal_free_string (val); printf ("FAILED101: %s\n", error.message); goto fail; } libhal_free_string (val); printf ("SUCCESS101\n"); } { dbus_bool_t b; b = libhal_device_get_property_bool ( ctx, "/org/freedesktop/Hal/devices/testobj1", "test.bool", &error); if (!b || dbus_error_is_set (&error)) { printf ("FAILED102: %s, %i\n", error.message, b); goto fail; } printf ("SUCCESS102\n"); } { double val; double expected_val = 0.53434343; val = libhal_device_get_property_double (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.double", &error); if ( memcmp (&val, &expected_val, sizeof (double)) != 0 || dbus_error_is_set (&error)) { printf ("FAILED103\n"); goto fail; } printf ("SUCCESS103\n"); } if (libhal_device_get_property_uint64 ( ctx, "/org/freedesktop/Hal/devices/testobj1", "test.uint64", &error) != ((((dbus_uint64_t)1)<<35) + 5) || dbus_error_is_set (&error)) { printf ("FAILED104: %s\n", error.message); goto fail; } printf ("SUCCESS104\n"); { char **val; val = libhal_device_get_property_strlist (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.strlist", &error); if (val == NULL || dbus_error_is_set (&error)) { libhal_free_string_array (val); printf ("FAILED105: %s\n", error.message); goto fail; } printf ("SUCCESS105\n"); if (libhal_string_array_length (val) != 2) { libhal_free_string_array (val); printf ("FAILED106\n"); goto fail; } printf ("SUCCESS106\n"); if (strcmp (val[0], "foostrlist2") != 0 || strcmp (val[1], "foostrlist3") != 0) { libhal_free_string_array (val); printf ("FAILED107\n"); goto fail; } printf ("SUCCESS107\n"); libhal_free_string_array (val); } if (libhal_device_get_property_int ( ctx, "/org/freedesktop/Hal/devices/testobj1", "test.int", &error) != 42 || dbus_error_is_set (&error)) { printf ("FAILED108\n"); goto fail; } printf ("SUCCESS108\n"); /* tests for libhal_psi */ { int type; char *key; LibHalPropertySet *pset; LibHalPropertySetIterator it; unsigned int psi_num_passed; unsigned int psi_num_elems; if ((pset = libhal_device_get_all_properties (ctx, "/org/freedesktop/Hal/devices/testobj1", &error)) == NULL) return FALSE; printf ("SUCCESS110\n"); psi_num_passed = 0; psi_num_elems = libhal_property_set_get_num_elems (pset); for (libhal_psi_init (&it, pset); libhal_psi_has_more (&it); libhal_psi_next (&it)) { type = libhal_psi_get_type (&it); key = libhal_psi_get_key (&it); switch (type) { case LIBHAL_PROPERTY_TYPE_STRING: if (strcmp (key, "info.udi") == 0) { if (strcmp (libhal_psi_get_string (&it), "/org/freedesktop/Hal/devices/testobj1") == 0) { psi_num_passed++; } else { printf ("fail on %s\n", key); } } else if (strcmp (key, "test.string") == 0) { if (strcmp (libhal_psi_get_string (&it), "fooooobar22") == 0) { psi_num_passed++; } else { printf ("fail on %s\n", key); } } else if (strcmp (key, "test.string2") == 0) { if (strcmp (libhal_psi_get_string (&it), "fooøةמ") == 0) { psi_num_passed++; } else { printf ("fail on %s\n", key); } } break; case LIBHAL_PROPERTY_TYPE_INT32: if (strcmp (key, "test.int") == 0) { if (libhal_psi_get_int (&it) == 42) psi_num_passed++; else printf ("fail on %s\n", key); } break; case LIBHAL_PROPERTY_TYPE_UINT64: if (strcmp (key, "test.uint64") == 0) { if (libhal_psi_get_uint64 (&it) == ((((dbus_uint64_t)1)<<35) + 5)) psi_num_passed++; else printf ("fail on %s\n", key); } break; case LIBHAL_PROPERTY_TYPE_BOOLEAN: if (strcmp (key, "test.bool") == 0) { if (libhal_psi_get_bool (&it) == TRUE) psi_num_passed++; else printf ("fail on %s\n", key); } break; case LIBHAL_PROPERTY_TYPE_DOUBLE: if (strcmp (key, "test.double") == 0) { double val = 0.53434343; double val2; val2 = libhal_psi_get_double (&it); if (memcmp (&val, &val2, sizeof (double)) == 0) psi_num_passed++; else printf ("fail on %s\n", key); } break; case LIBHAL_PROPERTY_TYPE_STRLIST: if (strcmp (key, "test.strlist") == 0) { char **val; val = libhal_psi_get_strlist (&it); if (libhal_string_array_length (val) == 2 && strcmp (val[0], "foostrlist2") == 0 && strcmp (val[1], "foostrlist3") == 0 && val[2] == NULL) psi_num_passed++; else printf ("fail on %s\n", key); } break; default: printf (" *** unknown type for key %s\n", key); break; } } libhal_free_property_set (pset); if (psi_num_passed != psi_num_elems) { printf ("FAILED111\n"); goto fail; } printf ("SUCCESS111\n"); } /* end libhal_test_psi */ printf ("Passed all libhal tests\n"); passed = TRUE; fail: if (dbus_error_is_set (&error)) dbus_error_free (&error); send_tests_done (conn, passed); exit (1); } else { printf ("child pid=%d\n", child_pid); } return TRUE; }
/** Entry point * * @param argc Number of arguments given to program * @param argv Arguments given to program * @return Return code */ int main (int argc, char *argv[]) { char *udi = NULL; char *key = NULL; int type; dbus_bool_t is_hex = FALSE; dbus_bool_t is_verbose = FALSE; dbus_bool_t is_version = FALSE; char *str; DBusError error; if (argc <= 1) { usage (argc, argv); return 1; } while (1) { int c; int option_index = 0; const char *opt; static struct option long_options[] = { {"udi", 1, NULL, 0}, {"key", 1, NULL, 0}, {"hex", 0, NULL, 0}, {"verbose", 0, NULL, 0}, {"version", 0, NULL, 0}, {"help", 0, NULL, 0}, {NULL, 0, NULL, 0} }; c = getopt_long (argc, argv, "", long_options, &option_index); if (c == -1) break; switch (c) { case 0: opt = long_options[option_index].name; if (strcmp (opt, "help") == 0) { usage (argc, argv); return 0; } else if (strcmp (opt, "hex") == 0) { is_hex = TRUE; } else if (strcmp (opt, "verbose") == 0) { is_verbose = TRUE; } else if (strcmp (opt, "version") == 0) { is_version = TRUE; } else if (strcmp (opt, "key") == 0) { key = strdup (optarg); } else if (strcmp (opt, "udi") == 0) { udi = strdup (optarg); } break; default: usage (argc, argv); return 1; break; } } if (is_version) { printf ("hal-get-property " PACKAGE_VERSION "\n"); return 0; } if (udi == NULL || key == NULL) { usage (argc, argv); return 1; } dbus_error_init (&error); if ((hal_ctx = libhal_ctx_new ()) == NULL) { fprintf (stderr, "error: libhal_ctx_new\n"); return 1; } if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) { fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 1; } if (!libhal_ctx_init (hal_ctx, &error)) { if (dbus_error_is_set(&error)) { fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); } fprintf (stderr, "Could not initialise connection to hald.\n" "Normally this means the HAL daemon (hald) is not running or not ready.\n"); return 1; } type = libhal_device_get_property_type (hal_ctx, udi, key, &error); if (type == LIBHAL_PROPERTY_TYPE_INVALID) { fprintf (stderr, "error: libhal_device_get_property_type: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 1; } /* emit the value to stdout */ switch (type) { case LIBHAL_PROPERTY_TYPE_STRING: str = libhal_device_get_property_string (hal_ctx, udi, key, &error); if (is_verbose) printf ("Type is string\n"); printf ("%s\n", str); libhal_free_string (str); break; case LIBHAL_PROPERTY_TYPE_INT32: if (is_verbose) printf ("Type is integer (shown in %s)\n", (is_hex ? "hexadecimal" : "decimal")); printf ((is_hex ? "%x\n" : "%d\n"), libhal_device_get_property_int (hal_ctx, udi, key, &error)); break; case LIBHAL_PROPERTY_TYPE_UINT64: if (is_verbose) printf ("Type is uint64 (shown in %s)\n", (is_hex ? "hexadecimal" : "decimal")); printf ((is_hex ? "%llx\n" : "%llu\n"), (long long unsigned int) libhal_device_get_property_uint64 (hal_ctx, udi, key, &error)); break; case LIBHAL_PROPERTY_TYPE_DOUBLE: if (is_verbose) printf ("Type is double\n"); printf ("%f\n", libhal_device_get_property_double (hal_ctx, udi, key, &error)); break; case LIBHAL_PROPERTY_TYPE_BOOLEAN: if (is_verbose) printf ("Type is boolean\n"); printf ("%s\n", libhal_device_get_property_bool (hal_ctx, udi, key, &error) ? "true" : "false"); break; case LIBHAL_PROPERTY_TYPE_STRLIST: { unsigned int i; char **strlist; if ((strlist = libhal_device_get_property_strlist (hal_ctx, udi, key, &error)) != NULL) { for (i = 0; strlist[i] != 0; i++) { printf ("%s", strlist[i]); if (strlist[i+1] != NULL) printf (" "); } } break; } default: printf ("Unknown type %d='%c'\n", type, type); return 1; break; } if (dbus_error_is_set (&error)) { fprintf (stderr, "error: %s: %s\n", error.name, error.message); dbus_error_free (&error); return 1; } return 0; }
int main (int argc, char **argv) { char *device_file; char *parent_udi; char *grandparent_udi; char *parent_drive_type; int fd = -1; struct volume_id *vid = NULL; int ret = 1; gboolean has_children; gboolean is_swap; gboolean is_cdrom; gboolean is_partition = FALSE; gboolean has_audio = FALSE; gboolean has_data = FALSE; gboolean is_blank = FALSE; const char *usage; char *label; unsigned int sector_size = 0; off_t media_size = 0; if (! hfp_init(argc, argv)) goto end; device_file = getenv("HAL_PROP_BLOCK_DEVICE"); if (! device_file) goto end; parent_udi = getenv("HAL_PROP_INFO_PARENT"); if (! parent_udi) goto end; /* give a meaningful process title for ps(1) */ setproctitle("%s", device_file); has_children = hfp_getenv_bool("HF_HAS_CHILDREN"); is_swap = hfp_getenv_bool("HF_IS_SWAP"); fd = open(device_file, O_RDONLY); if (fd < 0) goto end; parent_drive_type = libhal_device_get_property_string(hfp_ctx, parent_udi, "storage.drive_type", &hfp_error); dbus_error_free(&hfp_error); grandparent_udi = libhal_device_get_property_string(hfp_ctx, parent_udi, "info.parent", &hfp_error); dbus_error_free(&hfp_error); is_cdrom = parent_drive_type && ! strcmp(parent_drive_type, "cdrom"); g_free(parent_drive_type); if (is_cdrom) { hf_probe_volume_get_disc_info(fd, &has_audio, &has_data); is_blank = (! has_audio && ! has_data); } ioctl(fd, DIOCGMEDIASIZE, &media_size); /* * We only check for filesystems if the volume has no children, * otherwise volume_id might find a filesystem in what is actually * the first child partition of the volume. * * If hald (which has looked at the partition type) reports that it * is a swap partition, we probe it nevertheless in case the * partition type is incorrect. */ if (! has_children && ! (is_cdrom && ! has_data)) { vid = volume_id_open_fd(fd); if (vid) { if (volume_id_probe_all(vid, 0, media_size) == 0) has_data = TRUE; else { volume_id_close(vid); vid = NULL; } } } if (! has_children && ! is_swap && ! has_audio && ! has_data && ! is_blank) goto end; libhal_device_add_capability(hfp_ctx, hfp_udi, "volume", &hfp_error); if (is_cdrom) { HFPCDROM *cdrom; int type; guint64 capacity; libhal_device_set_property_string(hfp_ctx, hfp_udi, "info.category", "volume.disc", &hfp_error); libhal_device_add_capability(hfp_ctx, hfp_udi, "volume.disc", &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.has_audio", has_audio, &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.has_data", has_data, &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_vcd", FALSE, &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_svcd", FALSE, &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_videodvd", FALSE, &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_appendable", FALSE, &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_blank", is_blank, &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", FALSE, &hfp_error); libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "unknown", &hfp_error); /* the following code was adapted from linux's probe-volume.c */ cdrom = hfp_cdrom_new_from_fd(fd, device_file, grandparent_udi); if (cdrom) { type = get_disc_type(cdrom); if (type != -1) switch (type) { case 0x08: /* CD-ROM */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "cd_rom", &hfp_error); break; case 0x09: /* CD-R */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "cd_r", &hfp_error); break; case 0x0a: /* CD-RW */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "cd_rw", &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error); break; case 0x10: /* DVD-ROM */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_rom", &hfp_error); break; case 0x11: /* DVD-R Sequential */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_r", &hfp_error); break; case 0x12: /* DVD-RAM */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_ram", &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error); break; case 0x13: /* DVD-RW Restricted Overwrite */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_rw", &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error); break; case 0x14: /* DVD-RW Sequential */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_rw", &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error); break; case 0x1A: /* DVD+RW */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_plus_rw", &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error); break; case 0x1B: /* DVD+R */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_plus_r", &hfp_error); break; case 0x2B: /* DVD+R Double Layer */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "dvd_plus_r_dl", &hfp_error); break; case 0x40: /* BD-ROM */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "bd_rom", &hfp_error); break; case 0x41: /* BD-R Sequential */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "bd_r", &hfp_error); break; case 0x42: /* BD-R Random */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "bd_r", &hfp_error); break; case 0x43: /* BD-RE */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "bd_re", &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error); break; case 0x50: /* HD DVD-ROM */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "hddvd_rom", &hfp_error); break; case 0x51: /* HD DVD-R */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "hddvd_r", &hfp_error); break; case 0x52: /* HD DVD-Rewritable */ libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.disc.type", "hddvd_rw", &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error); break; } if (get_disc_capacity_for_type(cdrom, type, &capacity) == 0) libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "volume.disc.capacity", capacity, &hfp_error); /* * linux's probe-volume.c: "on some hardware the get_disc_type * call fails, so we use this as a backup". */ if (disc_is_rewritable(cdrom)) libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.disc.is_rewritable", TRUE, &hfp_error); if (disc_is_appendable(cdrom)) libhal_device_set_property_bool (hfp_ctx, hfp_udi, "volume.disc.is_appendable", TRUE, &hfp_error); hfp_cdrom_free(cdrom); } if (has_data && vid && (! strcmp(vid->type, "iso9660") || ! strcmp(vid->type, "udf"))) hf_probe_volume_advanced_disc_detect(fd); } else { libhal_device_set_property_string(hfp_ctx, hfp_udi, "info.category", "volume", &hfp_error); if (libhal_device_query_capability(hfp_ctx, parent_udi, "storage", &hfp_error)) { char *geom_class; char *type; char *scheme; int number; guint64 mediasize; guint64 offset; geom_class = getenv("HF_VOLUME_GEOM_CLASS"); if (geom_class) { if (hf_probe_volume_get_partition_info(geom_class, device_file, &number, &type, &scheme, &mediasize, &offset)) { is_partition = TRUE; libhal_device_set_property_int(hfp_ctx, hfp_udi, "volume.partition.number", number, &hfp_error); libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.partition.scheme", scheme, &hfp_error); libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.partition.type", type, &hfp_error); /* FIXME We need to fill in the supported partition flags. */ libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "volume.partition.media_size", mediasize, &hfp_error); libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "volume.partition.start", offset, &hfp_error); if (! strcmp(scheme, "gpt")) libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.partition.uuid", type, &hfp_error); if (! strcmp(scheme, "gpt") || ! strcmp(scheme, "apm")) libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.partition.label", "", &hfp_error); g_free(type); g_free(scheme); } } } else dbus_error_free(&hfp_error); } libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.is_disc", is_cdrom, &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.is_partition", is_partition, &hfp_error); libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.ignore", has_children || is_swap, &hfp_error); #ifdef HAVE_LIBUFS if (vid && ! strcmp (vid->type, "ufs")) { struct uufsd ufsdisk; if (ufs_disk_fillout(&ufsdisk, device_file) == 0) { char ufsid[64]; char **ufs_devs = NULL; int num_udis; int i; snprintf(ufsid, sizeof(ufsid), "%08x%08x", ufsdisk.d_fs.fs_id[0], ufsdisk.d_fs.fs_id[1]); libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.freebsd.ufsid", ufsid, &hfp_error); dbus_error_free(&hfp_error); ufs_devs = libhal_manager_find_device_string_match(hfp_ctx, "volume.freebsd.ufsid", ufsid, &num_udis, &hfp_error); dbus_error_free(&hfp_error); for (i = 0; i < num_udis; i++) { if (ufs_devs[i] != NULL && strcmp(ufs_devs[i], hfp_udi)) { gboolean mounted; mounted = libhal_device_get_property_bool(hfp_ctx, ufs_devs[i], "volume.is_mounted", &hfp_error); dbus_error_free(&hfp_error); if (mounted) { libhal_device_set_property_bool(hfp_ctx, hfp_udi, "volume.ignore", TRUE, &hfp_error); dbus_error_free(&hfp_error); break; } } } if (ufs_devs) libhal_free_string_array(ufs_devs); ufs_disk_close(&ufsdisk); } } #endif /* HAVE_LIBUFS */ if (has_children) usage = "partitiontable"; else if (is_swap) usage = "other"; else switch (vid ? vid->usage_id : (enum volume_id_usage) -1) { case VOLUME_ID_FILESYSTEM: usage = "filesystem"; break; case VOLUME_ID_DISKLABEL: usage = "disklabel"; break; case VOLUME_ID_OTHER: usage = "other"; break; case VOLUME_ID_RAID: usage = "raid"; break; case VOLUME_ID_CRYPTO: usage = "crypto"; break; case VOLUME_ID_UNUSED: usage = "unused"; break; default: usage = "unknown"; break; } libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.fsusage", usage, &hfp_error); libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.fstype", vid ? vid->type: "", &hfp_error); if (vid && *vid->type_version) libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.fsversion", vid->type_version, &hfp_error); label = hf_probe_volume_get_label(vid); libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.label", label ? label : "", &hfp_error); g_free(label); libhal_device_set_property_string(hfp_ctx, hfp_udi, "volume.uuid", vid ? vid->uuid : "", &hfp_error); ioctl(fd, DIOCGSECTORSIZE, §or_size); if (sector_size != 0) libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "volume.block_size", sector_size, &hfp_error); if (media_size != 0) libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "volume.size", media_size, &hfp_error); if (sector_size != 0 && media_size != 0) libhal_device_set_property_uint64(hfp_ctx, hfp_udi, "volume.num_blocks", media_size / sector_size, &hfp_error); ret = 0; /* is a volume */ end: return ret; }
static void unmount_childs (LibHalContext *ctx, const char *udi) { int num_volumes; char **volumes; DBusError error; /* need to force unmount all partitions */ dbus_error_init (&error); if ((volumes = libhal_manager_find_device_string_match (ctx, "block.storage_device", udi, &num_volumes, &error)) != NULL) { int i; for (i = 0; i < num_volumes; i++) { char *vol_udi; vol_udi = volumes[i]; LIBHAL_FREE_DBUS_ERROR (&error); if (libhal_device_get_property_bool (ctx, vol_udi, "block.is_volume", &error)) { dbus_bool_t is_crypto; /* unmount all cleartext devices associated with us */ is_crypto = unmount_cleartext_devices (ctx, vol_udi); LIBHAL_FREE_DBUS_ERROR (&error); if (libhal_device_get_property_bool (ctx, vol_udi, "volume.is_mounted", &error)) { HAL_DEBUG (("Forcing unmount of child '%s'", vol_udi)); force_unmount (ctx, vol_udi); } /* teardown crypto */ if (is_crypto) { DBusMessage *msg = NULL; DBusMessage *reply = NULL; /* tear down mapping */ HAL_DEBUG (("Teardown crypto for '%s'", vol_udi)); msg = dbus_message_new_method_call ("org.freedesktop.Hal", vol_udi, "org.freedesktop.Hal.Device.Volume.Crypto", "Teardown"); if (msg == NULL) { HAL_ERROR (("Could not create dbus message for %s", vol_udi)); goto teardown_failed; } LIBHAL_FREE_DBUS_ERROR (&error); if (!(reply = dbus_connection_send_with_reply_and_block ( libhal_ctx_get_dbus_connection (ctx), msg, -1, &error)) || dbus_error_is_set (&error)) { HAL_DEBUG (("Teardown failed for %s: %s : %s\n", udi, error.name, error.message)); dbus_error_free (&error); } teardown_failed: if (msg != NULL) dbus_message_unref (msg); if (reply != NULL) dbus_message_unref (reply); } } } libhal_free_string_array (volumes); } LIBHAL_FREE_DBUS_ERROR (&error); }
int main (int argc, char *argv[]) { DBusConnection *dbconn; DBusError dberr; LibHalContext *hal_ctx; LibHalPropertyType property_type; char *property_key; dbus_bool_t bool_value; int int_value; char **udis; int num_udis; int i; dbus_error_init (&dberr); dbconn = dbus_bus_get (DBUS_BUS_SYSTEM, &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "Can't get D-Bus system bus!"); return EXIT_FAILURE; } hal_ctx = libhal_ctx_new (); if (hal_ctx == NULL) { fprintf (stderr, "Can't create a LibHalContext!"); return EXIT_FAILURE; } /* Associate HAL with the D-Bus connection we established */ libhal_ctx_set_dbus_connection (hal_ctx, dbconn); dbus_error_init (&dberr); libhal_ctx_init (hal_ctx, &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "libhal_ctx_init() failed: '%s'. Is hald running?", dberr.message); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } /* Looking for optical drives: storage.cdrom is the capability */ udis = libhal_find_device_by_capability (hal_ctx, "storage.cdrom", &num_udis, &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "libhal_find_device_by_capability error: '%s'\n", dberr.message); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } printf ("Found %d Optical Device(s)\n", num_udis); for (i = 0; i < num_udis; i++) { /* Ensure our properties are the expected type */ property_type = libhal_device_get_property_type (hal_ctx, udis[i], "storage.cdrom.dvd", &dberr); if (dbus_error_is_set (&dberr) || property_type != LIBHAL_PROPERTY_TYPE_BOOLEAN) { fprintf (stderr, "error checking storage.cdrom.dvd type"); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } property_type = libhal_device_get_property_type (hal_ctx, udis[i], "storage.cdrom.read_speed", &dberr); if (dbus_error_is_set (&dberr) || property_type != LIBHAL_PROPERTY_TYPE_INT32) { fprintf (stderr, "error checking storage.cdrom.read_speed type"); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } /* Okay, now simply get property values */ bool_value = libhal_device_get_property_bool (hal_ctx, udis[i], "storage.cdrom.dvd", &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "error getting storage.cdrom.dvd"); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } int_value = libhal_device_get_property_int (hal_ctx, udis[i], "storage.cdrom.read_speed", &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "error getting storage.cdrom.dvd"); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } /* Display the info we just got */ printf ("Device %s has a maximum read spead of %d kb/s and %s read DVDs.\n", udis[i], int_value, bool_value ? "can" : "cannot"); } return EXIT_SUCCESS; }