1N/A/***************************************************************************
1N/A *
1N/A * hotplug.c : HAL-internal hotplug events
1N/A *
1N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
1N/A * Use is subject to license terms.
1N/A *
1N/A * Licensed under the Academic Free License version 2.1
1N/A *
1N/A **************************************************************************/
1N/A
1N/A
1N/A#ifdef HAVE_CONFIG_H
1N/A# include <config.h>
1N/A#endif
1N/A
1N/A#include <stdio.h>
1N/A#include <string.h>
1N/A#include <errno.h>
1N/A#include <sys/types.h>
1N/A#include <sys/stat.h>
1N/A#include <sys/un.h>
1N/A#include <sys/utsname.h>
1N/A#include <unistd.h>
1N/A
1N/A#include <glib.h>
1N/A#include <dbus/dbus.h>
1N/A#include <dbus/dbus-glib.h>
1N/A
1N/A#include "../osspec.h"
1N/A#include "../logger.h"
1N/A#include "../hald.h"
1N/A#include "../device_info.h"
1N/A
1N/A#include "osspec_solaris.h"
1N/A#include "hotplug.h"
1N/A#include "devinfo.h"
1N/A
1N/A/** Queue of ordered hotplug events */
1N/AGQueue *hotplug_event_queue;
1N/A
1N/A/** List of HotplugEvent objects we are currently processing */
1N/AGSList *hotplug_events_in_progress = NULL;
1N/A
1N/Astatic void hotplug_event_begin (HotplugEvent *hotplug_event);
1N/A
1N/Avoid
1N/Ahotplug_event_end (void *end_token)
1N/A{
1N/A HotplugEvent *hotplug_event = (HotplugEvent *) end_token;
1N/A
1N/A hotplug_events_in_progress = g_slist_remove (hotplug_events_in_progress, hotplug_event);
1N/A g_free (hotplug_event);
1N/A hotplug_event_process_queue ();
1N/A}
1N/A
1N/Astatic void
1N/Ahotplug_event_begin_devfs_add (HotplugEvent *hotplug_event, HalDevice *d)
1N/A{
1N/A HalDevice *parent;
1N/A const gchar *parent_udi;
1N/A void (*begin_add_func) (HalDevice *, HalDevice *, DevinfoDevHandler *, void *);
1N/A
1N/A if (d != NULL) {
1N/A /* XXX */
1N/A HAL_ERROR (("devpath %s already present in store, ignore event", hotplug_event->un.devfs.devfs_path));
1N/A
1N/A goto out;
1N/A }
1N/A
1N/A /* find parent */
1N/A parent_udi = hal_device_property_get_string (hotplug_event->d, "info.parent");
1N/A if (parent_udi == NULL || strlen(parent_udi) == 0) {
1N/A parent = NULL;
1N/A } else {
1N/A parent = hal_device_store_match_key_value_string (hald_get_gdl (), "info.udi", parent_udi);
1N/A }
1N/A /* only root node is allowed to be orphan */
1N/A if (parent == NULL) {
1N/A if (strcmp(hotplug_event->un.devfs.devfs_path, "/") != 0) {
1N/A HAL_ERROR (("Parent is NULL devfs_path=%s parent_udi=%s", hotplug_event->un.devfs.devfs_path, parent_udi ? parent_udi : "<null>"));
1N/A
1N/A goto out;
1N/A }
1N/A }
1N/A
1N/A /* children of ignored parent should be ignored */
1N/A if (parent != NULL && hal_device_property_get_bool (parent, "info.ignore")) {
1N/A HAL_INFO (("parent ignored %s", parent_udi));
1N/A
1N/A goto out;
1N/A }
1N/A
1N/A /* custom or generic add function */
1N/A begin_add_func = hotplug_event->un.devfs.handler->hotplug_begin_add;
1N/A if (begin_add_func == NULL) {
1N/A begin_add_func = hotplug_event_begin_add_devinfo;
1N/A }
1N/A begin_add_func (hotplug_event->d,
1N/A parent,
1N/A hotplug_event->un.devfs.handler,
1N/A (void *) hotplug_event);
1N/A return;
1N/A
1N/Aout:
1N/A g_object_unref (hotplug_event->d);
1N/A hotplug_event_end ((void *) hotplug_event);
1N/A
1N/A return;
1N/A}
1N/A
1N/Astatic void
1N/Ahotplug_event_begin_devfs_remove (HotplugEvent *hotplug_event, HalDevice *d)
1N/A{
1N/A if (d == NULL) {
1N/A HAL_ERROR (("devpath %s not present in store, ignore event", hotplug_event->un.devfs.devfs_path));
1N/A hotplug_event_end ((void *) hotplug_event);
1N/A return;
1N/A }
1N/A HAL_INFO (("hotplug_event_begin_devfs_remove %s", hal_device_get_udi (d)));
1N/A
1N/A hotplug_event_begin_remove_devinfo(d,
1N/A hotplug_event->un.devfs.devfs_path,
1N/A (void *) hotplug_event);
1N/A}
1N/A
1N/Astatic void
1N/Ahotplug_event_begin_devfs (HotplugEvent *hotplug_event)
1N/A{
1N/A HalDevice *d;
1N/A
1N/A HAL_INFO (("hotplug_event_begin_devfs: %s", hotplug_event->un.devfs.devfs_path));
1N/A d = hal_device_store_match_key_value_string (hald_get_gdl (),
1N/A "solaris.devfs_path",
1N/A hotplug_event->un.devfs.devfs_path);
1N/A
1N/A if (hotplug_event->action == HOTPLUG_ACTION_ADD) {
1N/A hotplug_event_begin_devfs_add (hotplug_event, d);
1N/A } else if (hotplug_event->action == HOTPLUG_ACTION_REMOVE) {
1N/A hotplug_event_begin_devfs_remove (hotplug_event, d);
1N/A } else {
1N/A HAL_ERROR (("unsupported action %d", hotplug_event->action));
1N/A g_object_unref (hotplug_event->d);
1N/A hotplug_event_end ((void *) hotplug_event);
1N/A }
1N/A}
1N/A
1N/Astatic void
1N/Ahotplug_event_begin (HotplugEvent *hotplug_event)
1N/A{
1N/A switch (hotplug_event->type) {
1N/A
1N/A case HOTPLUG_EVENT_DEVFS:
1N/A hotplug_event_begin_devfs (hotplug_event);
1N/A break;
1N/A
1N/A default:
1N/A HAL_ERROR (("Unknown hotplug event type %d", hotplug_event->type));
1N/A g_object_unref (hotplug_event->d);
1N/A hotplug_event_end ((void *) hotplug_event);
1N/A break;
1N/A }
1N/A}
1N/A
1N/Avoid
1N/Ahotplug_event_enqueue (HotplugEvent *hotplug_event, int front)
1N/A{
1N/A if (hotplug_event_queue == NULL)
1N/A hotplug_event_queue = g_queue_new ();
1N/A
1N/A if (front) {
1N/A g_queue_push_head (hotplug_event_queue, hotplug_event);
1N/A } else {
1N/A g_queue_push_tail (hotplug_event_queue, hotplug_event);
1N/A }
1N/A}
1N/A
1N/Avoid
1N/Ahotplug_event_process_queue (void)
1N/A{
1N/A HotplugEvent *hotplug_event;
1N/A
1N/A if (hotplug_events_in_progress == NULL &&
1N/A (hotplug_event_queue == NULL || g_queue_is_empty (hotplug_event_queue))) {
1N/A hotplug_queue_now_empty ();
1N/A goto out;
1N/A }
1N/A
1N/A /* do not process events if some other event is in progress */
1N/A if (hotplug_events_in_progress != NULL && g_slist_length (hotplug_events_in_progress) > 0)
1N/A goto out;
1N/A
1N/A hotplug_event = g_queue_pop_head (hotplug_event_queue);
1N/A if (hotplug_event == NULL)
1N/A goto out;
1N/A
1N/A hotplug_events_in_progress = g_slist_append (hotplug_events_in_progress, hotplug_event);
1N/A hotplug_event_begin (hotplug_event);
1N/A
1N/Aout:
1N/A ;
1N/A}