Undo part of a previous change. Move get/sethostname back from net/netdb to libc/unistd

This commit is contained in:
Gregory Nutt
2015-07-08 11:11:52 -06:00
parent 4e98d01098
commit f94fe747ae
13 changed files with 73 additions and 530 deletions
+1 -2
View File
@@ -43,7 +43,6 @@
#include <string.h>
#include <nuttx/version.h>
#include <nuttx/net/netdb.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
@@ -101,7 +100,7 @@ int uname(FAR struct utsname *name)
#ifdef CONFIG_NET
/* Get the hostname */
if (-1 == netdb_gethostname(name->nodename, HOST_NAME_MAX))
if (-1 == gethostname(name->nodename, HOST_NAME_MAX))
{
ret = -1;
}
+34 -7
View File
@@ -2,7 +2,9 @@
* libc/unistd/lib_gethostname.c
*
* Copyright (C) 2015 Stavros Polymenis. All rights reserved.
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Stavros Polymenis <sp@orbitalfox.com>
* Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -43,7 +45,7 @@
#include <string.h>
#include <unistd.h>
#include <nuttx/net/netdb.h>
#include <arch/irq.h>
/* This file is only compiled if network support is enabled */
@@ -53,6 +55,22 @@
* 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
/****************************************************************************
* Public Data
****************************************************************************/
/* This is the system hostname */
char g_hostname[HOST_NAME_MAX + 1] = CONFIG_NET_HOSTNAME;
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -94,6 +112,21 @@ int gethostname(FAR char *name, size_t namelen)
#if (!defined(CONFIG_BUILD_PROTECTED) && !defined(CONFIG_BUILD_KERNEL)) || \
defined(__KERNEL__)
irqstate_t flags;
/* Return the host name, truncating to fit into the user provided buffer.
* The hostname is global resource. There is a microscopic possibility
* that it could change while we are copying it.
*/
flags = irqsave();
strncpy(name, g_hostname, namelen);
irqrestore(flags);
return 0;
#else
struct utsname info;
int ret;
@@ -110,12 +143,6 @@ int gethostname(FAR char *name, size_t namelen)
strncpy(name, info.nodename, namelen);
return 0;
#else
/* Otherwise, this function is just a thin wrapper around
* netdb_gethostname().
*/
return netdb_gethostname(name, namelen);
#endif
}
+35 -3
View File
@@ -39,9 +39,10 @@
#include <nuttx/config.h>
#include <string.h>
#include <unistd.h>
#include <nuttx/net/netdb.h>
#include <arch/irq.h>
/* This file is only compiled if network support is enabled */
@@ -60,6 +61,26 @@
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef MIN
# define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
# define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/* This is the system hostname (defined in lib_gethostname). */
extern char g_hostname[HOST_NAME_MAX + 1];
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -89,9 +110,20 @@
int sethostname(FAR const char *name, size_t size)
{
/* This is just a thin layer over netdb_sethostname() */
irqstate_t flags;
return netdb_sethostname(name, size);
/* Save the new host name, truncating to HOST_NAME_MAX if necessary. This
* internal copy is always NUL terminated. The hostname is global resource.
* There is a microscopic possibility that it could be accessed while we
* are setting it.
*/
flags = irqsave();
strncpy(g_hostname, name, MIN(HOST_NAME_MAX, size));
g_hostname[HOST_NAME_MAX] = '\0';
irqrestore(flags);
return 0;
}
#endif /* CONFIG_NET */