virsh.c 26.4 KB
Newer Older
1
/*
2
 * virsh.c: a shell to exercise the libvirt API
3
 *
4
 * Copyright (C) 2005, 2007-2015 Red Hat, Inc.
5
 *
O
Osier Yang 已提交
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
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20
 */

21
#include <config.h>
E
Eric Blake 已提交
22
#include "virsh.h"
23

K
Karel Zak 已提交
24
#include <stdarg.h>
25
#include <unistd.h>
K
Karel Zak 已提交
26
#include <getopt.h>
K
Karel Zak 已提交
27
#include <sys/time.h>
28
#include <fcntl.h>
29
#include <time.h>
30
#include <sys/stat.h>
31
#include <inttypes.h>
32
#include <signal.h>
K
Karel Zak 已提交
33

34
#if WITH_READLINE
35 36
# include <readline/readline.h>
# include <readline/history.h>
37
#endif
K
Karel Zak 已提交
38

39
#include "internal.h"
40
#include "virerror.h"
41
#include "virbuffer.h"
42
#include "viralloc.h"
E
Eric Blake 已提交
43
#include "virfile.h"
44
#include "virthread.h"
45
#include "vircommand.h"
46
#include "virtypedparam.h"
47
#include "virstring.h"
48
#include "virgettext.h"
K
Karel Zak 已提交
49

50
#include "virsh-checkpoint.h"
51
#include "virsh-console.h"
E
Eric Blake 已提交
52
#include "virsh-domain.h"
53
#include "virsh-domain-monitor.h"
E
Eric Blake 已提交
54
#include "virsh-host.h"
E
Eric Blake 已提交
55
#include "virsh-interface.h"
E
Eric Blake 已提交
56
#include "virsh-network.h"
E
Eric Blake 已提交
57
#include "virsh-nodedev.h"
E
Eric Blake 已提交
58
#include "virsh-nwfilter.h"
E
Eric Blake 已提交
59
#include "virsh-pool.h"
E
Eric Blake 已提交
60
#include "virsh-secret.h"
E
Eric Blake 已提交
61
#include "virsh-snapshot.h"
E
Eric Blake 已提交
62
#include "virsh-volume.h"
E
Eric Blake 已提交
63

64 65 66 67 68
/* Gnulib doesn't guarantee SA_SIGINFO support.  */
#ifndef SA_SIGINFO
# define SA_SIGINFO 0
#endif

K
Karel Zak 已提交
69 70
static char *progname;

71
static const vshCmdGrp cmdGroups[];
72
static const vshClientHooks hooks;
J
John Levon 已提交
73

74 75 76
/*
 * Detection of disconnections and automatic reconnection support
 */
77
static int disconnected; /* we may have been disconnected */
78 79

/*
80
 * virshCatchDisconnect:
81
 *
82 83
 * We get here when the connection was closed.  We can't do much in the
 * handler, just save the fact it was raised.
84
 */
L
Laine Stump 已提交
85
static void
86
virshCatchDisconnect(virConnectPtr conn,
87
                     int reason,
88
                     void *opaque)
89
{
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    if (reason != VIR_CONNECT_CLOSE_REASON_CLIENT) {
        vshControl *ctl = opaque;
        const char *str = "unknown reason";
        virErrorPtr error;
        char *uri;

        error = virSaveLastError();
        uri = virConnectGetURI(conn);

        switch ((virConnectCloseReason) reason) {
        case VIR_CONNECT_CLOSE_REASON_ERROR:
            str = N_("Disconnected from %s due to I/O error");
            break;
        case VIR_CONNECT_CLOSE_REASON_EOF:
            str = N_("Disconnected from %s due to end of file");
            break;
        case VIR_CONNECT_CLOSE_REASON_KEEPALIVE:
            str = N_("Disconnected from %s due to keepalive timeout");
            break;
J
John Ferlan 已提交
109
        /* coverity[dead_error_condition] */
110 111 112 113 114
        case VIR_CONNECT_CLOSE_REASON_CLIENT:
        case VIR_CONNECT_CLOSE_REASON_LAST:
            break;
        }
        vshError(ctl, _(str), NULLSTR(uri));
115
        VIR_FREE(uri);
116 117 118 119 120

        if (error) {
            virSetError(error);
            virFreeError(error);
        }
121
        disconnected++;
122
        vshEventDone(ctl);
123
    }
124 125
}

126 127 128
/* Main Function which should be used for connecting.
 * This function properly handles keepalive settings. */
virConnectPtr
129
virshConnect(vshControl *ctl, const char *uri, bool readonly)
130 131 132 133 134
{
    virConnectPtr c = NULL;
    int interval = 5; /* Default */
    int count = 6;    /* Default */
    bool keepalive_forced = false;
135 136
    virPolkitAgentPtr pkagent = NULL;
    int authfail = 0;
137
    bool agentCreated = false;
138 139 140 141 142 143 144 145 146 147

    if (ctl->keepalive_interval >= 0) {
        interval = ctl->keepalive_interval;
        keepalive_forced = true;
    }
    if (ctl->keepalive_count >= 0) {
        count = ctl->keepalive_count;
        keepalive_forced = true;
    }

148 149 150 151 152 153 154 155 156 157 158
    do {
        virErrorPtr err;

        if ((c = virConnectOpenAuth(uri, virConnectAuthPtrDefault,
                                    readonly ? VIR_CONNECT_RO : 0)))
            break;

        if (readonly)
            goto cleanup;

        err = virGetLastError();
159 160
        if (!agentCreated &&
            err && err->domain == VIR_FROM_POLKIT &&
161
            err->code == VIR_ERR_AUTH_UNAVAILABLE) {
162
            if (!pkagent && !(pkagent = virPolkitAgentCreate()))
163
                goto cleanup;
164
            agentCreated = true;
165 166 167 168 169 170 171 172 173 174 175 176
        } else if (err && err->domain == VIR_FROM_POLKIT &&
                   err->code == VIR_ERR_AUTH_FAILED) {
            authfail++;
        } else {
            goto cleanup;
        }
        virResetLastError();
        /* Failure to authenticate 5 times should be enough.
         * No sense prolonging the agony.
         */
    } while (authfail < 5);

177
    if (!c)
178
        goto cleanup;
179 180 181 182 183 184 185 186

    if (interval > 0 &&
        virConnectSetKeepAlive(c, interval, count) != 0) {
        if (keepalive_forced) {
            vshError(ctl, "%s",
                     _("Cannot setup keepalive on connection "
                       "as requested, disconnecting"));
            virConnectClose(c);
187 188
            c = NULL;
            goto cleanup;
189 190 191 192 193
        }
        vshDebug(ctl, VSH_ERR_INFO, "%s",
                 _("Failed to setup keepalive on connection\n"));
    }

194 195
 cleanup:
    virPolkitAgentDestroy(pkagent);
196 197 198
    return c;
}

199
/*
200
 * virshReconnect:
201
 *
L
Laine Stump 已提交
202
 * Reconnect after a disconnect from libvirtd
203 204
 *
 */
205
static int
M
Martin Kletzander 已提交
206
virshReconnect(vshControl *ctl, const char *name, bool readonly, bool force)
207 208
{
    bool connected = false;
209
    virshControlPtr priv = ctl->privData;
210 211 212 213 214 215 216

    /* If the flag was not specified, then it depends on whether we are
     * reconnecting to the current URI (in which case we want to keep the
     * readonly flag as it was) or to a specified URI in which case it
     * should stay false */
    if (!readonly && !name)
        readonly = priv->readonly;
217

218
    if (priv->conn) {
219
        int ret;
220
        connected = true;
221

222 223
        virConnectUnregisterCloseCallback(priv->conn, virshCatchDisconnect);
        ret = virConnectClose(priv->conn);
224 225 226 227 228
        if (ret < 0)
            vshError(ctl, "%s", _("Failed to disconnect from the hypervisor"));
        else if (ret > 0)
            vshError(ctl, "%s", _("One or more references were leaked after "
                                  "disconnect from the hypervisor"));
229
    }
230

231
    priv->conn = virshConnect(ctl, name ? name : ctl->connname, readonly);
232

233
    if (!priv->conn) {
234 235 236 237
        if (disconnected)
            vshError(ctl, "%s", _("Failed to reconnect to the hypervisor"));
        else
            vshError(ctl, "%s", _("failed to connect to the hypervisor"));
238
        return -1;
239
    } else {
M
Martin Kletzander 已提交
240 241 242 243
        if (name) {
            VIR_FREE(ctl->connname);
            ctl->connname = vshStrdup(ctl, name);
        }
244 245 246

        priv->readonly = readonly;

247
        if (virConnectRegisterCloseCallback(priv->conn, virshCatchDisconnect,
248
                                            ctl, NULL) < 0)
249
            vshError(ctl, "%s", _("Unable to register disconnect callback"));
M
Martin Kletzander 已提交
250
        if (connected && !force)
251 252
            vshError(ctl, "%s", _("Reconnected to the hypervisor"));
    }
253
    disconnected = 0;
254 255 256
    priv->useGetInfo = false;
    priv->useSnapshotOld = false;
    priv->blockJobNoBytes = false;
257
    return 0;
258
}
259

260 261 262
/* ---------------
 * Command Connect
 * ---------------
263 264 265 266
 */

static const vshCmdOptDef opts_connect[] = {
    {.name = "name",
267
     .type = VSH_OT_STRING,
268 269 270 271 272 273 274 275 276 277
     .flags = VSH_OFLAG_EMPTY_OK,
     .help = N_("hypervisor connection URI")
    },
    {.name = "readonly",
     .type = VSH_OT_BOOL,
     .help = N_("read-only connection")
    },
    {.name = NULL}
};

278
static const vshCmdInfo info_connect[] = {
279
    {.name = "help",
280
     .data = N_("(re)connect to hypervisor")
281 282
    },
    {.name = "desc",
283 284
     .data = N_("Connect to local hypervisor. This is built-in "
                "command after shell start up.")
285 286
    },
    {.name = NULL}
P
Paolo Bonzini 已提交
287 288
};

E
Eric Blake 已提交
289
static bool
290
cmdConnect(vshControl *ctl, const vshCmd *cmd)
P
Paolo Bonzini 已提交
291
{
292 293
    bool ro = vshCommandOptBool(cmd, "readonly");
    const char *name = NULL;
294 295 296 297

    if (vshCommandOptStringReq(ctl, cmd, "name", &name) < 0)
        return false;

298 299
    if (virshReconnect(ctl, name, ro, true) < 0)
        return false;
300

301
    return true;
302 303
}

304 305 306 307
/* ---------------
 * Utils for work with runtime commands data
 * ---------------
 */
308

309 310
static bool
virshConnectionUsability(vshControl *ctl, virConnectPtr conn)
311
{
312 313 314 315
    if (!conn ||
        virConnectIsAlive(conn) == 0) {
        vshError(ctl, "%s", _("no valid connection"));
        return false;
316 317
    }

318
    /* The connection is considered dead only if
319
     * virConnectIsAlive() successfully says so.
320 321
     */
    vshResetLibvirtError();
322

323
    return true;
324 325
}

326 327
static void *
virshConnectionHandler(vshControl *ctl)
328
{
329
    virshControlPtr priv = ctl->privData;
330

331 332 333
    if ((!priv->conn || disconnected) &&
        virshReconnect(ctl, NULL, false, false) < 0)
        return NULL;
334

335 336
    if (virshConnectionUsability(ctl, priv->conn))
        return priv->conn;
337

338
    return NULL;
339 340
}

J
Ján Tomko 已提交
341

M
Martin Kletzander 已提交
342 343 344 345
/*
 * Initialize connection.
 */
static bool
346
virshInit(vshControl *ctl)
M
Martin Kletzander 已提交
347
{
348 349
    virshControlPtr priv = ctl->privData;

M
Martin Kletzander 已提交
350
    /* Since we have the commandline arguments parsed, we need to
E
Erik Skultety 已提交
351 352 353
     * reload our initial settings to make debugging and readline
     * work properly */
    vshInitReload(ctl);
M
Martin Kletzander 已提交
354

355
    if (priv->conn)
M
Martin Kletzander 已提交
356
        return false;
357

358
    /* set up the library error handler */
359
    virSetErrorFunc(NULL, vshErrorHandler);
360

361 362
    if (virEventRegisterDefaultImpl() < 0) {
        vshReportError(ctl);
E
Eric Blake 已提交
363
        return false;
364
    }
365

366 367
    if (virThreadCreate(&ctl->eventLoop, true, vshEventLoop, ctl) < 0) {
        vshReportError(ctl);
J
Jiri Denemark 已提交
368
        return false;
369
    }
J
Jiri Denemark 已提交
370 371
    ctl->eventLoopStarted = true;

372
    if ((ctl->eventTimerId = virEventAddTimeout(-1, vshEventTimeout, ctl,
373 374
                                                NULL)) < 0) {
        vshReportError(ctl);
375
        return false;
376
    }
377

378
    if (ctl->connname) {
379 380 381 382 383 384
        /* Connecting to a named connection must succeed, but we delay
         * connecting to the default connection until we need it
         * (since the first command might be 'connect' which allows a
         * non-default connection, or might be 'help' which needs no
         * connection).
         */
385
        if (virshReconnect(ctl, NULL, false, false) < 0) {
E
Eric Blake 已提交
386
            vshReportError(ctl);
387 388
            return false;
        }
389
    }
K
Karel Zak 已提交
390

E
Eric Blake 已提交
391
    return true;
K
Karel Zak 已提交
392 393
}

394
static void
395
virshDeinitTimer(int timer ATTRIBUTE_UNUSED, void *opaque ATTRIBUTE_UNUSED)
396 397 398 399
{
    /* nothing to be done here */
}

K
Karel Zak 已提交
400
/*
J
Jim Meyering 已提交
401
 * Deinitialize virsh
K
Karel Zak 已提交
402
 */
E
Eric Blake 已提交
403
static bool
404
virshDeinit(vshControl *ctl)
405
{
406 407 408
    virshControlPtr priv = ctl->privData;

    vshDeinit(ctl);
409
    VIR_FREE(ctl->connname);
410
    if (priv->conn) {
411
        int ret;
412 413
        virConnectUnregisterCloseCallback(priv->conn, virshCatchDisconnect);
        ret = virConnectClose(priv->conn);
414 415 416 417 418
        if (ret < 0)
            vshError(ctl, "%s", _("Failed to disconnect from the hypervisor"));
        else if (ret > 0)
            vshError(ctl, "%s", _("One or more references were leaked after "
                                  "disconnect from the hypervisor"));
K
Karel Zak 已提交
419
    }
D
Daniel P. Berrange 已提交
420 421
    virResetLastError();

J
Jiri Denemark 已提交
422
    if (ctl->eventLoopStarted) {
423 424 425 426
        int timer;

        virMutexLock(&ctl->lock);
        ctl->quit = true;
J
Jiri Denemark 已提交
427
        /* HACK: Add a dummy timeout to break event loop */
428
        timer = virEventAddTimeout(0, virshDeinitTimer, NULL, NULL);
429 430 431 432
        virMutexUnlock(&ctl->lock);

        virThreadJoin(&ctl->eventLoop);

J
Jiri Denemark 已提交
433 434 435
        if (timer != -1)
            virEventRemoveTimeout(timer);

436 437 438
        if (ctl->eventTimerId != -1)
            virEventRemoveTimeout(ctl->eventTimerId);

J
Jiri Denemark 已提交
439 440 441
        ctl->eventLoopStarted = false;
    }

442 443
    virMutexDestroy(&ctl->lock);

E
Eric Blake 已提交
444
    return true;
K
Karel Zak 已提交
445
}
446

K
Karel Zak 已提交
447 448 449
/*
 * Print usage
 */
E
Eric Blake 已提交
450
static void
451
virshUsage(void)
452
{
453
    const vshCmdGrp *grp;
454
    const vshCmdDef *cmd;
455

L
Lai Jiangshan 已提交
456 457
    fprintf(stdout, _("\n%s [options]... [<command_string>]"
                      "\n%s [options]... <command> [args...]\n\n"
458
                      "  options:\n"
459 460
                      "    -c | --connect=URI      hypervisor connection URI\n"
                      "    -d | --debug=NUM        debug level [0-4]\n"
461
                      "    -e | --escape <char>    set escape sequence for console\n"
462
                      "    -h | --help             this help\n"
463 464 465 466
                      "    -k | --keepalive-interval=NUM\n"
                      "                            keepalive interval in seconds, 0 for disable\n"
                      "    -K | --keepalive-count=NUM\n"
                      "                            number of possible missed keepalive messages\n"
467
                      "    -l | --log=FILE         output logging to file\n"
468
                      "    -q | --quiet            quiet mode\n"
469
                      "    -r | --readonly         connect readonly\n"
470
                      "    -t | --timing           print timing information\n"
471 472 473
                      "    -v                      short version\n"
                      "    -V                      long version\n"
                      "         --version[=TYPE]   version, TYPE is short or long (default short)\n"
474 475
                      "  commands (non interactive mode):\n\n"), progname,
            progname);
476

477
    for (grp = cmdGroups; grp->name; grp++) {
E
Eric Blake 已提交
478 479 480 481 482
        fprintf(stdout, _(" %s (help keyword '%s')\n"),
                grp->name, grp->keyword);
        for (cmd = grp->commands; cmd->name; cmd++) {
            if (cmd->flags & VSH_CMD_FLAG_ALIAS)
                continue;
483
            fprintf(stdout,
E
Eric Blake 已提交
484 485 486
                    "    %-30s %s\n", cmd->name,
                    _(vshCmddefGetInfo(cmd, "help")));
        }
487 488 489 490 491
        fprintf(stdout, "\n");
    }

    fprintf(stdout, "%s",
            _("\n  (specify help <group> for details about the commands in the group)\n"));
492 493 494
    fprintf(stdout, "%s",
            _("\n  (specify help <command> for details about the command)\n\n"));
    return;
K
Karel Zak 已提交
495 496
}

497 498 499 500
/*
 * Show version and options compiled in
 */
static void
501
virshShowVersion(vshControl *ctl ATTRIBUTE_UNUSED)
502 503 504
{
    /* FIXME - list a copyright blurb, as in GNU programs?  */
    vshPrint(ctl, _("Virsh command line tool of libvirt %s\n"), VERSION);
505
    vshPrint(ctl, _("See web site at %s\n\n"), "https://libvirt.org/");
506

L
Laine Stump 已提交
507 508
    vshPrint(ctl, "%s", _("Compiled with support for:\n"));
    vshPrint(ctl, "%s", _(" Hypervisors:"));
509
#ifdef WITH_QEMU
510
    vshPrint(ctl, " QEMU/KVM");
511
#endif
D
Doug Goldstein 已提交
512 513 514 515 516 517
#ifdef WITH_LXC
    vshPrint(ctl, " LXC");
#endif
#ifdef WITH_LIBXL
    vshPrint(ctl, " LibXL");
#endif
518 519 520
#ifdef WITH_OPENVZ
    vshPrint(ctl, " OpenVZ");
#endif
521 522 523
#ifdef WITH_VZ
    vshPrint(ctl, " Virtuozzo");
#endif
D
Doug Goldstein 已提交
524
#ifdef WITH_VMWARE
J
Ján Tomko 已提交
525
    vshPrint(ctl, " VMware");
526
#endif
D
Doug Goldstein 已提交
527 528
#ifdef WITH_PHYP
    vshPrint(ctl, " PHYP");
529
#endif
D
Doug Goldstein 已提交
530 531
#ifdef WITH_VBOX
    vshPrint(ctl, " VirtualBox");
532 533 534 535
#endif
#ifdef WITH_ESX
    vshPrint(ctl, " ESX");
#endif
D
Doug Goldstein 已提交
536 537
#ifdef WITH_HYPERV
    vshPrint(ctl, " Hyper-V");
538
#endif
539 540 541
#ifdef WITH_BHYVE
    vshPrint(ctl, " Bhyve");
#endif
542 543 544 545 546
#ifdef WITH_TEST
    vshPrint(ctl, " Test");
#endif
    vshPrint(ctl, "\n");

L
Laine Stump 已提交
547
    vshPrint(ctl, "%s", _(" Networking:"));
548 549 550 551 552 553 554 555 556
#ifdef WITH_REMOTE
    vshPrint(ctl, " Remote");
#endif
#ifdef WITH_NETWORK
    vshPrint(ctl, " Network");
#endif
#ifdef WITH_BRIDGE
    vshPrint(ctl, " Bridging");
#endif
557
#if defined(WITH_INTERFACE)
D
Doug Goldstein 已提交
558
    vshPrint(ctl, " Interface");
559 560
# if defined(WITH_NETCF)
    vshPrint(ctl, " netcf");
561
# elif defined(WITH_UDEV)
562
    vshPrint(ctl, " udev");
563
# endif
564 565 566 567 568 569 570 571 572
#endif
#ifdef WITH_NWFILTER
    vshPrint(ctl, " Nwfilter");
#endif
#ifdef WITH_VIRTUALPORT
    vshPrint(ctl, " VirtualPort");
#endif
    vshPrint(ctl, "\n");

L
Laine Stump 已提交
573
    vshPrint(ctl, "%s", _(" Storage:"));
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
#ifdef WITH_STORAGE_DIR
    vshPrint(ctl, " Dir");
#endif
#ifdef WITH_STORAGE_DISK
    vshPrint(ctl, " Disk");
#endif
#ifdef WITH_STORAGE_FS
    vshPrint(ctl, " Filesystem");
#endif
#ifdef WITH_STORAGE_SCSI
    vshPrint(ctl, " SCSI");
#endif
#ifdef WITH_STORAGE_MPATH
    vshPrint(ctl, " Multipath");
#endif
#ifdef WITH_STORAGE_ISCSI
    vshPrint(ctl, " iSCSI");
#endif
#ifdef WITH_STORAGE_LVM
    vshPrint(ctl, " LVM");
594 595 596
#endif
#ifdef WITH_STORAGE_RBD
    vshPrint(ctl, " RBD");
597 598 599
#endif
#ifdef WITH_STORAGE_SHEEPDOG
    vshPrint(ctl, " Sheepdog");
600 601 602
#endif
#ifdef WITH_STORAGE_GLUSTER
    vshPrint(ctl, " Gluster");
603 604 605
#endif
#ifdef WITH_STORAGE_ZFS
    vshPrint(ctl, " ZFS");
606 607 608
#endif
#ifdef WITH_STORAGE_VSTORAGE
    vshPrint(ctl, "Virtuozzo Storage");
609 610 611
#endif
    vshPrint(ctl, "\n");

612
    vshPrint(ctl, "%s", _(" Miscellaneous:"));
613 614 615
#ifdef WITH_LIBVIRTD
    vshPrint(ctl, " Daemon");
#endif
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
#ifdef WITH_NODE_DEVICES
    vshPrint(ctl, " Nodedev");
#endif
#ifdef WITH_SECDRIVER_APPARMOR
    vshPrint(ctl, " AppArmor");
#endif
#ifdef WITH_SECDRIVER_SELINUX
    vshPrint(ctl, " SELinux");
#endif
#ifdef WITH_SECRETS
    vshPrint(ctl, " Secrets");
#endif
#ifdef ENABLE_DEBUG
    vshPrint(ctl, " Debug");
#endif
#ifdef WITH_DTRACE_PROBES
    vshPrint(ctl, " DTrace");
#endif
634
#if WITH_READLINE
635 636 637 638 639 640
    vshPrint(ctl, " Readline");
#endif
    vshPrint(ctl, "\n");
}

static bool
641
virshAllowedEscapeChar(char c)
642 643 644 645 646 647 648 649 650 651 652 653
{
    /* Allowed escape characters:
     * a-z A-Z @ [ \ ] ^ _
     */
    return ('a' <= c && c <= 'z') ||
        ('@' <= c && c <= '_');
}

/*
 * argv[]:  virsh [options] [command]
 *
 */
E
Eric Blake 已提交
654
static bool
655
virshParseArgv(vshControl *ctl, int argc, char **argv)
656
{
657
    int arg, len, debug, keepalive;
658
    size_t i;
659
    int longindex = -1;
660
    virshControlPtr priv = ctl->privData;
661
    struct option opt[] = {
662
        {"connect", required_argument, NULL, 'c'},
663
        {"debug", required_argument, NULL, 'd'},
664
        {"escape", required_argument, NULL, 'e'},
665
        {"help", no_argument, NULL, 'h'},
666 667
        {"keepalive-interval", required_argument, NULL, 'k'},
        {"keepalive-count", required_argument, NULL, 'K'},
668
        {"log", required_argument, NULL, 'l'},
669
        {"quiet", no_argument, NULL, 'q'},
670
        {"readonly", no_argument, NULL, 'r'},
671 672 673 674 675 676 677 678
        {"timing", no_argument, NULL, 't'},
        {"version", optional_argument, NULL, 'v'},
        {NULL, 0, NULL, 0}
    };

    /* Standard (non-command) options. The leading + ensures that no
     * argument reordering takes place, so that command options are
     * not confused with top-level virsh options. */
679
    while ((arg = getopt_long(argc, argv, "+:c:d:e:hk:K:l:qrtvV", opt, &longindex)) != -1) {
680
        switch (arg) {
681
        case 'c':
682 683
            VIR_FREE(ctl->connname);
            ctl->connname = vshStrdup(ctl, optarg);
684
            break;
685
        case 'd':
686
            if (virStrToLong_i(optarg, NULL, 10, &debug) < 0) {
687 688
                vshError(ctl, _("option %s takes a numeric argument"),
                         longindex == -1 ? "-d" : "--debug");
689 690
                exit(EXIT_FAILURE);
            }
691 692 693 694 695
            if (debug < VSH_ERR_DEBUG || debug > VSH_ERR_ERROR)
                vshError(ctl, _("ignoring debug level %d out of range [%d-%d]"),
                         debug, VSH_ERR_DEBUG, VSH_ERR_ERROR);
            else
                ctl->debug = debug;
696
            break;
697 698 699 700
        case 'e':
            len = strlen(optarg);

            if ((len == 2 && *optarg == '^' &&
701
                 virshAllowedEscapeChar(optarg[1])) ||
702
                (len == 1 && *optarg != '^')) {
703
                priv->escapeChar = optarg;
704 705 706 707 708 709
            } else {
                vshError(ctl, _("Invalid string '%s' for escape sequence"),
                         optarg);
                exit(EXIT_FAILURE);
            }
            break;
710
        case 'h':
711
            virshUsage();
712 713
            exit(EXIT_SUCCESS);
            break;
714
        case 'k':
E
Erik Skultety 已提交
715 716 717 718 719 720 721 722 723 724
            if (virStrToLong_i(optarg, NULL, 0, &keepalive) < 0) {
                vshError(ctl,
                         _("Invalid value for option %s"),
                         longindex == -1 ? "-k" : "--keepalive-interval");
                exit(EXIT_FAILURE);
            }

            if (keepalive < 0) {
                vshError(ctl,
                         _("option %s requires a positive integer argument"),
725 726 727 728 729 730
                         longindex == -1 ? "-k" : "--keepalive-interval");
                exit(EXIT_FAILURE);
            }
            ctl->keepalive_interval = keepalive;
            break;
        case 'K':
E
Erik Skultety 已提交
731 732 733 734 735 736 737 738 739 740
            if (virStrToLong_i(optarg, NULL, 0, &keepalive) < 0) {
                vshError(ctl,
                         _("Invalid value for option %s"),
                         longindex == -1 ? "-K" : "--keepalive-count");
                exit(EXIT_FAILURE);
            }

            if (keepalive < 0) {
                vshError(ctl,
                         _("option %s requires a positive integer argument"),
741 742 743 744 745
                         longindex == -1 ? "-K" : "--keepalive-count");
                exit(EXIT_FAILURE);
            }
            ctl->keepalive_count = keepalive;
            break;
746 747 748 749 750
        case 'l':
            vshCloseLogFile(ctl);
            ctl->logfile = vshStrdup(ctl, optarg);
            vshOpenLogFile(ctl);
            break;
751 752 753 754 755 756
        case 'q':
            ctl->quiet = true;
            break;
        case 't':
            ctl->timing = true;
            break;
757
        case 'r':
758
            priv->readonly = true;
759 760 761 762 763 764
            break;
        case 'v':
            if (STRNEQ_NULLABLE(optarg, "long")) {
                puts(VERSION);
                exit(EXIT_SUCCESS);
            }
M
Marc Hartmayer 已提交
765
            ATTRIBUTE_FALLTHROUGH;
766
        case 'V':
767
            virshShowVersion(ctl);
768
            exit(EXIT_SUCCESS);
769
        case ':':
770
            for (i = 0; opt[i].name != NULL; i++) {
771 772
                if (opt[i].val == optopt)
                    break;
773
            }
774 775 776 777 778 779
            if (opt[i].name)
                vshError(ctl, _("option '-%c'/'--%s' requires an argument"),
                         optopt, opt[i].name);
            else
                vshError(ctl, _("option '-%c' requires an argument"), optopt);
            exit(EXIT_FAILURE);
780
        case '?':
781 782 783 784
            if (optopt)
                vshError(ctl, _("unsupported option '-%c'. See --help."), optopt);
            else
                vshError(ctl, _("unsupported option '%s'. See --help."), argv[optind - 1]);
785
            exit(EXIT_FAILURE);
786
        default:
787
            vshError(ctl, _("unknown option"));
788 789
            exit(EXIT_FAILURE);
        }
790
        longindex = -1;
791 792
    }

793 794 795
    if (argc == optind) {
        ctl->imode = true;
    } else {
796 797 798 799
        /* parse command */
        ctl->imode = false;
        if (argc - optind == 1) {
            vshDebug(ctl, VSH_ERR_INFO, "commands: \"%s\"\n", argv[optind]);
800
            return vshCommandStringParse(ctl, argv[optind], NULL);
801 802 803 804 805 806 807 808
        } else {
            return vshCommandArgvParse(ctl, argc - optind, argv + optind);
        }
    }
    return true;
}

static const vshCmdDef virshCmds[] = {
809 810 811 812 813 814
    VSH_CMD_CD,
    VSH_CMD_ECHO,
    VSH_CMD_EXIT,
    VSH_CMD_HELP,
    VSH_CMD_PWD,
    VSH_CMD_QUIT,
815
    VSH_CMD_SELF_TEST,
816
    VSH_CMD_COMPLETE,
817 818 819 820 821
    {.name = "connect",
     .handler = cmdConnect,
     .opts = opts_connect,
     .info = info_connect,
     .flags = VSH_CMD_FLAG_NOCONNECT
822 823
    },
    {.name = NULL}
824
};
825

826
static const vshCmdGrp cmdGroups[] = {
827 828 829
    {VIRSH_CMD_GRP_DOM_MANAGEMENT, "domain", domManagementCmds},
    {VIRSH_CMD_GRP_DOM_MONITORING, "monitor", domMonitoringCmds},
    {VIRSH_CMD_GRP_HOST_AND_HV, "host", hostAndHypervisorCmds},
830
    {VIRSH_CMD_GRP_CHECKPOINT, "checkpoint", checkpointCmds},
831 832 833 834 835 836 837 838 839
    {VIRSH_CMD_GRP_IFACE, "interface", ifaceCmds},
    {VIRSH_CMD_GRP_NWFILTER, "filter", nwfilterCmds},
    {VIRSH_CMD_GRP_NETWORK, "network", networkCmds},
    {VIRSH_CMD_GRP_NODEDEV, "nodedev", nodedevCmds},
    {VIRSH_CMD_GRP_SECRET, "secret", secretCmds},
    {VIRSH_CMD_GRP_SNAPSHOT, "snapshot", snapshotCmds},
    {VIRSH_CMD_GRP_STORAGE_POOL, "pool", storagePoolCmds},
    {VIRSH_CMD_GRP_STORAGE_VOL, "volume", storageVolCmds},
    {VIRSH_CMD_GRP_VIRSH, "virsh", virshCmds},
840 841
    {NULL, NULL, NULL}
};
K
Karel Zak 已提交
842

843 844 845 846
static const vshClientHooks hooks = {
    .connHandler = virshConnectionHandler
};

847 848 849 850
int
main(int argc, char **argv)
{
    vshControl _ctl, *ctl = &_ctl;
851
    virshControl virshCtl;
E
Eric Blake 已提交
852
    bool ret = true;
K
Karel Zak 已提交
853

854
    memset(ctl, 0, sizeof(vshControl));
855 856
    memset(&virshCtl, 0, sizeof(virshControl));
    ctl->name = "virsh";        /* hardcoded name of the binary */
857
    ctl->env_prefix = "VIRSH";
858
    ctl->log_fd = -1;           /* Initialize log file descriptor */
J
Jiri Denemark 已提交
859
    ctl->debug = VSH_DEBUG_DEFAULT;
860
    ctl->hooks = &hooks;
861 862 863 864 865

    /* In order to distinguish default from setting to 0 */
    ctl->keepalive_interval = -1;
    ctl->keepalive_count = -1;

866 867 868
    ctl->eventPipe[0] = -1;
    ctl->eventPipe[1] = -1;
    ctl->eventTimerId = -1;
869 870 871 872 873 874 875 876
    virshCtl.escapeChar = "^]";     /* Same default as telnet */
    ctl->privData = &virshCtl;

    if (!(progname = strrchr(argv[0], '/')))
        progname = argv[0];
    else
        progname++;
    ctl->progname = progname;
877

878
    if (virGettextInitialize() < 0)
E
Eric Blake 已提交
879
        return EXIT_FAILURE;
880

881 882 883
    if (isatty(STDIN_FILENO)) {
        ctl->istty = true;

884
#ifndef WIN32
885 886
        if (tcgetattr(STDIN_FILENO, &ctl->termattr) < 0)
            ctl->istty = false;
887
#endif
888 889
    }

890 891 892 893 894
    if (virMutexInit(&ctl->lock) < 0) {
        vshError(ctl, "%s", _("Failed to initialize mutex"));
        return EXIT_FAILURE;
    }

895 896 897 898 899
    if (virInitialize() < 0) {
        vshError(ctl, "%s", _("Failed to initialize libvirt"));
        return EXIT_FAILURE;
    }

900 901
    virFileActivateDirOverride(argv[0]);

902
    if (!vshInit(ctl, cmdGroups, NULL))
903
        exit(EXIT_FAILURE);
904

905 906 907
    if (!virshParseArgv(ctl, argc, argv) ||
        !virshInit(ctl)) {
        virshDeinit(ctl);
K
Karel Zak 已提交
908
        exit(EXIT_FAILURE);
D
Daniel P. Berrange 已提交
909
    }
910

911 912
    if (!ctl->connname)
        ctl->connname = vshStrdup(ctl,
913
                                  getenv("VIRSH_DEFAULT_CONNECT_URI"));
914

K
Karel Zak 已提交
915
    if (!ctl->imode) {
916
        ret = vshCommandRun(ctl, ctl->cmd);
917
    } else {
K
Karel Zak 已提交
918 919
        /* interactive mode */
        if (!ctl->quiet) {
K
Karel Zak 已提交
920
            vshPrint(ctl,
921
                     _("Welcome to %s, the virtualization interactive terminal.\n\n"),
922
                     progname);
J
Jim Meyering 已提交
923
            vshPrint(ctl, "%s",
924
                     _("Type:  'help' for help with commands\n"
925
                       "       'quit' to quit\n\n"));
K
Karel Zak 已提交
926
        }
927

K
Karel Zak 已提交
928
        do {
929 930
            const char *prompt = virshCtl.readonly ? VIRSH_PROMPT_RO
                : VIRSH_PROMPT_RW;
931
            ctl->cmdstr =
932
                vshReadline(ctl, prompt);
933 934
            if (ctl->cmdstr == NULL)
                break;          /* EOF */
K
Karel Zak 已提交
935
            if (*ctl->cmdstr) {
936
#if WITH_READLINE
K
Karel Zak 已提交
937
                add_history(ctl->cmdstr);
938
#endif
939
                if (vshCommandStringParse(ctl, ctl->cmdstr, NULL))
K
Karel Zak 已提交
940 941
                    vshCommandRun(ctl, ctl->cmd);
            }
942
            VIR_FREE(ctl->cmdstr);
943
        } while (ctl->imode);
K
Karel Zak 已提交
944

945 946
        if (ctl->cmdstr == NULL)
            fputc('\n', stdout);        /* line break after alone prompt */
K
Karel Zak 已提交
947
    }
948

949
    virshDeinit(ctl);
K
Karel Zak 已提交
950
    exit(ret ? EXIT_SUCCESS : EXIT_FAILURE);
951
}