You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ma...@apache.org on 2020/07/30 01:33:21 UTC

[incubator-nuttx] 01/02: libc: Make gethostname as syscall instead of uname

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

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

commit 3cff139b855a0c1ccfc6253f7d255bc9c2ac636f
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Tue Jul 28 17:52:17 2020 +0800

    libc: Make gethostname as syscall instead of uname
    
    simplify and symmetry the implementation in KERNEL/PROTECTED build
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
    Change-Id: Iefdeea5f6ef6348c774b2ca9f7e45fe89c0c22dd
---
 include/sys/syscall_lookup.h       |  2 +-
 libs/libc/misc/lib_utsname.c       | 20 +-----------------
 libs/libc/unistd/lib_gethostname.c | 42 +++++++++-----------------------------
 syscall/syscall.csv                |  2 +-
 4 files changed, 13 insertions(+), 53 deletions(-)

diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h
index a5f1d0e..f07906a 100644
--- a/include/sys/syscall_lookup.h
+++ b/include/sys/syscall_lookup.h
@@ -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 */
diff --git a/libs/libc/misc/lib_utsname.c b/libs/libc/misc/lib_utsname.c
index 03c91b3..181af54 100644
--- a/libs/libc/misc/lib_utsname.c
+++ b/libs/libc/misc/lib_utsname.c
@@ -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__ */
diff --git a/libs/libc/unistd/lib_gethostname.c b/libs/libc/unistd/lib_gethostname.c
index 9ed6f44..c02b2d9 100644
--- a/libs/libc/unistd/lib_gethostname.c
+++ b/libs/libc/unistd/lib_gethostname.c
@@ -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__ */
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index 475d287..3389ac8 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -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"