This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 06da5135ecb73b3c9706ef7b1b21b5bc1a09248d Author: anjiahao <[email protected]> AuthorDate: Tue Aug 26 15:21:46 2025 +0800 !syslog: make setlogmask() per process, add nx_setlogmask() setlogmask() changed one global mask, so a task that lowered its own log level silenced the SYSLOG output of every other task as well. POSIX makes the mask a per-process value, and the comment of setlogmask() listed this as a REVISIT. Keep the mask of setlogmask() in the task info of the calling task group. A new group starts with the system wide mask g_syslog_mask, which now lives in drivers/syslog. The new nx_setlogmask(), also a system call, sets g_syslog_mask and the mask of every existing group; it is for callers that must change the log level of the whole system, e.g. the setlogmask command, or a panic path that has to stop all output. vsyslog() lets everything pass while there is no task info yet (early boot), and nx_setlogmask() skips a group whose task info is already freed by an exiting task. The coredump to syslog in sched/misc/coredump.c lowers the log level to keep the other tasks' INFO and DEBUG messages out of the dump, and in panic context the task info of the crashed task may not be usable, so it now uses nx_setlogmask() too. BREAKING CHANGE: in the flat and protected builds, setlogmask() no longer changes the log level of other tasks. Quick fix: code that must change the log level of the whole system calls nx_setlogmask() instead, as the setlogmask command and the coredump code do now. Signed-off-by: anjiahao <[email protected]> Signed-off-by: xuxingliang <[email protected]> Signed-off-by: huojianchao <[email protected]> Signed-off-by: dongjiuzhu1 <[email protected]> Signed-off-by: Bowen Wang <[email protected]> --- .github/CODEOWNERS | 1 - drivers/syslog/CMakeLists.txt | 2 +- drivers/syslog/Make.defs | 2 +- .../syslog/setlogmask.c | 91 ++++++++++++++-------- include/nuttx/syslog/syslog.h | 23 ++++++ include/nuttx/tls.h | 3 + include/sys/syscall_lookup.h | 1 + include/syslog.h | 14 +--- libs/libc/syslog/lib_setlogmask.c | 36 ++++----- libs/libc/syslog/lib_syslog.c | 11 ++- libs/libc/syslog/syslog.h | 53 ------------- sched/misc/coredump.c | 5 +- sched/tls/task_initinfo.c | 5 ++ sched/tls/task_uninitinfo.c | 3 + syscall/syscall.csv | 1 + 15 files changed, 124 insertions(+), 127 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index e1aa22d3efb..ef717b332fa 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -15535,7 +15535,6 @@ libs/libc/symtab/symtab_sortbyname.c [email protected] [email protected] alin libs/libc/syslog/lib_setlogmask.c [email protected] [email protected] [email protected] [email protected] libs/libc/syslog/lib_syslog.c [email protected] [email protected] [email protected] [email protected] libs/libc/syslog/lib_syslog_none.c [email protected] -libs/libc/syslog/syslog.h [email protected] [email protected] libs/libc/termios/lib_cfmakeraw.c [email protected] [email protected] [email protected] [email protected] libs/libc/termios/lib_cfspeed.c [email protected] [email protected] [email protected] [email protected] [email protected] libs/libc/termios/lib_isatty.c [email protected] [email protected] [email protected] [email protected] diff --git a/drivers/syslog/CMakeLists.txt b/drivers/syslog/CMakeLists.txt index d66198d50a7..8769af8f179 100644 --- a/drivers/syslog/CMakeLists.txt +++ b/drivers/syslog/CMakeLists.txt @@ -36,7 +36,7 @@ if(CONFIG_SYSLOG) list(APPEND SRCS vsyslog.c) endif() - list(APPEND SRCS syslog_channel.c syslog_write.c syslog_flush.c) + list(APPEND SRCS setlogmask.c syslog_channel.c syslog_write.c syslog_flush.c) endif() if(CONFIG_SYSLOG_INTBUFFER) diff --git a/drivers/syslog/Make.defs b/drivers/syslog/Make.defs index 4420c2cff3d..ec0c40e1fba 100644 --- a/drivers/syslog/Make.defs +++ b/drivers/syslog/Make.defs @@ -35,7 +35,7 @@ else CSRCS += vsyslog.c endif -CSRCS += syslog_channel.c syslog_write.c syslog_flush.c +CSRCS += setlogmask.c syslog_channel.c syslog_write.c syslog_flush.c endif ifeq ($(CONFIG_SYSLOG_INTBUFFER),y) diff --git a/libs/libc/syslog/lib_setlogmask.c b/drivers/syslog/setlogmask.c similarity index 54% copy from libs/libc/syslog/lib_setlogmask.c copy to drivers/syslog/setlogmask.c index 1ce329ef48b..623ffb5a66d 100644 --- a/libs/libc/syslog/lib_setlogmask.c +++ b/drivers/syslog/setlogmask.c @@ -1,5 +1,5 @@ /**************************************************************************** - * libs/libc/syslog/lib_setlogmask.c + * drivers/syslog/setlogmask.c * * SPDX-License-Identifier: Apache-2.0 * @@ -29,60 +29,87 @@ #include <stdint.h> #include <syslog.h> -#include "syslog/syslog.h" +#include <nuttx/addrenv.h> +#include <nuttx/sched.h> +#include <nuttx/syslog/syslog.h> +#include <nuttx/tls.h> /**************************************************************************** * Public Data ****************************************************************************/ -/* The currently enabled set of syslog priorities */ - uint8_t g_syslog_mask = CONFIG_SYSLOG_DEFAULT_MASK; +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void task_syslogmask(FAR struct tcb_s *tcb, FAR void *arg) +{ + FAR struct task_group_s *group = tcb->group; +#ifdef CONFIG_ARCH_ADDRENV + FAR struct addrenv_s *oldenv; +#endif + + /* The task info of an exiting task may be gone already */ + + if (group == NULL || group->tg_info == NULL) + { + return; + } + +#ifdef CONFIG_ARCH_ADDRENV + /* The task info of an unprivileged group lives in its own address + * environment. + */ + + if (tcb->addrenv_own != NULL) + { + addrenv_select(tcb->addrenv_own, &oldenv); + } + else if ((group->tg_flags & GROUP_FLAG_PRIVILEGED) == 0) + { + return; + } +#endif + + group->tg_info->ta_syslog_mask = (uint8_t)(*(FAR const int *)arg); + +#ifdef CONFIG_ARCH_ADDRENV + if (tcb->addrenv_own != NULL) + { + addrenv_restore(oldenv); + } +#endif +} + /**************************************************************************** * Public Functions ****************************************************************************/ /**************************************************************************** - * Name: setlogmask + * Name: nx_setlogmask * * Description: - * The setlogmask() function sets the logmask and returns the previous - * mask. If the mask argument is 0, the current logmask is not modified. - * - * The SYSLOG priorities are: LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, - * LOG_WARNING, LOG_NOTICE, LOG_INFO, and LOG_DEBUG. The bit corresponding - * to a priority p is LOG_MASK(p); LOG_UPTO(p) provides the mask of all - * priorities in the above list up to and including p. - * - * NOTE: setlogmask is not a thread-safe, re-entrant function. Concurrent - * use of setlogmask() will have undefined behavior. + * Set the system wide syslog mask, which new task groups start with, and + * the setlogmask() mask of every existing task group. Unlike + * setlogmask(), a mask of zero is applied: LOG_NONE disables all output. * - * REVISIT: Per POSIX the syslog mask should be a per-process value but in - * NuttX, the scope of the mask is dependent on the nature of the build: + * Input Parameters: + * mask - The new mask * - * Flat Build: There is one, global SYSLOG mask that controls all output. - * Protected Build: There are two SYSLOG masks. One within the kernel - * that controls only kernel output. And one in user-space that controls - * only user SYSLOG output. - * Kernel Build: The kernel build is compliant with the POSIX requirement: - * There will be one mask for each user process, controlling the SYSLOG - * output only form that process. There will be a separate mask - * accessible only in the kernel code to control kernel SYSLOG output. + * Returned Value: + * The previous system wide mask. * ****************************************************************************/ -int setlogmask(int mask) +int nx_setlogmask(int mask) { uint8_t oldmask; oldmask = g_syslog_mask; - if (mask != 0) - { - /* If the mask argument is 0, the current logmask is not modified. */ - - g_syslog_mask = (uint8_t)mask; - } + g_syslog_mask = (uint8_t)mask; + nxsched_foreach(task_syslogmask, &mask); return oldmask; } diff --git a/include/nuttx/syslog/syslog.h b/include/nuttx/syslog/syslog.h index 0dc547dd699..cfef3d9a1a6 100644 --- a/include/nuttx/syslog/syslog.h +++ b/include/nuttx/syslog/syslog.h @@ -31,6 +31,7 @@ #include <nuttx/compiler.h> #include <nuttx/streams.h> +#include <stdint.h> #include <sys/types.h> #include <stdbool.h> #include <stdarg.h> @@ -196,6 +197,10 @@ extern "C" { #endif +/* The system wide syslog mask, which new task groups inherit */ + +extern uint8_t g_syslog_mask; + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -469,6 +474,24 @@ void early_syslog(FAR const char *fmt, ...); #endif +/**************************************************************************** + * Name: nx_setlogmask + * + * Description: + * Set the system wide syslog mask, which new task groups start with, and + * the setlogmask() mask of every existing task group. Unlike + * setlogmask(), a mask of zero is applied: LOG_NONE disables all output. + * + * Input Parameters: + * mask - The new mask + * + * Returned Value: + * The previous system wide mask. + * + ****************************************************************************/ + +int nx_setlogmask(int mask); + #ifdef __cplusplus } #endif diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h index 32ae6a206b1..10fcf5bd88a 100644 --- a/include/nuttx/tls.h +++ b/include/nuttx/tls.h @@ -169,6 +169,9 @@ struct __res_state; struct task_info_s { rmutex_t ta_lock; +#ifdef CONFIG_SYSLOG + uint8_t ta_syslog_mask; /* Mask of setlogmask() */ +#endif #if CONFIG_TLS_TASK_NELEM > 0 uintptr_t ta_telem[CONFIG_TLS_TASK_NELEM]; /* Task local storage elements */ #endif diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h index 2f68a1bd8cf..fe446225bf9 100644 --- a/include/sys/syscall_lookup.h +++ b/include/sys/syscall_lookup.h @@ -212,6 +212,7 @@ SYSCALL_LOOKUP(clock_settime, 2) #ifdef CONFIG_SYSLOG SYSCALL_LOOKUP(nx_vsyslog, 3) +SYSCALL_LOOKUP(nx_setlogmask, 1) #endif /* The following are defined if either file or socket descriptor are diff --git a/include/syslog.h b/include/syslog.h index 5dcae570093..43acbc8a534 100644 --- a/include/syslog.h +++ b/include/syslog.h @@ -245,17 +245,9 @@ void vsyslog(int priority, FAR const IPTR char *fmt, va_list ap) * NOTE: setlogmask is not a thread-safe, re-entrant function. Concurrent * use of setlogmask() will have undefined behavior. * - * REVISIT: Per POSIX the syslog mask should be a per-process value but in - * NuttX, the scope of the mask is dependent on the nature of the build: - * - * Flat Build: There is one, global SYSLOG mask that controls all output. - * Protected Build: There are two SYSLOG masks. One within the kernel - * that controls only kernel output. And one in user-space that controls - * only user SYSLOG output. - * Kernel Build: The kernel build is compliant with the POSIX requirement: - * There will be one mask for for each user process, controlling the - * SYSLOG output only form that process. There will be a separate mask - * accessible only in the kernel code to control kernel SYSLOG output. + * As POSIX requires, the mask is a per-process value: it controls the + * SYSLOG output of the calling task group only. A new task group starts + * with the system wide mask, which nx_setlogmask() sets. * ****************************************************************************/ diff --git a/libs/libc/syslog/lib_setlogmask.c b/libs/libc/syslog/lib_setlogmask.c index 1ce329ef48b..fc38029f19a 100644 --- a/libs/libc/syslog/lib_setlogmask.c +++ b/libs/libc/syslog/lib_setlogmask.c @@ -29,15 +29,7 @@ #include <stdint.h> #include <syslog.h> -#include "syslog/syslog.h" - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -/* The currently enabled set of syslog priorities */ - -uint8_t g_syslog_mask = CONFIG_SYSLOG_DEFAULT_MASK; +#include <nuttx/tls.h> /**************************************************************************** * Public Functions @@ -58,30 +50,30 @@ uint8_t g_syslog_mask = CONFIG_SYSLOG_DEFAULT_MASK; * NOTE: setlogmask is not a thread-safe, re-entrant function. Concurrent * use of setlogmask() will have undefined behavior. * - * REVISIT: Per POSIX the syslog mask should be a per-process value but in - * NuttX, the scope of the mask is dependent on the nature of the build: - * - * Flat Build: There is one, global SYSLOG mask that controls all output. - * Protected Build: There are two SYSLOG masks. One within the kernel - * that controls only kernel output. And one in user-space that controls - * only user SYSLOG output. - * Kernel Build: The kernel build is compliant with the POSIX requirement: - * There will be one mask for each user process, controlling the SYSLOG - * output only form that process. There will be a separate mask - * accessible only in the kernel code to control kernel SYSLOG output. + * As POSIX requires, the mask is a per-process value: it controls the + * SYSLOG output of the calling task group only. A new task group starts + * with the system wide mask, which nx_setlogmask() sets. * ****************************************************************************/ int setlogmask(int mask) { + FAR struct task_info_s *info = task_get_info(); uint8_t oldmask; - oldmask = g_syslog_mask; + /* Without task info (early boot), vsyslog() lets every priority pass */ + + if (info == NULL) + { + return LOG_ALL; + } + + oldmask = info->ta_syslog_mask; if (mask != 0) { /* If the mask argument is 0, the current logmask is not modified. */ - g_syslog_mask = (uint8_t)mask; + info->ta_syslog_mask = (uint8_t)mask; } return oldmask; diff --git a/libs/libc/syslog/lib_syslog.c b/libs/libc/syslog/lib_syslog.c index 0b44065357c..6e30cc65a09 100644 --- a/libs/libc/syslog/lib_syslog.c +++ b/libs/libc/syslog/lib_syslog.c @@ -34,8 +34,6 @@ #include <nuttx/syslog/syslog.h> #include <nuttx/tls.h> -#include "syslog/syslog.h" - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -114,9 +112,14 @@ static void vsyslog_perror(FAR const IPTR char *fmt, va_list ap) void vsyslog(int priority, FAR const IPTR char *fmt, va_list ap) { - /* Check if this priority is enabled */ + FAR struct task_info_s *info = task_get_info(); + + /* Check if this priority is enabled. info is NULL in the early boot, + * before the TLS of the idle task is set up; let everything pass then. + */ - if ((g_syslog_mask & LOG_MASK(LOG_PRI(priority))) != 0) + if (info == NULL || + (info->ta_syslog_mask & LOG_MASK(LOG_PRI(priority))) != 0) { /* Yes.. Perform the nx_vsyslog system call. * diff --git a/libs/libc/syslog/syslog.h b/libs/libc/syslog/syslog.h deleted file mode 100644 index 038c8754eef..00000000000 --- a/libs/libc/syslog/syslog.h +++ /dev/null @@ -1,53 +0,0 @@ -/**************************************************************************** - * libs/libc/syslog/syslog.h - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -#ifndef __LIBS_LIBC_SYSLOG_SYSLOG_H -#define __LIBS_LIBC_SYSLOG_SYSLOG_H - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -#if defined(__cplusplus) -extern "C" -{ -#endif - -/* The currently enabled set of syslog priorities */ - -extern uint8_t g_syslog_mask; - -/**************************************************************************** - * Public Function Prototypes - ****************************************************************************/ - -#if defined(__cplusplus) -} -#endif - -#endif /* __LIBS_LIBC_SYSLOG_SYSLOG_H */ diff --git a/sched/misc/coredump.c b/sched/misc/coredump.c index 3710e8e6c14..3b931de58d4 100644 --- a/sched/misc/coredump.c +++ b/sched/misc/coredump.c @@ -35,6 +35,7 @@ #include <nuttx/lib/lib.h> #include <nuttx/nuttx.h> #include <nuttx/sched.h> +#include <nuttx/syslog/syslog.h> #include "sched/sched.h" #include "coredump.h" @@ -772,7 +773,7 @@ static void coredump_dump_syslog(pid_t pid) FAR const char *streamname; int logmask; - logmask = setlogmask(LOG_UPTO(LOG_ALERT)); + logmask = nx_setlogmask(LOG_UPTO(LOG_ALERT)); _alert("Start coredump:\n"); @@ -809,7 +810,7 @@ static void coredump_dump_syslog(pid_t pid) _alert("Coredump finish. %s formatted\n", streamname); # endif - setlogmask(logmask); + nx_setlogmask(logmask); } #endif diff --git a/sched/tls/task_initinfo.c b/sched/tls/task_initinfo.c index 9f31426e217..385b242ac32 100644 --- a/sched/tls/task_initinfo.c +++ b/sched/tls/task_initinfo.c @@ -29,6 +29,7 @@ #include <nuttx/kmalloc.h> #include <nuttx/mutex.h> +#include <nuttx/syslog/syslog.h> #include "tls.h" @@ -147,6 +148,10 @@ int task_init_info(FAR struct task_group_s *group) task_init_stream(&info->ta_streamlist); #endif + +#ifdef CONFIG_SYSLOG + info->ta_syslog_mask = g_syslog_mask; +#endif } return ret; diff --git a/sched/tls/task_uninitinfo.c b/sched/tls/task_uninitinfo.c index 7752227f020..8192bb50827 100644 --- a/sched/tls/task_uninitinfo.c +++ b/sched/tls/task_uninitinfo.c @@ -216,6 +216,9 @@ void task_uninit_info(FAR struct task_group_s *group) nxrmutex_destroy(&info->ta_lock); #ifdef CONFIG_MM_KERNEL_HEAP + /* nx_setlogmask() may still visit the group of an exiting task */ + + group->tg_info = NULL; group_free(group, info); #endif } diff --git a/syscall/syscall.csv b/syscall/syscall.csv index b288514d9e8..6bd67e635ca 100644 --- a/syscall/syscall.csv +++ b/syscall/syscall.csv @@ -101,6 +101,7 @@ "nx_mkfifo","nuttx/fs/fs.h","defined(CONFIG_PIPES) && CONFIG_DEV_FIFO_SIZE > 0","int","FAR const char *|path","mode_t","size_t" "nx_pthread_create","nuttx/pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","pthread_trampoline_t","FAR pthread_t *","FAR const pthread_attr_t *","pthread_startroutine_t","pthread_addr_t" "nx_pthread_exit","nuttx/pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","noreturn","pthread_addr_t" +"nx_setlogmask","nuttx/syslog/syslog.h","defined(CONFIG_SYSLOG)","int","int" "nx_vsyslog","nuttx/syslog/syslog.h","!defined(CONFIG_SYSLOG_TO_SCHED_NOTE)","int","int","FAR const IPTR char *","FAR va_list *" "nxsched_get_stackinfo","nuttx/sched.h","","int","pid_t","FAR struct stackinfo_s *" "nxsem_tickwait","nuttx/semaphore.h","","int","FAR sem_t *","uint32_t"
