mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-21 20:15:17 +00:00
virsh: declare more common functions
In preparation for splitting virsh-interface.c, I found these functions need to be declared in virsh.h, as well as one that belongs more properly in virsh-domain.h. Also, since we use the VSH_BY* flags in more than one function, I improved how they are used. * tools/virsh.h (vshNameSorter, vshCmdHasOption): Declare. (VSH_BYID): Turn into enum. (vshCommandOptDomainBy): Move... * tools/virsh-domain.h): ...here. * tools/virsh.c: (vshNameSorter): Export. (cmd_has_option): Rename... (vshCmdHasOption): ...and export. (vshCommandOptDomainBy): Move... * tools/virsh-domain.c (vshCommandOptDomainBy): ...here, adjust signature, and check flags. * tools/virsh-network.c (vshCommandOptNetworkBy): Update callers. * tools/virsh-nwfilter.c (vshCommandOptNWFilterBy): Likewise. * tools/virsh-secret.c (vshCommandOptSecret): Likewise. * tools/virsh-domain-monitor.c (includes): Likewise. * tools/virsh-host.c (includes): Likewise.
This commit is contained in:
parent
ae8e89fb12
commit
4c10b3c7da
@ -36,6 +36,7 @@
|
|||||||
#include "intprops.h"
|
#include "intprops.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "virmacaddr.h"
|
#include "virmacaddr.h"
|
||||||
|
#include "virsh-domain.h"
|
||||||
#include "xml.h"
|
#include "xml.h"
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
|
@ -58,6 +58,57 @@
|
|||||||
# define SA_SIGINFO 0
|
# define SA_SIGINFO 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
virDomainPtr
|
||||||
|
vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
|
||||||
|
const char **name, unsigned int flags)
|
||||||
|
{
|
||||||
|
virDomainPtr dom = NULL;
|
||||||
|
const char *n = NULL;
|
||||||
|
int id;
|
||||||
|
const char *optname = "domain";
|
||||||
|
virCheckFlags(VSH_BYID | VSH_BYUUID | VSH_BYNAME, NULL);
|
||||||
|
|
||||||
|
if (!vshCmdHasOption(ctl, cmd, optname))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (vshCommandOptString(cmd, optname, &n) <= 0)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
vshDebug(ctl, VSH_ERR_INFO, "%s: found option <%s>: %s\n",
|
||||||
|
cmd->def->name, optname, n);
|
||||||
|
|
||||||
|
if (name)
|
||||||
|
*name = n;
|
||||||
|
|
||||||
|
/* try it by ID */
|
||||||
|
if (flags & VSH_BYID) {
|
||||||
|
if (virStrToLong_i(n, NULL, 10, &id) == 0 && id >= 0) {
|
||||||
|
vshDebug(ctl, VSH_ERR_DEBUG,
|
||||||
|
"%s: <%s> seems like domain ID\n",
|
||||||
|
cmd->def->name, optname);
|
||||||
|
dom = virDomainLookupByID(ctl->conn, id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* try it by UUID */
|
||||||
|
if (!dom && (flags & VSH_BYUUID) &&
|
||||||
|
strlen(n) == VIR_UUID_STRING_BUFLEN-1) {
|
||||||
|
vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as domain UUID\n",
|
||||||
|
cmd->def->name, optname);
|
||||||
|
dom = virDomainLookupByUUIDString(ctl->conn, n);
|
||||||
|
}
|
||||||
|
/* try it by NAME */
|
||||||
|
if (!dom && (flags & VSH_BYNAME)) {
|
||||||
|
vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as domain NAME\n",
|
||||||
|
cmd->def->name, optname);
|
||||||
|
dom = virDomainLookupByName(ctl->conn, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!dom)
|
||||||
|
vshError(ctl, _("failed to get domain '%s'"), n);
|
||||||
|
|
||||||
|
return dom;
|
||||||
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
vshDomainVcpuStateToString(int state)
|
vshDomainVcpuStateToString(int state)
|
||||||
{
|
{
|
||||||
|
@ -28,6 +28,13 @@
|
|||||||
|
|
||||||
# include "virsh.h"
|
# include "virsh.h"
|
||||||
|
|
||||||
|
virDomainPtr vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
|
||||||
|
const char **name, unsigned int flags);
|
||||||
|
|
||||||
|
/* default is lookup by Id, Name and UUID */
|
||||||
|
# define vshCommandOptDomain(_ctl, _cmd, _name) \
|
||||||
|
vshCommandOptDomainBy(_ctl, _cmd, _name, VSH_BYID|VSH_BYUUID|VSH_BYNAME)
|
||||||
|
|
||||||
extern const vshCmdDef domManagementCmds[];
|
extern const vshCmdDef domManagementCmds[];
|
||||||
|
|
||||||
#endif /* VIRSH_DOMAIN_H */
|
#endif /* VIRSH_DOMAIN_H */
|
||||||
|
@ -35,6 +35,7 @@
|
|||||||
#include "buf.h"
|
#include "buf.h"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "virsh-domain.h"
|
||||||
#include "xml.h"
|
#include "xml.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -38,7 +38,7 @@ vshCommandOptInterfaceBy(vshControl *ctl, const vshCmd *cmd,
|
|||||||
|
|
||||||
if (!optname)
|
if (!optname)
|
||||||
optname = "interface";
|
optname = "interface";
|
||||||
if (!cmd_has_option(ctl, cmd, optname))
|
if (!vshCmdHasOption(ctl, cmd, optname))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (vshCommandOptString(cmd, optname, &n) <= 0)
|
if (vshCommandOptString(cmd, optname, &n) <= 0)
|
||||||
|
@ -35,7 +35,7 @@ vshCommandOptNetworkBy(vshControl *ctl, const vshCmd *cmd,
|
|||||||
virNetworkPtr network = NULL;
|
virNetworkPtr network = NULL;
|
||||||
const char *n = NULL;
|
const char *n = NULL;
|
||||||
const char *optname = "network";
|
const char *optname = "network";
|
||||||
if (!cmd_has_option(ctl, cmd, optname))
|
if (!vshCmdHasOption(ctl, cmd, optname))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (vshCommandOptString(cmd, optname, &n) <= 0)
|
if (vshCommandOptString(cmd, optname, &n) <= 0)
|
||||||
|
@ -35,7 +35,7 @@ vshCommandOptNWFilterBy(vshControl *ctl, const vshCmd *cmd,
|
|||||||
virNWFilterPtr nwfilter = NULL;
|
virNWFilterPtr nwfilter = NULL;
|
||||||
const char *n = NULL;
|
const char *n = NULL;
|
||||||
const char *optname = "nwfilter";
|
const char *optname = "nwfilter";
|
||||||
if (!cmd_has_option(ctl, cmd, optname))
|
if (!vshCmdHasOption(ctl, cmd, optname))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (vshCommandOptString(cmd, optname, &n) <= 0)
|
if (vshCommandOptString(cmd, optname, &n) <= 0)
|
||||||
|
@ -30,7 +30,7 @@ vshCommandOptSecret(vshControl *ctl, const vshCmd *cmd, const char **name)
|
|||||||
const char *n = NULL;
|
const char *n = NULL;
|
||||||
const char *optname = "secret";
|
const char *optname = "secret";
|
||||||
|
|
||||||
if (!cmd_has_option(ctl, cmd, optname))
|
if (!vshCmdHasOption(ctl, cmd, optname))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (vshCommandOptString(cmd, optname, &n) <= 0)
|
if (vshCommandOptString(cmd, optname, &n) <= 0)
|
||||||
|
@ -128,7 +128,7 @@ _vshStrdup(vshControl *ctl, const char *s, const char *filename, int line)
|
|||||||
/* Poison the raw allocating identifiers in favor of our vsh variants. */
|
/* Poison the raw allocating identifiers in favor of our vsh variants. */
|
||||||
#define strdup use_vshStrdup_instead_of_strdup
|
#define strdup use_vshStrdup_instead_of_strdup
|
||||||
|
|
||||||
static int
|
int
|
||||||
vshNameSorter(const void *a, const void *b)
|
vshNameSorter(const void *a, const void *b)
|
||||||
{
|
{
|
||||||
const char **sa = (const char**)a;
|
const char **sa = (const char**)a;
|
||||||
@ -1441,8 +1441,8 @@ vshCommandOptArgv(const vshCmd *cmd, const vshCmdOpt *opt)
|
|||||||
/* Determine whether CMD->opts includes an option with name OPTNAME.
|
/* Determine whether CMD->opts includes an option with name OPTNAME.
|
||||||
If not, give a diagnostic and return false.
|
If not, give a diagnostic and return false.
|
||||||
If so, return true. */
|
If so, return true. */
|
||||||
static bool
|
bool
|
||||||
cmd_has_option(vshControl *ctl, const vshCmd *cmd, const char *optname)
|
vshCmdHasOption(vshControl *ctl, const vshCmd *cmd, const char *optname)
|
||||||
{
|
{
|
||||||
/* Iterate through cmd->opts, to ensure that there is an entry
|
/* Iterate through cmd->opts, to ensure that there is an entry
|
||||||
with name OPTNAME and type VSH_OT_DATA. */
|
with name OPTNAME and type VSH_OT_DATA. */
|
||||||
@ -1461,54 +1461,6 @@ cmd_has_option(vshControl *ctl, const vshCmd *cmd, const char *optname)
|
|||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
virDomainPtr
|
|
||||||
vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
|
|
||||||
const char **name, int flag)
|
|
||||||
{
|
|
||||||
virDomainPtr dom = NULL;
|
|
||||||
const char *n = NULL;
|
|
||||||
int id;
|
|
||||||
const char *optname = "domain";
|
|
||||||
if (!cmd_has_option(ctl, cmd, optname))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (vshCommandOptString(cmd, optname, &n) <= 0)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
vshDebug(ctl, VSH_ERR_INFO, "%s: found option <%s>: %s\n",
|
|
||||||
cmd->def->name, optname, n);
|
|
||||||
|
|
||||||
if (name)
|
|
||||||
*name = n;
|
|
||||||
|
|
||||||
/* try it by ID */
|
|
||||||
if (flag & VSH_BYID) {
|
|
||||||
if (virStrToLong_i(n, NULL, 10, &id) == 0 && id >= 0) {
|
|
||||||
vshDebug(ctl, VSH_ERR_DEBUG,
|
|
||||||
"%s: <%s> seems like domain ID\n",
|
|
||||||
cmd->def->name, optname);
|
|
||||||
dom = virDomainLookupByID(ctl->conn, id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* try it by UUID */
|
|
||||||
if (dom==NULL && (flag & VSH_BYUUID) && strlen(n)==VIR_UUID_STRING_BUFLEN-1) {
|
|
||||||
vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as domain UUID\n",
|
|
||||||
cmd->def->name, optname);
|
|
||||||
dom = virDomainLookupByUUIDString(ctl->conn, n);
|
|
||||||
}
|
|
||||||
/* try it by NAME */
|
|
||||||
if (dom==NULL && (flag & VSH_BYNAME)) {
|
|
||||||
vshDebug(ctl, VSH_ERR_DEBUG, "%s: <%s> trying as domain NAME\n",
|
|
||||||
cmd->def->name, optname);
|
|
||||||
dom = virDomainLookupByName(ctl->conn, n);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!dom)
|
|
||||||
vshError(ctl, _("failed to get domain '%s'"), n);
|
|
||||||
|
|
||||||
return dom;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Executes command(s) and returns return code from last command
|
* Executes command(s) and returns return code from last command
|
||||||
*/
|
*/
|
||||||
|
@ -286,18 +286,15 @@ int vshCommandOptScaledInt(const vshCmd *cmd, const char *name,
|
|||||||
bool vshCommandOptBool(const vshCmd *cmd, const char *name);
|
bool vshCommandOptBool(const vshCmd *cmd, const char *name);
|
||||||
const vshCmdOpt *vshCommandOptArgv(const vshCmd *cmd,
|
const vshCmdOpt *vshCommandOptArgv(const vshCmd *cmd,
|
||||||
const vshCmdOpt *opt);
|
const vshCmdOpt *opt);
|
||||||
|
bool vshCmdHasOption(vshControl *ctl, const vshCmd *cmd, const char *optname);
|
||||||
|
|
||||||
# define VSH_BYID (1 << 1)
|
/* Filter flags for various vshCommandOpt*By() functions */
|
||||||
# define VSH_BYUUID (1 << 2)
|
typedef enum {
|
||||||
# define VSH_BYNAME (1 << 3)
|
VSH_BYID = (1 << 1),
|
||||||
# define VSH_BYMAC (1 << 4)
|
VSH_BYUUID = (1 << 2),
|
||||||
|
VSH_BYNAME = (1 << 3),
|
||||||
virDomainPtr vshCommandOptDomainBy(vshControl *ctl, const vshCmd *cmd,
|
VSH_BYMAC = (1 << 4),
|
||||||
const char **name, int flag);
|
} vshLookupByFlags;
|
||||||
|
|
||||||
/* default is lookup by Id, Name and UUID */
|
|
||||||
# define vshCommandOptDomain(_ctl, _cmd, _name) \
|
|
||||||
vshCommandOptDomainBy(_ctl, _cmd, _name, VSH_BYID|VSH_BYUUID|VSH_BYNAME)
|
|
||||||
|
|
||||||
void vshPrintExtra(vshControl *ctl, const char *format, ...)
|
void vshPrintExtra(vshControl *ctl, const char *format, ...)
|
||||||
ATTRIBUTE_FMT_PRINTF(2, 3);
|
ATTRIBUTE_FMT_PRINTF(2, 3);
|
||||||
@ -309,6 +306,7 @@ void vshDebug(vshControl *ctl, int level, const char *format, ...)
|
|||||||
|
|
||||||
/* User visible sort, so we want locale-specific case comparison. */
|
/* User visible sort, so we want locale-specific case comparison. */
|
||||||
# define vshStrcasecmp(S1, S2) strcasecmp(S1, S2)
|
# define vshStrcasecmp(S1, S2) strcasecmp(S1, S2)
|
||||||
|
int vshNameSorter(const void *a, const void *b);
|
||||||
|
|
||||||
int vshDomainState(vshControl *ctl, virDomainPtr dom, int *reason);
|
int vshDomainState(vshControl *ctl, virDomainPtr dom, int *reason);
|
||||||
bool vshConnectionUsability(vshControl *ctl, virConnectPtr conn);
|
bool vshConnectionUsability(vshControl *ctl, virConnectPtr conn);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user