2009-07-28 18:46:53 +04:00
/*
Copyright Red Hat , Inc . 2006
This program is free software ; you can redistribute it and / or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation ; either version 2 , or ( at your option ) any
later version .
This program 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
General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; see the file COPYING . If not , write to the
Free Software Foundation , Inc . , 675 Mass Ave , Cambridge ,
MA 0213 9 , USA .
*/
# include <stdio.h>
# include <sys/types.h>
# include <stdlib.h>
# include <libvirt/libvirt.h>
# include <string.h>
# include <malloc.h>
# include <stdint.h>
# include <errno.h>
# include "virt.h"
static int
_compare_virt ( const void * _left , const void * _right )
{
virt_state_t * left = ( virt_state_t * ) _left ,
* right = ( virt_state_t * ) _right ;
return strcasecmp ( left - > v_name , right - > v_name ) ;
}
virt_list_t * vl_get ( virConnectPtr vp , int my_id )
{
virt_list_t * vl = NULL ;
2014-06-23 23:54:56 +04:00
int d_count , x , saved_errno ;
2014-06-23 07:29:50 +04:00
virDomainPtr * dom_list ;
2009-07-28 18:46:53 +04:00
errno = EINVAL ;
if ( ! vp )
return NULL ;
2014-06-23 07:29:50 +04:00
d_count = virConnectListAllDomains ( vp , & dom_list , 0 ) ;
if ( d_count < = 0 )
2009-07-28 18:46:53 +04:00
goto out_fail ;
2014-06-23 07:29:50 +04:00
vl = malloc ( sizeof ( uint32_t ) + sizeof ( virt_state_t ) * d_count ) ;
2009-07-28 18:46:53 +04:00
if ( ! vl )
goto out_fail ;
vl - > vm_count = d_count ;
/* Ok, we have the domain IDs - let's get their names and states */
for ( x = 0 ; x < d_count ; x + + ) {
2014-06-23 07:29:50 +04:00
char * d_name ;
virDomainInfo d_info ;
char d_uuid [ MAX_DOMAINNAME_LENGTH ] ;
virDomainPtr dom = dom_list [ x ] ;
2009-07-28 18:46:53 +04:00
2014-06-23 07:29:50 +04:00
if ( ! ( d_name = ( char * ) virDomainGetName ( dom ) ) )
2009-07-28 18:46:53 +04:00
goto out_fail ;
2014-06-23 07:29:50 +04:00
if ( virDomainGetUUIDString ( dom , d_uuid ) ! = 0 )
2009-07-28 18:46:53 +04:00
goto out_fail ;
2014-06-23 07:29:50 +04:00
if ( virDomainGetInfo ( dom , & d_info ) < 0 )
2009-07-28 18:46:53 +04:00
goto out_fail ;
/* Store the name & state */
strncpy ( vl - > vm_states [ x ] . v_name , d_name , MAX_DOMAINNAME_LENGTH ) ;
strncpy ( vl - > vm_states [ x ] . v_uuid , d_uuid , MAX_DOMAINNAME_LENGTH ) ;
vl - > vm_states [ x ] . v_state . s_state = d_info . state ;
vl - > vm_states [ x ] . v_state . s_owner = my_id ;
}
2014-06-23 07:29:50 +04:00
for ( x = 0 ; x < d_count ; x + + )
virDomainFree ( dom_list [ x ] ) ;
free ( dom_list ) ;
2009-07-28 18:46:53 +04:00
/* We have all the locally running domains & states now */
/* Sort */
qsort ( & vl - > vm_states [ 0 ] , vl - > vm_count , sizeof ( vl - > vm_states [ 0 ] ) ,
_compare_virt ) ;
return vl ;
out_fail :
2014-06-23 23:54:56 +04:00
saved_errno = errno ;
2014-06-23 07:29:50 +04:00
for ( x = 0 ; x < d_count ; x + + )
virDomainFree ( dom_list [ x ] ) ;
free ( dom_list ) ;
2009-07-28 18:46:53 +04:00
if ( vl )
free ( vl ) ;
2014-06-23 23:54:56 +04:00
errno = saved_errno ;
2009-07-28 18:46:53 +04:00
return NULL ;
}
/* Returns 0 if equal, nonzero if not */
int
vl_cmp ( virt_list_t * left , virt_list_t * right )
{
int x ;
/* Quick checks */
if ( ! left - > vm_count & & ! right - > vm_count )
return 1 ;
if ( left - > vm_count ! = right - > vm_count )
return 0 ;
for ( x = 0 ; x < left - > vm_count ; x + + ) {
if ( strcmp ( left - > vm_states [ x ] . v_name ,
right - > vm_states [ x ] . v_name ) )
return 1 ;
/*
if ( left - > vm_states [ x ] . v_state . s_state ! =
right - > vm_states [ x ] . v_state . s_state )
return 1 ;
*/
}
return 0 ;
}
void
vl_print ( virt_list_t * vl )
{
int x ;
printf ( " %-24.24s %-36.36s %-5.5s %-5.5s \n " , " Domain " , " UUID " ,
" Owner " , " State " ) ;
printf ( " %-24.24s %-36.36s %-5.5s %-5.5s \n " , " ------ " , " ---- " ,
" ----- " , " ----- " ) ;
if ( ! vl | | ! vl - > vm_count )
return ;
for ( x = 0 ; x < vl - > vm_count ; x + + ) {
printf ( " %-24.24s %-36.36s %-5.5d %-5.5d \n " ,
vl - > vm_states [ x ] . v_name ,
vl - > vm_states [ x ] . v_uuid ,
vl - > vm_states [ x ] . v_state . s_owner ,
vl - > vm_states [ x ] . v_state . s_state ) ;
}
}
virt_state_t *
2009-09-17 04:07:46 +04:00
vl_find_name ( virt_list_t * vl , const char * name )
2009-07-28 18:46:53 +04:00
{
int x ;
if ( ! vl | | ! name | | ! vl - > vm_count )
return NULL ;
for ( x = 0 ; x < vl - > vm_count ; x + + ) {
if ( ! strcasecmp ( vl - > vm_states [ x ] . v_name , name ) )
return & vl - > vm_states [ x ] ;
}
return NULL ;
}
virt_state_t *
2009-09-17 04:07:46 +04:00
vl_find_uuid ( virt_list_t * vl , const char * uuid )
2009-07-28 18:46:53 +04:00
{
int x ;
if ( ! vl | | ! uuid | | ! vl - > vm_count )
return NULL ;
for ( x = 0 ; x < vl - > vm_count ; x + + ) {
if ( ! strcasecmp ( vl - > vm_states [ x ] . v_uuid , uuid ) )
return & vl - > vm_states [ x ] ;
}
return NULL ;
}
void
vl_free ( virt_list_t * old )
{
free ( old ) ;
}