int main(int argc, char **argv) { LibHalContext *ctx = NULL; DBusError error; GMainLoop *loop = g_main_loop_new(NULL, FALSE); char *udi; if ((udi = getenv("UDI")) == NULL) { return (0); } drop_privileges(); setup_logger(); dbus_error_init(&error); if ((ctx = libhal_ctx_init_direct(&error)) == NULL) { return (0); } if (!libhal_device_addon_is_ready(ctx, udi, &error)) { return (0); } if (nds_claim_interface(ctx, udi, &error) != 0) { return (0); } g_main_loop_run(loop); /* NOTREACHED */ }
int main (int argc, char *argv[]) { DBusConnection *dbus_connection; DBusError error; const char *commandline; hal_set_proc_title_init (argc, argv); setup_logger (); dbus_error_init (&error); if ((ctx = libhal_ctx_init_direct (&error)) == NULL) { HAL_WARNING (("Unable to init libhal context")); goto out; } if ((dbus_connection = libhal_ctx_get_dbus_connection(ctx)) == NULL) { HAL_WARNING (("Cannot get DBus connection")); goto out; } if ((commandline = getenv ("SINGLETON_COMMAND_LINE")) == NULL) { HAL_WARNING (("SINGLETON_COMMAND_LINE not set")); goto out; } libhal_ctx_set_singleton_device_added (ctx, add_device); libhal_ctx_set_singleton_device_removed (ctx, remove_device); dbus_connection_setup_with_g_main (dbus_connection, NULL); dbus_connection_set_exit_on_disconnect (dbus_connection, 0); if (!libhal_device_singleton_addon_is_ready (ctx, commandline, &error)) { goto out; } leds = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); gmain = g_main_loop_new (NULL, FALSE); g_main_loop_run (gmain); return 0; out: HAL_DEBUG (("An error occured, exiting cleanly")); LIBHAL_FREE_DBUS_ERROR (&error); if (ctx != NULL) { libhal_ctx_shutdown (ctx, &error); LIBHAL_FREE_DBUS_ERROR (&error); libhal_ctx_free (ctx); } return 0; }
int main(int argc, char *argv[]) { int ret = 1; int fd = -1; char *udi; char device_file[HAL_PATH_MAX] = "/devices"; char *devfs_path; LibHalContext *ctx = NULL; DBusError error; if ((udi = getenv("UDI")) == NULL) goto out; if ((devfs_path = getenv("HAL_PROP_SOLARIS_DEVFS_PATH")) == NULL) goto out; strlcat(device_file, devfs_path, HAL_PATH_MAX); setup_logger(); dbus_error_init(&error); if ((ctx = libhal_ctx_init_direct(&error)) == NULL) goto out; HAL_DEBUG(("Doing probe-battery for %s (udi=%s)", device_file, udi)); if ((fd = open(device_file, O_RDONLY | O_NONBLOCK)) < 0) { HAL_DEBUG(("Cannot open %s: %s", device_file, strerror(errno))); goto out; } if (strstr(udi, "ac")) { ac_adapter_update(ctx, udi, fd); } else { battery_update(ctx, udi, fd); } ret = 0; out: if (fd >= 0) { close(fd); } if (ctx != NULL) { libhal_ctx_shutdown(ctx, &error); libhal_ctx_free(ctx); if (dbus_error_is_set(&error)) { dbus_error_free(&error); } } return (ret); }
int main (int argc, char *argv[]) { int ret; char *udi; DBusError error; LibHalContext *hal_ctx = NULL; LibHalChangeSet *cs; gboolean can_suspend = FALSE; gboolean can_suspend_hybrid = FALSE; gboolean can_hibernate = FALSE; int exit_status; ret = 1; udi = getenv ("HAL_PROP_INFO_UDI"); if (udi == NULL) goto out; dbus_error_init (&error); if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) { printf ("Cannot connect to hald\n"); if (dbus_error_is_set (&error)) { dbus_error_free (&error); } goto out; } cs = libhal_device_new_changeset (udi); if (cs == NULL) { printf ("Cannot initialize changeset\n"); goto out; } g_spawn_command_line_sync ("/usr/bin/pm-is-supported --suspend", NULL, NULL, &exit_status, NULL); can_suspend = (exit_status == 0); g_spawn_command_line_sync ("/usr/bin/pm-is-supported --suspend-hybrid", NULL, NULL, &exit_status, NULL); can_suspend_hybrid = (exit_status == 0); g_spawn_command_line_sync ("/usr/bin/pm-is-supported --hibernate", NULL, NULL, &exit_status, NULL); can_hibernate = (exit_status == 0); libhal_changeset_set_property_bool (cs, "power_management.can_suspend", can_suspend); libhal_changeset_set_property_bool (cs, "power_management.can_suspend_hybrid", can_suspend_hybrid); libhal_changeset_set_property_bool (cs, "power_management.can_hibernate", can_hibernate); libhal_device_commit_changeset (hal_ctx, cs, &error); libhal_device_free_changeset (cs); ret = 0; out: return ret; }
static gboolean hald_addon_bme_setup_hal(void) { DBusError error; gboolean result = FALSE; dbus_error_init(&error); if( !(hal_ctx = libhal_ctx_init_direct(&error)) ) { if ( dbus_error_is_set(&error) ) print_dbus_error("hal context init: %s: %s\n",&error); else log_print(("hal context init: unknown failure\n")); goto out; } if( !(hal_dbus = libhal_ctx_get_dbus_connection(hal_ctx)) ) { log_print("hal context does not have dbus connection\n"); goto out; } udi = getenv("UDI"); if (!udi) { log_print ("hal UDI not set in the environment\n"); goto out; } udi=g_strdup(udi); /* device id */ log_print("UDI: %s",udi); if( !libhal_device_addon_is_ready(hal_ctx,udi,&error) ) { if ( dbus_error_is_set(&error) ) print_dbus_error("hal addon is ready: %s: %s\n",&error); else log_print("hal addon is ready: unknown failure\n"); goto out; } dbus_connection_setup_with_g_main(hal_dbus, FALSE); dbus_connection_set_exit_on_disconnect(hal_dbus ,FALSE); result = TRUE; out: dbus_error_free(&error); return result; }
int main (int argc, char *argv[]) { int fd, rfd; int ret; char *udi; char *device_file, *raw_device_file; char *devpath, *rdevpath; boolean_t is_dos; int dos_num; LibHalContext *ctx = NULL; DBusError error; DBusConnection *conn; char *parent_udi; char *storage_device; char *is_disc_str; int fdc; dbus_bool_t is_disc = FALSE; dbus_bool_t is_floppy = FALSE; unsigned int block_size; dbus_uint64_t vol_size; dbus_bool_t has_data = TRUE; /* probe for fs by default */ dbus_bool_t has_audio = FALSE; char *partition_scheme = NULL; dbus_uint64_t partition_start = 0; int partition_number = 0; struct vtoc vtoc; dk_gpt_t *gpt; struct dk_minfo mi; int i, dos_cnt; fstyp_handle_t fstyp_handle; int systid, relsect, numsect; off_t probe_offset = 0; int num_volumes; char **volumes; dbus_uint64_t v_start; const char *fstype; nvlist_t *fsattr; fd = rfd = -1; ret = 1; if ((udi = getenv ("UDI")) == NULL) { goto out; } if ((device_file = getenv ("HAL_PROP_BLOCK_DEVICE")) == NULL) { goto out; } if ((raw_device_file = getenv ("HAL_PROP_BLOCK_SOLARIS_RAW_DEVICE")) == NULL) { goto out; } if (!dos_to_dev(device_file, &rdevpath, &dos_num)) { rdevpath = raw_device_file; } if (!(is_dos = dos_to_dev(device_file, &devpath, &dos_num))) { devpath = device_file; } if ((parent_udi = getenv ("HAL_PROP_INFO_PARENT")) == NULL) { goto out; } if ((storage_device = getenv ("HAL_PROP_BLOCK_STORAGE_DEVICE")) == NULL) { goto out; } is_disc_str = getenv ("HAL_PROP_VOLUME_IS_DISC"); if (is_disc_str != NULL && strcmp (is_disc_str, "true") == 0) { is_disc = TRUE; } else { is_disc = FALSE; } drop_privileges (); setup_logger (); dbus_error_init (&error); if ((ctx = libhal_ctx_init_direct (&error)) == NULL) goto out; HAL_DEBUG (("Doing probe-volume for %s\n", device_file)); fd = open (devpath, O_RDONLY | O_NONBLOCK); if (fd < 0) { goto out; } rfd = open (rdevpath, O_RDONLY | O_NONBLOCK); if (rfd < 0) { goto out; } /* if it's a floppy with no media, bail out */ if (ioctl(rfd, FDGETCHANGE, &fdc) == 0) { is_floppy = TRUE; if (fdc & FDGC_CURRENT) { goto out; } } /* block size and total size */ if (ioctl(rfd, DKIOCGMEDIAINFO, &mi) != -1) { block_size = mi.dki_lbsize; vol_size = mi.dki_capacity * block_size; } else if (errno == ENXIO) { /* driver supports ioctl, but media is not available */ goto out; } else { /* driver does not support ioctl, e.g. lofi */ block_size = 512; vol_size = 0; } libhal_device_set_property_int (ctx, udi, "volume.block_size", block_size, &error); my_dbus_error_free (&error); libhal_device_set_property_uint64 (ctx, udi, "volume.size", vol_size, &error); my_dbus_error_free (&error); if (is_disc) { if (!probe_disc (rfd, ctx, udi, &has_data, &has_audio)) { HAL_DEBUG (("probe_disc failed, skipping fstyp")); goto out; } /* with audio present, create volume even if fs probing fails */ if (has_audio) { ret = 0; } } if (!has_data) { goto skip_fs; } /* don't support partitioned floppy */ if (is_floppy) { goto skip_part; } /* * first get partitioning info */ if (is_dos) { /* for a dos drive find partition offset */ if (!find_dos_drive(fd, dos_num, &relsect, &numsect, &systid)) { goto out; } partition_scheme = "mbr"; partition_start = (dbus_uint64_t)relsect * 512; partition_number = dos_num; probe_offset = (off_t)relsect * 512; } else { if ((partition_number = read_vtoc(rfd, &vtoc)) >= 0) { if (!vtoc_one_slice_entire_disk(&vtoc)) { partition_scheme = "smi"; if (partition_number < vtoc.v_nparts) { if (vtoc.v_part[partition_number].p_size == 0) { HAL_DEBUG (("zero size partition")); } partition_start = vtoc.v_part[partition_number].p_start * block_size; } } } else if ((partition_number = efi_alloc_and_read(rfd, &gpt)) >= 0) { partition_scheme = "gpt"; if (partition_number < gpt->efi_nparts) { if (gpt->efi_parts[partition_number].p_size == 0) { HAL_DEBUG (("zero size partition")); } partition_start = gpt->efi_parts[partition_number].p_start * block_size; } efi_free(gpt); } probe_offset = 0; } if (partition_scheme != NULL) { libhal_device_set_property_string (ctx, udi, "volume.partition.scheme", partition_scheme, &error); my_dbus_error_free (&error); libhal_device_set_property_int (ctx, udi, "volume.partition.number", partition_number, &error); my_dbus_error_free (&error); libhal_device_set_property_uint64 (ctx, udi, "volume.partition.start", partition_start, &error); my_dbus_error_free (&error); libhal_device_set_property_bool (ctx, udi, "volume.is_partition", TRUE, &error); my_dbus_error_free (&error); } else { libhal_device_set_property_bool (ctx, udi, "volume.is_partition", FALSE, &error); my_dbus_error_free (&error); } /* * ignore duplicate partitions */ if ((volumes = libhal_manager_find_device_string_match ( ctx, "block.storage_device", storage_device, &num_volumes, &error)) != NULL) { my_dbus_error_free (&error); for (i = 0; i < num_volumes; i++) { if (strcmp (udi, volumes[i]) == 0) { continue; /* skip self */ } v_start = libhal_device_get_property_uint64 (ctx, volumes[i], "volume.partition.start", &error); if (dbus_error_is_set(&error)) { dbus_error_free(&error); continue; } if (v_start == partition_start) { HAL_DEBUG (("duplicate partition")); goto out; } } libhal_free_string_array (volumes); } skip_part: /* * now determine fs type * * XXX We could get better performance from block device, * but for now we use raw device because: * * - fstyp_udfs has a bug that it only works on raw * * - sd has a bug that causes extremely slow reads * and incorrect probing of hybrid audio/data media */ if (fstyp_init(rfd, probe_offset, NULL, &fstyp_handle) != 0) { HAL_DEBUG (("fstyp_init failed")); goto out; } if ((fstyp_ident(fstyp_handle, NULL, &fstype) != 0) || (fstyp_get_attr(fstyp_handle, &fsattr) != 0)) { HAL_DEBUG (("fstyp ident or get_attr failed")); fstyp_fini(fstyp_handle); goto out; } set_fstyp_properties (ctx, udi, fstype, fsattr); if (strcmp (fstype, "hsfs") == 0) { hsfs_contents (fd, probe_offset, ctx, udi); } fstyp_fini(fstyp_handle); skip_fs: ret = 0; out: if (fd >= 0) close (fd); if (rfd >= 0) close (rfd); if (ctx != NULL) { my_dbus_error_free (&error); libhal_ctx_shutdown (ctx, &error); libhal_ctx_free (ctx); } return ret; }
int main (int argc, char *argv[]) { char *udi; char *device_file, *raw_device_file; LibHalContext *ctx = NULL; DBusError error; char *bus; char *drive_type; int state, last_state; char *support_media_changed_str; int support_media_changed; int fd = -1; if ((udi = getenv ("UDI")) == NULL) goto out; if ((device_file = getenv ("HAL_PROP_BLOCK_DEVICE")) == NULL) goto out; if ((raw_device_file = getenv ("HAL_PROP_BLOCK_SOLARIS_RAW_DEVICE")) == NULL) goto out; if ((bus = getenv ("HAL_PROP_STORAGE_BUS")) == NULL) goto out; if ((drive_type = getenv ("HAL_PROP_STORAGE_DRIVE_TYPE")) == NULL) goto out; drop_privileges (); setup_logger (); support_media_changed_str = getenv ("HAL_PROP_STORAGE_CDROM_SUPPORT_MEDIA_CHANGED"); if (support_media_changed_str != NULL && strcmp (support_media_changed_str, "true") == 0) support_media_changed = TRUE; else support_media_changed = FALSE; dbus_error_init (&error); if ((ctx = libhal_ctx_init_direct (&error)) == NULL) { goto out; } my_dbus_error_free (&error); if (!libhal_device_addon_is_ready (ctx, udi, &error)) { goto out; } my_dbus_error_free (&error); printf ("Doing addon-storage for %s (bus %s) (drive_type %s) (udi %s)\n", device_file, bus, drive_type, udi); last_state = state = DKIO_NONE; /* Linux version of this addon attempts to re-open the device O_EXCL * every 2 seconds, trying to figure out if some other app, * like a cd burner, is using the device. Aside from questionable * value of this (apps should use HAL's locked property or/and * Solaris in_use facility), but also frequent opens/closes * keeps media constantly spun up. All this needs more thought. */ for (;;) { if (is_mounted (device_file)) { close_device (&fd); sleep (SLEEP_PERIOD); } else if ((fd < 0) && ((fd = open (raw_device_file, O_RDONLY | O_NONBLOCK)) < 0)) { HAL_DEBUG (("open failed for %s: %s", raw_device_file, strerror (errno))); sleep (SLEEP_PERIOD); } else { /* Check if a disc is in the drive */ /* XXX initial call always returns inserted * causing unnecessary rescan - optimize? */ if (ioctl (fd, DKIOCSTATE, &state) == 0) { if (state == last_state) { HAL_DEBUG (("state has not changed %d %s", state, device_file)); continue; } else { HAL_DEBUG (("new state %d %s", state, device_file)); } switch (state) { case DKIO_EJECTED: HAL_DEBUG (("Media removal detected on %s", device_file)); last_state = state; libhal_device_set_property_bool (ctx, udi, "storage.removable.media_available", FALSE, &error); my_dbus_error_free (&error); /* attempt to unmount all childs */ unmount_childs (ctx, udi); /* could have a fs on the main block device; do a rescan to remove it */ libhal_device_rescan (ctx, udi, &error); my_dbus_error_free (&error); break; case DKIO_INSERTED: HAL_DEBUG (("Media insertion detected on %s", device_file)); last_state = state; libhal_device_set_property_bool (ctx, udi, "storage.removable.media_available", TRUE, &error); my_dbus_error_free (&error); /* could have a fs on the main block device; do a rescan to add it */ libhal_device_rescan (ctx, udi, &error); my_dbus_error_free (&error); break; case DKIO_DEV_GONE: HAL_DEBUG (("Device gone detected on %s", device_file)); last_state = state; unmount_childs (ctx, udi); close_device (&fd); goto out; case DKIO_NONE: default: break; } } else { HAL_DEBUG (("DKIOCSTATE failed: %s\n", strerror(errno))); sleep (SLEEP_PERIOD); } } } out: if (ctx != NULL) { my_dbus_error_free (&error); libhal_ctx_shutdown (ctx, &error); libhal_ctx_free (ctx); } return 0; }
int main (int argc, char *argv[]) { off_t address = 0; size_t length = 0; int fd; int state; int retval = 0; struct pci_access *pacc; struct pci_dev *dev; DBusError err; setup_logger (); udi = getenv ("UDI"); HAL_DEBUG (("udi=%s", udi)); if (udi == NULL) { HAL_ERROR (("No device specified")); return -2; } dbus_error_init (&err); if ((halctx = libhal_ctx_init_direct (&err)) == NULL) { HAL_ERROR (("Cannot connect to hald")); retval = -3; goto out; } if (!libhal_device_addon_is_ready (halctx, udi, &err)) { retval = -4; goto out; } conn = libhal_ctx_get_dbus_connection (halctx); dbus_connection_setup_with_g_main (conn, NULL); dbus_connection_set_exit_on_disconnect (conn, 0); dbus_connection_add_filter (conn, filter_function, NULL, NULL); /* Search for the graphics card. */ /* Default values: */ /* address = 0x90300000; */ /* length = 0x20000; */ pacc = pci_alloc(); pci_init(pacc); pci_scan_bus(pacc); for(dev=pacc->devices; dev; dev=dev->next) { /* Iterate over all devices */ pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES); if ((dev->vendor_id == 0x1002) && (dev->device_id == 0x71c5)) { // ATI X1600 address = dev->base_addr[2]; length = dev->size[2]; } } pci_cleanup(pacc); HAL_DEBUG (("addr 0x%x len=%d", address, length)); if (!address) { HAL_DEBUG (("Failed to detect ATI X1600, aborting...")); retval = 1; goto out; } fd = open ("/dev/mem", O_RDWR); if (fd < 0) { HAL_DEBUG (("cannot open /dev/mem")); retval = 1; goto out; } memory = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, address); if (memory == MAP_FAILED) { HAL_ERROR (("mmap failed")); retval = 2; goto out; } /* Is it really necessary ? */ OUTREG(0x4dc, 0x00000005); state = INREG(0x7ae4); OUTREG(0x7ae4, state); /* Allow access to porta 0x300 through 0x304 */ if (ioperm (0x300, 5, 1) < 0) { HAL_ERROR (("ioperm failed (you should be root).")); exit(1); } /* this works because we hardcoded the udi's in the <spawn> in the fdi files */ if (!libhal_device_claim_interface (halctx, "/org/freedesktop/Hal/devices/macbook_pro_lcd_panel", "org.freedesktop.Hal.Device.LaptopPanel", " <method name=\"SetBrightness\">\n" " <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n" " <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n" " </method>\n" " <method name=\"GetBrightness\">\n" " <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n" " </method>\n", &err)) { HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.LaptopPanel'")); retval = -4; goto out; } if (!libhal_device_claim_interface (halctx, "/org/freedesktop/Hal/devices/macbook_pro_light_sensor", "org.freedesktop.Hal.Device.LightSensor", " <method name=\"GetBrightness\">\n" " <arg name=\"brightness_value\" direction=\"out\" type=\"ai\"/>\n" " </method>\n", &err)) { HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.LightSensor'")); retval = -4; goto out; } if (!libhal_device_claim_interface (halctx, "/org/freedesktop/Hal/devices/macbook_pro_keyboard_backlight", "org.freedesktop.Hal.Device.KeyboardBacklight", " <method name=\"GetBrightness\">\n" " <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n" " </method>\n" " <method name=\"SetBrightness\">\n" " <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n" " </method>\n", &err)) { HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.KeyboardBacklight'")); retval = -4; goto out; } main_loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (main_loop); return 0; out: HAL_DEBUG (("An error occured, exiting cleanly")); LIBHAL_FREE_DBUS_ERROR (&err); if (halctx != NULL) { libhal_ctx_shutdown (halctx, &err); LIBHAL_FREE_DBUS_ERROR (&err); libhal_ctx_free (halctx); } return retval; }
int main (int argc, char *argv[]) { DBusError error; LibHalContext *ctx_direct; DBusConnection *con_direct; char *bus; char *drive_type; char *support_media_changed_str; char *str; hal_set_proc_title_init (argc, argv); /* We could drop privs if we know that the haldaemon user is * to be able to access block devices... */ /*drop_privileges (1);*/ if ((udi = getenv ("UDI")) == NULL) goto out; if ((device_file = getenv ("HAL_PROP_BLOCK_DEVICE")) == NULL) goto out; if ((bus = getenv ("HAL_PROP_STORAGE_BUS")) == NULL) goto out; if ((drive_type = getenv ("HAL_PROP_STORAGE_DRIVE_TYPE")) == NULL) goto out; setup_logger (); support_media_changed_str = getenv ("HAL_PROP_STORAGE_CDROM_SUPPORT_MEDIA_CHANGED"); if (support_media_changed_str != NULL && strcmp (support_media_changed_str, "true") == 0) support_media_changed = TRUE; else support_media_changed = FALSE; dbus_error_init (&error); con = dbus_bus_get (DBUS_BUS_SYSTEM, &error); if (con == NULL) { HAL_ERROR (("Cannot connect to system bus")); goto out; } loop = g_main_loop_new (NULL, FALSE); dbus_connection_setup_with_g_main (con, NULL); dbus_connection_set_exit_on_disconnect (con, 0); if ((ctx_direct = libhal_ctx_init_direct (&error)) == NULL) { HAL_ERROR (("Cannot connect to hald")); goto out; } if (!libhal_device_addon_is_ready (ctx_direct, udi, &error)) { goto out; } con_direct = libhal_ctx_get_dbus_connection (ctx_direct); dbus_connection_setup_with_g_main (con_direct, NULL); dbus_connection_set_exit_on_disconnect (con_direct, 0); dbus_connection_add_filter (con_direct, direct_filter_function, NULL, NULL); if ((ctx = libhal_ctx_init_direct (&error)) == NULL) goto out; if (!libhal_device_addon_is_ready (ctx, udi, &error)) goto out; HAL_DEBUG (("**************************************************")); HAL_DEBUG (("Doing addon-storage for %s (bus %s) (drive_type %s) (udi %s)", device_file, bus, drive_type, udi)); HAL_DEBUG (("**************************************************")); if (strcmp (drive_type, "cdrom") == 0) is_cdrom = 1; else is_cdrom = 0; media_status = MEDIA_STATUS_UNKNOWN; #ifdef HAVE_CONKIT /* TODO: ideally we should track the sessions on the seats on * which the device belongs to. But right now we don't really * do multi-seat so I'm going to punt on this for now. */ get_system_idle_from_ck (); dbus_bus_add_match (con, "type='signal'" ",interface='org.freedesktop.ConsoleKit.Manager'" ",sender='org.freedesktop.ConsoleKit'" ",member='SystemIdleHintChanged'", NULL); dbus_bus_add_match (con, "type='signal'" ",interface='org.freedesktop.ConsoleKit.Seat'" ",sender='org.freedesktop.ConsoleKit'" ",member='ActiveSessionChanged'", NULL); #endif /* this is a bit weird; but we want to listen to signals about * locking from hald.. and signals are not pushed over direct * connections (for a good reason). */ dbus_bus_add_match (con, "type='signal'" ",interface='org.freedesktop.Hal.Manager'" ",sender='org.freedesktop.Hal'", NULL); dbus_bus_add_match (con, "type='signal'" ",interface='org.freedesktop.Hal.Manager'" ",sender='org.freedesktop.Hal'", NULL); str = g_strdup_printf ("type='signal'" ",interface='org.freedesktop.Hal.Device'" ",sender='org.freedesktop.Hal'" ",path='%s'", udi); dbus_bus_add_match (con, str, NULL); g_free (str); dbus_connection_add_filter (con, dbus_filter_function, NULL, NULL); if (!libhal_device_claim_interface (ctx, udi, "org.freedesktop.Hal.Device.Storage.Removable", " <method name=\"CheckForMedia\">\n" " <arg name=\"call_had_sideeffect\" direction=\"out\" type=\"b\"/>\n" " </method>\n", &error)) { HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.Storage.Removable'")); goto out; } update_polling_interval (); g_main_loop_run (loop); out: HAL_DEBUG (("An error occured, exiting cleanly")); LIBHAL_FREE_DBUS_ERROR (&error); if (ctx != NULL) { libhal_ctx_shutdown (ctx, &error); LIBHAL_FREE_DBUS_ERROR (&error); libhal_ctx_free (ctx); } return 0; }
/** * main: * @argc: Number of arguments given to program * @argv: Arguments given to program * * Returns: Return code * * Main entry point */ int main (int argc, char *argv[]) { dbus_bool_t rc = 0; char *udi = NULL; char *key = NULL; char *str_value = NULL; dbus_int32_t int_value = 0; dbus_uint64_t uint64_value = 0; double double_value = 0.0f; dbus_bool_t bool_value = TRUE; dbus_bool_t remove = FALSE; dbus_bool_t is_version = FALSE; int type = PROP_INVALID; DBusError error; dbus_bool_t direct = FALSE; 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}, {"int", 1, NULL, 0}, {"uint64", 1, NULL, 0}, {"string", 1, NULL, 0}, {"double", 1, NULL, 0}, {"bool", 1, NULL, 0}, {"strlist-pre", 1, NULL, 0}, {"strlist-post", 1, NULL, 0}, {"strlist-rem", 1, NULL, 0}, {"direct", 0, NULL, 0}, {"remove", 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, "key") == 0) { key = strdup (optarg); } else if (strcmp (opt, "string") == 0) { str_value = strdup (optarg); type = PROP_STRING; } else if (strcmp (opt, "int") == 0) { int_value = strtol (optarg, NULL, 0); type = PROP_INT; } else if (strcmp (opt, "uint64") == 0) { uint64_value = strtoull (optarg, NULL, 0); type = PROP_UINT64; } else if (strcmp (opt, "double") == 0) { double_value = (double) atof (optarg); type = PROP_DOUBLE; } else if (strcmp (opt, "bool") == 0) { if (strcmp (optarg, "true") == 0) bool_value = TRUE; else if (strcmp (optarg, "false") == 0) bool_value = FALSE; else { usage (argc, argv); return 1; } type = PROP_BOOL; } else if (strcmp (opt, "strlist-pre") == 0) { str_value = strdup (optarg); type = PROP_STRLIST_PRE; } else if (strcmp (opt, "strlist-post") == 0) { str_value = strdup (optarg); type = PROP_STRLIST_POST; } else if (strcmp (opt, "strlist-rem") == 0) { str_value = strdup (optarg); type = PROP_STRLIST_REM; } else if (strcmp (opt, "remove") == 0) { remove = TRUE; } else if (strcmp (opt, "direct") == 0) { direct = TRUE; } else if (strcmp (opt, "udi") == 0) { udi = strdup (optarg); } else if (strcmp (opt, "version") == 0) { is_version = TRUE; } break; default: usage (argc, argv); return 1; break; } } if (is_version) { printf ("hal-set-property " PACKAGE_VERSION "\n"); return 0; } /* must have at least one, but not neither or both */ if ((remove && type != PROP_INVALID) || ((!remove) && type == PROP_INVALID)) { usage (argc, argv); return 1; } fprintf (stderr, "\n"); dbus_error_init (&error); if (direct) { if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) { fprintf (stderr, "error: libhal_ctx_init_direct\n"); LIBHAL_FREE_DBUS_ERROR (&error); return 1; } } else { 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); dbus_error_free (&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; } } if (remove) { rc = libhal_device_remove_property (hal_ctx, udi, key, &error); if (!rc) { fprintf (stderr, "error: libhal_device_remove_property: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 1; } } else { switch (type) { case PROP_STRING: rc = libhal_device_set_property_string (hal_ctx, udi, key, str_value, &error); break; case PROP_INT: rc = libhal_device_set_property_int (hal_ctx, udi, key, int_value, &error); break; case PROP_UINT64: rc = libhal_device_set_property_uint64 (hal_ctx, udi, key, uint64_value, &error); break; case PROP_DOUBLE: rc = libhal_device_set_property_double (hal_ctx, udi, key, double_value, &error); break; case PROP_BOOL: rc = libhal_device_set_property_bool (hal_ctx, udi, key, bool_value, &error); break; case PROP_STRLIST_PRE: rc = libhal_device_property_strlist_prepend (hal_ctx, udi, key, str_value, &error); break; case PROP_STRLIST_POST: rc = libhal_device_property_strlist_append (hal_ctx, udi, key, str_value, &error); break; case PROP_STRLIST_REM: rc = libhal_device_property_strlist_remove (hal_ctx, udi, key, str_value, &error); break; } if (!rc) { fprintf (stderr, "error: libhal_device_set_property: %s: %s\n", error.name, error.message); dbus_error_free (&error); return 1; } } return rc ? 0 : 1; }
int main(int argc, char *argv[]) { int ret = 1; char *udi; char *printer_address, *community; DBusError error; LibHalContext *ctx = NULL; LibHalChangeSet *cs = NULL; char *manufacturer = NULL, *model = NULL, *serial_number = NULL, *description = NULL, **command_set = NULL, *device_uri = NULL; extern int snmp_printer_info(char *hostname, char *community, char **manufacturer, char **model, char **description, char **serial_number, char ***command_set, char **device_uri); dbus_error_init(&error); if ((udi = getenv("UDI")) == NULL) goto out; printer_address = getenv("HAL_PROP_NETWORK_DEVICE_ADDRESS"); if (printer_address == NULL) goto out; community = getenv("HAL_PROP_NETWORK_DEVICE_SNMP_COMMUNITY"); if (community == NULL) community = "public"; setup_logger(); dbus_error_init(&error); if ((ctx = libhal_ctx_init_direct(&error)) == NULL) goto out; if ((cs = libhal_device_new_changeset(udi)) == NULL) { HAL_DEBUG(("Cannot allocate changeset")); goto out; } /* Probe the printer for characteristics via SNMP */ ret = snmp_printer_info(printer_address, community, &manufacturer, &model, &description, &serial_number, &command_set, &device_uri); if (ret < 0) { HAL_DEBUG(("Cannot get snmp data for %s: %s", printer_address, strerror(errno))); goto out; } /* Add printer characteristics to the HAL device tree */ ret = add_printer_info(cs, udi, manufacturer, model, description, serial_number, command_set, device_uri); if (ret < 0) { HAL_DEBUG(("Cannot add printer data for %s to %s: %s", printer_address, udi, strerror(errno))); goto out; } libhal_device_commit_changeset(ctx, cs, &error); ret = 0; out: if (cs != NULL) { libhal_device_free_changeset(cs); } if (ctx != NULL) { if (dbus_error_is_set(&error)) { dbus_error_free(&error); } libhal_ctx_shutdown(ctx, &error); libhal_ctx_free(ctx); } return (ret); }
/** * main: * @argc: Number of arguments given to program * @argv: Arguments given to program * * Returns: Return code * * Main entry point */ int main (int argc, char *argv[]) { int ret; DBusError error; char buf[512]; char *nbuf; int dmipipe[2]; int nullfd; FILE *f; int dmiparser_state = DMIPARSER_STATE_IGNORE; /* on some system chassis pops up several times, * so only take the first entry for each */ int dmiparser_done_bios = FALSE; int dmiparser_done_system = FALSE; int dmiparser_done_chassis = FALSE; uint i; struct stat s; const char *path = NULL; const char *possible_paths[] = { "/usr/sbin/dmidecode", "/bin/dmidecode", "/sbin/dmidecode", "/usr/local/sbin/dmidecode", }; /* assume failure */ ret = 1; setup_logger (); dbus_error_init (&error); udi = getenv ("UDI"); if (udi == NULL) { HAL_ERROR (("UDI not set")); goto out; } if ((ctx = libhal_ctx_init_direct (&error)) == NULL) { HAL_ERROR (("ctx init failed")); goto out; } /* find the path to dmidecode */ for (i = 0; i < sizeof (possible_paths) / sizeof (char *); i++) { if (stat (possible_paths[i], &s) == 0 && S_ISREG (s.st_mode)) { path = possible_paths[i]; break; } } if (path == NULL) { HAL_ERROR(("Could not find dmidecode, exit!")); exit(1); } if(pipe (dmipipe) == -1) { HAL_ERROR(("Could not create pipe (error: '%s'), exit!", strerror(errno))); exit(1); } if ((f = fdopen (dmipipe[0], "r")) == NULL) { HAL_ERROR(("Could not open file (error: '%s'), exit!", strerror(errno))); exit(1); } if ((nullfd = open ("/dev/null", O_RDONLY)) == -1){ HAL_ERROR(("Could not open /dev/null (error: '%s'), exit!", strerror(errno))); exit(1); } /* fork the child process */ switch (fork ()) { case 0: /* child */ dup2 (nullfd, STDIN_FILENO); dup2 (dmipipe[1], STDOUT_FILENO); close (dmipipe[0]); close (dmipipe[1]); /* execute the child */ execl (path, path, NULL); /* throw an error if we ever reach this point */ HAL_ERROR (("Failed to execute dmidecode!")); exit (1); break; case -1: HAL_ERROR (("Cannot fork!")); goto out; } /* parent continues from here */ /* close unused descriptor */ close (dmipipe[1]); /* read the output of the child */ while(fgets (buf, sizeof(buf), f) != NULL) { int j; unsigned int len; unsigned int tabs = 0; /* trim whitespace */ len = strlen (buf); /* check that will fit in buffer */ if (len >= sizeof (buf)) continue; /* not big enough for data, and protects us from underflow */ if (len < 3) { dmiparser_state = DMIPARSER_STATE_IGNORE; continue; } /* find out number of leading tabs */ if (buf[0] == '\t' && buf[1] == '\t') tabs = 2; /* this is list data */ else if (buf[0] == '\t') tabs = 1; /* this is data, 0 is section type */ if (tabs == 2) /* we do not proccess data at depth 2 */ continue; /* set the section type */ if (tabs == 0) { if (!dmiparser_done_bios && strbegin (buf, "BIOS Information")) dmiparser_state = DMIPARSER_STATE_BIOS; else if (!dmiparser_done_system && strbegin (buf, "System Information")) dmiparser_state = DMIPARSER_STATE_SYSTEM; else if (!dmiparser_done_chassis && strbegin (buf, "Chassis Information")) dmiparser_state = DMIPARSER_STATE_CHASSIS; else if (!dmiparser_done_chassis && strbegin (buf, "Base Board Information")) dmiparser_state = DMIPARSER_STATE_BOARD; else /* * We do not match the other sections, * or sections we have processed before */ dmiparser_state = DMIPARSER_STATE_IGNORE; continue; /* next line */ } /* we are not in a section we know, no point continueing */ if (dmiparser_state == DMIPARSER_STATE_IGNORE) continue; /* return success only if there was something usefull to parse */ ret = 0; /* removes the leading tab */ nbuf = &buf[1]; /* removes the trailing spaces */ for (j = len - 2; isspace (nbuf[j]) && j >= 0; --j) nbuf[j] = '\0'; if (dmiparser_state == DMIPARSER_STATE_BIOS) { setstr (nbuf, "Vendor:", "system.firmware.vendor"); setstr (nbuf, "Version:", "system.firmware.version"); setstr (nbuf, "Release Date:", "system.firmware.release_date"); dmiparser_done_bios = TRUE; } else if (dmiparser_state == DMIPARSER_STATE_SYSTEM) { setstr (nbuf, "Manufacturer:", "system.hardware.vendor"); setstr (nbuf, "Product Name:", "system.hardware.product"); setstr (nbuf, "Version:", "system.hardware.version"); setstr (nbuf, "Serial Number:", "system.hardware.serial"); setstr (nbuf, "UUID:", "system.hardware.uuid"); dmiparser_done_system = TRUE; } else if (dmiparser_state == DMIPARSER_STATE_CHASSIS) { setstr (nbuf, "Manufacturer:", "system.chassis.manufacturer"); setstr (nbuf, "Type:", "system.chassis.type"); dmiparser_done_chassis = TRUE; } else if (dmiparser_state == DMIPARSER_STATE_BOARD) { setstr (nbuf, "Manufacturer:", "system.board.vendor"); setstr (nbuf, "Product Name:", "system.board.product"); setstr (nbuf, "Version:", "system.board.version"); setstr (nbuf, "Serial Number:", "system.board.serial"); dmiparser_done_system = TRUE; } } /* as read to EOF, close */ fclose (f); out: LIBHAL_FREE_DBUS_ERROR (&error); /* free ctx */ if (ctx != NULL) { libhal_ctx_shutdown (ctx, &error); LIBHAL_FREE_DBUS_ERROR (&error); libhal_ctx_free (ctx); } return ret; }
int main (int argc, char *argv[]) { DBusError err; int retval = 0; setup_logger (); setup_cb (); udi = getenv ("UDI"); HAL_DEBUG (("udi=%s", udi)); if (udi == NULL) { HAL_ERROR (("No device specified")); return -2; } dbus_error_init (&err); if ((halctx = libhal_ctx_init_direct (&err)) == NULL) { HAL_ERROR (("Cannot connect to hald")); retval = -3; goto out; } conn = libhal_ctx_get_dbus_connection (halctx); dbus_connection_setup_with_g_main (conn, NULL); dbus_connection_add_filter (conn, filter_function, NULL, NULL); if (!libhal_device_claim_interface (halctx, "/org/freedesktop/Hal/devices/omapfb_bl", "org.freedesktop.Hal.Device.LaptopPanel", " <method name=\"SetBrightness\">\n" " <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n" " <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n" " </method>\n" " <method name=\"GetBrightness\">\n" " <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n" " </method>\n", &err)) { HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.LaptopPanel'")); retval = -4; goto out; } if (!libhal_device_addon_is_ready (halctx, udi, &err)) { retval = -4; goto out; } bl_data.backlight_init (&bl_data); main_loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (main_loop); return 0; out: HAL_DEBUG (("An error occured, exiting cleanly")); LIBHAL_FREE_DBUS_ERROR (&err); if (halctx != NULL) { libhal_ctx_shutdown (halctx, &err); LIBHAL_FREE_DBUS_ERROR (&err); libhal_ctx_free (halctx); } return retval; }
int main (int argc, char *argv[]) { DBusError err; int retval = 0; hal_set_proc_title_init (argc, argv); setup_logger (); device_udi = getenv ("UDI"); HAL_DEBUG (("device:[%s]", device_udi)); if (device_udi == NULL) { HAL_ERROR (("No device specified")); return -2; } dbus_error_init (&err); if ((halctx = libhal_ctx_init_direct (&err)) == NULL) { HAL_ERROR (("Cannot connect to hald")); retval = -3; goto out; } /* update_properties */ libhal_device_set_property_bool (halctx, device_udi, "battery.present", TRUE, &err); LIBHAL_FREE_DBUS_ERROR (&err); if (!libhal_device_property_exists (halctx, device_udi, "battery.is_rechargeable", &err)) { LIBHAL_FREE_DBUS_ERROR (&err); libhal_device_set_property_bool (halctx, device_udi, "battery.is_rechargeable", FALSE, &err); } LIBHAL_FREE_DBUS_ERROR (&err); libhal_device_set_property_int (halctx, device_udi, "battery.charge_level.design", 7, &err); LIBHAL_FREE_DBUS_ERROR (&err); libhal_device_set_property_int (halctx, device_udi, "battery.charge_level.last_full", 7, &err); LIBHAL_FREE_DBUS_ERROR (&err); libhal_device_set_property_string (halctx, device_udi, "info.category", "battery", &err); LIBHAL_FREE_DBUS_ERROR (&err); libhal_device_set_property_string (halctx, device_udi, "battery.command_interface", "csr", &err); /* monitor change */ libhal_ctx_set_device_property_modified (halctx, property_modified); /* Initial fillup */ dev_props = property_cache_item_get (device_udi); HAL_ERROR (("** Initial fillup done")); /* init usb */ usb_init (); /* do coldplug */ check_all_batteries (NULL); /* only add capability when initial charge_level key has been set */ LIBHAL_FREE_DBUS_ERROR (&err); libhal_device_add_capability (halctx, device_udi, "battery", &err); LIBHAL_FREE_DBUS_ERROR (&err); if (!libhal_device_addon_is_ready (halctx, device_udi, &err)) { retval = -4; goto out; } hal_set_proc_title ("hald-addon-usb-csr: listening on '%s'", libhal_device_get_property_string(halctx, device_udi, "info.product", &err)); main_loop = g_main_loop_new (NULL, FALSE); #ifdef HAVE_GLIB_2_14 g_timeout_add_seconds (TIMEOUT, check_all_batteries, NULL); #else g_timeout_add (1000L * TIMEOUT, check_all_batteries, NULL); #endif g_main_loop_run (main_loop); return 0; out: HAL_DEBUG (("An error occured, exiting cleanly")); LIBHAL_FREE_DBUS_ERROR (&err); if (halctx != NULL) { libhal_ctx_shutdown (halctx, &err); LIBHAL_FREE_DBUS_ERROR (&err); libhal_ctx_free (halctx); } return retval; }
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; }