From e1c306f2dd3b8f4f304a77a908e965c05fb3eab2 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 5 Jul 2015 08:39:13 -0600 Subject: [PATCH] Add support for sethostname --- ChangeLog | 2 ++ include/unistd.h | 3 +- libc/unistd/lib_gethostname.c | 62 ++++++++++++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 10693ce3bb..9ccc57e55b 100755 --- a/ChangeLog +++ b/ChangeLog @@ -10665,3 +10665,5 @@ Neklyudov (2015-07-02). * arch/sim/src/up_head.S: Implement board_power_off() for the simulation platform (2015-07-04). + * libc/unistd/lib_gethostname.c: Add support for sethostname() (2015-07-05). + diff --git a/include/unistd.h b/include/unistd.h index 18d5f7860e..a0240e9d1b 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -194,7 +194,8 @@ int *getoptindp(void); /* Index into argv */ int *getoptoptp(void); /* unrecognized option character */ #ifdef CONFIG_NET -int gethostname(char *name, size_t size); +int gethostname(FAR char *name, size_t size); +int sethostname(FAR const char *name, size_t size); #endif #undef EXTERN diff --git a/libc/unistd/lib_gethostname.c b/libc/unistd/lib_gethostname.c index 5c0eaa7f79..060b19cbbe 100644 --- a/libc/unistd/lib_gethostname.c +++ b/libc/unistd/lib_gethostname.c @@ -41,17 +41,42 @@ #include #include +#include + +/* This file is only compiled if network support is enabled */ #ifdef CONFIG_NET +/* 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, these accessor + * functions call only be called from user space is only via kernel system + * calls. + */ + +#if (!defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_BUILD_KERNEL)) || \ + defined(__KERNEL__) + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ +/* The default host name is a system configuration setting. This may be + * changed via sethostname(), however. + */ + #ifndef CONFIG_NET_HOSTNAME # define CONFIG_NET_HOSTNAME "" #endif +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* This is the system hostname */ + +static char g_hostname[HOST_NAME_MAX + 1] = CONFIG_NET_HOSTNAME; + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -83,8 +108,43 @@ int gethostname(FAR char *name, size_t namelen) { /* Return the host name, truncating to fit into the user provided buffer */ - strncpy(name, CONFIG_NET_HOSTNAME, namelen); + strncpy(name, g_hostname, namelen); + return 0; +} + +/**************************************************************************** + * Name: sethostname + * + * Description: + * + * sethostname() sets the hostname to the value given in the character + * array name. The len argument specifies the number of bytes in name. + * (Thus, name does not require a terminating null byte.) + * + * Conforming To + * SVr4, 4.4BSD (these interfaces first appeared in 4.2BSD). POSIX.1-2001 + * specifies gethostname() but not sethostname(). + * + * Input Parameters: + * name - The user buffer to providing the new host name. + * namelen - The size of the user buffer in bytes. + * + * Returned Value: + * Upon successful completion, 0 will be returned; otherwise, -1 will be + * returned. No errors are defined; errno variable is not set. + * + ****************************************************************************/ + +int sethostname(FAR const char *name, size_t size) +{ + /* Save the new host name, truncating to HOST_NAME_MAX if necessary. This + * internal copy is always NUL terminated. + */ + + strncpy(g_hostname, name, MIN(HOST_NAME_MAX, size)); + g_hostname[HOST_NAME_MAX] = '\0'; return 0; } #endif /* CONFIG_NET */ +#endif /* (!CONFIG_BUILD_PROTECTED && !CONFIG_BUILD_KERNEL) || __KERNEL__ */