fix: parse_configuration not setting log correctly and not checking validity of the final config
This commit is contained in:
parent
7d428eda79
commit
ffd4159725
1 changed files with 36 additions and 18 deletions
|
|
@ -17,17 +17,28 @@
|
||||||
|
|
||||||
// == Static functions
|
// == Static functions
|
||||||
|
|
||||||
// Returns 0 if the servers array is valid 1 otherwise
|
// Returns 0 if the server config is valid, 1 otherwise
|
||||||
// static int check_server_config(struct server_config *server)
|
static int check_server_config(struct server_config *server)
|
||||||
// {
|
{
|
||||||
// if (server->server_name == NULL || server->port == NULL
|
if (server->server_name == NULL || server->port == NULL
|
||||||
// || server->ip == NULL || server->root_dir == NULL)
|
|| server->ip == NULL || server->root_dir == NULL)
|
||||||
// {
|
{
|
||||||
// return -1;
|
return -1;
|
||||||
// }
|
}
|
||||||
|
|
||||||
// return 0;
|
return 0;
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
// Returns 0 if the config is valid, 1 otherwise
|
||||||
|
static int check_config(struct config *cfg)
|
||||||
|
{
|
||||||
|
if (cfg->pid_file == NULL)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return check_server_config(cfg->servers);
|
||||||
|
}
|
||||||
|
|
||||||
static void print_help(char *program_name)
|
static void print_help(char *program_name)
|
||||||
{
|
{
|
||||||
|
|
@ -55,9 +66,9 @@ static int parse_daemon_arg(struct config *cfg)
|
||||||
static int parse_log_arg(struct config *cfg)
|
static int parse_log_arg(struct config *cfg)
|
||||||
{
|
{
|
||||||
if (strcmp(optarg, "true") == 0)
|
if (strcmp(optarg, "true") == 0)
|
||||||
cfg->daemon = true;
|
cfg->log = true;
|
||||||
else
|
else
|
||||||
cfg->daemon = false;
|
cfg->log = false;
|
||||||
|
|
||||||
return ARG_VALID;
|
return ARG_VALID;
|
||||||
}
|
}
|
||||||
|
|
@ -66,18 +77,18 @@ static int parse_log_arg(struct config *cfg)
|
||||||
static int handle_opt(char **argv, char opt, struct config *cfg)
|
static int handle_opt(char **argv, char opt, struct config *cfg)
|
||||||
{
|
{
|
||||||
// Options without value
|
// Options without value
|
||||||
switch (opt)
|
|
||||||
{
|
|
||||||
// Help
|
// Help
|
||||||
case 'h':
|
if (opt == 'h')
|
||||||
|
{
|
||||||
print_help(argv[0]);
|
print_help(argv[0]);
|
||||||
return ARG_HELP;
|
return ARG_HELP;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Options with value
|
||||||
|
|
||||||
if (optarg == NULL)
|
if (optarg == NULL)
|
||||||
return ARG_NO_VALUE;
|
return ARG_NO_VALUE;
|
||||||
|
|
||||||
// Options with value
|
|
||||||
switch (opt)
|
switch (opt)
|
||||||
{
|
{
|
||||||
// Daemon
|
// Daemon
|
||||||
|
|
@ -200,12 +211,19 @@ struct config *parse_configuration(int argc, char *argv[])
|
||||||
if (err != ARG_VALID)
|
if (err != ARG_VALID)
|
||||||
{
|
{
|
||||||
print_arg_error(err, argv, options, optindex);
|
print_arg_error(err, argv, options, optindex);
|
||||||
free(config->servers);
|
config_destroy(config);
|
||||||
free(config);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check config validity
|
||||||
|
if (check_config(config) != 0)
|
||||||
|
{
|
||||||
|
printf("%s: Missing mandatory flags, cannot continue.", argv[0]);
|
||||||
|
config_destroy(config);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue