libc: Make gethostname as syscall instead of uname

simplify and symmetry the implementation in KERNEL/PROTECTED build

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: Iefdeea5f6ef6348c774b2ca9f7e45fe89c0c22dd
This commit is contained in:
Xiang Xiao 2020-07-28 17:52:17 +08:00 committed by Masayuki Ishikawa
parent 0a6c81b19c
commit 3cff139b85
4 changed files with 13 additions and 53 deletions

View File

@ -45,7 +45,7 @@ SYSCALL_LOOKUP(nxsched_get_stackinfo, 2)
SYSCALL_LOOKUP(sched_setaffinity, 3)
#endif
SYSCALL_LOOKUP(uname, 1)
SYSCALL_LOOKUP(gethostname, 2)
SYSCALL_LOOKUP(sethostname, 2)
/* User identity */

View File

@ -46,18 +46,6 @@
#include <nuttx/version.h>
#include <unistd.h>
/* In the protected and kernel build modes where kernel and application code
* are separated, some of these common system property must reside only in
* the kernel. In that case, uname() can only be called from user space via
* a kernel system call.
*/
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
@ -100,11 +88,7 @@ int uname(FAR struct utsname *name)
/* Get the hostname */
if (-1 == gethostname(name->nodename, HOST_NAME_MAX))
{
ret = -1;
}
ret = gethostname(name->nodename, HOST_NAME_MAX);
name->nodename[HOST_NAME_MAX - 1] = '\0';
strncpy(name->release, CONFIG_VERSION_STRING, SYS_NAMELEN);
@ -123,5 +107,3 @@ int uname(FAR struct utsname *name)
return ret;
}
#endif /* CONFIG_BUILD_FLAT || __KERNEL__ */

View File

@ -41,12 +41,19 @@
#include <nuttx/config.h>
#include <sys/utsname.h>
#include <string.h>
#include <unistd.h>
#include <nuttx/irq.h>
/* Further, in the protected and kernel build modes where kernel and
* application code are separated, the hostname is a common system property
* and must reside only in the kernel. In that case, this accessor
* function only be called from user space is only via a kernel system call.
*/
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@ -96,17 +103,6 @@ char g_hostname[HOST_NAME_MAX + 1] = CONFIG_LIB_HOSTNAME;
int gethostname(FAR char *name, size_t namelen)
{
/* In the protected and kernel build modes where kernel and application code
* are separated, the hostname is a common system property and must reside
* only in the kernel. In that case, we need to do things differently.
*
* uname() is implemented as a system call and can be called from user space.
* So, in these configurations we will get the hostname via the uname
* function.
*/
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
irqstate_t flags;
/* Return the host name, truncating to fit into the user provided buffer.
@ -119,24 +115,6 @@ int gethostname(FAR char *name, size_t namelen)
leave_critical_section(flags);
return 0;
#else
struct utsname info;
int ret;
/* Get uname data */
ret = uname(&info);
if (ret < 0)
{
return ret;
}
/* Return the nodename from the uname data */
strncpy(name, info.nodename, namelen);
return 0;
#endif
}
#endif /* CONFIG_BUILD_FLAT || __KERNEL__ */

View File

@ -30,6 +30,7 @@
"ftruncate","unistd.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","int","off_t"
"getenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","FAR char *","FAR const char *"
"getgid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","gid_t"
"gethostname","unistd.h","","int","FAR char *","size_t"
"getitimer","sys/time.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","int","int","FAR struct itimerval *"
"getpeername","sys/socket.h","defined(CONFIG_NET)","int","int","FAR struct sockaddr *","FAR socklen_t *"
"getpid","unistd.h","","pid_t"
@ -176,7 +177,6 @@
"tls_alloc","nuttx/tls.h","CONFIG_TLS_NELEM > 0","int"
"tls_free","nuttx/tls.h","CONFIG_TLS_NELEM > 0","int","int"
"umount2","sys/mount.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const char *","unsigned int"
"uname","sys/utsname.h","","int","FAR struct utsname *"
"unlink","unistd.h","!defined(CONFIG_DISABLE_MOUNTPOINT)","int","FAR const char *"
"unsetenv","stdlib.h","!defined(CONFIG_DISABLE_ENVIRON)","int","FAR const char *"
"up_assert","nuttx/arch.h","","void","FAR const char *","int"

Can't render this file because it has a wrong number of fields in line 2.