node_device_driver.c 20.0 KB
Newer Older
1
/*
2
 * node_device_driver.c: node device enumeration
3
 *
4
 * Copyright (C) 2010-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 25 26 27 28
 *
 * Author: David F. Lively <dlively@virtualiron.com>
 */

#include <config.h>

#include <unistd.h>
#include <errno.h>
29 30
#include <fcntl.h>
#include <time.h>
31

32
#include "virerror.h"
33
#include "datatypes.h"
34
#include "viralloc.h"
35
#include "virfile.h"
36
#include "virstring.h"
37
#include "node_device_conf.h"
38
#include "node_device_event.h"
39
#include "node_device_driver.h"
40 41
#include "node_device_hal.h"
#include "node_device_linux_sysfs.h"
42 43
#include "virutil.h"
#include "viraccessapicheck.h"
44
#include "virnetdev.h"
45

46 47
#define VIR_FROM_THIS VIR_FROM_NODEDEV

48 49
virNodeDeviceDriverStatePtr driver;

50 51 52 53 54
static int update_caps(virNodeDeviceObjPtr dev)
{
    virNodeDevCapsDefPtr cap = dev->def->caps;

    while (cap) {
55 56
        switch (cap->data.type) {
        case VIR_NODE_DEV_CAP_SCSI_HOST:
57
            nodeDeviceSysfsGetSCSIHostCaps(&dev->def->caps->data);
58 59 60 61
            break;
        case VIR_NODE_DEV_CAP_NET:
            if (virNetDevGetLinkInfo(cap->data.net.ifname, &cap->data.net.lnk) < 0)
                return -1;
62 63 64
            virBitmapFree(cap->data.net.features);
            if (virNetDevGetFeatures(cap->data.net.ifname, &cap->data.net.features) < 0)
                return -1;
65
            break;
66 67 68 69 70
        case VIR_NODE_DEV_CAP_PCI_DEV:
           if (nodeDeviceSysfsGetPCIRelatedDevCaps(dev->def->sysfs_path,
                                                   &dev->def->caps->data) < 0)
              return -1;
           break;
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        /* all types that (supposedly) don't require any updates
         * relative to what's in the cache.
         */
        case VIR_NODE_DEV_CAP_SYSTEM:
        case VIR_NODE_DEV_CAP_USB_DEV:
        case VIR_NODE_DEV_CAP_USB_INTERFACE:
        case VIR_NODE_DEV_CAP_SCSI_TARGET:
        case VIR_NODE_DEV_CAP_SCSI:
        case VIR_NODE_DEV_CAP_STORAGE:
        case VIR_NODE_DEV_CAP_FC_HOST:
        case VIR_NODE_DEV_CAP_VPORTS:
        case VIR_NODE_DEV_CAP_SCSI_GENERIC:
        case VIR_NODE_DEV_CAP_LAST:
            break;
        }
87
        cap = cap->next;
88 89 90 91 92 93
    }

    return 0;
}


94 95 96 97 98 99 100 101 102
#if defined (__linux__) && ( defined (WITH_HAL) || defined(WITH_UDEV))
/* NB: It was previously believed that changes in driver name were
 * relayed to libvirt as "change" events by udev, and the udev event
 * notification is setup to recognize such events and effectively
 * recreate the device entry in the cache. However, neither the kernel
 * nor udev sends such an event, so it is necessary to manually update
 * the driver name for a device each time its entry is used, both for
 * udev *and* HAL backends.
 */
103
static int update_driver_name(virNodeDeviceObjPtr dev)
104 105
{
    char *driver_link = NULL;
106
    char *devpath = NULL;
107 108 109 110 111
    char *p;
    int ret = -1;

    VIR_FREE(dev->def->driver);

112
    if (virAsprintf(&driver_link, "%s/driver", dev->def->sysfs_path) < 0)
113 114 115 116 117 118 119 120 121
        goto cleanup;

    /* Some devices don't have an explicit driver, so just return
       without a name */
    if (access(driver_link, R_OK) < 0) {
        ret = 0;
        goto cleanup;
    }

122
    if (virFileResolveLink(driver_link, &devpath) < 0) {
123
        virReportSystemError(errno,
124 125 126 127 128
                             _("cannot resolve driver link %s"), driver_link);
        goto cleanup;
    }

    p = strrchr(devpath, '/');
129 130
    if (p && VIR_STRDUP(dev->def->driver, p + 1) < 0)
        goto cleanup;
131 132
    ret = 0;

133
 cleanup:
134
    VIR_FREE(driver_link);
135
    VIR_FREE(devpath);
136 137 138 139
    return ret;
}
#else
/* XXX: Implement me for non-linux */
140
static int update_driver_name(virNodeDeviceObjPtr dev ATTRIBUTE_UNUSED)
141 142 143 144 145
{
    return 0;
}
#endif

146

147
void nodeDeviceLock(void)
148
{
149
    virMutexLock(&driver->lock);
150
}
151
void nodeDeviceUnlock(void)
152
{
153
    virMutexUnlock(&driver->lock);
154 155
}

156 157 158
int
nodeNumOfDevices(virConnectPtr conn,
                 const char *cap,
E
Eric Blake 已提交
159
                 unsigned int flags)
160 161
{
    int ndevs = 0;
162
    size_t i;
163

164 165 166
    if (virNodeNumOfDevicesEnsureACL(conn) < 0)
        return -1;

E
Eric Blake 已提交
167 168
    virCheckFlags(0, -1);

169
    nodeDeviceLock();
170
    for (i = 0; i < driver->devs.count; i++) {
171 172 173 174 175
        virNodeDeviceObjPtr obj = driver->devs.objs[i];
        virNodeDeviceObjLock(obj);
        if (virNodeNumOfDevicesCheckACL(conn, obj->def) &&
            ((cap == NULL) ||
             virNodeDeviceHasCap(obj, cap)))
176
            ++ndevs;
177
        virNodeDeviceObjUnlock(obj);
178
    }
179
    nodeDeviceUnlock();
180 181 182 183

    return ndevs;
}

184
int
185 186 187
nodeListDevices(virConnectPtr conn,
                const char *cap,
                char **const names, int maxnames,
E
Eric Blake 已提交
188
                unsigned int flags)
189 190
{
    int ndevs = 0;
191
    size_t i;
192

193 194 195
    if (virNodeListDevicesEnsureACL(conn) < 0)
        return -1;

E
Eric Blake 已提交
196 197
    virCheckFlags(0, -1);

198
    nodeDeviceLock();
199
    for (i = 0; i < driver->devs.count && ndevs < maxnames; i++) {
200 201 202 203 204 205 206
        virNodeDeviceObjPtr obj = driver->devs.objs[i];
        virNodeDeviceObjLock(obj);
        if (virNodeListDevicesCheckACL(conn, obj->def) &&
            (cap == NULL ||
             virNodeDeviceHasCap(obj, cap))) {
            if (VIR_STRDUP(names[ndevs++], obj->def->name) < 0) {
                virNodeDeviceObjUnlock(obj);
207
                goto failure;
208 209
            }
        }
210
        virNodeDeviceObjUnlock(obj);
211
    }
212
    nodeDeviceUnlock();
213 214 215 216

    return ndevs;

 failure:
217
    nodeDeviceUnlock();
218 219 220 221 222 223
    --ndevs;
    while (--ndevs >= 0)
        VIR_FREE(names[ndevs]);
    return -1;
}

O
Osier Yang 已提交
224
int
225 226 227
nodeConnectListAllNodeDevices(virConnectPtr conn,
                              virNodeDevicePtr **devices,
                              unsigned int flags)
O
Osier Yang 已提交
228 229 230 231 232
{
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_NODE_DEVICES_FILTERS_CAP, -1);

233 234 235
    if (virConnectListAllNodeDevicesEnsureACL(conn) < 0)
        return -1;

236
    nodeDeviceLock();
237 238 239
    ret = virNodeDeviceObjListExport(conn, driver->devs, devices,
                                     virConnectListAllNodeDevicesCheckACL,
                                     flags);
240
    nodeDeviceUnlock();
O
Osier Yang 已提交
241 242
    return ret;
}
243

244 245
virNodeDevicePtr
nodeDeviceLookupByName(virConnectPtr conn, const char *name)
246
{
247 248
    virNodeDeviceObjPtr obj;
    virNodeDevicePtr ret = NULL;
249

250
    nodeDeviceLock();
251
    obj = virNodeDeviceFindByName(&driver->devs, name);
252
    nodeDeviceUnlock();
253

254
    if (!obj) {
255 256 257
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       name);
258
        goto cleanup;
259 260
    }

261 262 263
    if (virNodeDeviceLookupByNameEnsureACL(conn, obj->def) < 0)
        goto cleanup;

264
    ret = virGetNodeDevice(conn, name);
265

266
 cleanup:
267 268
    if (obj)
        virNodeDeviceObjUnlock(obj);
269
    return ret;
270 271
}

272

273 274 275 276 277
virNodeDevicePtr
nodeDeviceLookupSCSIHostByWWN(virConnectPtr conn,
                              const char *wwnn,
                              const char *wwpn,
                              unsigned int flags)
278
{
279
    size_t i;
280 281 282 283 284
    virNodeDeviceObjListPtr devs = &driver->devs;
    virNodeDevCapsDefPtr cap = NULL;
    virNodeDeviceObjPtr obj = NULL;
    virNodeDevicePtr dev = NULL;

285 286
    virCheckFlags(0, NULL);

287
    nodeDeviceLock();
288 289 290 291 292 293 294

    for (i = 0; i < devs->count; i++) {
        obj = devs->objs[i];
        virNodeDeviceObjLock(obj);
        cap = obj->def->caps;

        while (cap) {
295
            if (cap->data.type == VIR_NODE_DEV_CAP_SCSI_HOST) {
296
                nodeDeviceSysfsGetSCSIHostCaps(&cap->data);
297 298 299 300
                if (cap->data.scsi_host.flags &
                    VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
                    if (STREQ(cap->data.scsi_host.wwnn, wwnn) &&
                        STREQ(cap->data.scsi_host.wwpn, wwpn)) {
301 302 303 304

                        if (virNodeDeviceLookupSCSIHostByWWNEnsureACL(conn, obj->def) < 0)
                            goto out;

305 306 307 308 309 310 311 312 313 314 315 316
                        dev = virGetNodeDevice(conn, obj->def->name);
                        virNodeDeviceObjUnlock(obj);
                        goto out;
                    }
                }
            }
            cap = cap->next;
        }

        virNodeDeviceObjUnlock(obj);
    }

317
 out:
318
    nodeDeviceUnlock();
319 320 321 322
    return dev;
}


323 324
char *
nodeDeviceGetXMLDesc(virNodeDevicePtr dev,
E
Eric Blake 已提交
325
                     unsigned int flags)
326
{
327 328
    virNodeDeviceObjPtr obj;
    char *ret = NULL;
329

E
Eric Blake 已提交
330 331
    virCheckFlags(0, NULL);

332
    nodeDeviceLock();
333
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
334
    nodeDeviceUnlock();
335

336
    if (!obj) {
337 338 339
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
340
        goto cleanup;
341 342
    }

343 344 345
    if (virNodeDeviceGetXMLDescEnsureACL(dev->conn, obj->def) < 0)
        goto cleanup;

346
    update_driver_name(obj);
347 348
    if (update_caps(obj) < 0)
        goto cleanup;
349

350
    ret = virNodeDeviceDefFormat(obj->def);
351

352
 cleanup:
353 354
    if (obj)
        virNodeDeviceObjUnlock(obj);
355
    return ret;
356 357 358
}


359 360
char *
nodeDeviceGetParent(virNodeDevicePtr dev)
361
{
362 363
    virNodeDeviceObjPtr obj;
    char *ret = NULL;
364

365
    nodeDeviceLock();
366
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
367
    nodeDeviceUnlock();
368

369
    if (!obj) {
370 371 372
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
373
        goto cleanup;
374 375
    }

376 377 378
    if (virNodeDeviceGetParentEnsureACL(dev->conn, obj->def) < 0)
        goto cleanup;

379
    if (obj->def->parent) {
380 381
        if (VIR_STRDUP(ret, obj->def->parent) < 0)
            goto cleanup;
382
    } else {
383 384
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("no parent for this device"));
385
    }
386

387
 cleanup:
388 389
    if (obj)
        virNodeDeviceObjUnlock(obj);
390
    return ret;
391 392 393
}


394 395
int
nodeDeviceNumOfCaps(virNodeDevicePtr dev)
396
{
397
    virNodeDeviceObjPtr obj;
398 399
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
400
    int ret = -1;
401

402
    nodeDeviceLock();
403
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
404
    nodeDeviceUnlock();
405

406
    if (!obj) {
407 408 409
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
410
        goto cleanup;
411 412
    }

413 414 415
    if (virNodeDeviceNumOfCapsEnsureACL(dev->conn, obj->def) < 0)
        goto cleanup;

416
    for (caps = obj->def->caps; caps; caps = caps->next) {
417
        ++ncaps;
418

419
        if (caps->data.type == VIR_NODE_DEV_CAP_SCSI_HOST) {
420 421 422 423 424 425 426 427 428 429
            if (caps->data.scsi_host.flags &
                VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST)
                ncaps++;

            if (caps->data.scsi_host.flags &
                VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS)
                ncaps++;
        }
    }

430
    ret = ncaps;
431

432
 cleanup:
433 434
    if (obj)
        virNodeDeviceObjUnlock(obj);
435
    return ret;
436 437 438
}


439
int
440 441
nodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
442
    virNodeDeviceObjPtr obj;
443 444
    virNodeDevCapsDefPtr caps;
    int ncaps = 0;
445
    int ret = -1;
446

447
    nodeDeviceLock();
448
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
449
    nodeDeviceUnlock();
450

451
    if (!obj) {
452 453 454
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
455
        goto cleanup;
456 457
    }

458 459 460
    if (virNodeDeviceListCapsEnsureACL(dev->conn, obj->def) < 0)
        goto cleanup;

461
    for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
462
        if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->data.type)) < 0)
463
            goto cleanup;
464

465
        if (caps->data.type == VIR_NODE_DEV_CAP_SCSI_HOST) {
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
            if (ncaps < maxnames &&
                caps->data.scsi_host.flags &
                VIR_NODE_DEV_CAP_FLAG_HBA_FC_HOST) {
                if (VIR_STRDUP(names[ncaps++],
                               virNodeDevCapTypeToString(VIR_NODE_DEV_CAP_FC_HOST)) < 0)
                    goto cleanup;
            }

            if (ncaps < maxnames &&
                caps->data.scsi_host.flags &
                VIR_NODE_DEV_CAP_FLAG_HBA_VPORT_OPS) {
                if (VIR_STRDUP(names[ncaps++],
                               virNodeDevCapTypeToString(VIR_NODE_DEV_CAP_VPORTS)) < 0)
                    goto cleanup;
            }
        }
482
    }
483
    ret = ncaps;
484

485
 cleanup:
486 487
    if (obj)
        virNodeDeviceObjUnlock(obj);
488 489 490 491 492 493
    if (ret == -1) {
        --ncaps;
        while (--ncaps >= 0)
            VIR_FREE(names[ncaps]);
    }
    return ret;
494 495
}

496
static int
497
get_time(time_t *t)
498 499 500 501 502
{
    int ret = 0;

    *t = time(NULL);
    if (*t == (time_t)-1) {
503 504
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       "%s", _("Could not get current time"));
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537

        *t = 0;
        ret = -1;
    }

    return ret;
}


/* When large numbers of devices are present on the host, it's
 * possible for udev not to realize that it has work to do before we
 * get here.  We thus keep trying to find the new device we just
 * created for up to LINUX_NEW_DEVICE_WAIT_TIME.  Note that udev's
 * default settle time is 180 seconds, so once udev realizes that it
 * has work to do, it might take that long for the udev wait to
 * return.  Thus the total maximum time for this function to return is
 * the udev settle time plus LINUX_NEW_DEVICE_WAIT_TIME.
 *
 * This whole area is a race, but if we retry the udev wait for
 * LINUX_NEW_DEVICE_WAIT_TIME seconds and there's still no device,
 * it's probably safe to assume it's not going to appear.
 */
static virNodeDevicePtr
find_new_device(virConnectPtr conn, const char *wwnn, const char *wwpn)
{
    virNodeDevicePtr dev = NULL;
    time_t start = 0, now = 0;

    /* The thread that creates the device takes the driver lock, so we
     * must release it in order to allow the device to be created.
     * We're not doing anything with the driver pointer at this point,
     * so it's safe to release it, assuming that the pointer itself
     * doesn't become invalid.  */
538
    nodeDeviceUnlock();
539

540
    get_time(&start);
541 542 543

    while ((now - start) < LINUX_NEW_DEVICE_WAIT_TIME) {

544
        virFileWaitForDevices();
545

546
        dev = nodeDeviceLookupSCSIHostByWWN(conn, wwnn, wwpn, 0);
547

548
        if (dev != NULL)
549 550 551
            break;

        sleep(5);
552
        if (get_time(&now) == -1)
553 554 555
            break;
    }

556
    nodeDeviceLock();
557 558 559 560

    return dev;
}

561
virNodeDevicePtr
562 563
nodeDeviceCreateXML(virConnectPtr conn,
                    const char *xmlDesc,
E
Eric Blake 已提交
564
                    unsigned int flags)
565 566 567 568 569
{
    virNodeDeviceDefPtr def = NULL;
    char *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;
    virNodeDevicePtr dev = NULL;
570
    const char *virt_type = NULL;
571

E
Eric Blake 已提交
572
    virCheckFlags(0, NULL);
573
    virt_type  = virConnectGetType(conn);
E
Eric Blake 已提交
574

575
    nodeDeviceLock();
576

577
    def = virNodeDeviceDefParseString(xmlDesc, CREATE_DEVICE, virt_type);
578
    if (def == NULL)
579 580
        goto cleanup;

581 582 583
    if (virNodeDeviceCreateXMLEnsureACL(conn, def) < 0)
        goto cleanup;

584
    if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1)
585 586
        goto cleanup;

587 588 589 590 591 592
    if (def->parent) {
        if (virNodeDeviceGetParentHost(&driver->devs,
                                       def->name,
                                       def->parent,
                                       &parent_host) < 0)
            goto cleanup;
593 594 595 596 597 598 599 600 601 602 603 604 605
    } else if (def->parent_wwnn && def->parent_wwpn) {
        if (virNodeDeviceGetParentHostByWWNs(&driver->devs,
                                             def->name,
                                             def->parent_wwnn,
                                             def->parent_wwpn,
                                             &parent_host) < 0)
            goto cleanup;
    } else if (def->parent_fabric_wwn) {
        if (virNodeDeviceGetParentHostByFabricWWN(&driver->devs,
                                                  def->name,
                                                  def->parent_fabric_wwn,
                                                  &parent_host) < 0)
            goto cleanup;
606 607 608 609
    } else {
        /* Try to find a vport capable scsi_host when no parent supplied */
        if (virNodeDeviceFindVportParentHost(&driver->devs, &parent_host) < 0)
            goto cleanup;
610 611
    }

612 613 614 615
    if (virManageVport(parent_host,
                       wwpn,
                       wwnn,
                       VPORT_CREATE) == -1) {
616 617 618 619 620 621 622
        goto cleanup;
    }

    dev = find_new_device(conn, wwnn, wwpn);
    /* We don't check the return value, because one way or another,
     * we're returning what we get... */

623
    if (dev == NULL)
624 625 626 627
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device for '%s' with matching "
                         "wwnn '%s' and wwpn '%s'"),
                       def->name, wwnn, wwpn);
628
 cleanup:
629
    nodeDeviceUnlock();
630 631 632 633 634 635 636
    virNodeDeviceDefFree(def);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return dev;
}


637
int
638 639
nodeDeviceDestroy(virNodeDevicePtr dev)
{
D
David Allan 已提交
640
    int ret = -1;
641 642 643 644
    virNodeDeviceObjPtr obj = NULL;
    char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL;
    int parent_host = -1;

645
    nodeDeviceLock();
646
    obj = virNodeDeviceFindByName(&driver->devs, dev->name);
647
    nodeDeviceUnlock();
648 649

    if (!obj) {
650 651 652
        virReportError(VIR_ERR_NO_NODE_DEVICE,
                       _("no node device with matching name '%s'"),
                       dev->name);
653 654 655
        goto out;
    }

656 657 658
    if (virNodeDeviceDestroyEnsureACL(dev->conn, obj->def) < 0)
        goto out;

659
    if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1)
660 661 662
        goto out;


663
    /* virNodeDeviceGetParentHost will cause the device object's lock to be
664 665 666
     * taken, so we have to dup the parent's name and drop the lock
     * before calling it.  We don't need the reference to the object
     * any more once we have the parent's name.  */
667 668 669
    if (VIR_STRDUP(parent_name, obj->def->parent) < 0) {
        virNodeDeviceObjUnlock(obj);
        obj = NULL;
670 671
        goto out;
    }
672 673
    virNodeDeviceObjUnlock(obj);
    obj = NULL;
674

675
    if (virNodeDeviceGetParentHost(&driver->devs,
676 677 678
                                   dev->name,
                                   parent_name,
                                   &parent_host) == -1) {
679 680 681
        goto out;
    }

682 683 684 685
    if (virManageVport(parent_host,
                       wwpn,
                       wwnn,
                       VPORT_DELETE) == -1) {
686 687 688
        goto out;
    }

D
David Allan 已提交
689
    ret = 0;
690
 out:
691 692
    if (obj)
        virNodeDeviceObjUnlock(obj);
693 694 695 696 697 698
    VIR_FREE(parent_name);
    VIR_FREE(wwnn);
    VIR_FREE(wwpn);
    return ret;
}

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
int
nodeConnectNodeDeviceEventRegisterAny(virConnectPtr conn,
                                      virNodeDevicePtr dev,
                                      int eventID,
                                      virConnectNodeDeviceEventGenericCallback callback,
                                      void *opaque,
                                      virFreeCallback freecb)
{
    int callbackID = -1;

    if (virConnectNodeDeviceEventRegisterAnyEnsureACL(conn) < 0)
        goto cleanup;

    if (virNodeDeviceEventStateRegisterID(conn, driver->nodeDeviceEventState,
                                          dev, eventID, callback,
                                          opaque, freecb, &callbackID) < 0)
        callbackID = -1;
 cleanup:
    return callbackID;
}

int
nodeConnectNodeDeviceEventDeregisterAny(virConnectPtr conn,
                                        int callbackID)
{
    int ret = -1;

    if (virConnectNodeDeviceEventDeregisterAnyEnsureACL(conn) < 0)
        goto cleanup;

    if (virObjectEventStateDeregisterID(conn,
                                        driver->nodeDeviceEventState,
                                        callbackID) < 0)
        goto cleanup;

    ret = 0;

 cleanup:
    return ret;
}

740 741
int nodedevRegister(void)
{
742 743
#ifdef WITH_UDEV
    return udevNodeRegister();
744
#else
745
# ifdef WITH_HAL
746
    return halNodeRegister();
747
# endif
748 749
#endif
}