This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 249687edf73231a6d51f826f01644244d6945de8 Author: Marco Casaroli <[email protected]> AuthorDate: Wed Nov 11 19:59:45 2026 +0100 libs/libc/unistd: Add getdomainname() and setdomainname(). Programs from other systems read and set the NIS domain name with these two functions, for example the hostname program, and did not compile. Keep the domain name as the host name is kept: in libc in the flat build, and in the kernel in the protected and kernel builds, where user space calls the functions through two new system calls. The two system calls go at the end of the list, so the numbers of the other system calls do not change. The name is empty at boot and has at most HOST_NAME_MAX bytes. As on Linux, setdomainname() fails with EINVAL if the name is longer, and getdomainname() fails with EINVAL if the name and its terminating null byte do not fit in the buffer. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- include/sys/syscall_lookup.h | 5 ++ include/unistd.h | 2 + libs/libc/unistd/CMakeLists.txt | 1 + libs/libc/unistd/Kconfig | 3 +- libs/libc/unistd/Make.defs | 1 + libs/libc/unistd/lib_domainname.c | 125 ++++++++++++++++++++++++++++++++++++++ syscall/syscall.csv | 2 + 7 files changed, 138 insertions(+), 1 deletion(-) diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h index 196401508b8..17540383bd4 100644 --- a/include/sys/syscall_lookup.h +++ b/include/sys/syscall_lookup.h @@ -438,3 +438,8 @@ SYSCALL_LOOKUP(signal, 2) SYSCALL_LOOKUP(sched_note_vprintf_ip, 5) SYSCALL_LOOKUP(sched_note_event_ip, 5) #endif + +/* NIS domain name */ + +SYSCALL_LOOKUP(getdomainname, 2) +SYSCALL_LOOKUP(setdomainname, 2) diff --git a/include/unistd.h b/include/unistd.h index 5eb84aaf459..32cd51b6b82 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -591,6 +591,8 @@ extern int optopt; /* Unrecognized option character */ int gethostname(FAR char *name, size_t namelen); int sethostname(FAR const char *name, size_t namelen); +int getdomainname(FAR char *name, size_t namelen); +int setdomainname(FAR const char *name, size_t namelen); /* Get configurable system variables */ diff --git a/libs/libc/unistd/CMakeLists.txt b/libs/libc/unistd/CMakeLists.txt index 43707b1d748..47b4671e261 100644 --- a/libs/libc/unistd/CMakeLists.txt +++ b/libs/libc/unistd/CMakeLists.txt @@ -56,6 +56,7 @@ set(SRCS lib_lutimes.c lib_gethostname.c lib_sethostname.c + lib_domainname.c lib_fchownat.c lib_linkat.c lib_readlinkat.c diff --git a/libs/libc/unistd/Kconfig b/libs/libc/unistd/Kconfig index 4ef3cf7886f..7605e12c7bc 100644 --- a/libs/libc/unistd/Kconfig +++ b/libs/libc/unistd/Kconfig @@ -117,7 +117,8 @@ config HOST_NAME_MAX ---help--- The maximum size of a host name, not including the terminating null (HOST_NAME_MAX in limits.h). POSIX requires at least 255 - (_POSIX_HOST_NAME_MAX). + (_POSIX_HOST_NAME_MAX). It is also the maximum size of the NIS + domain name (setdomainname()). config LIBC_OPEN_MAX int "OPEN_MAX for this device" diff --git a/libs/libc/unistd/Make.defs b/libs/libc/unistd/Make.defs index eb12c2cbcf0..c95b20f17af 100644 --- a/libs/libc/unistd/Make.defs +++ b/libs/libc/unistd/Make.defs @@ -32,6 +32,7 @@ CSRCS += lib_nice.c CSRCS += lib_getrusage.c lib_utime.c lib_utimes.c lib_getdtablesize.c CSRCS += lib_getpagesize.c lib_setpriority.c lib_getpriority.c CSRCS += lib_futimes.c lib_lutimes.c lib_gethostname.c lib_sethostname.c +CSRCS += lib_domainname.c CSRCS += lib_fchownat.c lib_linkat.c lib_readlinkat.c lib_symlinkat.c CSRCS += lib_unlinkat.c lib_usleep.c lib_getpgrp.c lib_getpgid.c CSRCS += lib_getsid.c lib_setpgid.c lib_setpgrp.c lib_setsid.c diff --git a/libs/libc/unistd/lib_domainname.c b/libs/libc/unistd/lib_domainname.c new file mode 100644 index 00000000000..1637a47bc07 --- /dev/null +++ b/libs/libc/unistd/lib_domainname.c @@ -0,0 +1,125 @@ +/**************************************************************************** + * libs/libc/unistd/lib_domainname.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <limits.h> +#include <string.h> +#include <unistd.h> + +#include <nuttx/irq.h> +#include <nuttx/spinlock.h> + +/* The domain name is a system property, as the host name is. In the + * protected and kernel builds it is kept only in the kernel, and user + * space calls these functions through system calls. + */ + +#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static char g_domainname[HOST_NAME_MAX + 1]; +static spinlock_t g_domainname_lock = SP_UNLOCKED; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: getdomainname + * + * Description: + * Get the NIS domain name of the system. The name is empty until + * setdomainname() sets it. + * + * Input Parameters: + * name - The buffer for the name. + * namelen - The size of the buffer in bytes. + * + * Returned Value: + * Zero (OK) on success. -1 (ERROR) and errno EINVAL if the name and its + * terminating null byte do not fit in namelen bytes. + * + ****************************************************************************/ + +int getdomainname(FAR char *name, size_t namelen) +{ + irqstate_t flags; + size_t len; + + flags = spin_lock_irqsave(&g_domainname_lock); + len = strlcpy(name, g_domainname, namelen); + spin_unlock_irqrestore(&g_domainname_lock, flags); + + if (len >= namelen) + { + set_errno(EINVAL); + return ERROR; + } + + return OK; +} + +/**************************************************************************** + * Name: setdomainname + * + * Description: + * Set the NIS domain name of the system to the namelen bytes at name. + * The name does not need a terminating null byte. + * + * Input Parameters: + * name - The new name. + * namelen - The length of the name in bytes. + * + * Returned Value: + * Zero (OK) on success. -1 (ERROR) and errno EINVAL if namelen is more + * than HOST_NAME_MAX. + * + ****************************************************************************/ + +int setdomainname(FAR const char *name, size_t namelen) +{ + irqstate_t flags; + + if (namelen > HOST_NAME_MAX) + { + set_errno(EINVAL); + return ERROR; + } + + flags = spin_lock_irqsave(&g_domainname_lock); + memcpy(g_domainname, name, namelen); + g_domainname[namelen] = '\0'; + spin_unlock_irqrestore(&g_domainname_lock, flags); + + return OK; +} + +#endif /* CONFIG_BUILD_FLAT || __KERNEL__ */ diff --git a/syscall/syscall.csv b/syscall/syscall.csv index 7ae03c68d57..e71c3d0560b 100644 --- a/syscall/syscall.csv +++ b/syscall/syscall.csv @@ -38,6 +38,7 @@ "fsync","unistd.h","","int","int" "ftruncate","unistd.h","","int","int","off_t" "futimens","sys/stat.h","","int","int","const struct timespec [2]|FAR const struct timespec *" +"getdomainname","unistd.h","","int","FAR char *","size_t" "getegid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","gid_t" "getresgid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","gid_t *","gid_t *","gid_t *" "getresuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","uid_t *","uid_t *","uid_t *" @@ -164,6 +165,7 @@ "sendfile","sys/sendfile.h","","ssize_t","int","int","FAR off_t *","size_t" "sendmsg","sys/socket.h","defined(CONFIG_NET)","ssize_t","int","FAR const struct msghdr *","int" "sendto","sys/socket.h","defined(CONFIG_NET)","ssize_t","int","FAR const void *","size_t","int","FAR const struct sockaddr *","socklen_t" +"setdomainname","unistd.h","","int","FAR const char *","size_t" "setegid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","gid_t" "setgroups","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY) && (CONFIG_SCHED_NGROUPS > 0)","int","int","FAR const gid_t *" "setregid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","int","gid_t","gid_t"
