提交 5093b047 编写于 作者: E Eric Blake

virsh: common code for parsing --seconds

Several virsh commands ask for a --timeout parameter in
seconds, then use it to control interfaces that operate on
millisecond limits; I also plan on adding a 'virsh event'
command that also does this.  Factor this into a common
function.

* tools/virsh.h (vshCommandOptTimeoutToMs): New prototype.
* tools/virsh.c (vshCommandOptTimeoutToMs): New function.
* tools/virsh-domain.c (cmdBlockCommit, cmdBlockCopy)
(cmdBlockPull, cmdMigrate): Use it.
(vshWatchJob): Adjust timeout scale.
Signed-off-by: NEric Blake <eblake@redhat.com>
上级 84a6209d
/* /*
* virsh-domain.c: Commands to manage domain * virsh-domain.c: Commands to manage domain
* *
* Copyright (C) 2005, 2007-2013 Red Hat, Inc. * Copyright (C) 2005, 2007-2014 Red Hat, Inc.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
...@@ -1629,21 +1629,10 @@ cmdBlockCommit(vshControl *ctl, const vshCmd *cmd) ...@@ -1629,21 +1629,10 @@ cmdBlockCommit(vshControl *ctl, const vshCmd *cmd)
const char *path = NULL; const char *path = NULL;
bool quit = false; bool quit = false;
int abort_flags = 0; int abort_flags = 0;
int rv;
if (blocking) { if (blocking) {
if ((rv = vshCommandOptInt(cmd, "timeout", &timeout)) < 0 || if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0)
(rv > 0 && timeout < 1)) {
vshError(ctl, "%s", _("invalid timeout"));
return false; return false;
} else if (rv > 0) {
/* Ensure that we can multiply by 1000 without overflowing. */
if (timeout > INT_MAX / 1000) {
vshError(ctl, "%s", _("timeout is too big"));
return false;
}
timeout *= 1000;
}
if (vshCommandOptStringReq(ctl, cmd, "path", &path) < 0) if (vshCommandOptStringReq(ctl, cmd, "path", &path) < 0)
return false; return false;
if (vshCommandOptBool(cmd, "async")) if (vshCommandOptBool(cmd, "async"))
...@@ -1819,25 +1808,14 @@ cmdBlockCopy(vshControl *ctl, const vshCmd *cmd) ...@@ -1819,25 +1808,14 @@ cmdBlockCopy(vshControl *ctl, const vshCmd *cmd)
const char *path = NULL; const char *path = NULL;
bool quit = false; bool quit = false;
int abort_flags = 0; int abort_flags = 0;
int rv;
if (blocking) { if (blocking) {
if (pivot && finish) { if (pivot && finish) {
vshError(ctl, "%s", _("cannot mix --pivot and --finish")); vshError(ctl, "%s", _("cannot mix --pivot and --finish"));
return false; return false;
} }
if ((rv = vshCommandOptInt(cmd, "timeout", &timeout)) < 0 || if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0)
(rv > 0 && timeout < 1)) {
vshError(ctl, "%s", _("invalid timeout"));
return false; return false;
} else if (rv > 0) {
/* Ensure that we can multiply by 1000 without overflowing. */
if (timeout > INT_MAX / 1000) {
vshError(ctl, "%s", _("timeout is too big"));
return false;
}
timeout *= 1000;
}
if (vshCommandOptStringReq(ctl, cmd, "path", &path) < 0) if (vshCommandOptStringReq(ctl, cmd, "path", &path) < 0)
return false; return false;
if (vshCommandOptBool(cmd, "async")) if (vshCommandOptBool(cmd, "async"))
...@@ -2100,21 +2078,10 @@ cmdBlockPull(vshControl *ctl, const vshCmd *cmd) ...@@ -2100,21 +2078,10 @@ cmdBlockPull(vshControl *ctl, const vshCmd *cmd)
const char *path = NULL; const char *path = NULL;
bool quit = false; bool quit = false;
int abort_flags = 0; int abort_flags = 0;
int rv;
if (blocking) { if (blocking) {
if ((rv = vshCommandOptInt(cmd, "timeout", &timeout)) < 0 || if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0)
(rv > 0 && timeout < 1)) {
vshError(ctl, "%s", _("invalid timeout"));
return false; return false;
} else if (rv > 0) {
/* Ensure that we can multiply by 1000 without overflowing. */
if (timeout > INT_MAX / 1000) {
vshError(ctl, "%s", _("timeout is too big"));
return false;
}
timeout *= 1000;
}
if (vshCommandOptStringReq(ctl, cmd, "path", &path) < 0) if (vshCommandOptStringReq(ctl, cmd, "path", &path) < 0)
return false; return false;
if (vshCommandOptBool(cmd, "async")) if (vshCommandOptBool(cmd, "async"))
...@@ -3605,7 +3572,7 @@ vshWatchJob(vshControl *ctl, ...@@ -3605,7 +3572,7 @@ vshWatchJob(vshControl *ctl,
virDomainPtr dom, virDomainPtr dom,
bool verbose, bool verbose,
int pipe_fd, int pipe_fd,
int timeout, int timeout_ms,
jobWatchTimeoutFunc timeout_func, jobWatchTimeoutFunc timeout_func,
void *opaque, void *opaque,
const char *label) const char *label)
...@@ -3675,14 +3642,14 @@ vshWatchJob(vshControl *ctl, ...@@ -3675,14 +3642,14 @@ vshWatchJob(vshControl *ctl,
} }
GETTIMEOFDAY(&curr); GETTIMEOFDAY(&curr);
if (timeout && (((int)(curr.tv_sec - start.tv_sec) * 1000 + if (timeout_ms && (((int)(curr.tv_sec - start.tv_sec) * 1000 +
(int)(curr.tv_usec - start.tv_usec) / 1000) > (int)(curr.tv_usec - start.tv_usec) / 1000) >
timeout * 1000)) { timeout_ms)) {
/* suspend the domain when migration timeouts. */ /* suspend the domain when migration timeouts. */
vshDebug(ctl, VSH_ERR_DEBUG, "%s timeout", label); vshDebug(ctl, VSH_ERR_DEBUG, "%s timeout", label);
if (timeout_func) if (timeout_func)
(timeout_func)(ctl, dom, opaque); (timeout_func)(ctl, dom, opaque);
timeout = 0; timeout_ms = 0;
} }
if (verbose || !jobStarted) { if (verbose || !jobStarted) {
...@@ -8843,7 +8810,6 @@ cmdMigrate(vshControl *ctl, const vshCmd *cmd) ...@@ -8843,7 +8810,6 @@ cmdMigrate(vshControl *ctl, const vshCmd *cmd)
int timeout = 0; int timeout = 0;
bool live_flag = false; bool live_flag = false;
vshCtrlData data; vshCtrlData data;
int rv;
if (!(dom = vshCommandOptDomain(ctl, cmd, NULL))) if (!(dom = vshCommandOptDomain(ctl, cmd, NULL)))
return false; return false;
...@@ -8853,22 +8819,12 @@ cmdMigrate(vshControl *ctl, const vshCmd *cmd) ...@@ -8853,22 +8819,12 @@ cmdMigrate(vshControl *ctl, const vshCmd *cmd)
if (vshCommandOptBool(cmd, "live")) if (vshCommandOptBool(cmd, "live"))
live_flag = true; live_flag = true;
if ((rv = vshCommandOptInt(cmd, "timeout", &timeout)) < 0 || if (vshCommandOptTimeoutToMs(ctl, cmd, &timeout) < 0) {
(rv > 0 && timeout < 1)) { goto cleanup;
vshError(ctl, "%s", _("migrate: Invalid timeout")); } else if (timeout > 0 && !live_flag) {
vshError(ctl, "%s",
_("migrate: Unexpected timeout for offline migration"));
goto cleanup; goto cleanup;
} else if (rv > 0) {
if (! live_flag) {
vshError(ctl, "%s",
_("migrate: Unexpected timeout for offline migration"));
goto cleanup;
}
/* Ensure that we can multiply by 1000 without overflowing. */
if (timeout > INT_MAX / 1000) {
vshError(ctl, "%s", _("migrate: Timeout is too big"));
goto cleanup;
}
} }
if (pipe(p) < 0) if (pipe(p) < 0)
......
/* /*
* virsh.c: a shell to exercise the libvirt API * virsh.c: a shell to exercise the libvirt API
* *
* Copyright (C) 2005, 2007-2013 Red Hat, Inc. * Copyright (C) 2005, 2007-2014 Red Hat, Inc.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
...@@ -1721,6 +1721,30 @@ vshCmdHasOption(vshControl *ctl, const vshCmd *cmd, const char *optname) ...@@ -1721,6 +1721,30 @@ vshCmdHasOption(vshControl *ctl, const vshCmd *cmd, const char *optname)
return found; return found;
} }
/* Parse an optional --timeout parameter in seconds, but store the
* value of the timeout in milliseconds. Return -1 on error, 0 if
* no timeout was requested, and 1 if timeout was set. */
int
vshCommandOptTimeoutToMs(vshControl *ctl, const vshCmd *cmd, int *timeout)
{
int rv = vshCommandOptInt(cmd, "timeout", timeout);
if (rv < 0 || (rv > 0 && *timeout < 1)) {
vshError(ctl, "%s", _("invalid timeout"));
return -1;
}
if (rv > 0) {
/* Ensure that we can multiply by 1000 without overflowing. */
if (*timeout > INT_MAX / 1000) {
vshError(ctl, "%s", _("timeout is too big"));
return -1;
}
*timeout *= 1000;
}
return rv;
}
static bool static bool
vshConnectionUsability(vshControl *ctl, virConnectPtr conn) vshConnectionUsability(vshControl *ctl, virConnectPtr conn)
{ {
......
/* /*
* virsh.h: a shell to exercise the libvirt API * virsh.h: a shell to exercise the libvirt API
* *
* Copyright (C) 2005, 2007-2013 Red Hat, Inc. * Copyright (C) 2005, 2007-2014 Red Hat, Inc.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
...@@ -302,6 +302,8 @@ const vshCmdOpt *vshCommandOptArgv(const vshCmd *cmd, ...@@ -302,6 +302,8 @@ const vshCmdOpt *vshCommandOptArgv(const vshCmd *cmd,
const vshCmdOpt *opt); const vshCmdOpt *opt);
bool vshCmdHasOption(vshControl *ctl, const vshCmd *cmd, const char *optname); bool vshCmdHasOption(vshControl *ctl, const vshCmd *cmd, const char *optname);
int vshCommandOptTimeoutToMs(vshControl *ctl, const vshCmd *cmd, int *timeout);
/* Filter flags for various vshCommandOpt*By() functions */ /* Filter flags for various vshCommandOpt*By() functions */
typedef enum { typedef enum {
VSH_BYID = (1 << 1), VSH_BYID = (1 << 1),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册