void CHALManager::Stop() { if (g_advancedSettings.m_handleMounting) { // Unmount all media XBMC have mounted for (unsigned int i = 0; i < m_Volumes.size(); i++) { if (m_Volumes[i].MountedByXBMC && m_Volumes[i].Mounted) { CLog::Log(LOGNOTICE, "HAL: Unmounts %s", m_Volumes[i].FriendlyName.c_str()); UnMount(m_Volumes[i]); } } } m_Volumes.clear(); if (m_Context != NULL) libhal_ctx_shutdown(m_Context, NULL); if (m_Context != NULL) libhal_ctx_free(m_Context); if (m_DBusSystemConnection != NULL) { dbus_connection_unref(m_DBusSystemConnection); m_DBusSystemConnection = NULL; } dbus_error_free(&m_Error); // Needed? }
static void deinit(void) { struct device_t *iter, *tmp; if (hal_ctx) { libhal_ctx_shutdown(hal_ctx, (DBusError *)NULL); libhal_ctx_free(hal_ctx); } dbus_connection_unref(dbus_conn); iter = head; while (iter) { tmp = iter; if (!is_mounted(iter)) /* don't care to check the return values */ do_umount(iter); rmdir(iter->mountp); if (iter->should_remove_entry) remove_fstab_entry(iter); iter = iter->next; free_device(tmp); } if (pid_fd >= 0) if (close(pid_fd) < 0) /* releases lock */ syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__, strerror(errno)); closelog(); }
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; }
static char * uuid_get_from_hal(void) { LibHalContext *ctx; DBusError error; DBusConnection *con; dbus_error_init(&error); if (!(con = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) ) { goto bailout_nobus; } ctx = libhal_ctx_new(); libhal_ctx_set_dbus_connection(ctx, con); if (!libhal_ctx_init(ctx, &error)) { goto bailout; } if (!libhal_device_property_exists(ctx, UUID_PATH, UUID_PROPERTY, &error)) { goto bailout; } char *uuid = libhal_device_get_property_string(ctx, UUID_PATH, UUID_PROPERTY, &error); if (looks_like_a_uuid (uuid)) { return uuid; } bailout: { DBusError ctxerror; dbus_error_init(&ctxerror); if (!(libhal_ctx_shutdown(ctx, &ctxerror))) { dbus_error_free(&ctxerror); } } libhal_ctx_free(ctx); //dbus_connection_unref(con); bailout_nobus: if (dbus_error_is_set(&error)) { /*printf("Error %s\n", error.name);*/ dbus_error_free(&error); } return NULL; }
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); }
static void impl_deactivate (PeasActivatable *bplugin) { RBMtpPlugin *plugin = RB_MTP_PLUGIN (bplugin); GtkUIManager *uimanager = NULL; RBRemovableMediaManager *rmm = NULL; RBShell *shell; g_object_get (plugin, "object", &shell, NULL); g_object_get (shell, "ui-manager", &uimanager, "removable-media-manager", &rmm, NULL); gtk_ui_manager_remove_ui (uimanager, plugin->ui_merge_id); gtk_ui_manager_remove_action_group (uimanager, plugin->action_group); g_list_foreach (plugin->mtp_sources, (GFunc)rb_display_page_delete_thyself, NULL); g_list_free (plugin->mtp_sources); plugin->mtp_sources = NULL; #if defined(HAVE_GUDEV) g_signal_handler_disconnect (rmm, plugin->create_device_source_id); plugin->create_device_source_id = 0; #else if (plugin->hal_context != NULL) { DBusError error; dbus_error_init (&error); libhal_ctx_shutdown (plugin->hal_context, &error); libhal_ctx_free (plugin->hal_context); dbus_error_free (&error); plugin->hal_context = NULL; } if (plugin->dbus_connection != NULL) { dbus_connection_unref (plugin->dbus_connection); plugin->dbus_connection = NULL; } #endif g_object_unref (uimanager); g_object_unref (rmm); g_object_unref (shell); }
static void disconnect_hook(void *data) { DBusError error; struct config_hal_info *info = data; if (info->hal_ctx) { dbus_error_init(&error); if (!libhal_ctx_shutdown(info->hal_ctx, &error)) DebugF("[config/hal] couldn't shut down context: %s (%s)\n", error.name, error.message); libhal_ctx_free(info->hal_ctx); dbus_error_free(&error); } info->hal_ctx = NULL; info->system_bus = NULL; }
static void disconnect_hook(void *data) { DBusError error; struct config_hal_info *info = data; if (info->hal_ctx) { if (dbus_connection_get_is_connected(info->system_bus)) { dbus_error_init(&error); if (!libhal_ctx_shutdown(info->hal_ctx, &error)) LogMessage(X_WARNING, "config/hal: disconnect_hook couldn't shut down context: %s (%s)\n", error.name, error.message); dbus_error_free(&error); } libhal_ctx_free(info->hal_ctx); } info->hal_ctx = NULL; info->system_bus = NULL; }
static void thunar_vfs_volume_manager_hal_finalize (GObject *object) { ThunarVfsVolumeManagerHal *manager_hal = THUNAR_VFS_VOLUME_MANAGER_HAL (object); GList *lp; /* release all active volumes */ for (lp = manager_hal->volumes; lp != NULL; lp = lp->next) g_object_unref (G_OBJECT (lp->data)); g_list_free (manager_hal->volumes); /* shutdown the HAL context */ if (G_LIKELY (manager_hal->context != NULL)) { libhal_ctx_shutdown (manager_hal->context, NULL); libhal_ctx_free (manager_hal->context); } /* shutdown the D-BUS connection */ if (G_LIKELY (manager_hal->dbus_connection != NULL)) dbus_connection_unref (manager_hal->dbus_connection); (*G_OBJECT_CLASS (thunar_vfs_volume_manager_hal_parent_class)->finalize) (object); }
int main(int argc, char **argv) { DBusError error; DBusConnection *conn; LibHalContext *hal_ctx; int err; int c; int all = 0; int list = 0; int dev = 0; char *dev_name; if (argc < 2) { return (0); } while ((c = getopt(argc, argv, "ald:")) != EOF) { switch (c) { case 'a': all = 1; break; case 'l': list = 1; break; case 'd': dev = 1; dev_name = optarg; break; default: exit(1); } } dbus_error_init(&error); if (!(conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) { fprintf(stderr, "error: dbus_bus_get: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR(&error); return (2); } if (!(hal_ctx = libhal_ctx_new())) { return (3); } if (!libhal_ctx_set_dbus_connection(hal_ctx, conn)) { return (4); } 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 (5); } err = 0; if (list) { err = list_battery_devices(hal_ctx); } if (all) { err = dump_all_devices(hal_ctx); } if (dev) { err = dump_device(hal_ctx, dev_name); } libhal_ctx_shutdown(hal_ctx, &error); libhal_ctx_free(hal_ctx); dbus_connection_unref(conn); dbus_error_free(&error); return (err); }
std::map<int, std::pair<std::string, std::string> > SerialPortEnumerator::getPorts() { std::map<int, std::pair<std::string, std::string> > ports; #ifdef MACOSX // use IOKit to enumerates devices // get a matching dictionary to specify which IOService class we're interested in CFMutableDictionaryRef classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue); if (classesToMatch == NULL) throw DashelException(DashelException::EnumerationError, 0, "IOServiceMatching returned a NULL dictionary"); // specify all types of serial devices CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes)); // get an iterator to serial port services io_iterator_t matchingServices; kern_return_t kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatch, &matchingServices); if (KERN_SUCCESS != kernResult) throw DashelException(DashelException::EnumerationError, kernResult, "IOServiceGetMatchingServices failed"); // iterate over services io_object_t modemService; int index = 0; while((modemService = IOIteratorNext(matchingServices))) { // get path for device CFTypeRef bsdPathAsCFString = IORegistryEntryCreateCFProperty(modemService, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0); if (bsdPathAsCFString) { std::string path; char cStr[255]; std::string name; bool res = CFStringGetCString((CFStringRef) bsdPathAsCFString, cStr, 255, kCFStringEncodingUTF8); if(res) path = cStr; else throw DashelException(DashelException::EnumerationError, 0, "CFStringGetCString failed"); CFRelease(bsdPathAsCFString); CFTypeRef fn = IORegistryEntrySearchCFProperty(modemService, kIOServicePlane, CFSTR("USB Product Name"), kCFAllocatorDefault, kIORegistryIterateRecursively | kIORegistryIterateParents); if(fn) { res = CFStringGetCString((CFStringRef) fn, cStr, 255, kCFStringEncodingUTF8); if(res) name = cStr; else throw DashelException(DashelException::EnumerationError, 0, "CFStringGetString failed"); CFRelease(fn); } else name = "Serial Port"; name = name + " (" + path + ")"; ports[index++] = std::make_pair<std::string, std::string>(path, name); } else throw DashelException(DashelException::EnumerationError, 0, "IORegistryEntryCreateCFProperty returned a NULL path"); // release service IOObjectRelease(modemService); } IOObjectRelease(matchingServices); #elif defined(USE_LIBUDEV) struct udev *udev; struct udev_enumerate *enumerate; struct udev_list_entry *devices, *dev_list_entry; struct udev_device *dev; int index = 0; udev = udev_new(); if(!udev) throw DashelException(DashelException::EnumerationError, 0, "Cannot create udev context"); enumerate = udev_enumerate_new(udev); udev_enumerate_add_match_subsystem(enumerate, "tty"); udev_enumerate_scan_devices(enumerate); devices = udev_enumerate_get_list_entry(enumerate); udev_list_entry_foreach(dev_list_entry, devices) { const char *sysfs_path; struct udev_device *usb_dev; const char * path; struct stat st; unsigned int maj,min; /* Get sysfs path and create the udev device */ sysfs_path = udev_list_entry_get_name(dev_list_entry); dev = udev_device_new_from_syspath(udev, sysfs_path); // Some sanity check path = udev_device_get_devnode(dev); if(stat(path, &st)) throw DashelException(DashelException::EnumerationError, 0, "Cannot stat serial port"); if(!S_ISCHR(st.st_mode)) throw DashelException(DashelException::EnumerationError, 0, "Serial port is not character device"); // Get the major/minor number maj = major(st.st_rdev); min = minor(st.st_rdev); // Ignore all the non physical ports if(!(maj == 2 || (maj == 4 && min < 64) || maj == 3 || maj == 5)) { ostringstream oss; // Check if usb, if yes get the device name usb_dev = udev_device_get_parent_with_subsystem_devtype(dev,"usb","usb_device"); if(usb_dev) oss << udev_device_get_sysattr_value(usb_dev,"product"); else oss << "Serial Port"; oss << " (" << path << ")"; ports[index++] = std::make_pair<std::string, std::string>(path,oss.str()); } udev_device_unref(dev); } udev_enumerate_unref(enumerate); udev_unref(udev); #elif defined(USE_HAL) // use HAL to enumerates devices DBusConnection* dbusConnection = dbus_bus_get(DBUS_BUS_SYSTEM, 0); if (!dbusConnection) throw DashelException(DashelException::EnumerationError, 0, "cannot connect to D-BUS."); LibHalContext* halContext = libhal_ctx_new(); if (!halContext) throw DashelException(DashelException::EnumerationError, 0, "cannot create HAL context: cannot create context"); if (!libhal_ctx_set_dbus_connection(halContext, dbusConnection)) throw DashelException(DashelException::EnumerationError, 0, "cannot create HAL context: cannot connect to D-BUS"); if (!libhal_ctx_init(halContext, 0)) throw DashelException(DashelException::EnumerationError, 0, "cannot create HAL context: cannot init context"); int devicesCount; char** devices = libhal_find_device_by_capability(halContext, "serial", &devicesCount, 0); for (int i = 0; i < devicesCount; i++) { char* devFileName = libhal_device_get_property_string(halContext, devices[i], "serial.device", 0); char* info = libhal_device_get_property_string(halContext, devices[i], "info.product", 0); int port = libhal_device_get_property_int(halContext, devices[i], "serial.port", 0); ostringstream oss; oss << info << " " << port; ports[devicesCount - i] = std::make_pair<std::string, std::string>(devFileName, oss.str()); libhal_free_string(info); libhal_free_string(devFileName); } libhal_free_string_array(devices); libhal_ctx_shutdown(halContext, 0); libhal_ctx_free(halContext); #endif return ports; };
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; }
/* * gst_hal_get_string: * @udi: a #gchar corresponding to the UDI you want to get. * @device_type: a #GstHalDeviceType specifying the wanted device type. * * Get Hal UDI @udi's string value. * * Returns: a newly allocated #gchar string containing the appropriate pipeline * for UDI @udi, or NULL in the case of an error.. */ static gchar * gst_hal_get_string (const gchar * udi, GstHalDeviceType device_type) { DBusError error; LibHalContext *ctx; char *string = NULL; /* Don't query HAL for NULL UDIs. Passing NULL as UDI to HAL gives * an assertion failure in D-Bus when running with * DBUS_FATAL_WARNINGS=1. */ if (!udi) return NULL; dbus_error_init (&error); ctx = libhal_ctx_new (); /* Should only happen on OOM */ g_return_val_if_fail (ctx != NULL, NULL); if (!libhal_ctx_set_dbus_connection (ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) { GST_DEBUG ("Unable to set DBus connection: %s: %s", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); goto ctx_free; } if (!libhal_ctx_init (ctx, &error)) { GST_DEBUG ("Unable to set init HAL context: %s: %s", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); goto ctx_free; } /* Now first check if UDI is an alsa device, then oss and then * check the childs of the given device. If there are alsa and oss * children the first alsa one is used. */ string = gst_hal_get_alsa_element (ctx, udi, device_type); if (!string) string = gst_hal_get_oss_element (ctx, udi, device_type); if (!string) { int num_childs; char **childs = NULL; /* now try if one of the direct subdevices supports ALSA or OSS */ childs = libhal_manager_find_device_string_match (ctx, "info.parent", udi, &num_childs, &error); if (dbus_error_is_set (&error)) { GST_DEBUG ("Unable to retrieve childs of %s: %s: %s", udi, error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); goto ctx_shutdown; } if (childs && num_childs > 0) { int i; char *alsa_string = NULL, *oss_string = NULL; for (i = 0; i < num_childs && !alsa_string; i++) { alsa_string = gst_hal_get_alsa_element (ctx, childs[i], device_type); if (!oss_string) oss_string = gst_hal_get_oss_element (ctx, childs[i], device_type); } if (alsa_string) { string = alsa_string; g_free (oss_string); } else if (oss_string) { string = oss_string; } } libhal_free_string_array (childs); } ctx_shutdown: if (!libhal_ctx_shutdown (ctx, &error)) { GST_DEBUG ("Closing connection to HAL failed: %s: %s", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); } ctx_free: libhal_ctx_free (ctx); if (string == NULL) { GST_WARNING ("Problem finding a HAL audio device for udi %s", udi); } else { GST_INFO ("Using %s", string); } return string; }
static void connect_hook(DBusConnection *connection, void *data) { DBusError error; struct config_hal_info *info = data; char **devices; int num_devices, i; info->system_bus = connection; dbus_error_init(&error); if (!info->hal_ctx) info->hal_ctx = libhal_ctx_new(); if (!info->hal_ctx) { ErrorF("[config/hal] couldn't create HAL context\n"); goto out_err; } if (!libhal_ctx_set_dbus_connection(info->hal_ctx, info->system_bus)) { ErrorF("[config/hal] couldn't associate HAL context with bus\n"); goto out_ctx; } if (!libhal_ctx_init(info->hal_ctx, &error)) { ErrorF("[config/hal] couldn't initialise context: %s (%s)\n", error.name, error.message); goto out_ctx; } libhal_ctx_set_device_added(info->hal_ctx, device_added); libhal_ctx_set_device_removed(info->hal_ctx, device_removed); if (!libhal_device_property_watch_all(info->hal_ctx, &error)) { ErrorF("[config/hal] couldn't watch all properties: %s (%s)\n", error.name, error.message); goto out_ctx2; } devices = libhal_find_device_by_capability(info->hal_ctx, "input", &num_devices, &error); /* FIXME: Get default devices if error is set. */ for (i = 0; i < num_devices; i++) device_added(info->hal_ctx, devices[i]); libhal_free_string_array(devices); dbus_error_free(&error); return; out_ctx2: if (!libhal_ctx_shutdown(info->hal_ctx, &error)) DebugF("[config/hal] couldn't shut down context: %s (%s)\n", error.name, error.message); out_ctx: libhal_ctx_free(info->hal_ctx); out_err: dbus_error_free(&error); info->hal_ctx = NULL; info->system_bus = NULL; return; }
int main(int argc, char **argv) { DBusError error; DBusConnection *conn; LibHalContext *hal_ctx; int i, err; opterr = 0; opt.list = 1; while ((i = getopt_long(argc, argv, "a:hr:", options, NULL)) != -1) { switch (i) { case 'a': opt.add = 1; opt.list = 0; opt.udi = optarg; break; case 'r': opt.remove = 1; opt.list = 0; opt.udi = optarg; break; case 'h': help(); return 0; default: help(); return 1; } } dbus_error_init(&error); if (!(conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error))) { fprintf(stderr, "error: dbus_bus_get: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 2; } /* fprintf(stderr, "connected to: %s\n", dbus_bus_get_unique_name(conn)); */ if (!(hal_ctx = libhal_ctx_new())) return 3; if (!libhal_ctx_set_dbus_connection(hal_ctx, conn)) return 4; 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 5; } err = 0; if (opt.list) err = dump_devices(hal_ctx, argv[optind]); else if (opt.remove) err = remove_udi(hal_ctx, opt.udi); else if (opt.add) err = add_udi(hal_ctx, opt.udi); else err = 6; libhal_ctx_shutdown(hal_ctx, &error); libhal_ctx_free(hal_ctx); dbus_connection_unref(conn); dbus_error_free(&error); return err; }
// Initialize basic HAL connection LibHalContext *CHALManager::InitializeHal() { LibHalContext *ctx; char **devices; int nr; if (!InitializeDBus()) return NULL; if (!(ctx = libhal_ctx_new())) { CLog::Log(LOGERROR, "HAL: failed to create a HAL context!"); return NULL; } if (!libhal_ctx_set_dbus_connection(ctx, m_DBusSystemConnection)) CLog::Log(LOGERROR, "HAL: Failed to connect with dbus"); libhal_ctx_set_device_added(ctx, DeviceAdded); libhal_ctx_set_device_removed(ctx, DeviceRemoved); libhal_ctx_set_device_new_capability(ctx, DeviceNewCapability); libhal_ctx_set_device_lost_capability(ctx, DeviceLostCapability); libhal_ctx_set_device_property_modified(ctx, DevicePropertyModified); libhal_ctx_set_device_condition(ctx, DeviceCondition); if (!libhal_device_property_watch_all(ctx, &m_Error)) { CLog::Log(LOGERROR, "HAL: Failed to set property watch %s", m_Error.message); dbus_error_free(&m_Error); libhal_ctx_free(ctx); return NULL; } if (!libhal_ctx_init(ctx, &m_Error)) { CLog::Log(LOGERROR, "HAL: Failed to initialize hal context: %s", m_Error.message); dbus_error_free(&m_Error); libhal_ctx_free(ctx); return NULL; } /* * Do something to ping the HAL daemon - the above functions will * succeed even if hald is not running, so long as DBUS is. But we * want to exit silently if hald is not running, to behave on * pre-2.6 systems. */ if (!(devices = libhal_get_all_devices(ctx, &nr, &m_Error))) { CLog::Log(LOGERROR, "HAL: seems that Hal daemon is not running: %s", m_Error.message); dbus_error_free(&m_Error); libhal_ctx_shutdown(ctx, NULL); libhal_ctx_free(ctx); return NULL; } libhal_free_string_array(devices); return ctx; }
char * battstat_hal_initialise (void (*callback) (void)) { DBusConnection *connection; LibHalContext *ctx; DBusError error; char *error_str; char **devices; int i, num; status_updated_callback = callback; if( battstat_hal_ctx != NULL ) return g_strdup( "Already initialised!" ); dbus_error_init( &error ); if( (connection = dbus_bus_get( DBUS_BUS_SYSTEM, &error )) == NULL ) goto error_out; dbus_connection_setup_with_g_main( connection, g_main_context_default() ); if( (ctx = libhal_ctx_new()) == NULL ) { dbus_set_error( &error, _("HAL error"), _("Could not create libhal_ctx") ); goto error_out; } libhal_ctx_set_device_property_modified( ctx, property_callback ); libhal_ctx_set_device_added( ctx, device_added_callback ); libhal_ctx_set_device_removed( ctx, device_removed_callback ); libhal_ctx_set_dbus_connection( ctx, connection ); if( libhal_ctx_init( ctx, &error ) == 0 ) goto error_freectx; devices = libhal_find_device_by_capability( ctx, "battery", &num, &error ); if( devices == NULL ) goto error_shutdownctx; /* FIXME: for now, if 0 battery devices are present on first scan, then fail. * This allows fallover to the legacy (ACPI, APM, etc) backends if the * installed version of HAL doesn't know about batteries. This check should * be removed at some point in the future (maybe circa MATE 2.13..). */ if( num == 0 ) { dbus_free_string_array( devices ); dbus_set_error( &error, _("HAL error"), _("No batteries found") ); goto error_shutdownctx; } for( i = 0; i < num; i++ ) { char *type = libhal_device_get_property_string( ctx, devices[i], "battery.type", &error ); if( type ) { /* We only track 'primary' batteries (ie: to avoid monitoring * batteries in cordless mice or UPSes etc.) */ if( !strcmp( type, "primary" ) ) add_to_list( ctx, &batteries, devices[i], sizeof (struct battery_info) ); libhal_free_string( type ); } } dbus_free_string_array( devices ); devices = libhal_find_device_by_capability( ctx, "ac_adapter", &num, &error ); if( devices == NULL ) { batteries = free_entire_list( batteries ); goto error_shutdownctx; } for( i = 0; i < num; i++ ) add_to_list( ctx, &adaptors, devices[i], sizeof (struct adaptor_info) ); dbus_free_string_array( devices ); dbus_error_free( &error ); battstat_hal_ctx = ctx; return NULL; error_shutdownctx: libhal_ctx_shutdown( ctx, NULL ); error_freectx: libhal_ctx_free( ctx ); error_out: error_str = g_strdup_printf( _("Unable to initialise HAL: %s: %s"), error.name, error.message ); dbus_error_free( &error ); return error_str; }
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; }
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; }
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_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; }
static gboolean volume_is_nokia770 (GVolume *volume) { LibHalContext *ctx; DBusConnection *conn; char *parent_udi, *udi; char *parent_name; gboolean result; DBusError error; gboolean inited = FALSE; result = FALSE; dbus_error_init (&error); conn = NULL; udi = NULL; parent_udi = NULL; parent_name = NULL; ctx = libhal_ctx_new (); if (ctx == NULL) { rb_debug ("cannot connect to HAL"); goto end; } conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error); if (conn == NULL || dbus_error_is_set (&error)) goto end; libhal_ctx_set_dbus_connection (ctx, conn); if (!libhal_ctx_init (ctx, &error) || dbus_error_is_set (&error)) goto end; udi = rb_gvolume_get_udi (volume, ctx); if (udi == NULL) goto end; inited = TRUE; parent_udi = libhal_device_get_property_string (ctx, udi, "info.parent", &error); if (parent_udi == NULL || dbus_error_is_set (&error)) goto end; rb_debug ("Nokia detection: info.parent=%s", parent_udi); parent_name = libhal_device_get_property_string (ctx, parent_udi, "info.vendor", &error); rb_debug ("Nokia detection: info.vendor=%s", parent_name); if (parent_name == NULL || dbus_error_is_set (&error)) goto end; if (strcmp (parent_name, "Nokia") == 0) { g_free (parent_name); parent_name = libhal_device_get_property_string (ctx, parent_udi, "info.product", &error); rb_debug ("Nokia detection: info.product=%s", parent_name); if (parent_name == NULL || dbus_error_is_set (&error)) goto end; if (strcmp (parent_name, "770") == 0) { result = TRUE; } else if (strcmp (parent_name, "N800") == 0) { result = TRUE; } } end: g_free (udi); g_free (parent_name); g_free (parent_udi); if (dbus_error_is_set (&error)) { rb_debug ("Error: %s\n", error.message); dbus_error_free (&error); dbus_error_init (&error); } if (ctx) { if (inited) libhal_ctx_shutdown (ctx, &error); libhal_ctx_free (ctx); } 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[]) { 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); }
static BOOL connect_and_register(DBusConnection *connection, struct config_hal_info *info) { DBusError error; char **devices; int num_devices, i; if (info->hal_ctx) return TRUE; /* already registered, pretend we did something */ info->system_bus = connection; dbus_error_init(&error); info->hal_ctx = libhal_ctx_new(); if (!info->hal_ctx) { LogMessage(X_ERROR, "config/hal: couldn't create HAL context\n"); goto out_err; } if (!libhal_ctx_set_dbus_connection(info->hal_ctx, info->system_bus)) { LogMessage(X_ERROR, "config/hal: couldn't associate HAL context with bus\n"); goto out_err; } if (!libhal_ctx_init(info->hal_ctx, &error)) { LogMessage(X_ERROR, "config/hal: couldn't initialise context: %s (%s)\n", error.name ? error.name : "unknown error", error.message ? error.message : "null"); goto out_err; } if (!libhal_device_property_watch_all(info->hal_ctx, &error)) { LogMessage(X_ERROR, "config/hal: couldn't watch all properties: %s (%s)\n", error.name ? error.name : "unknown error", error.message ? error.message : "null"); goto out_ctx; } libhal_ctx_set_device_added(info->hal_ctx, device_added); libhal_ctx_set_device_removed(info->hal_ctx, device_removed); devices = libhal_find_device_by_capability(info->hal_ctx, "input", &num_devices, &error); /* FIXME: Get default devices if error is set. */ if (dbus_error_is_set(&error)) { LogMessage(X_ERROR, "config/hal: couldn't find input device: %s (%s)\n", error.name ? error.name : "unknown error", error.message ? error.message : "null"); goto out_ctx; } for (i = 0; i < num_devices; i++) device_added(info->hal_ctx, devices[i]); libhal_free_string_array(devices); dbus_error_free(&error); return TRUE; out_ctx: dbus_error_free(&error); if (!libhal_ctx_shutdown(info->hal_ctx, &error)) { LogMessage(X_WARNING, "config/hal: couldn't shut down context: %s (%s)\n", error.name ? error.name : "unknown error", error.message ? error.message : "null"); dbus_error_free(&error); } out_err: dbus_error_free(&error); if (info->hal_ctx) { libhal_ctx_free(info->hal_ctx); } info->hal_ctx = NULL; info->system_bus = NULL; return FALSE; }
/** * 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; }