]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/vty.c
Merge remote-tracking branch 'frr/master' into warnings
[mirror_frr.git] / lib / vty.c
index 280b2ace51a64bd64c5df459b5291854bc2ab554..921f92df1694ee220bc1ca221f81ebd632961823 100644 (file)
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -40,6 +40,7 @@
 #include "network.h"
 #include "libfrr.h"
 #include "frrstr.h"
+#include "lib_errors.h"
 
 #include <arpa/telnet.h>
 #include <termios.h>
@@ -313,8 +314,9 @@ static int vty_log_out(struct vty *vty, const char *level,
                /* Fatal I/O error. */
                vty->monitor =
                        0; /* disable monitoring to avoid infinite recursion */
-               zlog_warn("%s: write failed to vty client fd %d, closing: %s",
-                         __func__, vty->fd, safe_strerror(errno));
+               flog_err(LIB_ERR_SOCKET,
+                        "%s: write failed to vty client fd %d, closing: %s",
+                        __func__, vty->fd, safe_strerror(errno));
                buffer_reset(vty->obuf);
                buffer_reset(vty->lbuf);
                /* cannot call vty_close, because a parent routine may still try
@@ -479,6 +481,8 @@ static int vty_command(struct vty *vty, char *buf)
        const char *protocolname;
        char *cp = NULL;
 
+       assert(vty);
+
        /*
         * Log non empty command lines
         */
@@ -496,20 +500,20 @@ static int vty_command(struct vty *vty, char *buf)
 
                /* format the base vty info */
                snprintf(vty_str, sizeof(vty_str), "vty[??]@%s", vty->address);
-               if (vty)
-                       for (i = 0; i < vector_active(vtyvec); i++)
-                               if (vty == vector_slot(vtyvec, i)) {
-                                       snprintf(vty_str, sizeof(vty_str),
-                                                "vty[%d]@%s", i, vty->address);
-                                       break;
-                               }
+
+               for (i = 0; i < vector_active(vtyvec); i++)
+                       if (vty == vector_slot(vtyvec, i)) {
+                               snprintf(vty_str, sizeof(vty_str), "vty[%d]@%s",
+                                        i, vty->address);
+                               break;
+                       }
 
                /* format the prompt */
                snprintf(prompt_str, sizeof(prompt_str), cmd_prompt(vty->node),
                         vty_str);
 
                /* now log the command */
-               zlog_err("%s%s", prompt_str, buf);
+               zlog_notice("%s%s", prompt_str, buf);
        }
 
 #ifdef CONSUMED_TIME_CHECK
@@ -531,7 +535,8 @@ static int vty_command(struct vty *vty, char *buf)
                if ((realtime = thread_consumed_time(&after, &before, &cputime))
                    > CONSUMED_TIME_CHECK)
                        /* Warn about CPU hog that must be fixed. */
-                       zlog_warn(
+                       flog_warn(
+                               LIB_WARN_SLOW_THREAD,
                                "SLOW COMMAND: command took %lums (cpu time %lums): %s",
                                realtime / 1000, cputime / 1000, buf);
        }
@@ -808,10 +813,13 @@ static void vty_end_config(struct vty *vty)
        case LDP_L2VPN_NODE:
        case LDP_PSEUDOWIRE_NODE:
        case ISIS_NODE:
+       case OPENFABRIC_NODE:
        case KEYCHAIN_NODE:
        case KEYCHAIN_KEY_NODE:
        case VTY_NODE:
        case BGP_EVPN_VNI_NODE:
+       case BFD_NODE:
+       case BFD_PEER_NODE:
                vty_config_unlock(vty);
                vty->node = ENABLE_NODE;
                break;
@@ -1205,9 +1213,12 @@ static void vty_stop_input(struct vty *vty)
        case LDP_L2VPN_NODE:
        case LDP_PSEUDOWIRE_NODE:
        case ISIS_NODE:
+       case OPENFABRIC_NODE:
        case KEYCHAIN_NODE:
        case KEYCHAIN_KEY_NODE:
        case VTY_NODE:
+       case BFD_NODE:
+       case BFD_PEER_NODE:
                vty_config_unlock(vty);
                vty->node = ENABLE_NODE;
                break;
@@ -1317,15 +1328,16 @@ static int vty_telnet_option(struct vty *vty, unsigned char *buf, int nbytes)
                switch (vty->sb_buf[0]) {
                case TELOPT_NAWS:
                        if (vty->sb_len != TELNET_NAWS_SB_LEN)
-                               zlog_warn(
+                               flog_err(
+                                       LIB_ERR_SYSTEM_CALL,
                                        "RFC 1073 violation detected: telnet NAWS option "
                                        "should send %d characters, but we received %lu",
                                        TELNET_NAWS_SB_LEN,
                                        (unsigned long)vty->sb_len);
                        else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
-                               zlog_err(
-                                       "Bug detected: sizeof(vty->sb_buf) %lu < %d, "
-                                       "too small to handle the telnet NAWS option",
+                               flog_err(
+                                       LIB_ERR_DEVELOPMENT,
+                                       "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
                                        (unsigned long)sizeof(vty->sb_buf),
                                        TELNET_NAWS_SB_LEN);
                        else {
@@ -1439,7 +1451,8 @@ static int vty_read(struct thread *thread)
                        }
                        vty->monitor = 0; /* disable monitoring to avoid
                                             infinite recursion */
-                       zlog_warn(
+                       flog_err(
+                               LIB_ERR_SOCKET,
                                "%s: read error on vty client fd %d, closing: %s",
                                __func__, vty->fd, safe_strerror(errno));
                        buffer_reset(vty->obuf);
@@ -1646,7 +1659,7 @@ static int vty_flush(struct thread *thread)
        case BUFFER_ERROR:
                vty->monitor =
                        0; /* disable monitoring to avoid infinite recursion */
-               zlog_warn("buffer_flush failed on vty client fd %d, closing",
+               zlog_info("buffer_flush failed on vty client fd %d, closing",
                          vty->fd);
                buffer_reset(vty->lbuf);
                buffer_reset(vty->obuf);
@@ -1893,7 +1906,8 @@ static int vty_accept(struct thread *thread)
        /* We can handle IPv4 or IPv6 socket. */
        vty_sock = sockunion_accept(accept_sock, &su);
        if (vty_sock < 0) {
-               zlog_warn("can't accept vty socket : %s", safe_strerror(errno));
+               flog_err(LIB_ERR_SOCKET, "can't accept vty socket : %s",
+                        safe_strerror(errno));
                return -1;
        }
        set_nonblocking(vty_sock);
@@ -1966,7 +1980,8 @@ static void vty_serv_sock_addrinfo(const char *hostname, unsigned short port)
        ret = getaddrinfo(hostname, port_str, &req, &ainfo);
 
        if (ret != 0) {
-               zlog_err("getaddrinfo failed: %s", gai_strerror(ret));
+               flog_err_sys(LIB_ERR_SYSTEM_CALL, "getaddrinfo failed: %s",
+                            gai_strerror(ret));
                exit(1);
        }
 
@@ -2026,8 +2041,9 @@ static void vty_serv_un(const char *path)
        /* Make UNIX domain socket. */
        sock = socket(AF_UNIX, SOCK_STREAM, 0);
        if (sock < 0) {
-               zlog_err("Cannot create unix stream socket: %s",
-                        safe_strerror(errno));
+               flog_err_sys(LIB_ERR_SOCKET,
+                            "Cannot create unix stream socket: %s",
+                            safe_strerror(errno));
                return;
        }
 
@@ -2045,15 +2061,16 @@ static void vty_serv_un(const char *path)
 
        ret = bind(sock, (struct sockaddr *)&serv, len);
        if (ret < 0) {
-               zlog_err("Cannot bind path %s: %s", path, safe_strerror(errno));
+               flog_err_sys(LIB_ERR_SOCKET, "Cannot bind path %s: %s", path,
+                            safe_strerror(errno));
                close(sock); /* Avoid sd leak. */
                return;
        }
 
        ret = listen(sock, 5);
        if (ret < 0) {
-               zlog_err("listen(fd %d) failed: %s", sock,
-                        safe_strerror(errno));
+               flog_err_sys(LIB_ERR_SOCKET, "listen(fd %d) failed: %s", sock,
+                            safe_strerror(errno));
                close(sock); /* Avoid sd leak. */
                return;
        }
@@ -2068,8 +2085,9 @@ static void vty_serv_un(const char *path)
        if ((int)ids.gid_vty > 0) {
                /* set group of socket */
                if (chown(path, -1, ids.gid_vty)) {
-                       zlog_err("vty_serv_un: could chown socket, %s",
-                                safe_strerror(errno));
+                       flog_err_sys(LIB_ERR_SYSTEM_CALL,
+                                    "vty_serv_un: could chown socket, %s",
+                                    safe_strerror(errno));
                }
        }
 
@@ -2097,14 +2115,15 @@ static int vtysh_accept(struct thread *thread)
                      (socklen_t *)&client_len);
 
        if (sock < 0) {
-               zlog_warn("can't accept vty socket : %s", safe_strerror(errno));
+               flog_err(LIB_ERR_SOCKET, "can't accept vty socket : %s",
+                        safe_strerror(errno));
                return -1;
        }
 
        if (set_nonblocking(sock) < 0) {
-               zlog_warn(
-                       "vtysh_accept: could not set vty socket %d to non-blocking,"
-                       " %s, closing",
+               flog_err(
+                       LIB_ERR_SOCKET,
+                       "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
                        sock, safe_strerror(errno));
                close(sock);
                return -1;
@@ -2135,8 +2154,8 @@ static int vtysh_flush(struct vty *vty)
        case BUFFER_ERROR:
                vty->monitor =
                        0; /* disable monitoring to avoid infinite recursion */
-               zlog_warn("%s: write error to fd %d, closing", __func__,
-                         vty->fd);
+               flog_err(LIB_ERR_SOCKET, "%s: write error to fd %d, closing",
+                        __func__, vty->fd);
                buffer_reset(vty->lbuf);
                buffer_reset(vty->obuf);
                vty_close(vty);
@@ -2170,7 +2189,8 @@ static int vtysh_read(struct thread *thread)
                        }
                        vty->monitor = 0; /* disable monitoring to avoid
                                             infinite recursion */
-                       zlog_warn(
+                       flog_err(
+                               LIB_ERR_SOCKET,
                                "%s: read failed on vtysh client fd %d, closing: %s",
                                __func__, sock, safe_strerror(errno));
                }
@@ -2301,9 +2321,9 @@ void vty_close(struct vty *vty)
         * additionally, we'd need to replace these fds with /dev/null. */
        if (vty->wfd > STDERR_FILENO && vty->wfd != vty->fd)
                close(vty->wfd);
-       if (vty->fd > STDERR_FILENO) {
+       if (vty->fd > STDERR_FILENO)
                close(vty->fd);
-       } else
+       if (vty->fd == STDIN_FILENO)
                was_stdio = true;
 
        if (vty->buf)
@@ -2400,8 +2420,9 @@ static void vty_read_file(FILE *confp)
                nl = strchr(vty->error_buf, '\n');
                if (nl)
                        *nl = '\0';
-               zlog_err("ERROR: %s on config line %u: %s", message, line_num,
-                        vty->error_buf);
+               flog_err(LIB_ERR_VTY,
+                         "ERROR: %s on config line %u: %s", message, line_num,
+                         vty->error_buf);
        }
 
        vty_close(vty);
@@ -2462,18 +2483,20 @@ static FILE *vty_use_backup_config(const char *fullpath)
 }
 
 /* Read up configuration file from file_name. */
-void vty_read_config(const char *config_file, char *config_default_dir)
+bool vty_read_config(const char *config_file, char *config_default_dir)
 {
        char cwd[MAXPATHLEN];
        FILE *confp = NULL;
        const char *fullpath;
        char *tmp = NULL;
+       bool read_success = false;
 
        /* If -f flag specified. */
        if (config_file != NULL) {
                if (!IS_DIRECTORY_SEP(config_file[0])) {
                        if (getcwd(cwd, MAXPATHLEN) == NULL) {
-                               zlog_err(
+                               flog_err_sys(
+                                       LIB_ERR_SYSTEM_CALL,
                                        "Failure to determine Current Working Directory %d!",
                                        errno);
                                exit(1);
@@ -2488,16 +2511,20 @@ void vty_read_config(const char *config_file, char *config_default_dir)
                confp = fopen(fullpath, "r");
 
                if (confp == NULL) {
-                       zlog_err("%s: failed to open configuration file %s: %s",
-                                __func__, fullpath, safe_strerror(errno));
+                       flog_warn(
+                               LIB_WARN_BACKUP_CONFIG,
+                               "%s: failed to open configuration file %s: %s, checking backup",
+                               __func__, fullpath, safe_strerror(errno));
 
                        confp = vty_use_backup_config(fullpath);
                        if (confp)
-                               zlog_warn(
+                               flog_warn(
+                                       LIB_WARN_BACKUP_CONFIG,
                                        "WARNING: using backup configuration file!");
                        else {
-                               zlog_err("can't open configuration file [%s]",
-                                        config_file);
+                               flog_err(LIB_ERR_VTY,
+                                         "can't open configuration file [%s]",
+                                         config_file);
                                exit(1);
                        }
                }
@@ -2525,24 +2552,30 @@ void vty_read_config(const char *config_file, char *config_default_dir)
 
                if (strstr(config_default_dir, "vtysh") == NULL) {
                        ret = stat(integrate_default, &conf_stat);
-                       if (ret >= 0)
+                       if (ret >= 0) {
+                               read_success = true;
                                goto tmp_free_and_out;
+                       }
                }
 #endif /* VTYSH */
                confp = fopen(config_default_dir, "r");
                if (confp == NULL) {
-                       zlog_err("%s: failed to open configuration file %s: %s",
-                                __func__, config_default_dir,
-                                safe_strerror(errno));
+                       flog_err(
+                               LIB_ERR_SYSTEM_CALL,
+                               "%s: failed to open configuration file %s: %s, checking backup",
+                               __func__, config_default_dir,
+                               safe_strerror(errno));
 
                        confp = vty_use_backup_config(config_default_dir);
                        if (confp) {
-                               zlog_warn(
+                               flog_warn(
+                                       LIB_WARN_BACKUP_CONFIG,
                                        "WARNING: using backup configuration file!");
                                fullpath = config_default_dir;
                        } else {
-                               zlog_err("can't open configuration file [%s]",
-                                        config_default_dir);
+                               flog_err(LIB_ERR_VTY,
+                                         "can't open configuration file [%s]",
+                                         config_default_dir);
                                goto tmp_free_and_out;
                        }
                } else
@@ -2550,6 +2583,7 @@ void vty_read_config(const char *config_file, char *config_default_dir)
        }
 
        vty_read_file(confp);
+       read_success = true;
 
        fclose(confp);
 
@@ -2558,6 +2592,8 @@ void vty_read_config(const char *config_file, char *config_default_dir)
 tmp_free_and_out:
        if (tmp)
                XFREE(MTYPE_TMP, tmp);
+
+       return read_success;
 }
 
 /* Small utility function which output log to the VTY. */
@@ -3052,12 +3088,14 @@ static void vty_save_cwd(void)
                 * Hence not worrying about it too much.
                 */
                if (!chdir(SYSCONFDIR)) {
-                       zlog_err("Failure to chdir to %s, errno: %d",
-                                SYSCONFDIR, errno);
+                       flog_err_sys(LIB_ERR_SYSTEM_CALL,
+                                    "Failure to chdir to %s, errno: %d",
+                                    SYSCONFDIR, errno);
                        exit(-1);
                }
                if (getcwd(cwd, MAXPATHLEN) == NULL) {
-                       zlog_err("Failure to getcwd, errno: %d", errno);
+                       flog_err_sys(LIB_ERR_SYSTEM_CALL,
+                                    "Failure to getcwd, errno: %d", errno);
                        exit(-1);
                }
        }