You need to sign in or sign up before continuing.
virsh-secret.c 20.5 KB
Newer Older
1 2 3
/*
 * virsh-secret.c: Commands to manage secret
 *
4
 * Copyright (C) 2005, 2007-2016 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
18 19 20 21 22 23 24 25
 * <http://www.gnu.org/licenses/>.
 *
 *  Daniel Veillard <veillard@redhat.com>
 *  Karel Zak <kzak@redhat.com>
 *  Daniel P. Berrange <berrange@redhat.com>
 *
 */

E
Eric Blake 已提交
26 27 28 29 30
#include <config.h>
#include "virsh-secret.h"

#include "internal.h"
#include "base64.h"
31
#include "virbuffer.h"
32
#include "viralloc.h"
33
#include "virfile.h"
34
#include "virutil.h"
35
#include "virsecret.h"
36
#include "virstring.h"
37
#include "virtime.h"
38
#include "vsh-table.h"
E
Eric Blake 已提交
39

40
static virSecretPtr
41
virshCommandOptSecret(vshControl *ctl, const vshCmd *cmd, const char **name)
42 43 44 45
{
    virSecretPtr secret = NULL;
    const char *n = NULL;
    const char *optname = "secret";
46
    virshControlPtr priv = ctl->privData;
47

48
    if (vshCommandOptStringReq(ctl, cmd, optname, &n) < 0)
49 50 51 52 53 54 55 56
        return NULL;

    vshDebug(ctl, VSH_ERR_DEBUG,
             "%s: found option <%s>: %s\n", cmd->def->name, optname, n);

    if (name != NULL)
        *name = n;

57
    secret = virSecretLookupByUUIDString(priv->conn, n);
58 59 60 61 62 63 64 65 66 67 68

    if (secret == NULL)
        vshError(ctl, _("failed to get secret '%s'"), n);

    return secret;
}

/*
 * "secret-define" command
 */
static const vshCmdInfo info_secret_define[] = {
69 70 71 72 73 74 75
    {.name = "help",
     .data = N_("define or modify a secret from an XML file")
    },
    {.name = "desc",
     .data = N_("Define or modify a secret.")
    },
    {.name = NULL}
76 77 78
};

static const vshCmdOptDef opts_secret_define[] = {
79
    VIRSH_COMMON_OPT_FILE(N_("file containing secret attributes in XML")),
80
    {.name = NULL}
81 82 83 84 85 86 87 88 89
};

static bool
cmdSecretDefine(vshControl *ctl, const vshCmd *cmd)
{
    const char *from = NULL;
    char *buffer;
    virSecretPtr res;
    char uuid[VIR_UUID_STRING_BUFLEN];
90
    bool ret = false;
91
    virshControlPtr priv = ctl->privData;
92

93
    if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
94 95
        return false;

E
Eric Blake 已提交
96
    if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0)
97 98
        return false;

99
    if (!(res = virSecretDefineXML(priv->conn, buffer, 0))) {
100
        vshError(ctl, _("Failed to set attributes from %s"), from);
101
        goto cleanup;
102
    }
103

104 105
    if (virSecretGetUUIDString(res, &(uuid[0])) < 0) {
        vshError(ctl, "%s", _("Failed to get UUID of created secret"));
106
        goto cleanup;
107
    }
108

P
Pino Toscano 已提交
109
    vshPrintExtra(ctl, _("Secret %s created\n"), uuid);
110 111
    ret = true;

112
 cleanup:
113
    VIR_FREE(buffer);
114 115
    if (res)
        virSecretFree(res);
116
    return ret;
117 118 119 120 121 122
}

/*
 * "secret-dumpxml" command
 */
static const vshCmdInfo info_secret_dumpxml[] = {
123 124 125 126 127 128 129
    {.name = "help",
     .data = N_("secret attributes in XML")
    },
    {.name = "desc",
     .data = N_("Output attributes of a secret as an XML dump to stdout.")
    },
    {.name = NULL}
130 131 132
};

static const vshCmdOptDef opts_secret_dumpxml[] = {
133 134 135
    {.name = "secret",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
136 137
     .help = N_("secret UUID"),
     .completer = virshSecretUUIDCompleter,
138 139
    },
    {.name = NULL}
140 141 142 143 144 145 146 147 148
};

static bool
cmdSecretDumpXML(vshControl *ctl, const vshCmd *cmd)
{
    virSecretPtr secret;
    bool ret = false;
    char *xml;

149
    secret = virshCommandOptSecret(ctl, cmd, NULL);
150 151 152 153 154 155 156 157 158 159
    if (secret == NULL)
        return false;

    xml = virSecretGetXMLDesc(secret, 0);
    if (xml == NULL)
        goto cleanup;
    vshPrint(ctl, "%s", xml);
    VIR_FREE(xml);
    ret = true;

160
 cleanup:
161 162 163 164 165 166 167 168
    virSecretFree(secret);
    return ret;
}

/*
 * "secret-set-value" command
 */
static const vshCmdInfo info_secret_set_value[] = {
169 170 171 172 173 174 175
    {.name = "help",
     .data = N_("set a secret value")
    },
    {.name = "desc",
     .data = N_("Set a secret value.")
    },
    {.name = NULL}
176 177 178
};

static const vshCmdOptDef opts_secret_set_value[] = {
179 180 181
    {.name = "secret",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
182 183
     .help = N_("secret UUID"),
     .completer = virshSecretUUIDCompleter,
184 185 186 187 188 189 190
    },
    {.name = "base64",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
     .help = N_("base64-encoded secret value")
    },
    {.name = NULL}
191 192 193 194 195 196 197 198 199 200 201 202
};

static bool
cmdSecretSetValue(vshControl *ctl, const vshCmd *cmd)
{
    virSecretPtr secret;
    size_t value_size;
    const char *base64 = NULL;
    char *value;
    int res;
    bool ret = false;

203
    if (!(secret = virshCommandOptSecret(ctl, cmd, NULL)))
204 205
        return false;

206
    if (vshCommandOptStringReq(ctl, cmd, "base64", &base64) < 0)
207 208 209 210 211 212 213 214
        goto cleanup;

    if (!base64_decode_alloc(base64, strlen(base64), &value, &value_size)) {
        vshError(ctl, "%s", _("Invalid base64 data"));
        goto cleanup;
    }
    if (value == NULL) {
        vshError(ctl, "%s", _("Failed to allocate memory"));
215
        goto cleanup;
216 217 218 219 220 221 222 223 224 225
    }

    res = virSecretSetValue(secret, (unsigned char *)value, value_size, 0);
    memset(value, 0, value_size);
    VIR_FREE(value);

    if (res != 0) {
        vshError(ctl, "%s", _("Failed to set secret value"));
        goto cleanup;
    }
P
Pino Toscano 已提交
226
    vshPrintExtra(ctl, "%s", _("Secret value set\n"));
227 228
    ret = true;

229
 cleanup:
230 231 232 233 234 235 236 237
    virSecretFree(secret);
    return ret;
}

/*
 * "secret-get-value" command
 */
static const vshCmdInfo info_secret_get_value[] = {
238 239 240 241 242 243 244
    {.name = "help",
     .data = N_("Output a secret value")
    },
    {.name = "desc",
     .data = N_("Output a secret value to stdout.")
    },
    {.name = NULL}
245 246 247
};

static const vshCmdOptDef opts_secret_get_value[] = {
248 249 250
    {.name = "secret",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
251 252
     .help = N_("secret UUID"),
     .completer = virshSecretUUIDCompleter,
253 254
    },
    {.name = NULL}
255 256 257 258 259 260
};

static bool
cmdSecretGetValue(vshControl *ctl, const vshCmd *cmd)
{
    virSecretPtr secret;
261
    char *base64 = NULL;
262 263 264 265
    unsigned char *value;
    size_t value_size;
    bool ret = false;

266
    secret = virshCommandOptSecret(ctl, cmd, NULL);
267 268 269 270 271 272 273
    if (secret == NULL)
        return false;

    value = virSecretGetValue(secret, &value_size, 0);
    if (value == NULL)
        goto cleanup;

274
    if (!(base64 = virStringEncodeBase64(value, value_size)))
275
        goto cleanup;
276

277 278 279
    vshPrint(ctl, "%s", base64);
    ret = true;

280
 cleanup:
281 282
    VIR_DISPOSE_N(value, value_size);
    VIR_DISPOSE_STRING(base64);
283 284 285 286 287 288 289 290
    virSecretFree(secret);
    return ret;
}

/*
 * "secret-undefine" command
 */
static const vshCmdInfo info_secret_undefine[] = {
291 292 293 294 295 296 297
    {.name = "help",
     .data = N_("undefine a secret")
    },
    {.name = "desc",
     .data = N_("Undefine a secret.")
    },
    {.name = NULL}
298 299 300
};

static const vshCmdOptDef opts_secret_undefine[] = {
301 302 303
    {.name = "secret",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
304 305
     .help = N_("secret UUID"),
     .completer = virshSecretUUIDCompleter,
306 307
    },
    {.name = NULL}
308 309 310 311 312 313 314 315 316
};

static bool
cmdSecretUndefine(vshControl *ctl, const vshCmd *cmd)
{
    virSecretPtr secret;
    bool ret = false;
    const char *uuid;

317
    secret = virshCommandOptSecret(ctl, cmd, &uuid);
318 319 320 321 322 323 324
    if (secret == NULL)
        return false;

    if (virSecretUndefine(secret) < 0) {
        vshError(ctl, _("Failed to delete secret %s"), uuid);
        goto cleanup;
    }
P
Pino Toscano 已提交
325
    vshPrintExtra(ctl, _("Secret %s deleted\n"), uuid);
326 327
    ret = true;

328
 cleanup:
329 330 331 332
    virSecretFree(secret);
    return ret;
}

333
static int
334
virshSecretSorter(const void *a, const void *b)
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
{
    virSecretPtr *sa = (virSecretPtr *) a;
    virSecretPtr *sb = (virSecretPtr *) b;
    char uuid_sa[VIR_UUID_STRING_BUFLEN];
    char uuid_sb[VIR_UUID_STRING_BUFLEN];

    if (*sa && !*sb)
        return -1;

    if (!*sa)
        return *sb != NULL;

    virSecretGetUUIDString(*sa, uuid_sa);
    virSecretGetUUIDString(*sb, uuid_sb);

    return vshStrcasecmp(uuid_sa, uuid_sb);
}

353
struct virshSecretList {
354 355 356
    virSecretPtr *secrets;
    size_t nsecrets;
};
357
typedef struct virshSecretList *virshSecretListPtr;
358 359

static void
360
virshSecretListFree(virshSecretListPtr list)
361
{
362
    size_t i;
363

364
    if (list && list->secrets) {
365 366 367 368 369 370 371 372 373
        for (i = 0; i < list->nsecrets; i++) {
            if (list->secrets[i])
                virSecretFree(list->secrets[i]);
        }
        VIR_FREE(list->secrets);
    }
    VIR_FREE(list);
}

374 375 376
static virshSecretListPtr
virshSecretListCollect(vshControl *ctl,
                       unsigned int flags)
377
{
378
    virshSecretListPtr list = vshMalloc(ctl, sizeof(*list));
379
    size_t i;
380 381 382 383 384 385
    int ret;
    virSecretPtr secret;
    bool success = false;
    size_t deleted = 0;
    int nsecrets = 0;
    char **uuids = NULL;
386
    virshControlPtr priv = ctl->privData;
387 388

    /* try the list with flags support (0.10.2 and later) */
389
    if ((ret = virConnectListAllSecrets(priv->conn,
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
                                        &list->secrets,
                                        flags)) >= 0) {
        list->nsecrets = ret;
        goto finished;
    }

    /* check if the command is actually supported */
    if (last_error && last_error->code == VIR_ERR_NO_SUPPORT)
        goto fallback;

    /* there was an error during the call */
    vshError(ctl, "%s", _("Failed to list node secrets"));
    goto cleanup;


405
 fallback:
406 407 408 409 410 411 412 413
    /* fall back to old method (0.10.1 and older) */
    vshResetLibvirtError();

    if (flags) {
        vshError(ctl, "%s", _("Filtering is not supported by this libvirt"));
        goto cleanup;
    }

414
    nsecrets = virConnectNumOfSecrets(priv->conn);
415 416 417 418 419 420 421 422 423 424
    if (nsecrets < 0) {
        vshError(ctl, "%s", _("Failed to count secrets"));
        goto cleanup;
    }

    if (nsecrets == 0)
        return list;

    uuids = vshMalloc(ctl, sizeof(char *) * nsecrets);

425
    nsecrets = virConnectListSecrets(priv->conn, uuids, nsecrets);
426 427 428 429 430 431 432 433 434
    if (nsecrets < 0) {
        vshError(ctl, "%s", _("Failed to list secrets"));
        goto cleanup;
    }

    list->secrets = vshMalloc(ctl, sizeof(virSecretPtr) * (nsecrets));
    list->nsecrets = 0;

    /* get the secrets */
435
    for (i = 0; i < nsecrets; i++) {
436
        if (!(secret = virSecretLookupByUUIDString(priv->conn, uuids[i])))
437 438 439 440 441 442 443
            continue;
        list->secrets[list->nsecrets++] = secret;
    }

    /* truncate secrets that weren't found */
    deleted = nsecrets - list->nsecrets;

444
 finished:
445 446 447
    /* sort the list */
    if (list->secrets && list->nsecrets)
        qsort(list->secrets, list->nsecrets,
448
              sizeof(*list->secrets), virshSecretSorter);
449 450 451 452 453 454 455

    /* truncate the list for not found secret objects */
    if (deleted)
        VIR_SHRINK_N(list->secrets, list->nsecrets, deleted);

    success = true;

456
 cleanup:
457 458 459 460 461
    if (nsecrets > 0) {
        for (i = 0; i < nsecrets; i++)
            VIR_FREE(uuids[i]);
        VIR_FREE(uuids);
    }
462 463

    if (!success) {
464
        virshSecretListFree(list);
465 466 467 468 469 470
        list = NULL;
    }

    return list;
}

471 472 473 474
/*
 * "secret-list" command
 */
static const vshCmdInfo info_secret_list[] = {
475 476 477 478 479 480 481
    {.name = "help",
     .data = N_("list secrets")
    },
    {.name = "desc",
     .data = N_("Returns a list of secrets")
    },
    {.name = NULL}
482 483
};

484
static const vshCmdOptDef opts_secret_list[] = {
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
    {.name = "ephemeral",
     .type = VSH_OT_BOOL,
     .help = N_("list ephemeral secrets")
    },
    {.name = "no-ephemeral",
     .type = VSH_OT_BOOL,
     .help = N_("list non-ephemeral secrets")
    },
    {.name = "private",
     .type = VSH_OT_BOOL,
     .help = N_("list private secrets")
    },
    {.name = "no-private",
     .type = VSH_OT_BOOL,
     .help = N_("list non-private secrets")
    },
    {.name = NULL}
502 503
};

504 505 506
static bool
cmdSecretList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{
507
    size_t i;
508
    virshSecretListPtr list = NULL;
509 510
    bool ret = false;
    unsigned int flags = 0;
511
    vshTablePtr table = NULL;
512

513 514
    if (vshCommandOptBool(cmd, "ephemeral"))
        flags |= VIR_CONNECT_LIST_SECRETS_EPHEMERAL;
515

516 517 518 519 520
    if (vshCommandOptBool(cmd, "no-ephemeral"))
        flags |= VIR_CONNECT_LIST_SECRETS_NO_EPHEMERAL;

    if (vshCommandOptBool(cmd, "private"))
        flags |= VIR_CONNECT_LIST_SECRETS_PRIVATE;
521

522 523 524
    if (vshCommandOptBool(cmd, "no-private"))
        flags |= VIR_CONNECT_LIST_SECRETS_NO_PRIVATE;

525
    if (!(list = virshSecretListCollect(ctl, flags)))
526
        return false;
527

528 529 530
    table = vshTableNew(_("UUID"), _("Usage"), NULL);
    if (!table)
        goto cleanup;
531

532 533
    for (i = 0; i < list->nsecrets; i++) {
        virSecretPtr sec = list->secrets[i];
J
John Ferlan 已提交
534
        int usageType = virSecretGetUsageType(sec);
535
        const char *usageStr = virSecretUsageTypeToString(usageType);
536
        char uuid[VIR_UUID_STRING_BUFLEN];
537 538
        virBuffer buf = VIR_BUFFER_INITIALIZER;
        VIR_AUTOFREE(char *) usage = NULL;
J
John Ferlan 已提交
539

540
        if (virSecretGetUUIDString(sec, uuid) < 0) {
541 542 543 544
            vshError(ctl, "%s", _("Failed to get uuid of secret"));
            goto cleanup;
        }

545
        if (usageType) {
546 547 548 549 550 551 552 553
            virBufferStrcat(&buf, usageStr, " ",
                            virSecretGetUsageID(sec), NULL);
            usage = virBufferContentAndReset(&buf);
            if (!usage)
                goto cleanup;

            if (vshTableRowAppend(table, uuid, usage, NULL) < 0)
                goto cleanup;
554
        } else {
555 556
            if (vshTableRowAppend(table, uuid, _("Unused"), NULL) < 0)
                goto cleanup;
557 558
        }
    }
559

560 561
    vshTablePrintToStdout(table, ctl);

562 563
    ret = true;

564
 cleanup:
565
    vshTableFree(table);
566
    virshSecretListFree(list);
567
    return ret;
568
}
569

570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
/*
 * "Secret-event" command
 */
VIR_ENUM_DECL(virshSecretEvent)
VIR_ENUM_IMPL(virshSecretEvent,
              VIR_SECRET_EVENT_LAST,
              N_("Defined"),
              N_("Undefined"))

static const char *
virshSecretEventToString(int event)
{
    const char *str = virshSecretEventTypeToString(event);
    return str ? _(str) : _("unknown");
}

struct virshSecretEventData {
    vshControl *ctl;
    bool loop;
    bool timestamp;
    int count;
591
    virshSecretEventCallback *cb;
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
};
typedef struct virshSecretEventData virshSecretEventData;

static void
vshEventLifecyclePrint(virConnectPtr conn ATTRIBUTE_UNUSED,
                       virSecretPtr secret,
                       int event,
                       int detail ATTRIBUTE_UNUSED,
                       void *opaque)
{
    virshSecretEventData *data = opaque;
    char uuid[VIR_UUID_STRING_BUFLEN];

    if (!data->loop && data->count)
        return;

    virSecretGetUUIDString(secret, uuid);
    if (data->timestamp) {
        char timestamp[VIR_TIME_STRING_BUFLEN];

        if (virTimeStringNowRaw(timestamp) < 0)
            timestamp[0] = '\0';

        vshPrint(data->ctl, _("%s: event 'lifecycle' for secret %s: %s\n"),
                 timestamp, uuid, virshSecretEventToString(event));
    } else {
        vshPrint(data->ctl, _("event 'lifecycle' for secret %s: %s\n"),
                 uuid, virshSecretEventToString(event));
    }

    data->count++;
    if (!data->loop)
        vshEventDone(data->ctl);
}

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
static void
vshEventGenericPrint(virConnectPtr conn ATTRIBUTE_UNUSED,
                     virSecretPtr secret,
                     void *opaque)
{
    virshSecretEventData *data = opaque;
    char uuid[VIR_UUID_STRING_BUFLEN];

    if (!data->loop && data->count)
        return;

    virSecretGetUUIDString(secret, uuid);

    if (data->timestamp) {
        char timestamp[VIR_TIME_STRING_BUFLEN];

        if (virTimeStringNowRaw(timestamp) < 0)
            timestamp[0] = '\0';

        vshPrint(data->ctl, _("%s: event '%s' for secret %s\n"),
                 timestamp,
                 data->cb->name,
                 uuid);
    } else {
        vshPrint(data->ctl, _("event '%s' for secret %s\n"),
                 data->cb->name,
                 uuid);
    }

    data->count++;
    if (!data->loop)
        vshEventDone(data->ctl);
}

661
virshSecretEventCallback virshSecretEventCallbacks[] = {
662 663
    { "lifecycle",
      VIR_SECRET_EVENT_CALLBACK(vshEventLifecyclePrint), },
664
    { "value-changed", vshEventGenericPrint, },
665
};
666
verify(VIR_SECRET_EVENT_ID_LAST == ARRAY_CARDINALITY(virshSecretEventCallbacks));
667 668 669 670 671 672 673 674 675 676 677 678 679 680

static const vshCmdInfo info_secret_event[] = {
    {.name = "help",
     .data = N_("Secret Events")
    },
    {.name = "desc",
     .data = N_("List event types, or wait for secret events to occur")
    },
    {.name = NULL}
};

static const vshCmdOptDef opts_secret_event[] = {
    {.name = "secret",
     .type = VSH_OT_STRING,
681 682
     .help = N_("filter by secret name or uuid"),
     .completer = virshSecretUUIDCompleter,
683 684 685
    },
    {.name = "event",
     .type = VSH_OT_STRING,
686
     .completer = virshSecretEventNameCompleter,
687 688 689 690 691 692 693 694 695 696 697 698 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
     .help = N_("which event type to wait for")
    },
    {.name = "loop",
     .type = VSH_OT_BOOL,
     .help = N_("loop until timeout or interrupt, rather than one-shot")
    },
    {.name = "timeout",
     .type = VSH_OT_INT,
     .help = N_("timeout seconds")
    },
    {.name = "list",
     .type = VSH_OT_BOOL,
     .help = N_("list valid event types")
    },
    {.name = "timestamp",
     .type = VSH_OT_BOOL,
     .help = N_("show timestamp for each printed event")
    },
    {.name = NULL}
};

static bool
cmdSecretEvent(vshControl *ctl, const vshCmd *cmd)
{
    virSecretPtr secret = NULL;
    bool ret = false;
    int eventId = -1;
    int timeout = 0;
    virshSecretEventData data;
    const char *eventName = NULL;
    int event;
    virshControlPtr priv = ctl->privData;

    if (vshCommandOptBool(cmd, "list")) {
        size_t i;

        for (i = 0; i < VIR_SECRET_EVENT_ID_LAST; i++)
724
            vshPrint(ctl, "%s\n", virshSecretEventCallbacks[i].name);
725 726 727 728 729 730 731 732 733 734
        return true;
    }

    if (vshCommandOptStringReq(ctl, cmd, "event", &eventName) < 0)
        return false;
    if (!eventName) {
        vshError(ctl, "%s", _("either --list or --event <type> is required"));
        return false;
    }
    for (event = 0; event < VIR_SECRET_EVENT_ID_LAST; event++)
735
        if (STREQ(eventName, virshSecretEventCallbacks[event].name))
736 737 738 739 740 741 742 743 744 745
            break;
    if (event == VIR_SECRET_EVENT_ID_LAST) {
        vshError(ctl, _("unknown event type %s"), eventName);
        return false;
    }

    data.ctl = ctl;
    data.loop = vshCommandOptBool(cmd, "loop");
    data.timestamp = vshCommandOptBool(cmd, "timestamp");
    data.count = 0;
746
    data.cb = &virshSecretEventCallbacks[event];
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
    if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0)
        return false;

    if (vshCommandOptBool(cmd, "secret"))
        secret = virshCommandOptSecret(ctl, cmd, NULL);
    if (vshEventStart(ctl, timeout) < 0)
        goto cleanup;

    if ((eventId = virConnectSecretEventRegisterAny(priv->conn, secret, event,
                                                    data.cb->cb,
                                                    &data, NULL)) < 0)
        goto cleanup;
    switch (vshEventWait(ctl)) {
    case VSH_EVENT_INTERRUPT:
        vshPrint(ctl, "%s", _("event loop interrupted\n"));
        break;
    case VSH_EVENT_TIMEOUT:
        vshPrint(ctl, "%s", _("event loop timed out\n"));
        break;
    case VSH_EVENT_DONE:
        break;
    default:
        goto cleanup;
    }
    vshPrint(ctl, _("events received: %d\n"), data.count);
    if (data.count)
        ret = true;

 cleanup:
    vshEventCleanup(ctl);
    if (eventId >= 0 &&
        virConnectSecretEventDeregisterAny(priv->conn, eventId) < 0)
        ret = false;
    if (secret)
        virSecretFree(secret);
    return ret;
}

E
Eric Blake 已提交
785
const vshCmdDef secretCmds[] = {
786 787 788 789 790 791 792 793 794 795 796 797
    {.name = "secret-define",
     .handler = cmdSecretDefine,
     .opts = opts_secret_define,
     .info = info_secret_define,
     .flags = 0
    },
    {.name = "secret-dumpxml",
     .handler = cmdSecretDumpXML,
     .opts = opts_secret_dumpxml,
     .info = info_secret_dumpxml,
     .flags = 0
    },
798 799 800 801 802 803
    {.name = "secret-event",
     .handler = cmdSecretEvent,
     .opts = opts_secret_event,
     .info = info_secret_event,
     .flags = 0
    },
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
    {.name = "secret-get-value",
     .handler = cmdSecretGetValue,
     .opts = opts_secret_get_value,
     .info = info_secret_get_value,
     .flags = 0
    },
    {.name = "secret-list",
     .handler = cmdSecretList,
     .opts = opts_secret_list,
     .info = info_secret_list,
     .flags = 0
    },
    {.name = "secret-set-value",
     .handler = cmdSecretSetValue,
     .opts = opts_secret_set_value,
     .info = info_secret_set_value,
     .flags = 0
    },
    {.name = "secret-undefine",
     .handler = cmdSecretUndefine,
     .opts = opts_secret_undefine,
     .info = info_secret_undefine,
     .flags = 0
    },
    {.name = NULL}
829
};