2005-11-10 19:12:31 +03:00
/*
2010-06-09 13:00:30 +04:00
* virsh . c : a shell to exercise the libvirt API
2005-11-10 19:12:31 +03:00
*
2015-02-19 16:16:39 +03:00
* Copyright ( C ) 2005 , 2007 - 2015 Red Hat , Inc .
2005-11-10 19:12:31 +03:00
*
2012-07-27 13:39:53 +04:00
* 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
2012-09-21 02:30:55 +04:00
* License along with this library . If not , see
2012-07-27 13:39:53 +04:00
* < http : //www.gnu.org/licenses/>.
2005-11-10 19:12:31 +03:00
*
* Daniel Veillard < veillard @ redhat . com >
2005-12-08 17:22:52 +03:00
* Karel Zak < kzak @ redhat . com >
2006-05-29 19:39:31 +04:00
* Daniel P . Berrange < berrange @ redhat . com >
2005-11-10 19:12:31 +03:00
*/
2008-01-29 21:15:54 +03:00
# include <config.h>
2012-08-18 07:16:04 +04:00
# include "virsh.h"
2007-12-04 21:27:52 +03:00
2005-12-08 13:23:34 +03:00
# include <stdarg.h>
2005-12-01 19:35:42 +03:00
# include <unistd.h>
2005-12-08 13:23:34 +03:00
# include <getopt.h>
2005-12-08 17:22:52 +03:00
# include <sys/time.h>
2006-03-30 20:08:13 +04:00
# include <fcntl.h>
2007-11-29 12:18:04 +03:00
# include <time.h>
2007-06-06 16:24:31 +04:00
# include <sys/stat.h>
2007-08-21 14:08:12 +04:00
# include <inttypes.h>
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
# include <signal.h>
2005-12-08 13:23:34 +03:00
2013-10-04 21:51:41 +04:00
# if WITH_READLINE
2010-03-09 21:22:22 +03:00
# include <readline / readline.h>
# include <readline / history.h>
2007-12-04 21:27:52 +03:00
# endif
2005-12-08 13:23:34 +03:00
Standardize use of header files, making internal.h primary.
* qemud/internal.h, qemud/qemud.h: Rename this file so it
doesn't conflict with src/internal.h.
* HACKING: Document how header files should be used.
* qemud/Makefile.am: Add src/ directory to includes.
* qemud/event.c, qemud/mdns.c, qemud/qemud.c, qemud/remote.c,
qemud/remote_protocol.c, qemud/remote_protocol.h,
qemud/remote_protocol.x, src/buf.c, src/libvirt.c,
src/nodeinfo.c, src/qemu_conf.c, src/qemu_driver.c,
src/stats_linux.c, src/storage_backend.c, src/storage_backend_fs.c,
src/storage_backend_iscsi.c, src/storage_backend_logical.c,
src/storage_conf.c, src/storage_driver.c, src/util.c,
src/util.h, src/virsh.c, src/virterror.c, src/xend_internal.c,
src/xml.c, tests/reconnect.c, tests/xmlrpctest.c,
tests/qparamtest.c: Standardize use of header files.
* docs/*, po/*: Rebuild docs.
2008-05-23 12:24:41 +04:00
# include "internal.h"
2012-12-13 22:21:53 +04:00
# include "virerror.h"
2012-12-04 16:04:07 +04:00
# include "virbuffer.h"
2012-12-12 22:06:53 +04:00
# include "viralloc.h"
2013-04-17 14:19:19 +04:00
# include <libvirt/libvirt-qemu.h>
# include <libvirt/libvirt-lxc.h>
2011-07-19 22:32:58 +04:00
# include "virfile.h"
2012-12-13 19:49:48 +04:00
# include "virthread.h"
2012-12-12 20:27:01 +04:00
# include "vircommand.h"
2011-12-20 12:35:03 +04:00
# include "conf/domain_conf.h"
2012-01-03 02:03:19 +04:00
# include "virtypedparam.h"
2013-04-03 14:36:23 +04:00
# include "virstring.h"
2016-04-13 01:29:52 +03:00
# include "virgettext.h"
2005-12-08 13:23:34 +03:00
2013-08-26 13:53:43 +04:00
# include "virsh-console.h"
2012-08-18 08:00:42 +04:00
# include "virsh-domain.h"
2012-08-20 17:46:38 +04:00
# include "virsh-domain-monitor.h"
2012-08-21 00:01:45 +04:00
# include "virsh-host.h"
2012-08-21 00:30:53 +04:00
# include "virsh-interface.h"
2012-08-21 01:43:25 +04:00
# include "virsh-network.h"
2012-08-21 02:23:10 +04:00
# include "virsh-nodedev.h"
2012-08-21 02:56:03 +04:00
# include "virsh-nwfilter.h"
2012-08-21 02:56:53 +04:00
# include "virsh-pool.h"
2012-08-21 03:14:37 +04:00
# include "virsh-secret.h"
2012-08-21 03:29:03 +04:00
# include "virsh-snapshot.h"
2012-08-21 03:41:24 +04:00
# include "virsh-volume.h"
2012-08-18 08:00:42 +04:00
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
/* Gnulib doesn't guarantee SA_SIGINFO support. */
# ifndef SA_SIGINFO
# define SA_SIGINFO 0
# endif
2005-12-08 13:23:34 +03:00
static char * progname ;
2010-11-30 09:37:04 +03:00
static const vshCmdGrp cmdGroups [ ] ;
2015-06-15 19:53:58 +03:00
static const vshClientHooks hooks ;
2009-02-09 17:24:06 +03:00
2010-03-05 12:59:52 +03:00
/*
* Detection of disconnections and automatic reconnection support
*/
2014-10-28 21:38:04 +03:00
static int disconnected ; /* we may have been disconnected */
2010-03-05 12:59:52 +03:00
/*
2015-06-15 19:53:58 +03:00
* virshCatchDisconnect :
2010-03-05 12:59:52 +03:00
*
2012-08-02 21:15:16 +04:00
* We get here when the connection was closed . We can ' t do much in the
* handler , just save the fact it was raised .
2010-03-05 12:59:52 +03:00
*/
2010-03-12 13:39:24 +03:00
static void
2015-09-15 17:46:07 +03:00
virshCatchDisconnect ( virConnectPtr conn ,
2015-06-15 19:53:58 +03:00
int reason ,
2015-09-15 17:46:07 +03:00
void * opaque )
2012-08-02 21:15:16 +04:00
{
2015-09-15 17:46:07 +03:00
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 ;
2015-09-24 01:11:30 +03:00
/* coverity[dead_error_condition] */
2015-09-15 17:46:07 +03:00
case VIR_CONNECT_CLOSE_REASON_CLIENT :
case VIR_CONNECT_CLOSE_REASON_LAST :
break ;
}
vshError ( ctl , _ ( str ) , NULLSTR ( uri ) ) ;
2016-03-03 19:31:47 +03:00
VIR_FREE ( uri ) ;
2015-09-15 17:46:07 +03:00
if ( error ) {
virSetError ( error ) ;
virFreeError ( error ) ;
}
2012-08-02 21:15:16 +04:00
disconnected + + ;
2015-12-18 01:23:16 +03:00
vshEventDone ( ctl ) ;
2015-09-15 17:46:07 +03:00
}
2010-03-05 12:59:52 +03:00
}
2014-03-06 20:20:11 +04:00
/* Main Function which should be used for connecting.
* This function properly handles keepalive settings . */
virConnectPtr
2015-06-15 19:53:58 +03:00
virshConnect ( vshControl * ctl , const char * uri , bool readonly )
2014-03-06 20:20:11 +04:00
{
virConnectPtr c = NULL ;
int interval = 5 ; /* Default */
int count = 6 ; /* Default */
bool keepalive_forced = false ;
2016-02-09 22:08:42 +03:00
virPolkitAgentPtr pkagent = NULL ;
int authfail = 0 ;
2017-05-11 16:17:09 +03:00
bool agentCreated = false ;
2014-03-06 20:20:11 +04:00
if ( ctl - > keepalive_interval > = 0 ) {
interval = ctl - > keepalive_interval ;
keepalive_forced = true ;
}
if ( ctl - > keepalive_count > = 0 ) {
count = ctl - > keepalive_count ;
keepalive_forced = true ;
}
2016-02-09 22:08:42 +03:00
do {
virErrorPtr err ;
if ( ( c = virConnectOpenAuth ( uri , virConnectAuthPtrDefault ,
readonly ? VIR_CONNECT_RO : 0 ) ) )
break ;
if ( readonly )
goto cleanup ;
err = virGetLastError ( ) ;
2017-05-11 16:17:09 +03:00
if ( ! agentCreated & &
err & & err - > domain = = VIR_FROM_POLKIT & &
2016-02-09 22:08:42 +03:00
err - > code = = VIR_ERR_AUTH_UNAVAILABLE ) {
2016-08-03 15:41:50 +03:00
if ( ! pkagent & & ! ( pkagent = virPolkitAgentCreate ( ) ) )
2016-02-09 22:08:42 +03:00
goto cleanup ;
2017-05-11 16:17:09 +03:00
agentCreated = true ;
2016-02-09 22:08:42 +03:00
} 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 ) ;
2014-03-06 20:20:11 +04:00
if ( ! c )
2016-02-09 22:08:42 +03:00
goto cleanup ;
2014-03-06 20:20:11 +04:00
if ( interval > 0 & &
virConnectSetKeepAlive ( c , interval , count ) ! = 0 ) {
if ( keepalive_forced ) {
vshError ( ctl , " %s " ,
_ ( " Cannot setup keepalive on connection "
" as requested, disconnecting " ) ) ;
virConnectClose ( c ) ;
2016-02-09 22:08:42 +03:00
c = NULL ;
goto cleanup ;
2014-03-06 20:20:11 +04:00
}
vshDebug ( ctl , VSH_ERR_INFO , " %s " ,
_ ( " Failed to setup keepalive on connection \n " ) ) ;
}
2016-02-09 22:08:42 +03:00
cleanup :
virPolkitAgentDestroy ( pkagent ) ;
2014-03-06 20:20:11 +04:00
return c ;
}
2010-03-05 12:59:52 +03:00
/*
2015-06-15 19:53:58 +03:00
* virshReconnect :
2010-03-05 12:59:52 +03:00
*
2010-03-12 13:39:24 +03:00
* Reconnect after a disconnect from libvirtd
2010-03-05 12:59:52 +03:00
*
*/
2016-07-29 09:09:22 +03:00
static int
2016-04-21 15:06:10 +03:00
virshReconnect ( vshControl * ctl , const char * name , bool readonly , bool force )
virsh: optimize creation of default connection
Ramon de Carvalho Valle reported a problem with:
virsh connect qemu:///system
as a non-root user. The real root problem appears to be a regression
in libvirtd being auto-started on the default qemu:///session URI;
however, the symptom points to an independent flaw in virsh - we
shouldn't be wasting efforts on making a connection if we aren't going
to be using that connection. Fixing virsh avoids Ramon's issue, while
I work in the meantime to fix the real libvirtd regression.
This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
had to add 0 initialization to everyone (rather than my preference of
just adding the non-zero flags to virshCmds and to cmdConnect).
Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
patch _only_ optimizes the default connection to be deferred to a later
point where we know if a particular command to be run needs a connection.
* tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
(vshCmdDef): Add new flags field.
(vshCommandRun): Honor new flag.
(domManagementCmds, domMonitoringCmds, storagePoolCmds)
(storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
(nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
(hostAndHypervisorCmds): Populate new field.
(vshReconnect): Don't warn on initial connection.
2011-03-14 23:30:24 +03:00
{
bool connected = false ;
2015-06-15 19:53:58 +03:00
virshControlPtr priv = ctl - > privData ;
2017-08-18 17:35:23 +03:00
/* 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 ;
virsh: optimize creation of default connection
Ramon de Carvalho Valle reported a problem with:
virsh connect qemu:///system
as a non-root user. The real root problem appears to be a regression
in libvirtd being auto-started on the default qemu:///session URI;
however, the symptom points to an independent flaw in virsh - we
shouldn't be wasting efforts on making a connection if we aren't going
to be using that connection. Fixing virsh avoids Ramon's issue, while
I work in the meantime to fix the real libvirtd regression.
This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
had to add 0 initialization to everyone (rather than my preference of
just adding the non-zero flags to virshCmds and to cmdConnect).
Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
patch _only_ optimizes the default connection to be deferred to a later
point where we know if a particular command to be run needs a connection.
* tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
(vshCmdDef): Add new flags field.
(vshCommandRun): Honor new flag.
(domManagementCmds, domMonitoringCmds, storagePoolCmds)
(storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
(nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
(hostAndHypervisorCmds): Populate new field.
(vshReconnect): Don't warn on initial connection.
2011-03-14 23:30:24 +03:00
2015-06-15 19:53:58 +03:00
if ( priv - > conn ) {
2013-03-26 13:54:55 +04:00
int ret ;
virsh: optimize creation of default connection
Ramon de Carvalho Valle reported a problem with:
virsh connect qemu:///system
as a non-root user. The real root problem appears to be a regression
in libvirtd being auto-started on the default qemu:///session URI;
however, the symptom points to an independent flaw in virsh - we
shouldn't be wasting efforts on making a connection if we aren't going
to be using that connection. Fixing virsh avoids Ramon's issue, while
I work in the meantime to fix the real libvirtd regression.
This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
had to add 0 initialization to everyone (rather than my preference of
just adding the non-zero flags to virshCmds and to cmdConnect).
Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
patch _only_ optimizes the default connection to be deferred to a later
point where we know if a particular command to be run needs a connection.
* tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
(vshCmdDef): Add new flags field.
(vshCommandRun): Honor new flag.
(domManagementCmds, domMonitoringCmds, storagePoolCmds)
(storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
(nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
(hostAndHypervisorCmds): Populate new field.
(vshReconnect): Don't warn on initial connection.
2011-03-14 23:30:24 +03:00
connected = true ;
2013-03-26 13:54:55 +04:00
2015-06-15 19:53:58 +03:00
virConnectUnregisterCloseCallback ( priv - > conn , virshCatchDisconnect ) ;
ret = virConnectClose ( priv - > conn ) ;
2013-03-26 13:54:55 +04:00
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 " ) ) ;
virsh: optimize creation of default connection
Ramon de Carvalho Valle reported a problem with:
virsh connect qemu:///system
as a non-root user. The real root problem appears to be a regression
in libvirtd being auto-started on the default qemu:///session URI;
however, the symptom points to an independent flaw in virsh - we
shouldn't be wasting efforts on making a connection if we aren't going
to be using that connection. Fixing virsh avoids Ramon's issue, while
I work in the meantime to fix the real libvirtd regression.
This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
had to add 0 initialization to everyone (rather than my preference of
just adding the non-zero flags to virshCmds and to cmdConnect).
Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
patch _only_ optimizes the default connection to be deferred to a later
point where we know if a particular command to be run needs a connection.
* tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
(vshCmdDef): Add new flags field.
(vshCommandRun): Honor new flag.
(domManagementCmds, domMonitoringCmds, storagePoolCmds)
(storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
(nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
(hostAndHypervisorCmds): Populate new field.
(vshReconnect): Don't warn on initial connection.
2011-03-14 23:30:24 +03:00
}
2010-03-05 12:59:52 +03:00
2017-08-18 17:35:23 +03:00
priv - > conn = virshConnect ( ctl , name ? name : ctl - > connname , readonly ) ;
2014-03-06 20:20:11 +04:00
2015-06-15 19:53:58 +03:00
if ( ! priv - > conn ) {
2012-12-13 17:08:00 +04:00
if ( disconnected )
vshError ( ctl , " %s " , _ ( " Failed to reconnect to the hypervisor " ) ) ;
else
vshError ( ctl , " %s " , _ ( " failed to connect to the hypervisor " ) ) ;
2016-07-29 09:09:22 +03:00
return - 1 ;
2012-08-02 21:15:16 +04:00
} else {
2016-04-21 15:06:10 +03:00
if ( name ) {
VIR_FREE ( ctl - > connname ) ;
ctl - > connname = vshStrdup ( ctl , name ) ;
}
2017-11-27 03:18:11 +03:00
priv - > readonly = readonly ;
2015-06-15 19:53:58 +03:00
if ( virConnectRegisterCloseCallback ( priv - > conn , virshCatchDisconnect ,
2015-12-18 01:23:01 +03:00
ctl , NULL ) < 0 )
2012-08-02 21:15:16 +04:00
vshError ( ctl , " %s " , _ ( " Unable to register disconnect callback " ) ) ;
2016-04-21 15:06:10 +03:00
if ( connected & & ! force )
2012-08-02 21:15:16 +04:00
vshError ( ctl , " %s " , _ ( " Reconnected to the hypervisor " ) ) ;
}
2010-03-05 12:59:52 +03:00
disconnected = 0 ;
2015-06-15 19:53:58 +03:00
priv - > useGetInfo = false ;
priv - > useSnapshotOld = false ;
priv - > blockJobNoBytes = false ;
2016-07-29 09:09:22 +03:00
return 0 ;
2010-03-05 12:59:52 +03:00
}
2007-02-14 18:44:58 +03:00
2015-06-15 19:53:58 +03:00
/* ---------------
* Command Connect
* - - - - - - - - - - - - - - -
2013-03-27 17:22:47 +04:00
*/
static const vshCmdOptDef opts_connect [ ] = {
{ . name = " name " ,
2014-12-11 05:46:15 +03:00
. type = VSH_OT_STRING ,
2013-03-27 17:22:47 +04:00
. flags = VSH_OFLAG_EMPTY_OK ,
. help = N_ ( " hypervisor connection URI " )
} ,
{ . name = " readonly " ,
. type = VSH_OT_BOOL ,
. help = N_ ( " read-only connection " )
} ,
{ . name = NULL }
} ;
2015-06-15 19:53:58 +03:00
static const vshCmdInfo info_connect [ ] = {
2013-02-07 19:25:10 +04:00
{ . name = " help " ,
2015-06-15 19:53:58 +03:00
. data = N_ ( " (re)connect to hypervisor " )
2013-02-07 19:25:10 +04:00
} ,
{ . name = " desc " ,
2015-06-15 19:53:58 +03:00
. data = N_ ( " Connect to local hypervisor. This is built-in "
" command after shell start up. " )
2013-01-14 15:26:23 +04:00
} ,
{ . name = NULL }
2009-07-16 18:40:08 +04:00
} ;
2011-04-19 02:37:42 +04:00
static bool
2015-06-15 19:53:58 +03:00
cmdConnect ( vshControl * ctl , const vshCmd * cmd )
2009-07-16 18:40:08 +04:00
{
2015-06-15 19:53:58 +03:00
bool ro = vshCommandOptBool ( cmd , " readonly " ) ;
const char * name = NULL ;
2016-04-20 20:56:27 +03:00
if ( vshCommandOptStringReq ( ctl , cmd , " name " , & name ) < 0 )
return false ;
2016-07-29 09:09:22 +03:00
if ( virshReconnect ( ctl , name , ro , true ) < 0 )
return false ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
return true ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
}
2015-06-15 19:53:58 +03:00
/* ---------------
* Utils for work with runtime commands data
* - - - - - - - - - - - - - - -
*/
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
static bool
virshConnectionUsability ( vshControl * ctl , virConnectPtr conn )
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
{
2015-06-15 19:53:58 +03:00
if ( ! conn | |
virConnectIsAlive ( conn ) = = 0 ) {
vshError ( ctl , " %s " , _ ( " no valid connection " ) ) ;
return false ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
}
2015-06-15 19:53:58 +03:00
/* The connection is considered dead only if
2018-12-04 20:08:14 +03:00
* virConnectIsAlive ( ) successfully says so .
2015-06-15 19:53:58 +03:00
*/
vshResetLibvirtError ( ) ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
return true ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
}
2015-06-15 19:53:58 +03:00
static void *
virshConnectionHandler ( vshControl * ctl )
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
{
2015-06-15 19:53:58 +03:00
virshControlPtr priv = ctl - > privData ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2016-07-29 09:09:22 +03:00
if ( ( ! priv - > conn | | disconnected ) & &
virshReconnect ( ctl , NULL , false , false ) < 0 )
return NULL ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-06-15 19:53:58 +03:00
if ( virshConnectionUsability ( ctl , priv - > conn ) )
return priv - > conn ;
2016-07-29 09:09:22 +03:00
2015-06-15 19:53:58 +03:00
return NULL ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
}
2016-06-17 20:28:12 +03:00
2013-08-21 13:02:42 +04:00
/*
* Initialize connection .
*/
static bool
2015-06-15 19:53:58 +03:00
virshInit ( vshControl * ctl )
2013-08-21 13:02:42 +04:00
{
2015-06-15 19:53:58 +03:00
virshControlPtr priv = ctl - > privData ;
2013-08-27 15:19:24 +04:00
/* Since we have the commandline arguments parsed, we need to
2015-09-03 17:59:01 +03:00
* reload our initial settings to make debugging and readline
* work properly */
vshInitReload ( ctl ) ;
2013-08-27 15:19:24 +04:00
2015-06-15 19:53:58 +03:00
if ( priv - > conn )
2013-08-21 13:02:42 +04:00
return false ;
2007-06-06 16:24:31 +04:00
2006-02-28 00:34:28 +03:00
/* set up the library error handler */
2015-06-15 19:53:58 +03:00
virSetErrorFunc ( NULL , vshErrorHandler ) ;
2006-03-15 15:13:25 +03:00
2017-04-19 13:42:00 +03:00
if ( virEventRegisterDefaultImpl ( ) < 0 ) {
vshReportError ( ctl ) ;
2011-04-19 02:37:42 +04:00
return false ;
2017-04-19 13:42:00 +03:00
}
2010-07-27 13:40:30 +04:00
2017-04-19 13:42:00 +03:00
if ( virThreadCreate ( & ctl - > eventLoop , true , vshEventLoop , ctl ) < 0 ) {
vshReportError ( ctl ) ;
2011-10-11 17:05:52 +04:00
return false ;
2017-04-19 13:42:00 +03:00
}
2011-10-11 17:05:52 +04:00
ctl - > eventLoopStarted = true ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
if ( ( ctl - > eventTimerId = virEventAddTimeout ( - 1 , vshEventTimeout , ctl ,
2017-04-19 13:42:00 +03:00
NULL ) ) < 0 ) {
vshReportError ( ctl ) ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
return false ;
2017-04-19 13:42:00 +03:00
}
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
2015-12-10 15:46:45 +03:00
if ( ctl - > connname ) {
virsh: optimize creation of default connection
Ramon de Carvalho Valle reported a problem with:
virsh connect qemu:///system
as a non-root user. The real root problem appears to be a regression
in libvirtd being auto-started on the default qemu:///session URI;
however, the symptom points to an independent flaw in virsh - we
shouldn't be wasting efforts on making a connection if we aren't going
to be using that connection. Fixing virsh avoids Ramon's issue, while
I work in the meantime to fix the real libvirtd regression.
This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
had to add 0 initialization to everyone (rather than my preference of
just adding the non-zero flags to virshCmds and to cmdConnect).
Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
patch _only_ optimizes the default connection to be deferred to a later
point where we know if a particular command to be run needs a connection.
* tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
(vshCmdDef): Add new flags field.
(vshCommandRun): Honor new flag.
(domManagementCmds, domMonitoringCmds, storagePoolCmds)
(storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
(nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
(hostAndHypervisorCmds): Populate new field.
(vshReconnect): Don't warn on initial connection.
2011-03-14 23:30:24 +03:00
/* 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 ) .
*/
2016-07-29 09:09:22 +03:00
if ( virshReconnect ( ctl , NULL , false , false ) < 0 ) {
virsh: use common namespacing
Convert the exported items in virsh.h to use a common 'vsh' prefix.
* tools/virsh.h (VIRSH_MAX_XML_FILE): Rename...
(VSH_MAX_XML_FILE): ...and parenthesize.
(DIFF_MSEC, CTRL_CLOSE_BRACKET): Delete.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Remove prototype.
(editWriteToTempFile, editFile, editReadBackFile, prettyCapacity)
(virshReportError): Rename...
(vshEditWriteToTempFile, vshEditFile, vshEditReadBackFile)
(vshPrettyCapacity, vshReportError): ...into vsh namespace.
(jobWatchTimeoutFunc): Move to virsh-domain.c.
* tools/virsh.c (vshCommandRun): Inline former DIFF_MSEC.
(main): Inline former CTRL_CLOSE_BRACKET.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Make static.
(prettyCapacity, virshReportError, editWriteToTempFile, editFile):
Fix naming, and adjust usage.
(vshAskReedit, vshCommandRun, vshEventLoop, vshInit): Adjust
usage.
* tools/virsh-domain.c (cmdAttachDevice, cmdCPUCompare)
(cmdCPUBaseline, cmdCreate, cmdDefine, cmdDetachDevice)
(cmdUpdateDevice, cmdDesc, cmdUndefine, cmdStart, cmdVcpucount)
(cmdAttachDevice, cmdDomjobinfo): Likewise.
* tools/virsh-edit.c (do): Likewise.
* tools/virsh-interface.c (cmdInterfaceDefine): Likewise.
* tools/virsh-network.c (cmdNetworkCreate, cmdNetworkDefine):
Likewise.
* tools/virsh-nodedev.c (cmdNodeDeviceCreate): Likewise.
* tools/virsh-nwfilter.c (cmdNWFilterDefine): Likewise.
* tools/virsh-pool.c (cmdPoolCreate, cmdPoolDefine)
(cmdPoolDiscoverSources, cmdPoolList): Likewise.
* tools/virsh-secret.c (cmdSecretDefine): Likewise.
* tools/virsh-snapshot.c (cmdSnapshotCreate, vshSnapshotCreate)
(vshLookupSnapshot, cmdSnapshotEdit, cmdSnapshotCurrent)
(vshGetSnapshotParent): Likewise.
* tools/virsh-volume.c (cmdVolCreate, cmdVolCreateFrom)
(cmdVolInfo, cmdVolList): Likewise.
2012-08-19 08:10:17 +04:00
vshReportError ( ctl ) ;
virsh: optimize creation of default connection
Ramon de Carvalho Valle reported a problem with:
virsh connect qemu:///system
as a non-root user. The real root problem appears to be a regression
in libvirtd being auto-started on the default qemu:///session URI;
however, the symptom points to an independent flaw in virsh - we
shouldn't be wasting efforts on making a connection if we aren't going
to be using that connection. Fixing virsh avoids Ramon's issue, while
I work in the meantime to fix the real libvirtd regression.
This patch looks big, but that's because 'gcc -Wmissing-field-initializers'
gets triggered by './autobuild.sh --enable-compile-warnings=error', so I
had to add 0 initialization to everyone (rather than my preference of
just adding the non-zero flags to virshCmds and to cmdConnect).
Meanwhile, if you use 'virsh -c URI', the connection must succeed; this
patch _only_ optimizes the default connection to be deferred to a later
point where we know if a particular command to be run needs a connection.
* tools/virsh.c (VSH_CMD_FLAG_NOCONNECT): New flag.
(vshCmdDef): Add new flags field.
(vshCommandRun): Honor new flag.
(domManagementCmds, domMonitoringCmds, storagePoolCmds)
(storageVolCmds, networkCmds, nodedevCmds, ifaceCmds)
(nwfilterCmds, secretCmds, virshCmds, snapshotCmds)
(hostAndHypervisorCmds): Populate new field.
(vshReconnect): Don't warn on initial connection.
2011-03-14 23:30:24 +03:00
return false ;
}
2007-12-05 19:24:22 +03:00
}
2005-12-08 13:23:34 +03:00
2011-04-19 02:37:42 +04:00
return true ;
2005-12-08 13:23:34 +03:00
}
2011-11-30 23:42:20 +04:00
static void
2015-06-15 19:53:58 +03:00
virshDeinitTimer ( int timer ATTRIBUTE_UNUSED , void * opaque ATTRIBUTE_UNUSED )
2011-11-30 23:42:20 +04:00
{
/* nothing to be done here */
}
2005-12-08 13:23:34 +03:00
/*
2008-02-04 17:58:47 +03:00
* Deinitialize virsh
2005-12-08 13:23:34 +03:00
*/
virsh: use common namespacing
Convert the exported items in virsh.h to use a common 'vsh' prefix.
* tools/virsh.h (VIRSH_MAX_XML_FILE): Rename...
(VSH_MAX_XML_FILE): ...and parenthesize.
(DIFF_MSEC, CTRL_CLOSE_BRACKET): Delete.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Remove prototype.
(editWriteToTempFile, editFile, editReadBackFile, prettyCapacity)
(virshReportError): Rename...
(vshEditWriteToTempFile, vshEditFile, vshEditReadBackFile)
(vshPrettyCapacity, vshReportError): ...into vsh namespace.
(jobWatchTimeoutFunc): Move to virsh-domain.c.
* tools/virsh.c (vshCommandRun): Inline former DIFF_MSEC.
(main): Inline former CTRL_CLOSE_BRACKET.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Make static.
(prettyCapacity, virshReportError, editWriteToTempFile, editFile):
Fix naming, and adjust usage.
(vshAskReedit, vshCommandRun, vshEventLoop, vshInit): Adjust
usage.
* tools/virsh-domain.c (cmdAttachDevice, cmdCPUCompare)
(cmdCPUBaseline, cmdCreate, cmdDefine, cmdDetachDevice)
(cmdUpdateDevice, cmdDesc, cmdUndefine, cmdStart, cmdVcpucount)
(cmdAttachDevice, cmdDomjobinfo): Likewise.
* tools/virsh-edit.c (do): Likewise.
* tools/virsh-interface.c (cmdInterfaceDefine): Likewise.
* tools/virsh-network.c (cmdNetworkCreate, cmdNetworkDefine):
Likewise.
* tools/virsh-nodedev.c (cmdNodeDeviceCreate): Likewise.
* tools/virsh-nwfilter.c (cmdNWFilterDefine): Likewise.
* tools/virsh-pool.c (cmdPoolCreate, cmdPoolDefine)
(cmdPoolDiscoverSources, cmdPoolList): Likewise.
* tools/virsh-secret.c (cmdSecretDefine): Likewise.
* tools/virsh-snapshot.c (cmdSnapshotCreate, vshSnapshotCreate)
(vshLookupSnapshot, cmdSnapshotEdit, cmdSnapshotCurrent)
(vshGetSnapshotParent): Likewise.
* tools/virsh-volume.c (cmdVolCreate, cmdVolCreateFrom)
(cmdVolInfo, cmdVolList): Likewise.
2012-08-19 08:10:17 +04:00
static bool
2015-06-15 19:53:58 +03:00
virshDeinit ( vshControl * ctl )
2006-03-15 15:13:25 +03:00
{
2015-06-15 19:53:58 +03:00
virshControlPtr priv = ctl - > privData ;
vshDeinit ( ctl ) ;
2015-12-10 15:46:45 +03:00
VIR_FREE ( ctl - > connname ) ;
2015-06-15 19:53:58 +03:00
if ( priv - > conn ) {
2010-11-11 18:15:46 +03:00
int ret ;
2015-06-15 19:53:58 +03:00
virConnectUnregisterCloseCallback ( priv - > conn , virshCatchDisconnect ) ;
ret = virConnectClose ( priv - > conn ) ;
2013-03-26 13:54:55 +04:00
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 " ) ) ;
2005-12-08 13:23:34 +03:00
}
2007-12-01 18:45:25 +03:00
virResetLastError ( ) ;
2011-10-11 17:05:52 +04:00
if ( ctl - > eventLoopStarted ) {
2011-11-30 23:42:20 +04:00
int timer ;
virMutexLock ( & ctl - > lock ) ;
ctl - > quit = true ;
2011-10-11 17:05:52 +04:00
/* HACK: Add a dummy timeout to break event loop */
2015-06-15 19:53:58 +03:00
timer = virEventAddTimeout ( 0 , virshDeinitTimer , NULL , NULL ) ;
2011-11-30 23:42:20 +04:00
virMutexUnlock ( & ctl - > lock ) ;
virThreadJoin ( & ctl - > eventLoop ) ;
2011-10-11 17:05:52 +04:00
if ( timer ! = - 1 )
virEventRemoveTimeout ( timer ) ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
if ( ctl - > eventTimerId ! = - 1 )
virEventRemoveTimeout ( ctl - > eventTimerId ) ;
2011-10-11 17:05:52 +04:00
ctl - > eventLoopStarted = false ;
}
2011-11-30 23:42:20 +04:00
virMutexDestroy ( & ctl - > lock ) ;
2011-04-19 02:37:42 +04:00
return true ;
2005-12-08 13:23:34 +03:00
}
2006-03-15 15:13:25 +03:00
2005-12-08 13:23:34 +03:00
/*
* Print usage
*/
virsh: use common namespacing
Convert the exported items in virsh.h to use a common 'vsh' prefix.
* tools/virsh.h (VIRSH_MAX_XML_FILE): Rename...
(VSH_MAX_XML_FILE): ...and parenthesize.
(DIFF_MSEC, CTRL_CLOSE_BRACKET): Delete.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Remove prototype.
(editWriteToTempFile, editFile, editReadBackFile, prettyCapacity)
(virshReportError): Rename...
(vshEditWriteToTempFile, vshEditFile, vshEditReadBackFile)
(vshPrettyCapacity, vshReportError): ...into vsh namespace.
(jobWatchTimeoutFunc): Move to virsh-domain.c.
* tools/virsh.c (vshCommandRun): Inline former DIFF_MSEC.
(main): Inline former CTRL_CLOSE_BRACKET.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Make static.
(prettyCapacity, virshReportError, editWriteToTempFile, editFile):
Fix naming, and adjust usage.
(vshAskReedit, vshCommandRun, vshEventLoop, vshInit): Adjust
usage.
* tools/virsh-domain.c (cmdAttachDevice, cmdCPUCompare)
(cmdCPUBaseline, cmdCreate, cmdDefine, cmdDetachDevice)
(cmdUpdateDevice, cmdDesc, cmdUndefine, cmdStart, cmdVcpucount)
(cmdAttachDevice, cmdDomjobinfo): Likewise.
* tools/virsh-edit.c (do): Likewise.
* tools/virsh-interface.c (cmdInterfaceDefine): Likewise.
* tools/virsh-network.c (cmdNetworkCreate, cmdNetworkDefine):
Likewise.
* tools/virsh-nodedev.c (cmdNodeDeviceCreate): Likewise.
* tools/virsh-nwfilter.c (cmdNWFilterDefine): Likewise.
* tools/virsh-pool.c (cmdPoolCreate, cmdPoolDefine)
(cmdPoolDiscoverSources, cmdPoolList): Likewise.
* tools/virsh-secret.c (cmdSecretDefine): Likewise.
* tools/virsh-snapshot.c (cmdSnapshotCreate, vshSnapshotCreate)
(vshLookupSnapshot, cmdSnapshotEdit, cmdSnapshotCurrent)
(vshGetSnapshotParent): Likewise.
* tools/virsh-volume.c (cmdVolCreate, cmdVolCreateFrom)
(cmdVolInfo, cmdVolList): Likewise.
2012-08-19 08:10:17 +04:00
static void
2015-06-15 19:53:58 +03:00
virshUsage ( void )
2006-03-15 15:13:25 +03:00
{
2010-11-30 09:37:04 +03:00
const vshCmdGrp * grp ;
2008-08-01 16:19:56 +04:00
const vshCmdDef * cmd ;
2010-11-30 09:37:04 +03:00
2010-10-12 11:14:01 +04:00
fprintf ( stdout , _ ( " \n %s [options]... [<command_string>] "
" \n %s [options]... <command> [args...] \n \n "
2008-12-08 16:14:48 +03:00
" options: \n "
2011-11-22 20:08:05 +04:00
" -c | --connect=URI hypervisor connection URI \n "
" -d | --debug=NUM debug level [0-4] \n "
2014-03-06 19:53:53 +04:00
" -e | --escape <char> set escape sequence for console \n "
2008-12-08 16:14:48 +03:00
" -h | --help this help \n "
2014-03-06 20:20:11 +04:00
" -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 "
2014-03-06 19:53:53 +04:00
" -l | --log=FILE output logging to file \n "
2008-12-08 16:14:48 +03:00
" -q | --quiet quiet mode \n "
2014-03-06 19:53:53 +04:00
" -r | --readonly connect readonly \n "
2008-12-08 16:14:48 +03:00
" -t | --timing print timing information \n "
2011-11-22 20:08:05 +04:00
" -v short version \n "
" -V long version \n "
" --version[=TYPE] version, TYPE is short or long (default short) \n "
2015-06-15 19:53:58 +03:00
" commands (non interactive mode): \n \n " ) , progname ,
progname ) ;
2008-12-08 16:14:48 +03:00
2010-11-30 09:37:04 +03:00
for ( grp = cmdGroups ; grp - > name ; grp + + ) {
2012-03-16 23:23:00 +04:00
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 ;
2010-11-30 09:37:04 +03:00
fprintf ( stdout ,
2012-03-16 23:23:00 +04:00
" %-30s %s \n " , cmd - > name ,
_ ( vshCmddefGetInfo ( cmd , " help " ) ) ) ;
}
2010-11-30 09:37:04 +03:00
fprintf ( stdout , " \n " ) ;
}
fprintf ( stdout , " %s " ,
_ ( " \n (specify help <group> for details about the commands in the group) \n " ) ) ;
2008-12-08 16:14:48 +03:00
fprintf ( stdout , " %s " ,
_ ( " \n (specify help <command> for details about the command) \n \n " ) ) ;
return ;
2005-12-08 13:23:34 +03:00
}
2010-11-08 17:03:32 +03:00
/*
* Show version and options compiled in
*/
static void
2015-06-15 19:53:58 +03:00
virshShowVersion ( vshControl * ctl ATTRIBUTE_UNUSED )
2010-11-08 17:03:32 +03:00
{
/* FIXME - list a copyright blurb, as in GNU programs? */
vshPrint ( ctl , _ ( " Virsh command line tool of libvirt %s \n " ) , VERSION ) ;
2017-10-13 18:30:41 +03:00
vshPrint ( ctl , _ ( " See web site at %s \n \n " ) , " https://libvirt.org/ " ) ;
2010-11-08 17:03:32 +03:00
2010-11-10 01:37:05 +03:00
vshPrint ( ctl , " %s " , _ ( " Compiled with support for: \n " ) ) ;
vshPrint ( ctl , " %s " , _ ( " Hypervisors: " ) ) ;
2010-11-08 17:03:32 +03:00
# ifdef WITH_QEMU
2012-09-10 02:07:28 +04:00
vshPrint ( ctl , " QEMU/KVM " ) ;
2010-11-08 17:03:32 +03:00
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_LXC
vshPrint ( ctl , " LXC " ) ;
# endif
# ifdef WITH_LIBXL
vshPrint ( ctl , " LibXL " ) ;
# endif
2010-11-08 17:03:32 +03:00
# ifdef WITH_OPENVZ
vshPrint ( ctl , " OpenVZ " ) ;
# endif
2016-03-28 16:20:40 +03:00
# ifdef WITH_VZ
vshPrint ( ctl , " Virtuozzo " ) ;
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_VMWARE
2016-02-15 17:34:24 +03:00
vshPrint ( ctl , " VMware " ) ;
2010-11-08 17:03:32 +03:00
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_PHYP
vshPrint ( ctl , " PHYP " ) ;
2010-11-08 17:03:32 +03:00
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_VBOX
vshPrint ( ctl , " VirtualBox " ) ;
2010-11-08 17:03:32 +03:00
# endif
# ifdef WITH_ESX
vshPrint ( ctl , " ESX " ) ;
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_HYPERV
vshPrint ( ctl , " Hyper-V " ) ;
2010-11-08 17:03:32 +03:00
# endif
2012-06-26 22:31:31 +04:00
# ifdef WITH_XENAPI
vshPrint ( ctl , " XenAPI " ) ;
2010-11-08 17:03:32 +03:00
# endif
2014-06-10 22:25:10 +04:00
# ifdef WITH_BHYVE
vshPrint ( ctl , " Bhyve " ) ;
# endif
2010-11-08 17:03:32 +03:00
# ifdef WITH_TEST
vshPrint ( ctl , " Test " ) ;
# endif
vshPrint ( ctl , " \n " ) ;
2010-11-10 01:37:05 +03:00
vshPrint ( ctl , " %s " , _ ( " Networking: " ) ) ;
2010-11-08 17:03:32 +03:00
# ifdef WITH_REMOTE
vshPrint ( ctl , " Remote " ) ;
# endif
# ifdef WITH_NETWORK
vshPrint ( ctl , " Network " ) ;
# endif
# ifdef WITH_BRIDGE
vshPrint ( ctl , " Bridging " ) ;
# endif
2012-09-18 05:27:06 +04:00
# if defined(WITH_INTERFACE)
2012-06-26 22:31:31 +04:00
vshPrint ( ctl , " Interface " ) ;
2012-09-18 05:27:06 +04:00
# if defined(WITH_NETCF)
vshPrint ( ctl , " netcf " ) ;
2012-09-20 18:24:47 +04:00
# elif defined(WITH_UDEV)
2012-10-06 23:20:25 +04:00
vshPrint ( ctl , " udev " ) ;
2012-09-18 05:27:06 +04:00
# endif
2010-11-08 17:03:32 +03:00
# endif
# ifdef WITH_NWFILTER
vshPrint ( ctl , " Nwfilter " ) ;
# endif
# ifdef WITH_VIRTUALPORT
vshPrint ( ctl , " VirtualPort " ) ;
# endif
vshPrint ( ctl , " \n " ) ;
2010-11-10 01:37:05 +03:00
vshPrint ( ctl , " %s " , _ ( " Storage: " ) ) ;
2010-11-08 17:03:32 +03:00
# 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 " ) ;
2012-05-14 13:06:42 +04:00
# endif
# ifdef WITH_STORAGE_RBD
vshPrint ( ctl , " RBD " ) ;
2012-07-18 23:06:58 +04:00
# endif
# ifdef WITH_STORAGE_SHEEPDOG
vshPrint ( ctl , " Sheepdog " ) ;
2013-12-12 07:08:10 +04:00
# endif
# ifdef WITH_STORAGE_GLUSTER
vshPrint ( ctl , " Gluster " ) ;
2016-01-03 05:25:35 +03:00
# endif
# ifdef WITH_STORAGE_ZFS
vshPrint ( ctl , " ZFS " ) ;
2017-01-17 17:10:55 +03:00
# endif
# ifdef WITH_STORAGE_VSTORAGE
vshPrint ( ctl , " Virtuozzo Storage " ) ;
2010-11-08 17:03:32 +03:00
# endif
vshPrint ( ctl , " \n " ) ;
2012-07-23 07:57:53 +04:00
vshPrint ( ctl , " %s " , _ ( " Miscellaneous: " ) ) ;
2012-09-10 02:07:27 +04:00
# ifdef WITH_LIBVIRTD
vshPrint ( ctl , " Daemon " ) ;
# endif
2012-07-23 07:57:53 +04:00
# 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
2013-10-04 21:51:41 +04:00
# if WITH_READLINE
2012-07-23 07:57:53 +04:00
vshPrint ( ctl , " Readline " ) ;
# endif
vshPrint ( ctl , " \n " ) ;
}
static bool
2015-06-15 19:53:58 +03:00
virshAllowedEscapeChar ( char c )
2012-07-23 07:57:53 +04:00
{
/* Allowed escape characters:
* a - z A - Z @ [ \ ] ^ _
*/
return ( ' a ' < = c & & c < = ' z ' ) | |
( ' @ ' < = c & & c < = ' _ ' ) ;
}
/*
* argv [ ] : virsh [ options ] [ command ]
*
*/
virsh: use common namespacing
Convert the exported items in virsh.h to use a common 'vsh' prefix.
* tools/virsh.h (VIRSH_MAX_XML_FILE): Rename...
(VSH_MAX_XML_FILE): ...and parenthesize.
(DIFF_MSEC, CTRL_CLOSE_BRACKET): Delete.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Remove prototype.
(editWriteToTempFile, editFile, editReadBackFile, prettyCapacity)
(virshReportError): Rename...
(vshEditWriteToTempFile, vshEditFile, vshEditReadBackFile)
(vshPrettyCapacity, vshReportError): ...into vsh namespace.
(jobWatchTimeoutFunc): Move to virsh-domain.c.
* tools/virsh.c (vshCommandRun): Inline former DIFF_MSEC.
(main): Inline former CTRL_CLOSE_BRACKET.
(vshUsage, vshInit, vshDeinit, vshParseArgv): Make static.
(prettyCapacity, virshReportError, editWriteToTempFile, editFile):
Fix naming, and adjust usage.
(vshAskReedit, vshCommandRun, vshEventLoop, vshInit): Adjust
usage.
* tools/virsh-domain.c (cmdAttachDevice, cmdCPUCompare)
(cmdCPUBaseline, cmdCreate, cmdDefine, cmdDetachDevice)
(cmdUpdateDevice, cmdDesc, cmdUndefine, cmdStart, cmdVcpucount)
(cmdAttachDevice, cmdDomjobinfo): Likewise.
* tools/virsh-edit.c (do): Likewise.
* tools/virsh-interface.c (cmdInterfaceDefine): Likewise.
* tools/virsh-network.c (cmdNetworkCreate, cmdNetworkDefine):
Likewise.
* tools/virsh-nodedev.c (cmdNodeDeviceCreate): Likewise.
* tools/virsh-nwfilter.c (cmdNWFilterDefine): Likewise.
* tools/virsh-pool.c (cmdPoolCreate, cmdPoolDefine)
(cmdPoolDiscoverSources, cmdPoolList): Likewise.
* tools/virsh-secret.c (cmdSecretDefine): Likewise.
* tools/virsh-snapshot.c (cmdSnapshotCreate, vshSnapshotCreate)
(vshLookupSnapshot, cmdSnapshotEdit, cmdSnapshotCurrent)
(vshGetSnapshotParent): Likewise.
* tools/virsh-volume.c (cmdVolCreate, cmdVolCreateFrom)
(cmdVolInfo, cmdVolList): Likewise.
2012-08-19 08:10:17 +04:00
static bool
2015-06-15 19:53:58 +03:00
virshParseArgv ( vshControl * ctl , int argc , char * * argv )
2012-07-23 07:57:53 +04:00
{
2014-03-06 20:20:11 +04:00
int arg , len , debug , keepalive ;
Convert 'int i' to 'size_t i' in tools/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 18:09:33 +04:00
size_t i ;
2013-04-29 21:12:17 +04:00
int longindex = - 1 ;
2015-06-15 19:53:58 +03:00
virshControlPtr priv = ctl - > privData ;
2012-07-23 07:57:53 +04:00
struct option opt [ ] = {
2014-03-06 19:53:53 +04:00
{ " connect " , required_argument , NULL , ' c ' } ,
2012-07-23 07:57:53 +04:00
{ " debug " , required_argument , NULL , ' d ' } ,
2014-03-06 19:53:53 +04:00
{ " escape " , required_argument , NULL , ' e ' } ,
2012-07-23 07:57:53 +04:00
{ " help " , no_argument , NULL , ' h ' } ,
2014-03-06 20:20:11 +04:00
{ " keepalive-interval " , required_argument , NULL , ' k ' } ,
{ " keepalive-count " , required_argument , NULL , ' K ' } ,
2014-03-06 19:53:53 +04:00
{ " log " , required_argument , NULL , ' l ' } ,
2012-07-23 07:57:53 +04:00
{ " quiet " , no_argument , NULL , ' q ' } ,
2014-03-06 19:53:53 +04:00
{ " readonly " , no_argument , NULL , ' r ' } ,
2012-07-23 07:57:53 +04:00
{ " 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 . */
2014-03-06 20:20:11 +04:00
while ( ( arg = getopt_long ( argc , argv , " +:c:d:e:hk:K:l:qrtvV " , opt , & longindex ) ) ! = - 1 ) {
2012-07-23 07:57:53 +04:00
switch ( arg ) {
2014-03-06 19:53:53 +04:00
case ' c ' :
2015-12-10 15:46:45 +03:00
VIR_FREE ( ctl - > connname ) ;
ctl - > connname = vshStrdup ( ctl , optarg ) ;
2014-03-06 19:53:53 +04:00
break ;
2012-07-23 07:57:53 +04:00
case ' d ' :
2012-07-26 18:05:51 +04:00
if ( virStrToLong_i ( optarg , NULL , 10 , & debug ) < 0 ) {
2013-04-29 21:12:17 +04:00
vshError ( ctl , _ ( " option %s takes a numeric argument " ) ,
longindex = = - 1 ? " -d " : " --debug " ) ;
2012-07-23 07:57:53 +04:00
exit ( EXIT_FAILURE ) ;
}
2012-07-26 18:05:51 +04:00
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 ;
2012-07-23 07:57:53 +04:00
break ;
2014-03-06 19:53:53 +04:00
case ' e ' :
len = strlen ( optarg ) ;
if ( ( len = = 2 & & * optarg = = ' ^ ' & &
2015-06-15 19:53:58 +03:00
virshAllowedEscapeChar ( optarg [ 1 ] ) ) | |
2014-03-06 19:53:53 +04:00
( len = = 1 & & * optarg ! = ' ^ ' ) ) {
2015-06-15 19:53:58 +03:00
priv - > escapeChar = optarg ;
2014-03-06 19:53:53 +04:00
} else {
vshError ( ctl , _ ( " Invalid string '%s' for escape sequence " ) ,
optarg ) ;
exit ( EXIT_FAILURE ) ;
}
break ;
2012-07-23 07:57:53 +04:00
case ' h ' :
2015-06-15 19:53:58 +03:00
virshUsage ( ) ;
2012-07-23 07:57:53 +04:00
exit ( EXIT_SUCCESS ) ;
break ;
2014-03-06 20:20:11 +04:00
case ' k ' :
2014-08-27 18:20:29 +04:00
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 " ) ,
2014-03-06 20:20:11 +04:00
longindex = = - 1 ? " -k " : " --keepalive-interval " ) ;
exit ( EXIT_FAILURE ) ;
}
ctl - > keepalive_interval = keepalive ;
break ;
case ' K ' :
2014-08-27 18:20:29 +04:00
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 " ) ,
2014-03-06 20:20:11 +04:00
longindex = = - 1 ? " -K " : " --keepalive-count " ) ;
exit ( EXIT_FAILURE ) ;
}
ctl - > keepalive_count = keepalive ;
break ;
2014-03-06 19:53:53 +04:00
case ' l ' :
vshCloseLogFile ( ctl ) ;
ctl - > logfile = vshStrdup ( ctl , optarg ) ;
vshOpenLogFile ( ctl ) ;
break ;
2012-07-23 07:57:53 +04:00
case ' q ' :
ctl - > quiet = true ;
break ;
case ' t ' :
ctl - > timing = true ;
break ;
2014-03-06 19:53:53 +04:00
case ' r ' :
2015-06-15 19:53:58 +03:00
priv - > readonly = true ;
2012-07-23 07:57:53 +04:00
break ;
case ' v ' :
if ( STRNEQ_NULLABLE ( optarg , " long " ) ) {
puts ( VERSION ) ;
exit ( EXIT_SUCCESS ) ;
}
2017-06-07 11:46:41 +03:00
ATTRIBUTE_FALLTHROUGH ;
2012-07-23 07:57:53 +04:00
case ' V ' :
2015-06-15 19:53:58 +03:00
virshShowVersion ( ctl ) ;
2012-07-23 07:57:53 +04:00
exit ( EXIT_SUCCESS ) ;
2013-02-19 12:50:59 +04:00
case ' : ' :
2013-04-29 21:12:17 +04:00
for ( i = 0 ; opt [ i ] . name ! = NULL ; i + + ) {
2013-05-01 19:07:56 +04:00
if ( opt [ i ] . val = = optopt )
break ;
2013-04-29 21:12:17 +04:00
}
2013-05-01 19:07:56 +04:00
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 ) ;
2013-02-19 12:50:59 +04:00
case ' ? ' :
2013-04-29 21:12:17 +04:00
if ( optopt )
vshError ( ctl , _ ( " unsupported option '-%c'. See --help. " ) , optopt ) ;
else
vshError ( ctl , _ ( " unsupported option '%s'. See --help. " ) , argv [ optind - 1 ] ) ;
2013-02-19 12:50:59 +04:00
exit ( EXIT_FAILURE ) ;
2012-07-23 07:57:53 +04:00
default :
2013-02-19 12:50:59 +04:00
vshError ( ctl , _ ( " unknown option " ) ) ;
2012-07-23 07:57:53 +04:00
exit ( EXIT_FAILURE ) ;
}
2013-04-29 21:12:17 +04:00
longindex = - 1 ;
2012-07-23 07:57:53 +04:00
}
2015-09-04 12:19:55 +03:00
if ( argc = = optind ) {
ctl - > imode = true ;
} else {
2012-07-23 07:57:53 +04:00
/* parse command */
ctl - > imode = false ;
if ( argc - optind = = 1 ) {
vshDebug ( ctl , VSH_ERR_INFO , " commands: \" %s \" \n " , argv [ optind ] ) ;
2017-11-06 17:46:50 +03:00
return vshCommandStringParse ( ctl , argv [ optind ] , NULL ) ;
2012-07-23 07:57:53 +04:00
} else {
return vshCommandArgvParse ( ctl , argc - optind , argv + optind ) ;
}
}
return true ;
}
static const vshCmdDef virshCmds [ ] = {
2015-06-15 19:53:58 +03:00
VSH_CMD_CD ,
VSH_CMD_ECHO ,
VSH_CMD_EXIT ,
VSH_CMD_HELP ,
VSH_CMD_PWD ,
VSH_CMD_QUIT ,
2016-09-14 10:50:22 +03:00
VSH_CMD_SELF_TEST ,
2017-11-01 17:34:14 +03:00
VSH_CMD_COMPLETE ,
2013-03-27 17:22:47 +04:00
{ . name = " connect " ,
. handler = cmdConnect ,
. opts = opts_connect ,
. info = info_connect ,
. flags = VSH_CMD_FLAG_NOCONNECT
2013-02-07 19:25:10 +04:00
} ,
{ . name = NULL }
2012-07-23 07:57:53 +04:00
} ;
2011-11-22 20:08:05 +04:00
2012-07-23 07:57:53 +04:00
static const vshCmdGrp cmdGroups [ ] = {
2015-06-15 19:53:58 +03:00
{ VIRSH_CMD_GRP_DOM_MANAGEMENT , " domain " , domManagementCmds } ,
{ VIRSH_CMD_GRP_DOM_MONITORING , " monitor " , domMonitoringCmds } ,
{ VIRSH_CMD_GRP_HOST_AND_HV , " host " , hostAndHypervisorCmds } ,
{ 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 } ,
2012-07-23 07:57:53 +04:00
{ NULL , NULL , NULL }
} ;
2005-12-08 13:23:34 +03:00
2015-06-15 19:53:58 +03:00
static const vshClientHooks hooks = {
. connHandler = virshConnectionHandler
} ;
2006-03-15 15:13:25 +03:00
int
main ( int argc , char * * argv )
{
vshControl _ctl , * ctl = & _ctl ;
2015-06-15 19:53:58 +03:00
virshControl virshCtl ;
2011-04-19 02:37:42 +04:00
bool ret = true ;
2005-12-08 13:23:34 +03:00
2011-05-09 15:57:09 +04:00
memset ( ctl , 0 , sizeof ( vshControl ) ) ;
2015-06-15 19:53:58 +03:00
memset ( & virshCtl , 0 , sizeof ( virshControl ) ) ;
ctl - > name = " virsh " ; /* hardcoded name of the binary */
2016-07-28 13:54:16 +03:00
ctl - > env_prefix = " VIRSH " ;
2011-05-09 15:57:09 +04:00
ctl - > log_fd = - 1 ; /* Initialize log file descriptor */
2011-07-14 15:58:02 +04:00
ctl - > debug = VSH_DEBUG_DEFAULT ;
2015-06-15 19:53:58 +03:00
ctl - > hooks = & hooks ;
2014-03-06 20:20:11 +04:00
/* In order to distinguish default from setting to 0 */
ctl - > keepalive_interval = - 1 ;
ctl - > keepalive_count = - 1 ;
virsh: common code for waiting for an event
I plan to add 'virsh event' to virsh-domain.c and 'virsh
net-event' to virsh-network.c; but as they will share quite
a bit of common boilerplate, it's better to set that up now
in virsh.c.
* tools/virsh.h (_vshControl): Add fields.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup): New
prototypes.
* tools/virsh.c (vshEventFd, vshEventOldAction, vshEventInt)
(vshEventTimeout): New helper variables and functions.
(vshEventStart, vshEventWait, vshEventDone, vshEventCleanup):
Implement new functions.
(vshInit, vshDeinit, main): Manage event timeout.
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-15 02:30:23 +04:00
ctl - > eventPipe [ 0 ] = - 1 ;
ctl - > eventPipe [ 1 ] = - 1 ;
ctl - > eventTimerId = - 1 ;
2015-06-15 19:53:58 +03:00
virshCtl . escapeChar = " ^] " ; /* Same default as telnet */
ctl - > privData = & virshCtl ;
if ( ! ( progname = strrchr ( argv [ 0 ] , ' / ' ) ) )
progname = argv [ 0 ] ;
else
progname + + ;
ctl - > progname = progname ;
2011-05-09 15:57:09 +04:00
2016-04-13 01:29:52 +03:00
if ( virGettextInitialize ( ) < 0 )
2010-11-16 22:01:37 +03:00
return EXIT_FAILURE ;
2006-09-21 19:24:37 +04:00
2013-08-29 12:36:00 +04:00
if ( isatty ( STDIN_FILENO ) ) {
ctl - > istty = true ;
2013-09-05 01:57:30 +04:00
# ifndef WIN32
2013-08-29 12:36:00 +04:00
if ( tcgetattr ( STDIN_FILENO , & ctl - > termattr ) < 0 )
ctl - > istty = false ;
2013-09-05 01:57:30 +04:00
# endif
2013-08-29 12:36:00 +04:00
}
2011-11-30 23:42:20 +04:00
if ( virMutexInit ( & ctl - > lock ) < 0 ) {
vshError ( ctl , " %s " , _ ( " Failed to initialize mutex " ) ) ;
return EXIT_FAILURE ;
}
2011-05-09 15:57:09 +04:00
if ( virInitialize ( ) < 0 ) {
vshError ( ctl , " %s " , _ ( " Failed to initialize libvirt " ) ) ;
return EXIT_FAILURE ;
}
2014-04-24 18:57:36 +04:00
virFileActivateDirOverride ( argv [ 0 ] ) ;
2015-09-03 17:52:44 +03:00
if ( ! vshInit ( ctl , cmdGroups , NULL ) )
2015-06-15 19:53:58 +03:00
exit ( EXIT_FAILURE ) ;
2006-03-15 15:13:25 +03:00
2015-06-15 19:53:58 +03:00
if ( ! virshParseArgv ( ctl , argc , argv ) | |
! virshInit ( ctl ) ) {
virshDeinit ( ctl ) ;
2005-12-08 13:23:34 +03:00
exit ( EXIT_FAILURE ) ;
2007-12-01 18:45:25 +03:00
}
2006-03-15 15:13:25 +03:00
2016-04-21 11:26:31 +03:00
if ( ! ctl - > connname )
ctl - > connname = vshStrdup ( ctl ,
virGetEnvBlockSUID ( " VIRSH_DEFAULT_CONNECT_URI " ) ) ;
2005-12-08 13:23:34 +03:00
if ( ! ctl - > imode ) {
2006-03-15 15:13:25 +03:00
ret = vshCommandRun ( ctl , ctl - > cmd ) ;
2005-12-01 19:35:42 +03:00
} else {
2005-12-08 13:23:34 +03:00
/* interactive mode */
if ( ! ctl - > quiet ) {
2006-05-22 18:38:33 +04:00
vshPrint ( ctl ,
2006-09-21 19:24:37 +04:00
_ ( " Welcome to %s, the virtualization interactive terminal. \n \n " ) ,
2006-03-15 15:13:25 +03:00
progname ) ;
2008-01-16 20:13:23 +03:00
vshPrint ( ctl , " %s " ,
2006-09-21 19:24:37 +04:00
_ ( " Type: 'help' for help with commands \n "
2007-02-07 16:50:18 +03:00
" 'quit' to quit \n \n " ) ) ;
2005-12-08 13:23:34 +03:00
}
2010-01-03 17:45:10 +03:00
2005-12-08 13:23:34 +03:00
do {
2015-06-15 19:53:58 +03:00
const char * prompt = virshCtl . readonly ? VIRSH_PROMPT_RO
: VIRSH_PROMPT_RW ;
2006-03-15 15:13:25 +03:00
ctl - > cmdstr =
2007-12-06 19:36:21 +03:00
vshReadline ( ctl , prompt ) ;
2006-03-15 15:13:25 +03:00
if ( ctl - > cmdstr = = NULL )
break ; /* EOF */
2005-12-08 13:23:34 +03:00
if ( * ctl - > cmdstr ) {
2013-10-04 21:51:41 +04:00
# if WITH_READLINE
2005-12-08 13:23:34 +03:00
add_history ( ctl - > cmdstr ) ;
2007-12-04 21:27:52 +03:00
# endif
2017-11-06 17:46:50 +03:00
if ( vshCommandStringParse ( ctl , ctl - > cmdstr , NULL ) )
2005-12-08 13:23:34 +03:00
vshCommandRun ( ctl , ctl - > cmd ) ;
}
2010-01-03 19:13:27 +03:00
VIR_FREE ( ctl - > cmdstr ) ;
2006-03-15 15:13:25 +03:00
} while ( ctl - > imode ) ;
2005-12-08 13:23:34 +03:00
2006-03-15 15:13:25 +03:00
if ( ctl - > cmdstr = = NULL )
fputc ( ' \n ' , stdout ) ; /* line break after alone prompt */
2005-12-08 13:23:34 +03:00
}
2006-03-15 15:13:25 +03:00
2015-06-15 19:53:58 +03:00
virshDeinit ( ctl ) ;
2005-12-08 13:23:34 +03:00
exit ( ret ? EXIT_SUCCESS : EXIT_FAILURE ) ;
2005-11-10 19:12:31 +03:00
}