Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vtysh: make hostname and domainname persistent #8847

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -2522,10 +2522,6 @@ void cmd_init(int terminal)
hash_cmd_init();
}

install_element(CONFIG_NODE, &hostname_cmd);
install_element(CONFIG_NODE, &no_hostname_cmd);
install_element(CONFIG_NODE, &domainname_cmd);
install_element(CONFIG_NODE, &no_domainname_cmd);

if (terminal > 0) {
full_cli = true;
Expand All @@ -2537,6 +2533,11 @@ void cmd_init(int terminal)
install_element(CONFIG_NODE, &enable_password_cmd);
install_element(CONFIG_NODE, &no_enable_password_cmd);

install_element(CONFIG_NODE, &hostname_cmd);
install_element(CONFIG_NODE, &no_hostname_cmd);
install_element(CONFIG_NODE, &domainname_cmd);
install_element(CONFIG_NODE, &no_domainname_cmd);

install_element(CONFIG_NODE, &service_password_encrypt_cmd);
install_element(CONFIG_NODE, &no_service_password_encrypt_cmd);
install_element(CONFIG_NODE, &banner_motd_default_cmd);
Expand Down
86 changes: 84 additions & 2 deletions vtysh/vtysh.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,31 @@ static void vtysh_client_config(struct vtysh_client *head_client, char *line)
vty->of = vty->of_saved;
}

/* Command execution to some special commands */
int vtysh_client_run_special_commands()
{
char line[] = "do write terminal\n";
struct vtysh_client *head_client;
unsigned int i;

for (i = 0; i < array_size(vtysh_client); i++) {
head_client = &vtysh_client[i];

/* One daemon is enough */
if (head_client->flag != VTYSH_ZEBRA)
continue;

/* suppress output to user */
vty->of_saved = vty->of;
vty->of = NULL;
vtysh_client_run_all(head_client, line, 1, vtysh_config_parse_name_line,
NULL);
vty->of = vty->of_saved;
}

return 1;
}

/* Command execution over the vty interface. */
static int vtysh_execute_func(const char *line, int pager)
{
Expand Down Expand Up @@ -652,8 +677,16 @@ static int vtysh_execute_func(const char *line, int pager)
if (cmd_stat != CMD_SUCCESS)
break;

if (cmd->func)
(*cmd->func)(cmd, vty, 0, NULL);
if (cmd->func) {
/* XX
* Need make it better
*/
if (!strcmp(cmd->string, "hostname WORD")
|| !strcmp(cmd->string, "domainname WORD"))
(*cmd->func)(cmd, vty, 1, (struct cmd_token **)line);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but this is a hard NACK. The last thing vtysh needs is more special casing.

Also, you misspelled domainname which tells me that whatever this does, it isn't tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the misspelled word, i append a commit to fix it. It is caused by my git operation of combining many to one commit.
On the contrary, i do much test.

Yes, this hard code here is ugly. I have no idea of it, i will try other method. And can you shed light on it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since vtysh send commands to other daemons, then other daemons can call hostname_cmd.
vtysh need also run these commands (call hostname_cmd or domainname_cmd), here is for that.

else
(*cmd->func)(cmd, vty, 0, NULL);
}
}
}
if (vty->is_paged)
Expand Down Expand Up @@ -3171,6 +3204,50 @@ DEFUNSH(VTYSH_ALL, no_vtysh_config_enable_password,
return CMD_SUCCESS;
}

/* Hostname configuration */
DEFUNSH(VTYSH_ALL, vtysh_config_hostname,
vtysh_hostname_cmd,
"hostname WORD",
"Set system's network name\n"
"This system's network name\n")
{
/* Skip all checks, which are already checked in other daemons */
char * m = strrchr((char*)argv, ' ');
return cmd_hostname_set(m + 1);
}

DEFUNSH(VTYSH_ALL, vtysh_config_no_hostname,
vtysh_no_hostname_cmd,
"no hostname [HOSTNAME]",
NO_STR
"Reset system's network name\n"
"Host name of this router\n")
{
return cmd_hostname_set(NULL);
}

/* Domainname configuration */
DEFUNSH(VTYSH_ALL, vtysh_config_domainname,
vtysh_domainname_cmd,
"domainname WORD",
"Set system's domain name\n"
"This system's domain name\n")
{
/* Skip all checks, which are already checked in other daemons */
char *m = strrchr((char*)argv, ' ');
return cmd_domainname_set(m + 1);
}

DEFUNSH(VTYSH_ALL, vysh_config_no_domainname,
vtysh_no_domainname_cmd,
"no domainname [DOMAINNAME]",
NO_STR
"Reset system's domain name\n"
"domain name of this router\n")
{
return cmd_domainname_set(NULL);
}

DEFUN (vtysh_write_terminal,
vtysh_write_terminal_cmd,
"write terminal ["DAEMONS_LIST"] [no-header]",
Expand Down Expand Up @@ -4568,4 +4645,9 @@ void vtysh_init_vty(void)
install_element(CONFIG_NODE, &no_vtysh_password_cmd);
install_element(CONFIG_NODE, &vtysh_enable_password_cmd);
install_element(CONFIG_NODE, &no_vtysh_enable_password_cmd);

install_element(CONFIG_NODE, &vtysh_hostname_cmd);
install_element(CONFIG_NODE, &vtysh_no_hostname_cmd);
install_element(CONFIG_NODE, &vtysh_domainname_cmd);
install_element(CONFIG_NODE, &vtysh_no_domainname_cmd);
}
2 changes: 2 additions & 0 deletions vtysh/vtysh.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ int vtysh_mark_file(const char *filename);

int vtysh_read_config(const char *filename, bool dry_run);
int vtysh_write_config_integrated(void);
int vtysh_client_run_special_commands(void);

void vtysh_config_parse_line(void *, const char *);
void vtysh_config_parse_name_line(void *, const char *);

void vtysh_config_dump(void);

Expand Down
33 changes: 21 additions & 12 deletions vtysh/vtysh_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,27 @@ static void config_add_line_uniq_end(struct list *config, const char *line)
listnode_move_to_tail(config, node);
}

void vtysh_config_parse_name_line(void *arg, const char *line)
{
char c;

if (!line)
return;

c = line[0];

if (c == '\0')
return;

if (strncmp(line, "hostname", strlen("hostname")) == 0
|| strncmp(line, "domainname", strlen("domainname")) == 0) {
vtysh_execute("configure terminal");
vtysh_execute(line);
vtysh_execute("end");
}
return;
}

void vtysh_config_parse_line(void *arg, const char *line)
{
char c;
Expand Down Expand Up @@ -582,18 +603,6 @@ int vtysh_read_config(const char *config_default_dir, bool dry_run)
*/
void vtysh_config_write(void)
{
char line[512];

if (cmd_hostname_get()) {
snprintf(line, sizeof(line), "hostname %s", cmd_hostname_get());
vtysh_config_parse_line(NULL, line);
}

if (cmd_domainname_get()) {
snprintf(line, sizeof(line), "domainname %s",
cmd_domainname_get());
vtysh_config_parse_line(NULL, line);
}
if (vtysh_write_integrated == WRITE_INTEGRATED_NO)
vtysh_config_parse_line(NULL,
"no service integrated-vtysh-config");
Expand Down
10 changes: 10 additions & 0 deletions vtysh/vtysh_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,16 @@ int main(int argc, char **argv, char **env)
}
vtysh_execute("enable");
return vtysh_write_config_integrated();
} else {
/*
* Exec some special commands from other daemon at booting
* Because these commands of vtysh itself maybe not exact
*
* Here, maybe better way?
*/
vtysh_execute("enable");
vtysh_client_run_special_commands();
vtysh_execute("disable");
}

if (inputfile) {
Expand Down