You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2023/07/12 17:04:13 UTC

[nuttx] branch master updated: syslog: add syslog channel filtering function

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new ce98f186c0 syslog: add syslog channel filtering function
ce98f186c0 is described below

commit ce98f186c0d4210c51a599aff488b55d15ed9e25
Author: yinshengkai <yi...@xiaomi.com>
AuthorDate: Tue Jul 4 17:33:04 2023 +0800

    syslog: add syslog channel filtering function
    
    support to control the opening or closing of the specified channel through the syslogmask command at runtime
    
    Signed-off-by: yinshengkai <yi...@xiaomi.com>
---
 .../components/drivers/special/syslog.rst          | 17 ++++++
 drivers/syslog/Kconfig                             |  9 +++
 drivers/syslog/syslog_channel.c                    | 24 ++++++++
 drivers/syslog/syslog_chardev.c                    | 67 +++++++++++++++++++++-
 drivers/syslog/syslog_intbuffer.c                  | 14 +++++
 drivers/syslog/syslog_putc.c                       | 14 +++++
 drivers/syslog/syslog_write.c                      | 14 +++++
 include/nuttx/fs/ioctl.h                           |  6 ++
 include/nuttx/syslog/syslog.h                      | 26 +++++++++
 9 files changed, 190 insertions(+), 1 deletion(-)

diff --git a/Documentation/components/drivers/special/syslog.rst b/Documentation/components/drivers/special/syslog.rst
index c1ea618486..e46b6ca5c5 100644
--- a/Documentation/components/drivers/special/syslog.rst
+++ b/Documentation/components/drivers/special/syslog.rst
@@ -233,6 +233,23 @@ even further initialization. For example, the file SYSLOG channel
 (described below) cannot be initialized until the necessary file
 systems have been mounted.
 
+SYSLOG Channel Filtering
+-----------------------------
+If you enable the CONFIG_SYSLOG_IOCTL configuration, you can enable
+syslog to open or close the specified channel at runtime.
+
+You can control SYSLOG channels by using the ioctl command in NuttX
+with either the SYSLOGIOC_GETCHANNELS or SYSLOGIOC_SETFILTER.
+
+-  ``SYSLOGIOC_GETCHANNELS``. This command can get a list of all channels
+-  ``SYSLOGIOC_SETFILTER``. This command enables/disables the specified channel.
+
+In nsh, you can view/set the syslog channel status through the setlogmask command.
+
+-  ``setlogmask list``. Print all channel status
+-  ``setlogmask <enable/disable> <channel>``. Enable or disable the
+   specified channel.
+
 Interrupt Level SYSLOG Output
 -----------------------------
 
diff --git a/drivers/syslog/Kconfig b/drivers/syslog/Kconfig
index 49d238bb3e..3af6fb8144 100644
--- a/drivers/syslog/Kconfig
+++ b/drivers/syslog/Kconfig
@@ -379,4 +379,13 @@ config SYSLOG_CHARDEV
 		byte output with no time-stamping or any other SYSLOG features
 		supported.
 
+config SYSLOG_IOCTL
+	bool "SYSLOG IOCTL support"
+	default n
+	---help---
+		Enables support for the SYSLOG IOCTL command.  This command is used
+		to control the behavior of the SYSLOG device. Currently, the supported
+		commands are SYSLOGIOC_SETFILTER/SYSLOGIOC_GETCHANNELS, which can be
+		used to set the enable status of different channels
+
 endmenu # System logging
diff --git a/drivers/syslog/syslog_channel.c b/drivers/syslog/syslog_channel.c
index 88ee9b98f0..73b243ad64 100644
--- a/drivers/syslog/syslog_channel.c
+++ b/drivers/syslog/syslog_channel.c
@@ -82,6 +82,10 @@ static const struct syslog_channel_ops_s g_ramlog_channel_ops =
 static struct syslog_channel_s g_ramlog_channel =
 {
   &g_ramlog_channel_ops
+#  ifdef CONFIG_SYSLOG_IOCTL
+  , "ram"
+  , false
+#  endif
 };
 #endif
 
@@ -98,6 +102,10 @@ static const struct syslog_channel_ops_s g_rpmsg_channel_ops =
 static struct syslog_channel_s g_rpmsg_channel =
 {
   &g_rpmsg_channel_ops
+#  ifdef CONFIG_SYSLOG_IOCTL
+  , "rpmsg"
+  , false
+#  endif
 };
 #endif
 
@@ -114,6 +122,10 @@ static const struct syslog_channel_ops_s g_rtt_channel_ops =
 static struct syslog_channel_s g_rtt_channel =
 {
   &g_rtt_channel_ops
+#  ifdef CONFIG_SYSLOG_IOCTL
+  , "rtt"
+  , false
+#  endif
 };
 #endif
 
@@ -129,6 +141,10 @@ static const struct syslog_channel_ops_s g_default_channel_ops =
 static struct syslog_channel_s g_default_channel =
 {
   &g_default_channel_ops
+#  ifdef CONFIG_SYSLOG_IOCTL
+  , "default"
+  , false
+#  endif
 };
 #endif
 
@@ -272,6 +288,14 @@ int syslog_channel(FAR struct syslog_channel_s *channel)
         {
           if (g_syslog_channel[i] == NULL)
             {
+#  ifdef CONFIG_SYSLOG_IOCTL
+              if (channel->sc_name[0] == '\0')
+                {
+                  snprintf(channel->sc_name, sizeof(channel->sc_name),
+                           "channel-%p", channel->sc_ops);
+                }
+#  endif
+
               g_syslog_channel[i] = channel;
               return OK;
             }
diff --git a/drivers/syslog/syslog_chardev.c b/drivers/syslog/syslog_chardev.c
index bee43b4a61..61f8d4b783 100644
--- a/drivers/syslog/syslog_chardev.c
+++ b/drivers/syslog/syslog_chardev.c
@@ -29,9 +29,10 @@
 #include <string.h>
 #include <poll.h>
 #include <errno.h>
+#include <syslog.h>
 
 #include <nuttx/fs/fs.h>
-#include <syslog.h>
+#include <nuttx/syslog/syslog.h>
 
 #include "syslog.h"
 
@@ -43,6 +44,10 @@
 
 static ssize_t syslog_chardev_write(FAR struct file *filep,
                                     FAR const char *buffer, size_t buflen);
+#ifdef CONFIG_SYSLOG_IOCTL
+static int syslog_chardev_ioctl(FAR struct file *filep,
+                                int cmd, unsigned long arg);
+#endif
 
 /****************************************************************************
  * Private Data
@@ -54,6 +59,10 @@ static const struct file_operations g_syslog_fops =
   NULL,                 /* close */
   NULL,                 /* read */
   syslog_chardev_write, /* write */
+  NULL,                 /* seek */
+#ifdef CONFIG_SYSLOG_IOCTL
+  syslog_chardev_ioctl, /* ioctl */
+#endif
 };
 
 /****************************************************************************
@@ -71,6 +80,62 @@ static ssize_t syslog_chardev_write(FAR struct file *filep,
   return len;
 }
 
+#ifdef CONFIG_SYSLOG_IOCTL
+static int syslog_chardev_ioctl(FAR struct file *filep,
+                                int cmd, unsigned long arg)
+{
+  FAR struct syslog_channel_info_s *info;
+  FAR struct syslog_channel_s *channel = NULL;
+  int i;
+
+  if (arg == 0)
+    {
+      return -EINVAL;
+    }
+
+  if (cmd == SYSLOGIOC_GETCHANNELS)
+    {
+      info = (FAR struct syslog_channel_info_s *)arg;
+
+      for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++)
+        {
+          channel = g_syslog_channel[i];
+          if (channel == NULL || channel->sc_name[0] == '\0')
+            {
+              break;
+            }
+
+          strlcpy(info[i].sc_name, channel->sc_name,
+                  sizeof(info[i].sc_name));
+          info[i].sc_disable = channel->sc_disable;
+        }
+    }
+  else if (cmd == SYSLOGIOC_SETFILTER)
+    {
+      info = (FAR struct syslog_channel_info_s *)arg;
+
+      for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++)
+        {
+          if (strncmp(g_syslog_channel[i]->sc_name, info->sc_name,
+                      sizeof(info->sc_name)) == 0)
+            {
+              channel = g_syslog_channel[i];
+              break;
+            }
+        }
+
+      if (channel == NULL)
+        {
+          return -ENOENT;
+        }
+
+      channel->sc_disable = info->sc_disable;
+    }
+
+  return OK;
+}
+#endif
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
diff --git a/drivers/syslog/syslog_intbuffer.c b/drivers/syslog/syslog_intbuffer.c
index 878ca06063..6347e179f0 100644
--- a/drivers/syslog/syslog_intbuffer.c
+++ b/drivers/syslog/syslog_intbuffer.c
@@ -192,6 +192,13 @@ int syslog_add_intbuffer(int ch)
               break;
             }
 
+#ifdef CONFIG_SYSLOG_IOCTL
+          if (g_syslog_channel[i]->sc_disable)
+            {
+              continue;
+            }
+#endif
+
           /* Select which putc function to use for this flush */
 
           if (g_syslog_channel[i]->sc_ops->sc_force)
@@ -269,6 +276,13 @@ int syslog_flush_intbuffer(bool force)
               break;
             }
 
+#ifdef CONFIG_SYSLOG_IOCTL
+          if (g_syslog_channel[i]->sc_disable)
+            {
+              continue;
+            }
+#endif
+
           /* Select which putc function to use for this flush */
 
           putfunc = force ? g_syslog_channel[i]->sc_ops->sc_force :
diff --git a/drivers/syslog/syslog_putc.c b/drivers/syslog/syslog_putc.c
index 633a71c508..f6dc23240c 100644
--- a/drivers/syslog/syslog_putc.c
+++ b/drivers/syslog/syslog_putc.c
@@ -90,6 +90,13 @@ int syslog_putc(int ch)
                   break;
                 }
 
+#ifdef CONFIG_SYSLOG_IOCTL
+              if (channel->sc_disable)
+                {
+                  continue;
+                }
+#endif
+
               if (channel->sc_ops->sc_force != NULL)
                 {
                   channel->sc_ops->sc_force(channel,  ch);
@@ -123,6 +130,13 @@ int syslog_putc(int ch)
               break;
             }
 
+#ifdef CONFIG_SYSLOG_IOCTL
+          if (channel->sc_disable)
+            {
+              continue;
+            }
+#endif
+
           if (channel->sc_ops->sc_putc != NULL)
             {
               channel->sc_ops->sc_putc(channel, ch);
diff --git a/drivers/syslog/syslog_write.c b/drivers/syslog/syslog_write.c
index b7da658db8..8c3254f44c 100644
--- a/drivers/syslog/syslog_write.c
+++ b/drivers/syslog/syslog_write.c
@@ -82,6 +82,13 @@ static ssize_t syslog_default_write(FAR const char *buffer, size_t buflen)
                   break;
                 }
 
+#ifdef CONFIG_SYSLOG_IOCTL
+              if (channel->sc_disable)
+                {
+                  continue;
+                }
+#endif
+
               if (channel->sc_ops->sc_write_force != NULL)
                 {
                   nwritten =
@@ -109,6 +116,13 @@ static ssize_t syslog_default_write(FAR const char *buffer, size_t buflen)
               break;
             }
 
+#ifdef CONFIG_SYSLOG_IOCTL
+          if (channel->sc_disable)
+            {
+              continue;
+            }
+#endif
+
           if (channel->sc_ops->sc_write != NULL)
             {
               nwritten = channel->sc_ops->sc_write(channel, buffer, buflen);
diff --git a/include/nuttx/fs/ioctl.h b/include/nuttx/fs/ioctl.h
index 25bd11c944..3fed64f823 100644
--- a/include/nuttx/fs/ioctl.h
+++ b/include/nuttx/fs/ioctl.h
@@ -95,6 +95,7 @@
 #define _VIDIOCBASE     (0x3700) /* Video device ioctl commands */
 #define _CELLIOCBASE    (0x3800) /* Cellular device ioctl commands */
 #define _MIPIDSIBASE    (0x3900) /* Mipidsi device ioctl commands */
+#define _SYSLOGBASE     (0x3c00) /* Syslog device ioctl commands */
 #define _WLIOCBASE      (0x8b00) /* Wireless modules ioctl network commands */
 
 /* boardctl() commands share the same number space */
@@ -639,6 +640,11 @@
 #define _MIPIDSIIOCVALID(c)    (_IOC_TYPE(c)==_MIPIDSIBASE)
 #define _MIPIDSIIOC(nr)        _IOC(_MIPIDSIBASE,nr)
 
+/* syslog driver ioctl definitions ******************************************/
+
+#define _SYSLOGVALID(c) (_IOC_TYPE(c)==_SYSLOGBASE)
+#define _SYSLOGIOC(nr)  _IOC(_SYSLOGBASE,nr)
+
 /* Wireless driver network ioctl definitions ********************************/
 
 /* (see nuttx/include/wireless/wireless.h */
diff --git a/include/nuttx/syslog/syslog.h b/include/nuttx/syslog/syslog.h
index 5a5f7e0235..fa429810ed 100644
--- a/include/nuttx/syslog/syslog.h
+++ b/include/nuttx/syslog/syslog.h
@@ -79,6 +79,16 @@
 #  endif
 #endif
 
+/* Get a list of syslog channels */
+
+#define SYSLOGIOC_GETCHANNELS _SYSLOGIOC(0x0001)
+
+/* Set syslog channel filter */
+
+#define SYSLOGIOC_SETFILTER _SYSLOGIOC(0x0002)
+
+#define SYSLOG_CHANNEL_NAME_LEN 32
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
@@ -108,6 +118,12 @@ struct syslog_channel_ops_s
   syslog_close_t sc_close;        /* Channel close callback */
 };
 
+struct syslog_channel_info_s
+{
+  char sc_name[SYSLOG_CHANNEL_NAME_LEN];
+  bool sc_disable;
+};
+
 /* This structure provides the interface to a SYSLOG channel */
 
 struct syslog_channel_s
@@ -117,6 +133,16 @@ struct syslog_channel_s
   FAR const struct syslog_channel_ops_s *sc_ops;
 
   /* Implementation specific logic may follow */
+
+#ifdef CONFIG_SYSLOG_IOCTL
+  /* Syslog channel name */
+
+  char sc_name[SYSLOG_CHANNEL_NAME_LEN];
+
+  /* Syslog channel enable status, true is disable */
+
+  bool sc_disable;
+#endif
 };
 
 /****************************************************************************