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 1e22ef636bded88949f1737b869360e1079c6b35
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Oct 19 15:39:53 2026 +0200

    sched/group: Keep the file mode creation mask per process.
    
    In a kernel build, libc kept the mask in a static variable of each
    process.  The kernel has its own copy of libc, so open(), mkdir(),
    mq_open() and sem_open() in the kernel always used the mask 0.  A
    program that posix_spawn() or exec() started also got the mask 0.
    In the flat and protected builds, the mask was in the task_info_s of
    the task group, and a task that task_create() or posix_spawn() started
    got the mask 0.
    
    Keep the mask in the task group, and make umask() and getumask() system
    calls that read and write it.  A new process gets the mask of its
    parent, as POSIX says.  Kernel threads keep the mask 0.  umask() keeps
    only the file permission bits of the mask.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/nuttx/sched.h                              |  1 +
 include/nuttx/tls.h                                |  1 -
 include/sys/syscall_lookup.h                       |  2 ++
 libs/libc/misc/CMakeLists.txt                      |  1 -
 libs/libc/misc/Make.defs                           |  2 +-
 sched/group/CMakeLists.txt                         |  1 +
 sched/group/Make.defs                              |  2 +-
 sched/group/group_create.c                         |  9 +++++
 .../misc/lib_umask.c => sched/group/group_umask.c  | 42 ++++++++--------------
 syscall/syscall.csv                                |  2 ++
 10 files changed, 31 insertions(+), 32 deletions(-)

diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index 053a2995f29..c21332968dd 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -463,6 +463,7 @@ struct task_group_s
   pid_t tg_ppid;                    /* This is the ID of the parent thread     
 */
   uint8_t tg_flags;                 /* See GROUP_FLAG_* definitions            
 */
   uint8_t tg_caps;                  /* See PR_CAP_* definitions                
 */
+  mode_t  tg_umask;                 /* File mode creation mask (umask())       
 */
 
   /* Resource limits (getrlimit() and setrlimit()) **************************/
 
diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h
index facbf18dee4..641117b647c 100644
--- a/include/nuttx/tls.h
+++ b/include/nuttx/tls.h
@@ -144,7 +144,6 @@ struct task_info_s
 #endif
 #ifndef CONFIG_BUILD_KERNEL
   struct getopt_s ta_getopt; /* Globals used by getopt() */
-  mode_t          ta_umask;  /* File mode creation mask */
 #  ifdef CONFIG_LIBC_LOCALE
   char            ta_domain[NAME_MAX]; /* Current domain for gettext */
 #  endif
diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h
index d5153628194..ccd78776e67 100644
--- a/include/sys/syscall_lookup.h
+++ b/include/sys/syscall_lookup.h
@@ -58,6 +58,8 @@ SYSCALL_LOOKUP(nxsched_get_stackinfo,      2)
 SYSCALL_LOOKUP(sysinfo,                    1)
 SYSCALL_LOOKUP(getrlimit,                  2)
 SYSCALL_LOOKUP(setrlimit,                  2)
+SYSCALL_LOOKUP(umask,                      1)
+SYSCALL_LOOKUP(getumask,                   0)
 
 SYSCALL_LOOKUP(gethostname,                2)
 SYSCALL_LOOKUP(sethostname,                2)
diff --git a/libs/libc/misc/CMakeLists.txt b/libs/libc/misc/CMakeLists.txt
index afa2b1f6630..fd504de5ab2 100644
--- a/libs/libc/misc/CMakeLists.txt
+++ b/libs/libc/misc/CMakeLists.txt
@@ -30,7 +30,6 @@ list(
   lib_circbuf.c
   lib_creat.c
   lib_mknod.c
-  lib_umask.c
   lib_utsname.c
   lib_getrandom.c
   lib_xorshift128.c
diff --git a/libs/libc/misc/Make.defs b/libs/libc/misc/Make.defs
index ecca32f3d1e..62d64118321 100644
--- a/libs/libc/misc/Make.defs
+++ b/libs/libc/misc/Make.defs
@@ -22,7 +22,7 @@
 
 # Add the internal C files to the build
 
-CSRCS += lib_bitmap.c lib_circbuf.c lib_creat.c lib_mknod.c lib_umask.c
+CSRCS += lib_bitmap.c lib_circbuf.c lib_creat.c lib_mknod.c
 CSRCS += lib_utsname.c lib_getrandom.c lib_xorshift128.c lib_tea_encrypt.c
 CSRCS += lib_tea_decrypt.c lib_impure.c lib_memfd.c
 CSRCS += lib_mutex.c lib_fchmodat.c lib_fstatat.c lib_getfullpath.c
diff --git a/sched/group/CMakeLists.txt b/sched/group/CMakeLists.txt
index 58919779e4e..597402076d9 100644
--- a/sched/group/CMakeLists.txt
+++ b/sched/group/CMakeLists.txt
@@ -23,6 +23,7 @@
 set(SRCS
     group_create.c
     group_rlimit.c
+    group_umask.c
     group_join.c
     group_leave.c
     group_setupidlefiles.c
diff --git a/sched/group/Make.defs b/sched/group/Make.defs
index e060dcc8c66..534fa83629c 100644
--- a/sched/group/Make.defs
+++ b/sched/group/Make.defs
@@ -20,7 +20,7 @@
 #
 ############################################################################
 
-CSRCS += group_create.c group_join.c group_leave.c group_rlimit.c
+CSRCS += group_create.c group_join.c group_leave.c group_rlimit.c group_umask.c
 CSRCS += group_setupidlefiles.c group_setuptaskfiles.c
 CSRCS += group_foreachchild.c group_killchildren.c
 
diff --git a/sched/group/group_create.c b/sched/group/group_create.c
index aa20c811718..bda2f876a22 100644
--- a/sched/group/group_create.c
+++ b/sched/group/group_create.c
@@ -258,6 +258,15 @@ int group_allocate(FAR struct tcb_s *tcb, uint8_t ttype)
 
   group_init_rlimit(group, this_task()->group);
 
+  /* Inherit the file mode creation mask.  Kernel threads share one group
+   * and keep the mask 0.
+   */
+
+  if (ttype != TCB_FLAG_TTYPE_KERNEL && this_task()->group != NULL)
+    {
+      group->tg_umask = this_task()->group->tg_umask;
+    }
+
   /* Initialize file descriptors for the TCB */
 
   fdlist_init(&group->tg_fdlist);
diff --git a/libs/libc/misc/lib_umask.c b/sched/group/group_umask.c
similarity index 73%
rename from libs/libc/misc/lib_umask.c
rename to sched/group/group_umask.c
index d1e340b6510..28a2d1ac59b 100644
--- a/libs/libc/misc/lib_umask.c
+++ b/sched/group/group_umask.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * libs/libc/misc/lib_umask.c
+ * sched/group/group_umask.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -25,15 +25,8 @@
  ****************************************************************************/
 
 #include <sys/stat.h>
-#include <nuttx/tls.h>
 
-/****************************************************************************
- * Private Data
- ****************************************************************************/
-
-#ifdef CONFIG_BUILD_KERNEL
-static mode_t g_umask;
-#endif
+#include <nuttx/sched.h>
 
 /****************************************************************************
  * Public Functions
@@ -43,10 +36,11 @@ static mode_t g_umask;
  * Name: umask
  *
  * Description:
- *    Set and get the file mode creation mask
+ *   Set the file mode creation mask of the calling process.  Only the file
+ *   permission bits of the mask are used.
  *
  * Input Parameters:
- *   mask - The process' file mode creation mask
+ *   mask - The new file mode creation mask.
  *
  * Returned Value:
  *   The previous value of the mask.
@@ -55,33 +49,25 @@ static mode_t g_umask;
 
 mode_t umask(mode_t mask)
 {
+  FAR struct task_group_s *group = nxsched_self()->group;
   mode_t prev;
-#ifndef CONFIG_BUILD_KERNEL
-  FAR struct task_info_s *info;
-
-  info = task_get_info();
-  prev = info->ta_umask;
-  info->ta_umask = mask;
-#else
-  prev = g_umask;
-  g_umask = mask;
-#endif
 
+  prev = group->tg_umask;
+  group->tg_umask = mask & (S_IRWXU | S_IRWXG | S_IRWXO);
   return prev;
 }
 
 /****************************************************************************
  * Name: getumask
+ *
+ * Description:
+ *   Return the file mode creation mask of the calling process.
+ *
  ****************************************************************************/
 
 mode_t getumask(void)
 {
-#ifndef CONFIG_BUILD_KERNEL
-  FAR struct task_info_s *info;
+  FAR struct task_group_s *group = nxsched_self()->group;
 
-  info = task_get_info();
-  return info->ta_umask;
-#else
-  return g_umask;
-#endif
+  return group != NULL ? group->tg_umask : 0;
 }
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index bcb29d3c853..8a1d685fa42 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -53,6 +53,7 @@
 "getsockopt","sys/socket.h","defined(CONFIG_NET)","int","int","int","int","FAR 
void *","FAR socklen_t *"
 "gettimeofday","sys/time.h","","int","FAR struct timeval *","FAR struct 
timezone *"
 "getuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","uid_t"
+"getumask","sys/stat.h","","mode_t"
 
"inotify_add_watch","sys/inotify.h","defined(CONFIG_FS_NOTIFY)","int","int","FAR
 const char *","uint32_t"
 "inotify_init","sys/inotify.h","defined(CONFIG_FS_NOTIFY)","int"
 "inotify_init1","sys/inotify.h","defined(CONFIG_FS_NOTIFY)","int","int"
@@ -208,6 +209,7 @@
 "timerfd_create","sys/timerfd.h","defined(CONFIG_TIMER_FD)","int","int","int"
 "timerfd_gettime","sys/timerfd.h","defined(CONFIG_TIMER_FD)","int","int","FAR 
struct itimerspec *"
 
"timerfd_settime","sys/timerfd.h","defined(CONFIG_TIMER_FD)","int","int","int","FAR
 const struct itimerspec *","FAR struct itimerspec *"
+"umask","sys/stat.h","","mode_t","mode_t"
 "umount2","sys/mount.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const 
char *","unsigned int"
 "unlink","unistd.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const 
char *"
 "up_fork","nuttx/arch.h","defined(CONFIG_ARCH_HAVE_VFORK) || 
defined(CONFIG_ARCH_HAVE_FORK)","pid_t","bool"

Reply via email to