2003-10-16 00:17:19 +04:00
/*
2008-01-30 17:00:02 +03:00
* Copyright ( C ) 2003 - 2004 Sistina Software , Inc . All rights reserved .
2015-11-26 23:52:22 +03:00
* Copyright ( C ) 2004 - 2015 Red Hat , Inc . All rights reserved .
2003-10-16 00:17:19 +04:00
*
2004-03-30 23:35:44 +04:00
* This file is part of LVM2 .
2003-10-16 00:17:19 +04:00
*
2004-03-30 23:35:44 +04:00
* This copyrighted material is made available to anyone wishing to use ,
* modify , copy , or redistribute it subject to the terms and conditions
2007-08-21 00:55:30 +04:00
* of the GNU Lesser General Public License v .2 .1 .
2003-10-16 00:17:19 +04:00
*
2007-08-21 00:55:30 +04:00
* You should have received a copy of the GNU Lesser General Public License
2004-03-30 23:35:44 +04:00
* along with this program ; if not , write to the Free Software Foundation ,
2016-01-21 13:49:46 +03:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
2003-10-16 00:17:19 +04:00
*/
# include "tools.h"
2013-07-08 17:34:27 +04:00
static int _get_vsn ( struct cmd_context * cmd , uint16_t * version_int )
2013-03-05 20:48:29 +04:00
{
2015-11-26 23:52:22 +03:00
const char * vsn ;
2013-07-08 15:45:37 +04:00
unsigned int major , minor , patchlevel ;
2013-03-05 20:48:29 +04:00
2016-07-12 17:41:25 +03:00
if ( ! ( vsn = arg_str_value ( cmd , atversion_ARG , NULL ) ) & &
! ( vsn = arg_str_value ( cmd , sinceversion_ARG , NULL ) ) )
2015-11-26 23:52:22 +03:00
vsn = LVM_VERSION ;
2015-11-25 16:09:20 +03:00
2015-11-26 23:52:22 +03:00
if ( sscanf ( vsn , " %u.%u.%u " , & major , & minor , & patchlevel ) ! = 3 ) {
2013-03-05 20:48:29 +04:00
log_error ( " Incorrect version format. " ) ;
return 0 ;
}
2013-07-08 17:34:27 +04:00
* version_int = vsn ( major , minor , patchlevel ) ;
2013-03-05 20:48:29 +04:00
return 1 ;
}
2014-03-24 16:19:15 +04:00
static int _do_def_check ( struct config_def_tree_spec * spec ,
struct dm_config_tree * cft ,
2013-07-08 17:34:27 +04:00
struct cft_check_handle * * cft_check_handle )
2013-07-08 16:13:09 +04:00
{
struct cft_check_handle * handle ;
2014-05-19 15:23:12 +04:00
if ( ! ( handle = get_config_tree_check_handle ( spec - > cmd , cft ) ) )
2013-07-08 16:13:09 +04:00
return 0 ;
handle - > force_check = 1 ;
handle - > suppress_messages = 1 ;
2014-05-19 16:51:11 +04:00
if ( spec - > type = = CFG_DEF_TREE_DIFF ) {
if ( ! handle - > check_diff )
handle - > skip_if_checked = 0 ;
2014-03-24 16:19:15 +04:00
handle - > check_diff = 1 ;
2014-05-19 16:51:11 +04:00
} else {
2014-03-24 16:19:15 +04:00
handle - > skip_if_checked = 1 ;
2014-05-19 16:51:11 +04:00
handle - > check_diff = 0 ;
}
2014-03-24 16:19:15 +04:00
2015-04-29 17:07:52 +03:00
handle - > ignoreunsupported = spec - > ignoreunsupported ;
handle - > ignoreadvanced = spec - > ignoreadvanced ;
2014-03-19 11:45:05 +04:00
config_def_check ( handle ) ;
2013-07-08 16:13:09 +04:00
* cft_check_handle = handle ;
return 1 ;
}
2013-07-08 17:34:27 +04:00
static int _merge_config_cascade ( struct cmd_context * cmd , struct dm_config_tree * cft_cascaded ,
struct dm_config_tree * * cft_merged )
{
if ( ! cft_cascaded )
return 1 ;
if ( ! * cft_merged & & ! ( * cft_merged = config_open ( CONFIG_MERGED_FILES , NULL , 0 ) ) )
return_0 ;
if ( ! _merge_config_cascade ( cmd , cft_cascaded - > cascade , cft_merged ) )
return_0 ;
return merge_config_tree ( cmd , * cft_merged , cft_cascaded , CONFIG_MERGE_TYPE_RAW ) ;
}
2014-05-19 15:23:12 +04:00
static int _config_validate ( struct cmd_context * cmd , struct dm_config_tree * cft )
{
struct cft_check_handle * handle ;
if ( ! ( handle = get_config_tree_check_handle ( cmd , cft ) ) )
return 1 ;
handle - > force_check = 1 ;
handle - > skip_if_checked = 1 ;
handle - > suppress_messages = 0 ;
return config_def_check ( handle ) ;
}
2003-10-16 00:17:19 +04:00
int dumpconfig ( struct cmd_context * cmd , int argc , char * * argv )
{
2009-11-03 18:50:42 +03:00
const char * file = arg_str_value ( cmd , file_ARG , NULL ) ;
2016-06-22 00:24:52 +03:00
const char * type = arg_str_value ( cmd , configtype_ARG , arg_is_set ( cmd , list_ARG ) ? " list " : " current " ) ;
2013-03-05 20:48:29 +04:00
struct config_def_tree_spec tree_spec = { 0 } ;
2013-07-08 15:45:37 +04:00
struct dm_config_tree * cft = NULL ;
2013-07-08 16:13:09 +04:00
struct cft_check_handle * cft_check_handle = NULL ;
2014-05-20 16:45:20 +04:00
struct profile * profile = NULL ;
2013-03-05 20:48:29 +04:00
int r = ECMD_PROCESSED ;
config: add CFG_DEFAULT_RUN_TIME for config options with runtime defaults
Previously, we declared a default value as undefined ("NULL") for
settings which require runtime context to be set first (e.g. settings
for paths that rely on SYSTEM_DIR environment variable or they depend
on any other setting in some way).
If we want to output default values as they are really used in runtime,
we should make it possible to define a default value as function which
is evaluated, not just providing a firm constant value as it was before.
This patch defines simple prototypes for such functions. Also, there's
new helper macros "cfg_runtime" and "cfg_array_runtime" - they provide
exactly the same functionality as the original "cfg" and "cfg_array"
macros when defining the configuration settings in config_settings.h,
but they don't set the constant default value. Instead, they automatically
link the configuration setting definition with one of these functions:
typedef int (*t_fn_CFG_TYPE_BOOL) (struct cmd_context *cmd, struct profile *profile);
typedef int (*t_fn_CFG_TYPE_INT) (struct cmd_context *cmd, struct profile *profile);
typedef float (*t_fn_CFG_TYPE_FLOAT) (struct cmd_context *cmd, struct profile *profile);
typedef const char* (*t_fn_CFG_TYPE_STRING) (struct cmd_context *cmd, struct profile *profile);
typedef const char* (*t_fn_CFG_TYPE_ARRAY) (struct cmd_context *cmd, struct profile *profile);
(The new macros actually set the CFG_DEFAULT_RUNTIME flag properly and
set the default value link to the function accordingly).
Then such configuration setting requires a function of selected type to
be defined. This function has a predefined name:
get_default_<id>
...where the <id> is the id of the setting as defined in
config_settings.h. For example "backup_archive_dir_CFG" if defined
as a setting with default value evaluated in runtime with "cfg_runtime"
will automatically have "get_default_backup_archive_dir_CFG" function
linked to this setting to get the default value.
2014-03-03 15:34:11 +04:00
tree_spec . cmd = cmd ;
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , configtype_ARG ) & & arg_is_set ( cmd , validate_ARG ) ) {
2013-03-05 20:48:29 +04:00
log_error ( " Only one of --type and --validate permitted. " ) ;
return EINVALID_CMD_LINE ;
}
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , configtype_ARG ) & & arg_is_set ( cmd , list_ARG ) ) {
2015-04-29 12:11:58 +03:00
log_error ( " Only one of --type and --list permitted. " ) ;
return EINVALID_CMD_LINE ;
}
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , atversion_ARG ) ) {
if ( arg_is_set ( cmd , sinceversion_ARG ) ) {
2015-11-25 16:09:20 +03:00
log_error ( " Only one of --atversion and --sinceversion permitted. " ) ;
return EINVALID_CMD_LINE ;
}
2016-06-22 00:24:52 +03:00
if ( ! arg_is_set ( cmd , configtype_ARG ) & & ! arg_is_set ( cmd , list_ARG ) ) {
2015-11-25 16:09:20 +03:00
log_error ( " --atversion requires --type or --list " ) ;
return EINVALID_CMD_LINE ;
}
2016-06-22 00:24:52 +03:00
} else if ( arg_is_set ( cmd , sinceversion_ARG ) ) {
if ( ! arg_is_set ( cmd , configtype_ARG ) | | strcmp ( type , " new " ) ) {
2015-11-25 16:09:20 +03:00
log_error ( " --sinceversion requires --type new " ) ;
return EINVALID_CMD_LINE ;
}
2013-03-05 20:48:29 +04:00
}
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , ignoreadvanced_ARG ) )
2013-03-06 12:35:33 +04:00
tree_spec . ignoreadvanced = 1 ;
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , ignoreunsupported_ARG ) ) {
if ( arg_is_set ( cmd , showunsupported_ARG ) ) {
2015-04-29 17:07:52 +03:00
log_error ( " Only one of --ignoreunsupported and --showunsupported permitted. " ) ;
return EINVALID_CMD_LINE ;
}
2013-03-06 12:35:33 +04:00
tree_spec . ignoreunsupported = 1 ;
2016-06-22 00:24:52 +03:00
} else if ( arg_is_set ( cmd , showunsupported_ARG ) ) {
2015-04-29 17:07:52 +03:00
tree_spec . ignoreunsupported = 0 ;
} else if ( strcmp ( type , " current " ) & & strcmp ( type , " diff " ) ) {
/*
* By default hide unsupported settings
* for all display types except " current "
* and " diff " .
*/
tree_spec . ignoreunsupported = 1 ;
}
2013-03-06 12:35:33 +04:00
2015-04-30 18:40:24 +03:00
if ( strcmp ( type , " current " ) & & strcmp ( type , " diff " ) ) {
/*
* By default hide deprecated settings
* for all display types except " current "
* and " diff " unless - - showdeprecated is set .
*
* N . B . Deprecated settings are visible if
* - - atversion is used with a version that
* is lower than the version in which the
* setting was deprecated .
*/
2016-06-22 00:24:52 +03:00
if ( ! arg_is_set ( cmd , showdeprecated_ARG ) )
2015-04-30 18:40:24 +03:00
tree_spec . ignoredeprecated = 1 ;
}
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , ignorelocal_ARG ) )
2015-04-21 22:53:54 +03:00
tree_spec . ignorelocal = 1 ;
2015-06-25 11:51:30 +03:00
if ( ! strcmp ( type , " current " ) | | ! strcmp ( type , " full " ) ) {
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , atversion_ARG ) ) {
2015-06-25 11:51:30 +03:00
log_error ( " --atversion has no effect with --type %s " , type ) ;
2013-07-08 15:45:37 +04:00
return EINVALID_CMD_LINE ;
}
2016-06-22 00:24:52 +03:00
if ( ( arg_is_set ( cmd , ignoreunsupported_ARG ) | |
arg_is_set ( cmd , ignoreadvanced_ARG ) ) & &
2015-06-25 11:51:30 +03:00
! strcmp ( type , " current " ) ) {
2015-04-29 17:07:52 +03:00
/* FIXME: allow these even for --type current */
2013-07-08 15:45:37 +04:00
log_error ( " --ignoreadvanced and --ignoreunsupported has "
" no effect with --type current " ) ;
return EINVALID_CMD_LINE ;
}
2016-06-22 00:24:52 +03:00
} else if ( arg_is_set ( cmd , mergedconfig_ARG ) ) {
2015-06-25 11:51:30 +03:00
log_error ( " --mergedconfig has no effect without --type current or --type full " ) ;
2014-03-18 14:04:21 +04:00
return EINVALID_CMD_LINE ;
2013-07-08 15:45:37 +04:00
}
if ( ! _get_vsn ( cmd , & tree_spec . version ) )
return EINVALID_CMD_LINE ;
2014-05-20 16:45:20 +04:00
/*
* The profile specified by - - profile cmd arg is like - - commandprofile ,
* but it is used just for dumping the profile content and not for
* application .
*/
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , profile_ARG ) & &
2014-05-20 16:45:20 +04:00
( ! ( profile = add_profile ( cmd , arg_str_value ( cmd , profile_ARG , NULL ) , CONFIG_PROFILE_COMMAND ) ) | |
! override_config_tree_from_profile ( cmd , profile ) ) ) {
log_error ( " Failed to load profile %s. " , arg_str_value ( cmd , profile_ARG , NULL ) ) ;
return ECMD_FAILED ;
}
2013-07-08 17:34:27 +04:00
/*
* Set the ' cft ' to work with based on whether we need the plain
* config tree or merged config tree cascade if - - mergedconfig is used .
*/
2016-06-22 00:24:52 +03:00
if ( ( arg_is_set ( cmd , mergedconfig_ARG ) | | ! strcmp ( type , " full " ) | | ! strcmp ( type , " diff " ) ) & & cmd - > cft - > cascade ) {
2013-07-08 17:34:27 +04:00
if ( ! _merge_config_cascade ( cmd , cmd - > cft , & cft ) ) {
log_error ( " Failed to merge configuration. " ) ;
r = ECMD_FAILED ;
goto out ;
}
} else
cft = cmd - > cft ;
2015-06-25 11:51:30 +03:00
tree_spec . current_cft = cft ;
2013-07-08 17:34:27 +04:00
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , validate_ARG ) ) {
2014-05-19 15:23:12 +04:00
if ( _config_validate ( cmd , cft ) ) {
2013-03-05 20:48:29 +04:00
log_print ( " LVM configuration valid. " ) ;
2013-07-08 17:34:27 +04:00
goto out ;
2013-03-05 20:48:29 +04:00
} else {
log_error ( " LVM configuration invalid. " ) ;
2013-07-08 17:34:27 +04:00
r = ECMD_FAILED ;
goto out ;
2013-03-05 20:48:29 +04:00
}
}
2016-06-22 00:24:52 +03:00
if ( ! strcmp ( type , " list " ) | | arg_is_set ( cmd , list_ARG ) ) {
2015-04-29 12:11:58 +03:00
tree_spec . type = CFG_DEF_TREE_LIST ;
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , withcomments_ARG ) ) {
2015-04-29 12:11:58 +03:00
log_error ( " --withcomments has no effect with --type list " ) ;
return EINVALID_CMD_LINE ;
}
2017-08-05 18:18:36 +03:00
if ( arg_is_set ( cmd , withlocalpreamble_ARG ) ) {
log_error ( " --withlocalpreamble has no effect with --type list " ) ;
return EINVALID_CMD_LINE ;
}
if ( arg_is_set ( cmd , withgeneralpreamble_ARG ) ) {
log_error ( " --withgeneralpreamble has no effect with --type list " ) ;
return EINVALID_CMD_LINE ;
}
2022-08-16 17:56:06 +03:00
if ( arg_is_set ( cmd , valuesonly_ARG ) ) {
log_err ( " --valuesonly has no effect with --type list " ) ;
return EINVALID_CMD_LINE ;
}
2015-04-29 12:11:58 +03:00
/* list type does not require status check */
2015-06-25 11:51:30 +03:00
} else if ( ! strcmp ( type , " full " ) ) {
tree_spec . type = CFG_DEF_TREE_FULL ;
if ( ! _do_def_check ( & tree_spec , cft , & cft_check_handle ) ) {
r = ECMD_FAILED ;
goto_out ;
}
2015-04-29 12:11:58 +03:00
} else if ( ! strcmp ( type , " current " ) ) {
2013-03-05 20:48:29 +04:00
tree_spec . type = CFG_DEF_TREE_CURRENT ;
2014-03-24 16:19:15 +04:00
if ( ! _do_def_check ( & tree_spec , cft , & cft_check_handle ) ) {
2013-07-08 17:34:27 +04:00
r = ECMD_FAILED ;
goto_out ;
}
2013-03-05 20:48:29 +04:00
}
2013-07-08 16:13:09 +04:00
else if ( ! strcmp ( type , " missing " ) ) {
2013-03-05 20:48:29 +04:00
tree_spec . type = CFG_DEF_TREE_MISSING ;
2014-03-24 16:19:15 +04:00
if ( ! _do_def_check ( & tree_spec , cft , & cft_check_handle ) ) {
2013-07-08 17:34:27 +04:00
r = ECMD_FAILED ;
goto_out ;
}
2013-07-08 16:13:09 +04:00
}
else if ( ! strcmp ( type , " default " ) ) {
tree_spec . type = CFG_DEF_TREE_DEFAULT ;
/* default type does not require check status */
}
2014-03-24 16:19:15 +04:00
else if ( ! strcmp ( type , " diff " ) ) {
tree_spec . type = CFG_DEF_TREE_DIFF ;
if ( ! _do_def_check ( & tree_spec , cft , & cft_check_handle ) ) {
r = ECMD_FAILED ;
goto_out ;
}
}
2013-07-08 16:13:09 +04:00
else if ( ! strcmp ( type , " new " ) ) {
2016-06-22 00:24:52 +03:00
tree_spec . type = arg_is_set ( cmd , sinceversion_ARG ) ? CFG_DEF_TREE_NEW_SINCE
2015-11-25 16:09:20 +03:00
: CFG_DEF_TREE_NEW ;
2013-07-08 16:13:09 +04:00
/* new type does not require check status */
}
2013-07-09 11:57:46 +04:00
else if ( ! strcmp ( type , " profilable " ) ) {
tree_spec . type = CFG_DEF_TREE_PROFILABLE ;
/* profilable type does not require check status */
}
2014-05-20 16:45:20 +04:00
else if ( ! strcmp ( type , " profilable-command " ) ) {
tree_spec . type = CFG_DEF_TREE_PROFILABLE_CMD ;
/* profilable-command type does not require check status */
}
else if ( ! strcmp ( type , " profilable-metadata " ) ) {
tree_spec . type = CFG_DEF_TREE_PROFILABLE_MDA ;
/* profilable-metadata type does not require check status */
}
2013-03-05 20:48:29 +04:00
else {
log_error ( " Incorrect type of configuration specified. "
2015-06-25 11:51:30 +03:00
" Expected one of: current, default, diff, full, list, missing, "
" new, profilable, profilable-command, profilable-metadata. " ) ;
2013-07-08 17:34:27 +04:00
r = EINVALID_CMD_LINE ;
goto out ;
2013-03-05 20:48:29 +04:00
}
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , withsummary_ARG ) | | arg_is_set ( cmd , list_ARG ) )
2015-04-29 11:53:25 +03:00
tree_spec . withsummary = 1 ;
2017-08-05 18:18:36 +03:00
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , withcomments_ARG ) )
2014-02-25 14:05:23 +04:00
tree_spec . withcomments = 1 ;
2017-08-05 18:18:36 +03:00
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , unconfigured_ARG ) )
2015-04-17 19:31:16 +03:00
tree_spec . unconfigured = 1 ;
2014-02-25 14:05:23 +04:00
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , withversions_ARG ) )
2014-02-25 14:05:23 +04:00
tree_spec . withversions = 1 ;
2017-08-05 18:18:36 +03:00
if ( arg_is_set ( cmd , withgeneralpreamble_ARG ) )
tree_spec . withgeneralpreamble = 1 ;
if ( arg_is_set ( cmd , withlocalpreamble_ARG ) )
tree_spec . withlocalpreamble = 1 ;
2016-06-22 00:24:52 +03:00
if ( arg_is_set ( cmd , withspaces_ARG ) )
2015-06-24 14:14:30 +03:00
tree_spec . withspaces = 1 ;
2022-08-16 17:56:06 +03:00
if ( arg_is_set ( cmd , valuesonly_ARG ) )
tree_spec . valuesonly = 1 ;
2013-07-08 16:13:09 +04:00
if ( cft_check_handle )
tree_spec . check_status = cft_check_handle - > status ;
2013-07-19 19:32:08 +04:00
if ( ( tree_spec . type ! = CFG_DEF_TREE_CURRENT ) & &
2014-03-24 16:19:15 +04:00
( tree_spec . type ! = CFG_DEF_TREE_DIFF ) & &
2013-07-19 19:32:08 +04:00
! ( cft = config_def_create_tree ( & tree_spec ) ) ) {
r = ECMD_FAILED ;
goto_out ;
}
2013-03-05 20:48:29 +04:00
2014-02-25 14:05:23 +04:00
if ( ! config_write ( cft , & tree_spec , file , argc , argv ) ) {
2009-09-15 02:47:49 +04:00
stack ;
2013-03-05 20:48:29 +04:00
r = ECMD_FAILED ;
2009-09-15 02:47:49 +04:00
}
2013-07-08 17:34:27 +04:00
out :
2015-06-25 11:51:30 +03:00
if ( tree_spec . current_cft & & ( tree_spec . current_cft ! = cft ) & &
( tree_spec . current_cft ! = cmd - > cft ) )
/*
* This happens in case of CFG_DEF_TREE_FULL where we
* have merged explicitly defined config trees and also
* we have used default tree .
*/
dm_config_destroy ( tree_spec . current_cft ) ;
2013-07-08 17:34:27 +04:00
if ( cft & & ( cft ! = cmd - > cft ) )
2015-06-25 11:51:30 +03:00
dm_config_destroy ( cft ) ;
2014-05-20 16:45:20 +04:00
else if ( profile )
remove_config_tree_by_source ( cmd , CONFIG_PROFILE_COMMAND ) ;
2013-03-05 20:48:29 +04:00
2013-07-08 15:45:37 +04:00
/*
* The cmd - > cft ( the " current " tree ) is destroyed
* together with cmd context destroy . . .
*/
2013-03-05 20:48:29 +04:00
return r ;
2003-10-16 00:17:19 +04:00
}
2015-04-28 19:00:37 +03:00
int config ( struct cmd_context * cmd , int argc , char * * argv )
{
return dumpconfig ( cmd , argc , argv ) ;
}
int lvmconfig ( struct cmd_context * cmd , int argc , char * * argv )
{
return dumpconfig ( cmd , argc , argv ) ;
}