|
|
7f924d |
From 8b6e812dcee8a54b4b6ad4116cb0782730079952 Mon Sep 17 00:00:00 2001
|
|
|
7f924d |
From: Alexandre Cassen <acassen@gmail.com>
|
|
|
7f924d |
Date: Tue, 7 Jan 2014 16:10:46 +0100
|
|
|
7f924d |
Subject: [PATCH 6/7] lib: extend vty to support logger
|
|
|
7f924d |
|
|
|
7f924d |
add support to logger and some cosmetics.
|
|
|
7f924d |
---
|
|
|
7f924d |
lib/vty.c | 52 +++++++++++++++++++++++++---------------------------
|
|
|
7f924d |
lib/vty.h | 2 +-
|
|
|
7f924d |
2 files changed, 26 insertions(+), 28 deletions(-)
|
|
|
7f924d |
|
|
|
7f924d |
diff --git a/lib/vty.c b/lib/vty.c
|
|
|
7f924d |
index e8c6dc6..b84b232 100644
|
|
|
7f924d |
--- a/lib/vty.c
|
|
|
7f924d |
+++ b/lib/vty.c
|
|
|
7f924d |
@@ -1600,7 +1600,7 @@ vty_timeout(thread_t *thread)
|
|
|
7f924d |
}
|
|
|
7f924d |
|
|
|
7f924d |
/* Read up configuration file from file_name. */
|
|
|
7f924d |
-static void
|
|
|
7f924d |
+static int
|
|
|
7f924d |
vty_read_file(FILE *confp)
|
|
|
7f924d |
{
|
|
|
7f924d |
int ret;
|
|
|
7f924d |
@@ -1617,19 +1617,20 @@ vty_read_file(FILE *confp)
|
|
|
7f924d |
if (!((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO))) {
|
|
|
7f924d |
switch (ret) {
|
|
|
7f924d |
case CMD_ERR_AMBIGUOUS:
|
|
|
7f924d |
- fprintf(stderr, "Ambiguous command.\n");
|
|
|
7f924d |
+ log_message(LOG_ERR, "Ambiguous command.\n");
|
|
|
7f924d |
break;
|
|
|
7f924d |
case CMD_ERR_NO_MATCH:
|
|
|
7f924d |
- fprintf(stderr, "There is no such command.\n");
|
|
|
7f924d |
+ log_message(LOG_ERR, "There is no such command.\n");
|
|
|
7f924d |
break;
|
|
|
7f924d |
}
|
|
|
7f924d |
- fprintf(stderr, "Error occured during reading below line.\n%s\n"
|
|
|
7f924d |
- , vty->buf);
|
|
|
7f924d |
+ log_message(LOG_ERR, "Error occured during reading below line.\n%s\n"
|
|
|
7f924d |
+ , vty->buf);
|
|
|
7f924d |
vty_close(vty);
|
|
|
7f924d |
- exit(1);
|
|
|
7f924d |
+ return -1;
|
|
|
7f924d |
}
|
|
|
7f924d |
|
|
|
7f924d |
vty_close(vty);
|
|
|
7f924d |
+ return 0;
|
|
|
7f924d |
}
|
|
|
7f924d |
|
|
|
7f924d |
static FILE *
|
|
|
7f924d |
@@ -1703,7 +1704,7 @@ vty_use_backup_config(char *fullpath)
|
|
|
7f924d |
}
|
|
|
7f924d |
|
|
|
7f924d |
/* Read up configuration file from file_name. */
|
|
|
7f924d |
-void
|
|
|
7f924d |
+int
|
|
|
7f924d |
vty_read_config(char *config_file, char *config_default_dir)
|
|
|
7f924d |
{
|
|
|
7f924d |
char cwd[MAXPATHLEN];
|
|
|
7f924d |
@@ -1717,9 +1718,9 @@ vty_read_config(char *config_file, char *config_default_dir)
|
|
|
7f924d |
if (!IS_DIRECTORY_SEP(config_file[0])) {
|
|
|
7f924d |
retpath = getcwd(cwd, MAXPATHLEN);
|
|
|
7f924d |
if (!retpath) {
|
|
|
7f924d |
- fprintf(stderr, "%s: failed to get current working directory: %s\n"
|
|
|
7f924d |
- , __func__, strerror(errno));
|
|
|
7f924d |
- exit(1);
|
|
|
7f924d |
+ log_message(LOG_ERR, "%s: failed to get current working directory: %s\n"
|
|
|
7f924d |
+ , __func__, strerror(errno));
|
|
|
7f924d |
+ return -1;
|
|
|
7f924d |
}
|
|
|
7f924d |
tmp = MALLOC(strlen(cwd) + strlen(config_file) + 2);
|
|
|
7f924d |
sprintf(tmp, "%s/%s", cwd, config_file);
|
|
|
7f924d |
@@ -1731,32 +1732,32 @@ vty_read_config(char *config_file, char *config_default_dir)
|
|
|
7f924d |
confp = fopen(fullpath, "r");
|
|
|
7f924d |
|
|
|
7f924d |
if (confp == NULL) {
|
|
|
7f924d |
- fprintf(stderr, "%s: failed to open configuration file %s: %s\n"
|
|
|
7f924d |
- , __func__, fullpath, strerror (errno));
|
|
|
7f924d |
+ log_message(LOG_ERR, "%s: failed to open configuration file %s: %s\n"
|
|
|
7f924d |
+ , __func__, fullpath, strerror (errno));
|
|
|
7f924d |
|
|
|
7f924d |
confp = vty_use_backup_config(fullpath);
|
|
|
7f924d |
if (confp) {
|
|
|
7f924d |
- fprintf(stderr, "WARNING: using backup configuration file!\n");
|
|
|
7f924d |
+ log_message(LOG_ERR, "WARNING: using backup configuration file!\n");
|
|
|
7f924d |
} else {
|
|
|
7f924d |
- fprintf(stderr, "can't open configuration file [%s]\n"
|
|
|
7f924d |
- , config_file);
|
|
|
7f924d |
- exit(1);
|
|
|
7f924d |
+ log_message(LOG_ERR, "can't open configuration file [%s]\n"
|
|
|
7f924d |
+ , config_file);
|
|
|
7f924d |
+ return -1;
|
|
|
7f924d |
}
|
|
|
7f924d |
}
|
|
|
7f924d |
} else {
|
|
|
7f924d |
confp = fopen(config_default_dir, "r");
|
|
|
7f924d |
if (confp == NULL) {
|
|
|
7f924d |
- fprintf(stderr, "%s: failed to open configuration file %s: %s\n"
|
|
|
7f924d |
- , __func__, config_default_dir, strerror(errno));
|
|
|
7f924d |
+ log_message(LOG_ERR, "%s: failed to open configuration file %s: %s\n"
|
|
|
7f924d |
+ , __func__, config_default_dir, strerror(errno));
|
|
|
7f924d |
|
|
|
7f924d |
confp = vty_use_backup_config(config_default_dir);
|
|
|
7f924d |
if (confp) {
|
|
|
7f924d |
- fprintf(stderr, "WARNING: using backup configuration file!\n");
|
|
|
7f924d |
+ log_message(LOG_ERR, "WARNING: using backup configuration file!\n");
|
|
|
7f924d |
fullpath = config_default_dir;
|
|
|
7f924d |
} else {
|
|
|
7f924d |
- fprintf(stderr, "can't open configuration file [%s]\n"
|
|
|
7f924d |
- , config_default_dir);
|
|
|
7f924d |
- exit (1);
|
|
|
7f924d |
+ log_message(LOG_ERR, "can't open configuration file [%s]\n"
|
|
|
7f924d |
+ , config_default_dir);
|
|
|
7f924d |
+ return -1;
|
|
|
7f924d |
}
|
|
|
7f924d |
} else {
|
|
|
7f924d |
fullpath = config_default_dir;
|
|
|
7f924d |
@@ -1764,12 +1765,10 @@ vty_read_config(char *config_file, char *config_default_dir)
|
|
|
7f924d |
}
|
|
|
7f924d |
|
|
|
7f924d |
vty_read_file(confp);
|
|
|
7f924d |
-
|
|
|
7f924d |
fclose(confp);
|
|
|
7f924d |
-
|
|
|
7f924d |
host_config_set(fullpath);
|
|
|
7f924d |
-
|
|
|
7f924d |
FREE_PTR(tmp);
|
|
|
7f924d |
+ return 0;
|
|
|
7f924d |
}
|
|
|
7f924d |
|
|
|
7f924d |
int
|
|
|
7f924d |
@@ -2038,8 +2037,7 @@ vty_config_write(vty_t *vty)
|
|
|
7f924d |
return CMD_SUCCESS;
|
|
|
7f924d |
}
|
|
|
7f924d |
|
|
|
7f924d |
-cmd_node_t vty_node =
|
|
|
7f924d |
-{
|
|
|
7f924d |
+cmd_node_t vty_node = {
|
|
|
7f924d |
VTY_NODE,
|
|
|
7f924d |
"%s(config-line)# ",
|
|
|
7f924d |
1,
|
|
|
7f924d |
diff --git a/lib/vty.h b/lib/vty.h
|
|
|
7f924d |
index 6134ad3..2c4b44f 100644
|
|
|
7f924d |
--- a/lib/vty.h
|
|
|
7f924d |
+++ b/lib/vty.h
|
|
|
7f924d |
@@ -181,7 +181,7 @@ extern int vty_listen(struct sockaddr_storage *);
|
|
|
7f924d |
extern void vty_reset(void);
|
|
|
7f924d |
extern vty_t *vty_new(void);
|
|
|
7f924d |
extern int vty_out(vty_t *, const char *, ...) PRINTF_ATTRIBUTE(2, 3);
|
|
|
7f924d |
-extern void vty_read_config(char *, char *);
|
|
|
7f924d |
+extern int vty_read_config(char *, char *);
|
|
|
7f924d |
extern void vty_time_print(vty_t *, int);
|
|
|
7f924d |
extern void vty_serv_sock(const char *, unsigned short, const char *);
|
|
|
7f924d |
extern void vty_close(vty_t *);
|
|
|
7f924d |
--
|
|
|
7f924d |
1.8.1.4
|
|
|
7f924d |
|