MySQL  8.0.27
Source Code Documentation
my_default.cc File Reference

Add all options from files named "group".cnf from the default_directories before the command line arguments. More...

#include "my_config.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include "m_ctype.h"
#include "m_string.h"
#include "my_aes.h"
#include "my_alloc.h"
#include "my_byteorder.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_default.h"
#include "my_dir.h"
#include "my_getopt.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_loglevel.h"
#include "my_macros.h"
#include "my_psi_config.h"
#include "mysql/psi/mysql_file.h"
#include "mysql_version.h"
#include "mysys/my_default_priv.h"
#include "mysys/mysys_priv.h"
#include "mysys_err.h"
#include "typelib.h"
#include <algorithm>
#include <map>
#include <string>
#include "prealloced_array.h"

Classes

struct  my_variable_sources
 
struct  handle_option_ctx
 

Macros

#define MAX_DEFAULT_DIRS   6
 
#define DEFAULT_DIRS_SIZE   (MAX_DEFAULT_DIRS + 1) /* Terminate with NULL */
 

Typedefs

typedef Prealloced_array< char *, 100 > My_args
 
using mysql_file_getline_ret = std::unique_ptr< char, decltype(std::free) * >
 

Functions

static void set_args_separator (const char **arg)
 
void set_persist_args_separator (char **arg)
 
bool my_getopt_is_ro_persist_args_separator (const char *arg)
 
bool my_getopt_is_args_separator (const char *arg)
 
static int handle_default_option (void *in_ctx, const char *group_name, const char *option, const char *cnf_file)
 
static int search_default_file (Process_option_func func, void *func_ctx, const char *dir, const char *config_file, bool is_login_file)
 
static int search_default_file_with_ext (Process_option_func func, void *func_ctx, const char *dir, const char *ext, const char *config_file, int recursion_level, bool is_login_file, bool report_os_error_on_open)
 
static mysql_file_getline_ret mysql_file_getline (char *buff, int size, MYSQL_FILE *file, bool is_login_file)
 Read one line from the specified file. More...
 
static const char ** init_default_directories (MEM_ROOT *alloc)
 Create the list of default directories. More...
 
static char * remove_end_comment (char *ptr)
 
static int fn_expand (const char *filename, char *result_buf)
 
int my_search_option_files (const char *conf_file, int *argc, char ***argv, uint *args_used, Process_option_func func, void *func_ctx, const char **default_directories, bool is_login_file, bool found_no_defaults)
 
int get_defaults_options (int argc, char **argv, char **defaults, char **extra_defaults, char **group_suffix, char **login_path, bool found_no_defaults)
 
int load_defaults (const char *conf_file, const char **groups, int *argc, char ***argv, MEM_ROOT *alloc)
 
int my_load_defaults (const char *conf_file, const char **groups, int *argc, char ***argv, MEM_ROOT *alloc, const char ***default_directories)
 
static char * get_argument (const char *keyword, size_t kwlen, char *ptr, char *name, uint line)
 
void my_print_default_files (const char *conf_file)
 
void print_defaults (const char *conf_file, const char **groups)
 
void init_variable_default_paths ()
 Initialize all the mappings between default config file paths/ command line options/persistent config file path/login file path and corresponding enum_variable_source values. More...
 
void update_variable_source (const char *opt_name, const char *value)
 Track all options loaded from config files and command line options along with the path from where options are loaded. More...
 
void set_variable_source (const char *opt_name, void *value)
 This function will set value for my_option::arg_source by doing a lookup into variables_hash based on opt_name as key. More...
 
static int add_directory (MEM_ROOT *alloc, const char *dir, const char **dirs)
 
int my_default_get_login_file (char *file_name, size_t file_name_size)
 Place the login file name in the specified buffer. More...
 
int check_file_permissions (const char *file_name, bool is_login_file, myf flags)
 Check file permissions of the option file. More...
 

Variables

static std::map< string, my_variable_sourcesvariables_hash
 Defines mapping between variable names (set as part of config files or command line) and its config file/source value. More...
 
static std::map< string, enum_variable_sourcedefault_paths
 Defines mapping between config files names and its corresponding enum values. More...
 
char datadir_buffer [FN_REFLEN] = {0}
 Holds a reference to the directory where the persisted configuration file is located. More...
 
PSI_file_key key_file_cnf
 
PSI_memory_key key_memory_defaults
 
static const char * args_separator = "----args-separator----"
 arguments separator More...
 
static const char * persist_args_separator = "----persist-args-separator----"
 
bool my_getopt_use_args_separator = false
 
const char * my_defaults_file = nullptr
 
const char * my_defaults_group_suffix = nullptr
 
const char * my_defaults_extra_file = nullptr
 
static const char * my_login_path = nullptr
 
static char my_defaults_file_buffer [FN_REFLEN]
 
static char my_defaults_extra_file_buffer [FN_REFLEN]
 
static bool defaults_already_read = false
 
bool no_defaults = false
 
static const char ** default_directories = nullptr
 
static const char * f_extensions [] = {".cnf", nullptr}
 
bool my_defaults_read_login_file = true
 A global to turn off or on reading the mylogin file. More...
 

Detailed Description

Add all options from files named "group".cnf from the default_directories before the command line arguments.

On Windows defaults will also search in the Windows directory for a file called 'group'.ini As long as the program uses the last argument for conflicting options one only have to add a call to "load_defaults" to enable use of default values. pre- and end 'blank space' are removed from options and values. The following escape sequences are recognized in values:

const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:85

The following arguments are handled automatically; If used, they must be first argument on the command line! –no-defaults ; no options are read, except for the ones provided in the login file. –defaults-file=full-path-to-default-file ; Only this file will be read. –defaults-extra-file=full-path-to-default-file ; Read this file before ~/ –defaults-group-suffix ; Also read groups with concat(group, suffix) –print-defaults ; Print the modified command line and exit –login-path=login-path-name ; Read options under login-path-name from the login file.

Macro Definition Documentation

◆ DEFAULT_DIRS_SIZE

#define DEFAULT_DIRS_SIZE   (MAX_DEFAULT_DIRS + 1) /* Terminate with NULL */

◆ MAX_DEFAULT_DIRS

#define MAX_DEFAULT_DIRS   6

Typedef Documentation

◆ My_args

typedef Prealloced_array<char *, 100> My_args

◆ mysql_file_getline_ret

using mysql_file_getline_ret = std::unique_ptr<char, decltype(std::free) *>

Function Documentation

◆ add_directory()

static int add_directory ( MEM_ROOT alloc,
const char *  dir,
const char **  dirs 
)
static

◆ check_file_permissions()

int check_file_permissions ( const char *  file_name,
bool  is_login_file,
myf  flags 
)

Check file permissions of the option file.

Parameters
[in]file_nameName of the option file.
[in]is_login_fileTRUE, when login file is being processed.
[in]flagserror handling flags
Returns
0 - Non-allowable file permissions. 1 - Failed to stat. 2 - Success.

◆ fn_expand()

static int fn_expand ( const char *  filename,
char *  result_buf 
)
static

◆ get_argument()

static char* get_argument ( const char *  keyword,
size_t  kwlen,
char *  ptr,
char *  name,
uint  line 
)
static

◆ get_defaults_options()

int get_defaults_options ( int  argc,
char **  argv,
char **  defaults,
char **  extra_defaults,
char **  group_suffix,
char **  login_path,
bool  found_no_defaults 
)

◆ handle_default_option()

static int handle_default_option ( void *  in_ctx,
const char *  group_name,
const char *  option,
const char *  cnf_file 
)
static

◆ init_default_directories()

static const char ** init_default_directories ( MEM_ROOT alloc)
static

Create the list of default directories.

The directories searched, in order, are:
- Windows:     GetSystemWindowsDirectory()
- Windows:     GetWindowsDirectory()
- Windows:     C:/
- Windows:     Directory above where the executable is located
- Unix:        /etc/
- Unix:        /etc/mysql/
- Unix:        --sysconfdir=<path> (compile-time option)
- ALL:         getenv("MYSQL_HOME")
- ALL:         --defaults-extra-file=<path> (run-time option)
- Unix:        ~/

On all systems, if a directory is already in the list, it will be moved to the end of the list. This avoids reading defaults files multiple times, while ensuring the correct precedence.

Parameters
allocMEM_ROOT where the list of directories is stored
Return values
NULLFailure (out of memory, probably)
otherPointer to NULL-terminated array of default directories

◆ init_variable_default_paths()

void init_variable_default_paths ( )

Initialize all the mappings between default config file paths/ command line options/persistent config file path/login file path and corresponding enum_variable_source values.

◆ load_defaults()

int load_defaults ( const char *  conf_file,
const char **  groups,
int *  argc,
char ***  argv,
MEM_ROOT alloc 
)

◆ my_default_get_login_file()

int my_default_get_login_file ( char *  file_name,
size_t  file_name_size 
)

Place the login file name in the specified buffer.

Parameters
[out]file_nameBuffer to hold login file name
[in]file_name_sizeLength of the buffer
Returns
1 - Success 0 - Failure

◆ my_getopt_is_args_separator()

bool my_getopt_is_args_separator ( const char *  arg)

◆ my_getopt_is_ro_persist_args_separator()

bool my_getopt_is_ro_persist_args_separator ( const char *  arg)

◆ my_load_defaults()

int my_load_defaults ( const char *  conf_file,
const char **  groups,
int *  argc,
char ***  argv,
MEM_ROOT alloc,
const char ***  default_directories 
)

◆ my_print_default_files()

void my_print_default_files ( const char *  conf_file)

◆ my_search_option_files()

int my_search_option_files ( const char *  conf_file,
int *  argc,
char ***  argv,
uint args_used,
Process_option_func  func,
void *  func_ctx,
const char **  default_directories,
bool  is_login_file,
bool  found_no_defaults 
)

copy group

Construct new group

copy group

Construct new group

◆ mysql_file_getline()

static mysql_file_getline_ret mysql_file_getline ( char *  buff,
int  size,
MYSQL_FILE file,
bool  is_login_file 
)
static

Read one line from the specified file.

In case of scrambled login file, the line read is first decrypted and then returned.

Parameters
[out]buffStatic buffer to store the read line. If the buffer is not enough, this function will allocate a dynamic buffer from heap.
[in]sizeAt max, size-1 bytes to be read.
[in]fileSource file.
[in]is_login_fileTRUE, when login file is being processed.
Returns
nullptr On Error The next line from the supplied file On Success

◆ print_defaults()

void print_defaults ( const char *  conf_file,
const char **  groups 
)

◆ remove_end_comment()

static char * remove_end_comment ( char *  ptr)
static

◆ search_default_file()

static int search_default_file ( Process_option_func  func,
void *  func_ctx,
const char *  dir,
const char *  config_file,
bool  is_login_file 
)
static

◆ search_default_file_with_ext()

static int search_default_file_with_ext ( Process_option_func  func,
void *  func_ctx,
const char *  dir,
const char *  ext,
const char *  config_file,
int  recursion_level,
bool  is_login_file,
bool  report_os_error_on_open 
)
static

◆ set_args_separator()

static void set_args_separator ( const char **  arg)
inlinestatic

◆ set_persist_args_separator()

void set_persist_args_separator ( char **  arg)

◆ set_variable_source()

void set_variable_source ( const char *  opt_name,
void *  value 
)

This function will set value for my_option::arg_source by doing a lookup into variables_hash based on opt_name as key.

If key is present corresponding value (config file, enum value) will be set in value.

Parameters
[in]opt_namePointer to option name.
[out]valuePointer to struct holding config file path and variable source

◆ update_variable_source()

void update_variable_source ( const char *  opt_name,
const char *  value 
)

Track all options loaded from config files and command line options along with the path from where options are loaded.

For command line options path is empty string.

Ex: /etc/my.cnf has max_connections /$datadir/mysqld.auto.cnf has max_user_connections ./mysqld –server-id=47 with this setup, variables_hash has 3 entires of the above options along with path of config files and its enum value which is as below: max_connections -> (/etc/my.cnf , enum_variable_source::GLOBAL) max_user_connections -> ($datadir/mysqld.auto.cnf , enum_variable_source::PERSISTED) server-id -> ("" , enum_variable_source::COMMAND_LINE)

Parameters
[in]opt_namePointer to option name. opt_name must be in the form off –XXXXXX
[in]valuePointer to config file path

Variable Documentation

◆ args_separator

const char* args_separator = "----args-separator----"
static

arguments separator

load_defaults() loads arguments from config file and put them before the arguments from command line, this separator is used to separate the arguments loaded from config file and arguments user provided on command line.

Options with value loaded from config file are always in the form '–option=value', while for command line options, the value can be given as the next argument. Thus we used a separator so that handle_options() can distinguish them.

Note: any other places that does not need to distinguish them should skip the separator.

The content of arguments separator does not matter, one should only check the pointer, use "----args-separator----" here to ease debug if someone misused it.

The args seprator will only be added when my_getopt_use_args_seprator is set to TRUE before calling load_defaults();

See BUG#25192

◆ datadir_buffer

char datadir_buffer[FN_REFLEN] = {0}

Holds a reference to the directory where the persisted configuration file is located.

This usually is the data directory. This is different from mysql_real_data_home mostly because it's passed down to mysys. And is also filled in a bit differently from mysql_real_data_home by Persisted_variables_cache::init()

◆ default_directories

const char** default_directories = nullptr
static

◆ default_paths

std::map<string, enum_variable_source> default_paths
static

Defines mapping between config files names and its corresponding enum values.

ex: File /etc/my.cnf is mapped to enum_variable_source::GLOBAL ~/.my.cnf is mapped to enum_variable_source::USER command line options are mapped to enum_variable_source::COMMAND_LINE

◆ defaults_already_read

bool defaults_already_read = false
static

◆ f_extensions

const char* f_extensions[] = {".cnf", nullptr}
static

◆ key_file_cnf

PSI_file_key key_file_cnf
extern

◆ key_memory_defaults

PSI_memory_key key_memory_defaults

◆ my_defaults_extra_file

const char* my_defaults_extra_file = nullptr

◆ my_defaults_extra_file_buffer

char my_defaults_extra_file_buffer[FN_REFLEN]
static

◆ my_defaults_file

const char* my_defaults_file = nullptr

◆ my_defaults_file_buffer

char my_defaults_file_buffer[FN_REFLEN]
static

◆ my_defaults_group_suffix

const char* my_defaults_group_suffix = nullptr

◆ my_defaults_read_login_file

bool my_defaults_read_login_file = true

A global to turn off or on reading the mylogin file.

On by default

◆ my_getopt_use_args_separator

bool my_getopt_use_args_separator = false

◆ my_login_path

const char* my_login_path = nullptr
static

◆ no_defaults

bool no_defaults = false

◆ persist_args_separator

const char* persist_args_separator = "----persist-args-separator----"
static

◆ variables_hash

std::map<string, my_variable_sources> variables_hash
static

Defines mapping between variable names (set as part of config files or command line) and its config file/source value.

ex: If config file /etc/my.cnf has variables max_connections= 30 and $datadir/mysqld-auto.cnf has variables max_heap_table_size=887808 then this variable will have following key/value pair. max_connections -> (/etc/my.cnf , enum_variable_source::GLOBAL) max_heap_table_size -> ($datadir/mysqld-auto.cnf , enum_variable_source::PERSISTED)