2001-12-05 19:41:52 +03:00
/*
2004-03-30 23:08:57 +04:00
* Copyright ( C ) 2001 - 2004 Sistina Software , Inc . All rights reserved .
2005-01-06 21:22:44 +03:00
* Copyright ( C ) 2004 - 2005 Red Hat , Inc . All rights reserved .
2001-12-05 19:41:52 +03:00
*
2004-03-30 23:08:57 +04:00
* This file is part of the device - mapper userspace tools .
*
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License v .2 .1 .
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program ; if not , write to the Free Software Foundation ,
* Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307 USA
2001-12-05 19:41:52 +03:00
*/
2005-01-06 21:22:44 +03:00
# include "lib.h"
2002-11-14 17:44:42 +03:00
# include "libdm-targets.h"
# include "libdm-common.h"
2003-07-02 01:20:58 +04:00
# include "list.h"
2004-07-01 19:14:29 +04:00
# include "kdev_t.h"
2001-12-05 19:41:52 +03:00
# include <stdarg.h>
# include <sys/param.h>
2002-11-14 17:44:42 +03:00
# include <linux/dm-ioctl.h>
2001-12-05 19:41:52 +03:00
2004-04-06 22:54:00 +04:00
# ifdef HAVE_SELINUX
# include <selinux / selinux.h>
# endif
2001-12-05 19:41:52 +03:00
# define DEV_DIR " / dev / "
static char _dm_dir [ PATH_MAX ] = DEV_DIR DM_DIR ;
2003-01-22 00:25:11 +03:00
static int _verbose = 0 ;
2002-01-03 13:39:21 +03:00
/*
2001-12-05 19:41:52 +03:00
* Library users can provide their own logging
* function .
*/
2002-11-14 22:26:28 +03:00
static void _default_log ( int level , const char * file , int line ,
const char * f , . . . )
2001-12-05 19:41:52 +03:00
{
2002-03-19 02:39:42 +03:00
va_list ap ;
2001-12-05 19:41:52 +03:00
2003-01-22 00:25:11 +03:00
if ( level > _LOG_WARN & & ! _verbose )
2002-03-14 16:39:33 +03:00
return ;
2002-03-19 02:39:42 +03:00
va_start ( ap , f ) ;
2002-03-14 16:39:33 +03:00
2003-01-22 00:25:11 +03:00
if ( level < _LOG_WARN )
2002-03-19 02:39:42 +03:00
vfprintf ( stderr , f , ap ) ;
2003-01-22 00:25:11 +03:00
else
vprintf ( f , ap ) ;
2002-03-14 16:39:33 +03:00
2002-03-19 02:39:42 +03:00
va_end ( ap ) ;
2001-12-05 19:41:52 +03:00
2003-01-22 00:25:11 +03:00
if ( level < _LOG_WARN )
fprintf ( stderr , " \n " ) ;
else
2004-10-01 23:07:41 +04:00
fprintf ( stdout , " \n " ) ;
2001-12-05 19:41:52 +03:00
}
dm_log_fn _log = _default_log ;
void dm_log_init ( dm_log_fn fn )
{
2003-12-21 19:08:20 +03:00
if ( fn )
_log = fn ;
else
_log = _default_log ;
2001-12-05 19:41:52 +03:00
}
2003-01-22 00:25:11 +03:00
void dm_log_init_verbose ( int level )
{
_verbose = level ;
}
2002-11-14 22:26:28 +03:00
static void _build_dev_path ( char * buffer , size_t len , const char * dev_name )
2001-12-14 16:30:04 +03:00
{
/* If there's a /, assume caller knows what they're doing */
if ( strchr ( dev_name , ' / ' ) )
snprintf ( buffer , len , " %s " , dev_name ) ;
else
2002-03-19 02:39:42 +03:00
snprintf ( buffer , len , " %s/%s " , _dm_dir , dev_name ) ;
2001-12-14 16:30:04 +03:00
}
2002-01-17 17:13:25 +03:00
int dm_get_library_version ( char * version , size_t size )
{
strncpy ( version , DM_LIB_VERSION , size ) ;
return 1 ;
}
2001-12-05 19:41:52 +03:00
struct dm_task * dm_task_create ( int type )
{
2002-03-19 02:39:42 +03:00
struct dm_task * dmt = malloc ( sizeof ( * dmt ) ) ;
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
if ( ! dmt ) {
log_error ( " dm_task_create: malloc(%d) failed " , sizeof ( * dmt ) ) ;
return NULL ;
}
2001-12-05 19:41:52 +03:00
2005-01-06 21:22:44 +03:00
if ( ! dm_check_version ( ) )
return NULL ;
2002-03-19 02:39:42 +03:00
memset ( dmt , 0 , sizeof ( * dmt ) ) ;
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
dmt - > type = type ;
2002-01-11 15:12:46 +03:00
dmt - > minor = - 1 ;
2003-04-02 23:03:00 +04:00
dmt - > major = - 1 ;
2005-01-06 01:00:40 +03:00
dmt - > uid = DEVICE_UID ;
dmt - > gid = DEVICE_GID ;
dmt - > mode = DEVICE_MODE ;
2003-01-22 00:25:11 +03:00
2002-03-19 02:39:42 +03:00
return dmt ;
2001-12-05 19:41:52 +03:00
}
int dm_task_set_name ( struct dm_task * dmt , const char * name )
{
2001-12-14 16:30:04 +03:00
char * pos ;
char path [ PATH_MAX ] ;
struct stat st1 , st2 ;
2002-03-19 02:39:42 +03:00
if ( dmt - > dev_name ) {
free ( dmt - > dev_name ) ;
2002-03-12 01:44:36 +03:00
dmt - > dev_name = NULL ;
}
2001-12-05 19:41:52 +03:00
2001-12-14 16:30:04 +03:00
/* If path was supplied, remove it if it points to the same device
* as its last component .
*/
if ( ( pos = strrchr ( name , ' / ' ) ) ) {
2002-01-17 16:19:55 +03:00
snprintf ( path , sizeof ( path ) , " %s/%s " , _dm_dir , pos + 1 ) ;
2001-12-14 16:30:04 +03:00
if ( stat ( name , & st1 ) | | stat ( path , & st2 ) | |
! ( st1 . st_dev = = st2 . st_dev ) ) {
2002-03-19 02:39:42 +03:00
log_error ( " dm_task_set_name: Device %s not found " ,
name ) ;
2001-12-14 16:30:04 +03:00
return 0 ;
}
name = pos + 1 ;
}
2002-03-19 02:39:42 +03:00
if ( ! ( dmt - > dev_name = strdup ( name ) ) ) {
2002-01-18 22:37:26 +03:00
log_error ( " dm_task_set_name: strdup(%s) failed " , name ) ;
2001-12-14 16:30:04 +03:00
return 0 ;
}
2002-03-19 02:39:42 +03:00
return 1 ;
2001-12-05 19:41:52 +03:00
}
2002-03-12 01:44:36 +03:00
int dm_task_set_uuid ( struct dm_task * dmt , const char * uuid )
{
if ( dmt - > uuid ) {
free ( dmt - > uuid ) ;
dmt - > uuid = NULL ;
}
if ( ! ( dmt - > uuid = strdup ( uuid ) ) ) {
log_error ( " dm_task_set_uuid: strdup(%s) failed " , uuid ) ;
return 0 ;
}
return 1 ;
}
2003-04-02 23:03:00 +04:00
int dm_task_set_major ( struct dm_task * dmt , int major )
{
dmt - > major = major ;
log_debug ( " Setting major: %d " , dmt - > major ) ;
return 1 ;
}
2002-01-11 15:12:46 +03:00
int dm_task_set_minor ( struct dm_task * dmt , int minor )
{
2002-03-19 02:39:42 +03:00
dmt - > minor = minor ;
log_debug ( " Setting minor: %d " , dmt - > minor ) ;
2002-01-11 15:12:46 +03:00
2002-03-19 02:39:42 +03:00
return 1 ;
}
2002-01-11 15:12:46 +03:00
2002-03-19 02:39:42 +03:00
int dm_task_add_target ( struct dm_task * dmt , uint64_t start , uint64_t size ,
const char * ttype , const char * params )
2001-12-05 19:41:52 +03:00
{
2002-03-19 02:39:42 +03:00
struct target * t = create_target ( start , size , ttype , params ) ;
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
if ( ! t )
return 0 ;
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
if ( ! dmt - > head )
dmt - > head = dmt - > tail = t ;
else {
dmt - > tail - > next = t ;
dmt - > tail = t ;
}
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
return 1 ;
2001-12-05 19:41:52 +03:00
}
2004-04-06 22:54:00 +04:00
# ifdef HAVE_SELINUX
2005-01-06 21:22:44 +03:00
int set_selinux_context ( const char * path )
2004-04-06 22:54:00 +04:00
{
security_context_t scontext ;
log_debug ( " Setting SELinux context for %s " , path ) ;
if ( is_selinux_enabled ( ) < = 0 )
2004-07-03 22:14:12 +04:00
return 1 ;
2004-04-06 22:54:00 +04:00
if ( matchpathcon ( path , 0 , & scontext ) < 0 ) {
log_error ( " %s: matchpathcon failed: %s " , path , strerror ( errno ) ) ;
return 0 ;
}
2004-04-16 16:24:46 +04:00
if ( ( lsetfilecon ( path , scontext ) < 0 ) & & ( errno ! = ENOTSUP ) ) {
2004-04-06 22:54:00 +04:00
log_error ( " %s: lsetfilecon failed: %s " , path , strerror ( errno ) ) ;
free ( scontext ) ;
return 0 ;
}
free ( scontext ) ;
return 1 ;
}
# endif
2005-01-06 01:00:40 +03:00
static int _add_dev_node ( const char * dev_name , uint32_t major , uint32_t minor ,
uid_t uid , gid_t gid , mode_t mode )
2001-12-05 19:41:52 +03:00
{
2002-03-19 02:39:42 +03:00
char path [ PATH_MAX ] ;
struct stat info ;
2003-01-22 00:25:11 +03:00
dev_t dev = MKDEV ( major , minor ) ;
2005-01-06 01:00:40 +03:00
mode_t old_mask ;
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
_build_dev_path ( path , sizeof ( path ) , dev_name ) ;
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
if ( stat ( path , & info ) > = 0 ) {
if ( ! S_ISBLK ( info . st_mode ) ) {
log_error ( " A non-block device file at '%s' "
" is already present " , path ) ;
return 0 ;
}
2001-12-05 19:41:52 +03:00
2005-01-06 01:00:40 +03:00
/* If right inode already exists we don't touch uid etc. */
2002-03-19 02:39:42 +03:00
if ( info . st_rdev = = dev )
return 1 ;
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
if ( unlink ( path ) < 0 ) {
log_error ( " Unable to unlink device node for '%s' " ,
dev_name ) ;
return 0 ;
}
}
2001-12-05 19:41:52 +03:00
2005-01-06 01:00:40 +03:00
old_mask = umask ( 0 ) ;
if ( mknod ( path , S_IFBLK | mode , dev ) < 0 ) {
2002-03-19 02:39:42 +03:00
log_error ( " Unable to make device node for '%s' " , dev_name ) ;
return 0 ;
}
2005-01-06 01:00:40 +03:00
umask ( old_mask ) ;
if ( chown ( path , uid , gid ) < 0 ) {
log_error ( " %s: chown failed: %s " , path , strerror ( errno ) ) ;
return 0 ;
}
2004-04-06 22:54:00 +04:00
# ifdef HAVE_SELINUX
2005-01-06 21:22:44 +03:00
if ( ! set_selinux_context ( path ) )
2004-04-06 22:54:00 +04:00
return 0 ;
# endif
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
return 1 ;
2001-12-05 19:41:52 +03:00
}
2003-07-02 01:20:58 +04:00
static int _rename_dev_node ( const char * old_name , const char * new_name )
2002-04-11 16:45:18 +04:00
{
char oldpath [ PATH_MAX ] ;
char newpath [ PATH_MAX ] ;
struct stat info ;
_build_dev_path ( oldpath , sizeof ( oldpath ) , old_name ) ;
_build_dev_path ( newpath , sizeof ( newpath ) , new_name ) ;
2002-04-24 01:47:50 +04:00
if ( stat ( newpath , & info ) = = 0 ) {
2002-04-11 16:45:18 +04:00
if ( ! S_ISBLK ( info . st_mode ) ) {
log_error ( " A non-block device file at '%s' "
" is already present " , newpath ) ;
return 0 ;
}
if ( unlink ( newpath ) < 0 ) {
2003-01-22 00:25:11 +03:00
if ( errno = = EPERM ) {
2002-04-24 01:47:50 +04:00
/* devfs, entry has already been renamed */
return 1 ;
2002-04-11 16:45:18 +04:00
}
log_error ( " Unable to unlink device node for '%s' " ,
new_name ) ;
return 0 ;
}
}
if ( rename ( oldpath , newpath ) < 0 ) {
2002-04-24 01:47:50 +04:00
log_error ( " Unable to rename device node from '%s' to '%s' " ,
old_name , new_name ) ;
2002-04-11 16:45:18 +04:00
return 0 ;
}
return 1 ;
}
2003-07-02 01:20:58 +04:00
static int _rm_dev_node ( const char * dev_name )
2001-12-05 19:41:52 +03:00
{
2002-03-19 02:39:42 +03:00
char path [ PATH_MAX ] ;
struct stat info ;
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
_build_dev_path ( path , sizeof ( path ) , dev_name ) ;
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
if ( stat ( path , & info ) < 0 )
return 1 ;
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
if ( unlink ( path ) < 0 ) {
log_error ( " Unable to unlink device node for '%s' " , dev_name ) ;
return 0 ;
}
2001-12-05 19:41:52 +03:00
2002-03-19 02:39:42 +03:00
return 1 ;
2001-12-05 19:41:52 +03:00
}
2003-07-02 01:20:58 +04:00
typedef enum {
NODE_ADD ,
NODE_DEL ,
NODE_RENAME
} node_op_t ;
static int _do_node_op ( node_op_t type , const char * dev_name , uint32_t major ,
2005-01-06 01:00:40 +03:00
uint32_t minor , uid_t uid , gid_t gid , mode_t mode ,
const char * old_name )
2003-07-02 01:20:58 +04:00
{
switch ( type ) {
case NODE_ADD :
2005-01-06 01:00:40 +03:00
return _add_dev_node ( dev_name , major , minor , uid , gid , mode ) ;
2003-07-02 01:20:58 +04:00
case NODE_DEL :
return _rm_dev_node ( dev_name ) ;
case NODE_RENAME :
return _rename_dev_node ( old_name , dev_name ) ;
}
return 1 ;
}
static LIST_INIT ( _node_ops ) ;
struct node_op_parms {
struct list list ;
node_op_t type ;
char * dev_name ;
uint32_t major ;
uint32_t minor ;
2005-01-06 01:00:40 +03:00
uid_t uid ;
gid_t gid ;
mode_t mode ;
2003-07-02 01:20:58 +04:00
char * old_name ;
char names [ 0 ] ;
} ;
static void _store_str ( char * * pos , char * * ptr , const char * str )
{
strcpy ( * pos , str ) ;
* ptr = * pos ;
* pos + = strlen ( * ptr ) + 1 ;
}
static int _stack_node_op ( node_op_t type , const char * dev_name , uint32_t major ,
2005-01-06 01:00:40 +03:00
uint32_t minor , uid_t uid , gid_t gid , mode_t mode ,
const char * old_name )
2003-07-02 01:20:58 +04:00
{
struct node_op_parms * nop ;
size_t len = strlen ( dev_name ) + strlen ( old_name ) + 2 ;
char * pos ;
if ( ! ( nop = malloc ( sizeof ( * nop ) + len ) ) ) {
log_error ( " Insufficient memory to stack mknod operation " ) ;
return 0 ;
}
pos = nop - > names ;
nop - > type = type ;
nop - > major = major ;
nop - > minor = minor ;
2005-01-06 01:00:40 +03:00
nop - > uid = uid ;
nop - > gid = gid ;
nop - > mode = mode ;
2003-07-02 01:20:58 +04:00
_store_str ( & pos , & nop - > dev_name , dev_name ) ;
_store_str ( & pos , & nop - > old_name , old_name ) ;
list_add ( & _node_ops , & nop - > list ) ;
return 1 ;
}
static void _pop_node_ops ( void )
{
struct list * noph , * nopht ;
struct node_op_parms * nop ;
list_iterate_safe ( noph , nopht , & _node_ops ) {
nop = list_item ( noph , struct node_op_parms ) ;
_do_node_op ( nop - > type , nop - > dev_name , nop - > major , nop - > minor ,
2005-01-06 01:00:40 +03:00
nop - > uid , nop - > gid , nop - > mode , nop - > old_name ) ;
2003-07-02 01:20:58 +04:00
list_del ( & nop - > list ) ;
free ( nop ) ;
}
}
2005-01-06 01:00:40 +03:00
int add_dev_node ( const char * dev_name , uint32_t major , uint32_t minor ,
uid_t uid , gid_t gid , mode_t mode )
2003-07-02 01:20:58 +04:00
{
2005-01-06 01:00:40 +03:00
return _stack_node_op ( NODE_ADD , dev_name , major , minor , uid , gid , mode ,
" " ) ;
2003-07-02 01:20:58 +04:00
}
int rename_dev_node ( const char * old_name , const char * new_name )
{
2005-01-06 01:00:40 +03:00
return _stack_node_op ( NODE_RENAME , new_name , 0 , 0 , 0 , 0 , 0 , old_name ) ;
2003-07-02 01:20:58 +04:00
}
int rm_dev_node ( const char * dev_name )
{
2005-01-06 01:00:40 +03:00
return _stack_node_op ( NODE_DEL , dev_name , 0 , 0 , 0 , 0 , 0 , " " ) ;
2003-07-02 01:20:58 +04:00
}
void update_devs ( void )
{
_pop_node_ops ( ) ;
}
2001-12-05 19:41:52 +03:00
int dm_set_dev_dir ( const char * dir )
{
2002-03-19 02:39:42 +03:00
snprintf ( _dm_dir , sizeof ( _dm_dir ) , " %s%s " , dir , DM_DIR ) ;
return 1 ;
2001-12-05 19:41:52 +03:00
}
const char * dm_dir ( void )
{
2002-03-19 02:39:42 +03:00
return _dm_dir ;
2001-12-05 19:41:52 +03:00
}