node_device_hal.c 22.7 KB
Newer Older
1 2 3
/*
 * node_device_hal.c: node device enumeration - HAL-based implementation
 *
4
 * Copyright (C) 2011-2015 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2008 Virtual Iron Software, Inc.
 * Copyright (C) 2008 David F. Lively
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library.  If not, see
O
Osier Yang 已提交
20
 * <http://www.gnu.org/licenses/>.
21 22 23 24
 *
 * Author: David F. Lively <dlively@virtualiron.com>
 */

25 26
#include <config.h>

27 28 29 30 31
#include <stdio.h>
#include <stdlib.h>
#include <libhal.h>

#include "node_device_conf.h"
32
#include "node_device_driver.h"
33
#include "node_device_hal.h"
34
#include "node_device_linux_sysfs.h"
35
#include "virerror.h"
36 37
#include "driver.h"
#include "datatypes.h"
38
#include "viralloc.h"
39
#include "viruuid.h"
40
#include "virpci.h"
41
#include "virlog.h"
42
#include "virdbus.h"
43
#include "virstring.h"
44

45 46
#define VIR_FROM_THIS VIR_FROM_NODEDEV

47 48
VIR_LOG_INIT("node_device.node_device_hal");

49 50 51 52 53 54 55
/*
 * Host device enumeration (HAL implementation)
 */

#define DRV_STATE_HAL_CTX(ds) ((LibHalContext *)((ds)->privateData))


56 57
static const char *
hal_name(const char *udi)
58 59 60
{
    const char *name = strrchr(udi, '/');
    if (name)
61
        return name + 1;
62 63 64 65
    return udi;
}


66 67 68
static int
get_str_prop(LibHalContext *ctxt, const char *udi,
             const char *prop, char **val_p)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
{
    char *val = libhal_device_get_property_string(ctxt, udi, prop, NULL);

    if (val) {
        if (*val) {
            *val_p = val;
            return 0;
        } else {
            /* Treat empty strings as NULL values */
            VIR_FREE(val);
        }
    }

    return -1;
}

85 86 87
static int
get_int_prop(LibHalContext *ctxt, const char *udi,
             const char *prop, int *val_p)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
{
    DBusError err;
    int val;
    int rv;

    dbus_error_init(&err);
    val = libhal_device_get_property_int(ctxt, udi, prop, &err);
    rv = dbus_error_is_set(&err);
    dbus_error_free(&err);
    if (rv == 0)
        *val_p = val;

    return rv;
}

103 104 105
static int
get_bool_prop(LibHalContext *ctxt, const char *udi,
              const char *prop, int *val_p)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
{
    DBusError err;
    int val;
    int rv;

    dbus_error_init(&err);
    val = libhal_device_get_property_bool(ctxt, udi, prop, &err);
    rv = dbus_error_is_set(&err);
    dbus_error_free(&err);
    if (rv == 0)
        *val_p = val;

    return rv;
}

121 122 123
static int
get_uint64_prop(LibHalContext *ctxt, const char *udi,
                const char *prop, unsigned long long *val_p)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
{
    DBusError err;
    unsigned long long val;
    int rv;

    dbus_error_init(&err);
    val = libhal_device_get_property_uint64(ctxt, udi, prop, &err);
    rv = dbus_error_is_set(&err);
    dbus_error_free(&err);
    if (rv == 0)
        *val_p = val;

    return rv;
}

139 140
static int
gather_pci_cap(LibHalContext *ctx, const char *udi,
141
               virNodeDevCapDataPtr d)
142 143 144 145 146 147
{
    char *sysfs_path;

    if (get_str_prop(ctx, udi, "pci.linux.sysfs_path", &sysfs_path) == 0) {
        char *p = strrchr(sysfs_path, '/');
        if (p) {
148 149 150 151
            ignore_value(virStrToLong_ui(p+1, &p, 16, &d->pci_dev.domain));
            ignore_value(virStrToLong_ui(p+1, &p, 16, &d->pci_dev.bus));
            ignore_value(virStrToLong_ui(p+1, &p, 16, &d->pci_dev.slot));
            ignore_value(virStrToLong_ui(p+1, &p, 16, &d->pci_dev.function));
152
        }
153

154
        if (nodeDeviceSysfsGetPCIRelatedDevCaps(sysfs_path, &d->pci_dev) < 0) {
155 156 157
            VIR_FREE(sysfs_path);
            return -1;
        }
158 159
        VIR_FREE(sysfs_path);
    }
160

161 162 163 164 165 166
    (void)get_int_prop(ctx, udi, "pci.vendor_id", (int *)&d->pci_dev.vendor);
    if (get_str_prop(ctx, udi, "pci.vendor", &d->pci_dev.vendor_name) != 0)
        (void)get_str_prop(ctx, udi, "info.vendor", &d->pci_dev.vendor_name);
    (void)get_int_prop(ctx, udi, "pci.product_id", (int *)&d->pci_dev.product);
    if (get_str_prop(ctx, udi, "pci.product", &d->pci_dev.product_name) != 0)
        (void)get_str_prop(ctx, udi, "info.product", &d->pci_dev.product_name);
167

168 169 170 171
    return 0;
}


172 173
static int
gather_usb_cap(LibHalContext *ctx, const char *udi,
174
               virNodeDevCapDataPtr d)
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
{
    (void)get_int_prop(ctx, udi, "usb.interface.number",
                       (int *)&d->usb_if.number);
    (void)get_int_prop(ctx, udi, "usb.interface.class",
                       (int *)&d->usb_if._class);
    (void)get_int_prop(ctx, udi, "usb.interface.subclass",
                       (int *)&d->usb_if.subclass);
    (void)get_int_prop(ctx, udi, "usb.interface.protocol",
                       (int *)&d->usb_if.protocol);
    (void)get_str_prop(ctx, udi, "usb.interface.description",
                       &d->usb_if.description);
    return 0;
}


190 191
static int
gather_usb_device_cap(LibHalContext *ctx, const char *udi,
192
                      virNodeDevCapDataPtr d)
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
{
    (void)get_int_prop(ctx, udi, "usb_device.bus_number",
                       (int *)&d->usb_dev.bus);
    (void)get_int_prop(ctx, udi, "usb_device.linux.device_number",
                       (int *)&d->usb_dev.device);
    (void)get_int_prop(ctx, udi, "usb_device.vendor_id",
                       (int *)&d->usb_dev.vendor);
    if (get_str_prop(ctx, udi, "usb_device.vendor",
                     &d->usb_dev.vendor_name) != 0)
        (void)get_str_prop(ctx, udi, "info.vendor", &d->usb_dev.vendor_name);
    (void)get_int_prop(ctx, udi, "usb_device.product_id",
                       (int *)&d->usb_dev.product);
    if (get_str_prop(ctx, udi, "usb_device.product",
                     &d->usb_dev.product_name) != 0)
        (void)get_str_prop(ctx, udi, "info.product", &d->usb_dev.product_name);
    return 0;
}


212 213
static int
gather_net_cap(LibHalContext *ctx, const char *udi,
214
               virNodeDevCapDataPtr d)
215 216
{
    unsigned long long dummy;
217
    (void)get_str_prop(ctx, udi, "net.interface", &d->net.ifname);
218 219 220 221 222 223 224 225 226 227 228 229 230 231
    (void)get_str_prop(ctx, udi, "net.address", &d->net.address);
    if (get_uint64_prop(ctx, udi, "net.80203.mac_address",
                        &dummy) == 0)
        d->net.subtype = VIR_NODE_DEV_CAP_NET_80203;
    else if (get_uint64_prop(ctx, udi, "net.80211.mac_address",
                             &dummy) == 0)
        d->net.subtype = VIR_NODE_DEV_CAP_NET_80211;
    else
        d->net.subtype = VIR_NODE_DEV_CAP_NET_LAST;

    return 0;
}


232 233
static int
gather_scsi_host_cap(LibHalContext *ctx, const char *udi,
234
                     virNodeDevCapDataPtr d)
235
{
236 237
    int retval = 0;

238
    (void)get_int_prop(ctx, udi, "scsi_host.host", (int *)&d->scsi_host.host);
239

240
    retval = nodeDeviceSysfsGetSCSIHostCaps(&d->scsi_host);
241

242
    if (retval == -1)
243 244
        goto out;

245
 out:
246
    return retval;
247 248 249
}


250 251
static int
gather_scsi_cap(LibHalContext *ctx, const char *udi,
252
                virNodeDevCapDataPtr d)
253 254 255 256 257 258 259 260 261 262
{
    (void)get_int_prop(ctx, udi, "scsi.host", (int *)&d->scsi.host);
    (void)get_int_prop(ctx, udi, "scsi.bus", (int *)&d->scsi.bus);
    (void)get_int_prop(ctx, udi, "scsi.target", (int *)&d->scsi.target);
    (void)get_int_prop(ctx, udi, "scsi.lun", (int *)&d->scsi.lun);
    (void)get_str_prop(ctx, udi, "scsi.type", &d->scsi.type);
    return 0;
}


263 264
static int
gather_storage_cap(LibHalContext *ctx, const char *udi,
265
                   virNodeDevCapDataPtr d)
266 267
{
    int val;
268
    (void)get_str_prop(ctx, udi, "block.device", &d->storage.block);
269 270 271 272
    (void)get_str_prop(ctx, udi, "storage.bus", &d->storage.bus);
    (void)get_str_prop(ctx, udi, "storage.drive_type", &d->storage.drive_type);
    (void)get_str_prop(ctx, udi, "storage.model", &d->storage.model);
    (void)get_str_prop(ctx, udi, "storage.vendor", &d->storage.vendor);
273
    (void)get_str_prop(ctx, udi, "storage.serial", &d->storage.serial);
274 275
    if (get_bool_prop(ctx, udi, "storage.removable", &val) == 0 && val) {
        d->storage.flags |= VIR_NODE_DEV_CAP_STORAGE_REMOVABLE;
276 277
        if (get_bool_prop(ctx, udi, "storage.removable.media_available",
                          &val) == 0 && val) {
278 279 280 281 282 283 284 285 286 287 288 289 290
            d->storage.flags |=
                VIR_NODE_DEV_CAP_STORAGE_REMOVABLE_MEDIA_AVAILABLE;
            (void)get_uint64_prop(ctx, udi, "storage.removable.media_size",
                                  &d->storage.removable_media_size);
        }
    } else {
        (void)get_uint64_prop(ctx, udi, "storage.size", &d->storage.size);
    }
    if (get_bool_prop(ctx, udi, "storage.hotpluggable", &val) == 0 && val)
        d->storage.flags |= VIR_NODE_DEV_CAP_STORAGE_HOTPLUGGABLE;
    return 0;
}

291 292
static int
gather_scsi_generic_cap(LibHalContext *ctx, const char *udi,
293
                        virNodeDevCapDataPtr d)
294 295 296 297 298
{
    (void)get_str_prop(ctx, udi, "scsi_generic.device", &d->sg.path);
    return 0;
}

299

300 301
static int
gather_system_cap(LibHalContext *ctx, const char *udi,
302
                  virNodeDevCapDataPtr d)
303 304 305 306 307 308 309 310 311 312 313
{
    char *uuidstr;

    (void)get_str_prop(ctx, udi, "system.product", &d->system.product_name);
    (void)get_str_prop(ctx, udi, "system.hardware.vendor",
                       &d->system.hardware.vendor_name);
    (void)get_str_prop(ctx, udi, "system.hardware.version",
                       &d->system.hardware.version);
    (void)get_str_prop(ctx, udi, "system.hardware.serial",
                       &d->system.hardware.serial);
    if (get_str_prop(ctx, udi, "system.hardware.uuid", &uuidstr) == 0) {
314
        ignore_value(virUUIDParse(uuidstr, d->system.hardware.uuid));
315 316 317 318 319 320 321 322 323 324 325 326 327 328
        VIR_FREE(uuidstr);
    }
    (void)get_str_prop(ctx, udi, "system.firmware.vendor",
                       &d->system.firmware.vendor_name);
    (void)get_str_prop(ctx, udi, "system.firmware.version",
                       &d->system.firmware.version);
    (void)get_str_prop(ctx, udi, "system.firmware.release_date",
                       &d->system.firmware.release_date);
    return 0;
}


struct _caps_tbl_entry {
    const char *cap_name;
329
    virNodeDevCapType type;
330 331
    int (*gather_fn)(LibHalContext *ctx,
                     const char *udi,
332
                     virNodeDevCapDataPtr data);
333 334 335 336 337 338 339 340 341 342 343 344 345
};

typedef struct _caps_tbl_entry caps_tbl_entry;

static caps_tbl_entry caps_tbl[] = {
    { "system",     VIR_NODE_DEV_CAP_SYSTEM,        gather_system_cap },
    { "pci",        VIR_NODE_DEV_CAP_PCI_DEV,       gather_pci_cap },
    { "usb",        VIR_NODE_DEV_CAP_USB_INTERFACE, gather_usb_cap },
    { "usb_device", VIR_NODE_DEV_CAP_USB_DEV,       gather_usb_device_cap },
    { "net",        VIR_NODE_DEV_CAP_NET,           gather_net_cap },
    { "scsi_host",  VIR_NODE_DEV_CAP_SCSI_HOST,     gather_scsi_host_cap },
    { "scsi",       VIR_NODE_DEV_CAP_SCSI,          gather_scsi_cap },
    { "storage",    VIR_NODE_DEV_CAP_STORAGE,       gather_storage_cap },
346
    { "scsi_generic", VIR_NODE_DEV_CAP_SCSI_GENERIC, gather_scsi_generic_cap },
347 348 349 350
};


/* qsort/bsearch string comparator */
351 352
static int
cmpstringp(const void *p1, const void *p2)
353 354 355 356 357 358
{
    /* from man 3 qsort */
    return strcmp(* (char * const *) p1, * (char * const *) p2);
}


359 360 361 362
static int
gather_capability(LibHalContext *ctx, const char *udi,
                  const char *cap_name,
                  virNodeDevCapsDefPtr *caps_p)
363 364 365 366 367 368 369 370 371 372
{
    caps_tbl_entry *entry;

    entry = bsearch(&cap_name, caps_tbl, ARRAY_CARDINALITY(caps_tbl),
                    sizeof(caps_tbl[0]), cmpstringp);

    if (entry) {
        virNodeDevCapsDefPtr caps;
        if (VIR_ALLOC(caps) < 0)
            return ENOMEM;
373
        caps->data.type = entry->type;
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        if (entry->gather_fn) {
            int rv = (*entry->gather_fn)(ctx, udi, &caps->data);
            if (rv != 0) {
                virNodeDevCapsDefFree(caps);
                return rv;
            }
        }
        caps->next = *caps_p;
        *caps_p = caps;
    }

    return 0;
}


389 390 391
static int
gather_capabilities(LibHalContext *ctx, const char *udi,
                    virNodeDevCapsDefPtr *caps_p)
392 393 394
{
    char *bus_name = NULL;
    virNodeDevCapsDefPtr caps = NULL;
395
    char **hal_cap_names = NULL;
396 397
    int rv;
    size_t i;
398 399 400 401 402 403 404

    if (STREQ(udi, "/org/freedesktop/Hal/devices/computer")) {
        rv = gather_capability(ctx, udi, "system", &caps);
        if (rv != 0)
            goto failure;
    }

405 406
    if (get_str_prop(ctx, udi, "info.subsystem", &bus_name) == 0 ||
        get_str_prop(ctx, udi, "linux.subsystem", &bus_name) == 0) {
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
        rv = gather_capability(ctx, udi, bus_name, &caps);
        if (rv != 0)
            goto failure;
    }

    hal_cap_names = libhal_device_get_property_strlist(ctx, udi,
                                                       "info.capabilities",
                                                       NULL);
    if (hal_cap_names) {
        for (i = 0; hal_cap_names[i]; i++) {
            if (! (bus_name && STREQ(hal_cap_names[i], bus_name))) {
                rv = gather_capability(ctx, udi, hal_cap_names[i], &caps);
                if (rv != 0)
                    goto failure;
            }
        }
        for (i = 0; hal_cap_names[i]; i++)
            VIR_FREE(hal_cap_names[i]);
        VIR_FREE(hal_cap_names);
    }
    VIR_FREE(bus_name);

    *caps_p = caps;
    return 0;

 failure:
    VIR_FREE(bus_name);
    if (hal_cap_names) {
        for (i = 0; hal_cap_names[i]; i++)
            VIR_FREE(hal_cap_names[i]);
        VIR_FREE(hal_cap_names);
    }
    while (caps) {
        virNodeDevCapsDefPtr next = caps->next;
        virNodeDevCapsDefFree(caps);
        caps = next;
    }
    return rv;
}

447 448
static void
dev_create(const char *udi)
449
{
450
    LibHalContext *ctx;
451
    char *parent_key = NULL;
452 453
    virNodeDeviceObjPtr dev = NULL;
    virNodeDeviceDefPtr def = NULL;
454
    virNodeDeviceDefPtr objdef;
455 456
    const char *name = hal_name(udi);
    int rv;
457
    char *devicePath = NULL;
458

459 460
    nodeDeviceLock();
    ctx = DRV_STATE_HAL_CTX(driver);
461 462 463

    if (VIR_ALLOC(def) < 0)
        goto failure;
464

465
    if (VIR_STRDUP(def->name, name) < 0)
466 467 468
        goto failure;

    if (get_str_prop(ctx, udi, "info.parent", &parent_key) == 0) {
469 470
        if (VIR_STRDUP(def->parent, hal_name(parent_key)) < 0) {
            VIR_FREE(parent_key);
471
            goto failure;
472 473
        }
        VIR_FREE(parent_key);
474 475
    }

476
    rv = gather_capabilities(ctx, udi, &def->caps);
477 478
    if (rv != 0) goto failure;

479 480
    if (def->caps == NULL)
        goto cleanup;
481

482
    /* Some devices don't have a path in sysfs, so ignore failure */
483
    (void)get_str_prop(ctx, udi, "linux.sysfs_path", &devicePath);
484

485
    if (!(dev = virNodeDeviceObjAssignDef(&driver->devs, def))) {
486
        VIR_FREE(devicePath);
487
        goto failure;
488
    }
489
    objdef = virNodeDeviceObjGetDef(dev);
490

491
    objdef->sysfs_path = devicePath;
492

493
    virNodeDeviceObjUnlock(dev);
494

495
    nodeDeviceUnlock();
496 497 498
    return;

 failure:
499
    VIR_DEBUG("FAILED TO ADD dev %s", name);
500
 cleanup:
501
    virNodeDeviceDefFree(def);
502
    nodeDeviceUnlock();
503 504
}

505 506
static void
dev_refresh(const char *udi)
507 508 509 510
{
    const char *name = hal_name(udi);
    virNodeDeviceObjPtr dev;

511
    nodeDeviceLock();
512
    dev = virNodeDeviceObjFindByName(&driver->devs, name);
513 514 515 516
    if (dev) {
        /* Simply "rediscover" device -- incrementally handling changes
         * to sub-capabilities (like net.80203) is nasty ... so avoid it.
         */
517
        virNodeDeviceObjRemove(&driver->devs, &dev);
518
    } else {
519
        VIR_DEBUG("no device named %s", name);
520
    }
521
    nodeDeviceUnlock();
522

523
    if (dev)
524 525
        dev_create(udi);
}
526

527 528 529
static void
device_added(LibHalContext *ctx ATTRIBUTE_UNUSED,
             const char *udi)
530
{
531
    VIR_DEBUG("%s", hal_name(udi));
532
    dev_create(udi);
533 534 535
}


536 537 538
static void
device_removed(LibHalContext *ctx ATTRIBUTE_UNUSED,
               const char *udi)
539 540
{
    const char *name = hal_name(udi);
541 542
    virNodeDeviceObjPtr dev;

543
    nodeDeviceLock();
544
    dev = virNodeDeviceObjFindByName(&driver->devs, name);
545
    VIR_DEBUG("%s", name);
546
    if (dev)
547
        virNodeDeviceObjRemove(&driver->devs, &dev);
548
    else
549
        VIR_DEBUG("no device named %s", name);
550
    nodeDeviceUnlock();
551 552 553
}


554 555 556
static void
device_cap_added(LibHalContext *ctx,
                 const char *udi, const char *cap)
557 558
{
    const char *name = hal_name(udi);
559
    virNodeDeviceObjPtr dev;
560
    virNodeDeviceDefPtr def;
561

562
    nodeDeviceLock();
563
    dev = virNodeDeviceObjFindByName(&driver->devs, name);
564
    nodeDeviceUnlock();
565
    VIR_DEBUG("%s %s", cap, name);
566
    if (dev) {
567 568
        def = virNodeDeviceObjGetDef(dev);
        (void)gather_capability(ctx, udi, cap, &def->caps);
569 570
        virNodeDeviceObjUnlock(dev);
    } else {
571
        VIR_DEBUG("no device named %s", name);
572
    }
573 574 575
}


576 577 578 579
static void
device_cap_lost(LibHalContext *ctx ATTRIBUTE_UNUSED,
                const char *udi,
                const char *cap)
580 581
{
    const char *name = hal_name(udi);
582
    VIR_DEBUG("%s %s", cap, name);
583 584

    dev_refresh(udi);
585 586 587
}


588 589 590 591 592 593
static void
device_prop_modified(LibHalContext *ctx ATTRIBUTE_UNUSED,
                     const char *udi,
                     const char *key,
                     dbus_bool_t is_removed ATTRIBUTE_UNUSED,
                     dbus_bool_t is_added ATTRIBUTE_UNUSED)
594 595
{
    const char *name = hal_name(udi);
596
    VIR_DEBUG("%s %s", name, key);
597

598
    dev_refresh(udi);
599 600 601
}


602 603 604 605
static int
nodeStateInitialize(bool privileged ATTRIBUTE_UNUSED,
                    virStateInhibitCallback callback ATTRIBUTE_UNUSED,
                    void *opaque ATTRIBUTE_UNUSED)
606 607 608
{
    LibHalContext *hal_ctx = NULL;
    char **udi = NULL;
609 610
    int num_devs;
    size_t i;
611
    int ret = -1;
612 613
    DBusConnection *sysbus;
    DBusError err;
614 615 616 617 618

    /* Ensure caps_tbl is sorted by capability name */
    qsort(caps_tbl, ARRAY_CARDINALITY(caps_tbl), sizeof(caps_tbl[0]),
          cmpstringp);

619
    if (VIR_ALLOC(driver) < 0)
620 621
        return -1;

622 623
    if (virMutexInit(&driver->lock) < 0) {
        VIR_FREE(driver);
624 625
        return -1;
    }
626
    nodeDeviceLock();
627

628
    dbus_error_init(&err);
629
    if (!(sysbus = virDBusGetSystemBus())) {
630 631 632
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("DBus not available, disabling HAL driver: %s"),
                       virGetLastErrorMessage());
633 634 635
        ret = 0;
        goto failure;
    }
636

637 638
    hal_ctx = libhal_ctx_new();
    if (hal_ctx == NULL) {
639 640
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libhal_ctx_new returned NULL"));
641 642
        goto failure;
    }
643

644
    if (!libhal_ctx_set_dbus_connection(hal_ctx, sysbus)) {
645 646
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libhal_ctx_set_dbus_connection failed"));
647 648 649
        goto failure;
    }
    if (!libhal_ctx_init(hal_ctx, &err)) {
650 651 652
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libhal_ctx_init failed, haldaemon is probably "
                         "not running"));
653 654 655 656
        /* We don't want to show a fatal error here,
           otherwise entire libvirtd shuts down when
           hald isn't running */
        ret = 0;
657 658 659
        goto failure;
    }

660
    /* Populate with known devices */
661
    driver->privateData = hal_ctx;
662 663 664 665 666 667

    /* We need to unlock state now, since setting these callbacks cause
     * a dbus RPC call, and while this call is waiting for the reply,
     * a signal may already arrive, triggering the callback and thus
     * requiring the lock !
     */
668
    nodeDeviceUnlock();
669

670 671 672 673 674
    /* Register HAL event callbacks */
    if (!libhal_ctx_set_device_added(hal_ctx, device_added) ||
        !libhal_ctx_set_device_removed(hal_ctx, device_removed) ||
        !libhal_ctx_set_device_new_capability(hal_ctx, device_cap_added) ||
        !libhal_ctx_set_device_lost_capability(hal_ctx, device_cap_lost) ||
675
        !libhal_ctx_set_device_property_modified(hal_ctx, device_prop_modified) ||
676
        !libhal_device_property_watch_all(hal_ctx, &err)) {
677 678
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("setting up HAL callbacks failed"));
679 680 681 682 683
        goto failure;
    }

    udi = libhal_get_all_devices(hal_ctx, &num_devs, &err);
    if (udi == NULL) {
684 685
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libhal_get_all_devices failed"));
686 687
        goto failure;
    }
688
    for (i = 0; i < num_devs; i++) {
689
        dev_create(udi[i]);
690 691 692
        VIR_FREE(udi[i]);
    }
    VIR_FREE(udi);
693 694 695 696 697

    return 0;

 failure:
    if (dbus_error_is_set(&err)) {
698 699
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("%s: %s"), err.name, err.message);
700 701
        dbus_error_free(&err);
    }
702
    virNodeDeviceObjListFree(&driver->devs);
703 704
    if (hal_ctx)
        (void)libhal_ctx_free(hal_ctx);
705 706
    nodeDeviceUnlock();
    VIR_FREE(driver);
707

708
    return ret;
709 710 711
}


712 713
static int
nodeStateCleanup(void)
714
{
715 716 717 718
    if (driver) {
        nodeDeviceLock();
        LibHalContext *hal_ctx = DRV_STATE_HAL_CTX(driver);
        virNodeDeviceObjListFree(&driver->devs);
719 720
        (void)libhal_ctx_shutdown(hal_ctx, NULL);
        (void)libhal_ctx_free(hal_ctx);
721 722 723
        nodeDeviceUnlock();
        virMutexDestroy(&driver->lock);
        VIR_FREE(driver);
724 725 726 727 728 729
        return 0;
    }
    return -1;
}


730 731
static int
nodeStateReload(void)
732
{
733 734
    DBusError err;
    char **udi = NULL;
735 736
    int num_devs;
    size_t i;
737 738
    LibHalContext *hal_ctx;

739
    VIR_INFO("Reloading HAL device state");
740
    nodeDeviceLock();
741
    VIR_INFO("Removing existing objects");
742 743
    virNodeDeviceObjListFree(&driver->devs);
    nodeDeviceUnlock();
744

745
    hal_ctx = DRV_STATE_HAL_CTX(driver);
746
    VIR_INFO("Creating new objects");
747 748 749
    dbus_error_init(&err);
    udi = libhal_get_all_devices(hal_ctx, &num_devs, &err);
    if (udi == NULL) {
750 751
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("libhal_get_all_devices failed"));
752 753 754 755 756 757 758
        return -1;
    }
    for (i = 0; i < num_devs; i++) {
        dev_create(udi[i]);
        VIR_FREE(udi[i]);
    }
    VIR_FREE(udi);
759
    VIR_INFO("HAL device reload complete");
760 761

    return 0;
762 763 764
}


765
static virNodeDeviceDriver halNodeDeviceDriver = {
766
    .name = "HAL",
767 768
    .nodeNumOfDevices = nodeNumOfDevices, /* 0.5.0 */
    .nodeListDevices = nodeListDevices, /* 0.5.0 */
769
    .connectListAllNodeDevices = nodeConnectListAllNodeDevices, /* 0.10.2 */
770 771 772 773 774 775 776 777
    .nodeDeviceLookupByName = nodeDeviceLookupByName, /* 0.5.0 */
    .nodeDeviceLookupSCSIHostByWWN = nodeDeviceLookupSCSIHostByWWN, /* 1.0.2 */
    .nodeDeviceGetXMLDesc = nodeDeviceGetXMLDesc, /* 0.5.0 */
    .nodeDeviceGetParent = nodeDeviceGetParent, /* 0.5.0 */
    .nodeDeviceNumOfCaps = nodeDeviceNumOfCaps, /* 0.5.0 */
    .nodeDeviceListCaps = nodeDeviceListCaps, /* 0.5.0 */
    .nodeDeviceCreateXML = nodeDeviceCreateXML, /* 0.6.5 */
    .nodeDeviceDestroy = nodeDeviceDestroy, /* 0.6.5 */
778 779 780 781
};


static virStateDriver halStateDriver = {
782
    .name = "HAL",
783 784 785
    .stateInitialize = nodeStateInitialize, /* 0.5.0 */
    .stateCleanup = nodeStateCleanup, /* 0.5.0 */
    .stateReload = nodeStateReload, /* 0.5.0 */
786 787
};

788 789
int
halNodeRegister(void)
790
{
791
    if (virSetSharedNodeDeviceDriver(&halNodeDeviceDriver) < 0)
792 793 794
        return -1;
    return virRegisterStateDriver(&halStateDriver);
}