src,include: define UV_MAXHOSTNAMESIZE
This commit adds UV_MAXHOSTNAMESIZE for working with uv_os_gethostname(). Prior to this commit, this logic was duplicated in several places across libuv and Node.js alone. PR-URL: https://github.com/libuv/libuv/pull/2175 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
parent
e2baa87b59
commit
8865e72e25
@ -534,6 +534,10 @@ API
|
|||||||
|
|
||||||
.. versionadded:: 1.12.0
|
.. versionadded:: 1.12.0
|
||||||
|
|
||||||
|
.. versionchanged:: 1.26.0 `UV_MAXHOSTNAMESIZE` is available and represents
|
||||||
|
the maximum `buffer` size required to store a
|
||||||
|
hostname and terminating `nul` character.
|
||||||
|
|
||||||
.. c:function:: int uv_os_getpriority(uv_pid_t pid, int* priority)
|
.. c:function:: int uv_os_getpriority(uv_pid_t pid, int* priority)
|
||||||
|
|
||||||
Retrieves the scheduling priority of the process specified by `pid`. The
|
Retrieves the scheduling priority of the process specified by `pid`. The
|
||||||
|
|||||||
11
include/uv.h
11
include/uv.h
@ -1144,6 +1144,17 @@ UV_EXTERN int uv_os_getenv(const char* name, char* buffer, size_t* size);
|
|||||||
UV_EXTERN int uv_os_setenv(const char* name, const char* value);
|
UV_EXTERN int uv_os_setenv(const char* name, const char* value);
|
||||||
UV_EXTERN int uv_os_unsetenv(const char* name);
|
UV_EXTERN int uv_os_unsetenv(const char* name);
|
||||||
|
|
||||||
|
#ifdef MAXHOSTNAMELEN
|
||||||
|
# define UV_MAXHOSTNAMESIZE (MAXHOSTNAMELEN + 1)
|
||||||
|
#else
|
||||||
|
/*
|
||||||
|
Fallback for the maximum hostname size, including the null terminator. The
|
||||||
|
Windows gethostname() documentation states that 256 bytes will always be
|
||||||
|
large enough to hold the null-terminated hostname.
|
||||||
|
*/
|
||||||
|
# define UV_MAXHOSTNAMESIZE 256
|
||||||
|
#endif
|
||||||
|
|
||||||
UV_EXTERN int uv_os_gethostname(char* buffer, size_t* size);
|
UV_EXTERN int uv_os_gethostname(char* buffer, size_t* size);
|
||||||
|
|
||||||
UV_EXTERN int uv_os_uname(uv_utsname_t* buffer);
|
UV_EXTERN int uv_os_uname(uv_utsname_t* buffer);
|
||||||
|
|||||||
@ -31,13 +31,14 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h> /* MAXHOSTNAMELEN on Solaris */
|
||||||
|
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
|
|
||||||
#if !defined(__MVS__)
|
#if !defined(__MVS__)
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
|
#include <sys/param.h> /* MAXHOSTNAMELEN on Linux and the BSDs */
|
||||||
#endif
|
#endif
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|||||||
@ -43,7 +43,6 @@
|
|||||||
#include <sys/utsname.h>
|
#include <sys/utsname.h>
|
||||||
|
|
||||||
#ifdef __sun
|
#ifdef __sun
|
||||||
# include <netdb.h> /* MAXHOSTNAMELEN on Solaris */
|
|
||||||
# include <sys/filio.h>
|
# include <sys/filio.h>
|
||||||
# include <sys/types.h>
|
# include <sys/types.h>
|
||||||
# include <sys/wait.h>
|
# include <sys/wait.h>
|
||||||
@ -88,15 +87,6 @@
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(__MVS__)
|
|
||||||
#include <sys/param.h> /* MAXHOSTNAMELEN on Linux and the BSDs */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Fallback for the maximum hostname length */
|
|
||||||
#ifndef MAXHOSTNAMELEN
|
|
||||||
# define MAXHOSTNAMELEN 256
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int uv__run_pending(uv_loop_t* loop);
|
static int uv__run_pending(uv_loop_t* loop);
|
||||||
|
|
||||||
/* Verify that uv_buf_t is ABI-compatible with struct iovec. */
|
/* Verify that uv_buf_t is ABI-compatible with struct iovec. */
|
||||||
@ -1291,7 +1281,7 @@ int uv_os_gethostname(char* buffer, size_t* size) {
|
|||||||
instead by creating a large enough buffer and comparing the hostname length
|
instead by creating a large enough buffer and comparing the hostname length
|
||||||
to the size input.
|
to the size input.
|
||||||
*/
|
*/
|
||||||
char buf[MAXHOSTNAMELEN + 1];
|
char buf[UV_MAXHOSTNAMESIZE];
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
if (buffer == NULL || size == NULL || *size == 0)
|
if (buffer == NULL || size == NULL || *size == 0)
|
||||||
|
|||||||
@ -59,13 +59,6 @@
|
|||||||
# define UNLEN 256
|
# define UNLEN 256
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
|
||||||
Max hostname length. The Windows gethostname() documentation states that 256
|
|
||||||
bytes will always be large enough to hold the null-terminated hostname.
|
|
||||||
*/
|
|
||||||
#ifndef MAXHOSTNAMELEN
|
|
||||||
# define MAXHOSTNAMELEN 256
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* Maximum environment variable size, including the terminating null */
|
/* Maximum environment variable size, including the terminating null */
|
||||||
#define MAX_ENV_VAR_LENGTH 32767
|
#define MAX_ENV_VAR_LENGTH 32767
|
||||||
@ -1507,7 +1500,7 @@ int uv_os_unsetenv(const char* name) {
|
|||||||
|
|
||||||
|
|
||||||
int uv_os_gethostname(char* buffer, size_t* size) {
|
int uv_os_gethostname(char* buffer, size_t* size) {
|
||||||
char buf[MAXHOSTNAMELEN + 1];
|
char buf[UV_MAXHOSTNAMESIZE];
|
||||||
size_t len;
|
size_t len;
|
||||||
|
|
||||||
if (buffer == NULL || size == NULL || *size == 0)
|
if (buffer == NULL || size == NULL || *size == 0)
|
||||||
|
|||||||
@ -23,12 +23,8 @@
|
|||||||
#include "task.h"
|
#include "task.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#ifndef MAXHOSTNAMELEN
|
|
||||||
# define MAXHOSTNAMELEN 256
|
|
||||||
#endif
|
|
||||||
|
|
||||||
TEST_IMPL(gethostname) {
|
TEST_IMPL(gethostname) {
|
||||||
char buf[MAXHOSTNAMELEN + 1];
|
char buf[UV_MAXHOSTNAMESIZE];
|
||||||
size_t size;
|
size_t size;
|
||||||
size_t enobufs_size;
|
size_t enobufs_size;
|
||||||
int r;
|
int r;
|
||||||
@ -52,7 +48,7 @@ TEST_IMPL(gethostname) {
|
|||||||
ASSERT(enobufs_size > 1);
|
ASSERT(enobufs_size > 1);
|
||||||
|
|
||||||
/* Successfully get the hostname */
|
/* Successfully get the hostname */
|
||||||
size = MAXHOSTNAMELEN + 1;
|
size = UV_MAXHOSTNAMESIZE;
|
||||||
r = uv_os_gethostname(buf, &size);
|
r = uv_os_gethostname(buf, &size);
|
||||||
ASSERT(r == 0);
|
ASSERT(r == 0);
|
||||||
ASSERT(size > 1 && size == strlen(buf));
|
ASSERT(size > 1 && size == strlen(buf));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user