Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.getcwd
libc.src.unistd.getentropy
libc.src.unistd.geteuid
libc.src.unistd.gethostname
libc.src.unistd.getpid
libc.src.unistd.getppid
libc.src.unistd.getsid
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.ftruncate
libc.src.unistd.getcwd
libc.src.unistd.geteuid
libc.src.unistd.gethostname
libc.src.unistd.getpid
libc.src.unistd.getppid
libc.src.unistd.getsid
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.getcwd
libc.src.unistd.getentropy
libc.src.unistd.geteuid
libc.src.unistd.gethostname
libc.src.unistd.getpid
libc.src.unistd.getppid
libc.src.unistd.getsid
Expand Down
7 changes: 7 additions & 0 deletions libc/include/unistd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ functions:
return_type: uid_t
arguments:
- type: void
- name: gethostname
standards:
- POSIX
return_type: int
arguments:
- type: char *
- type: size_t
- name: getopt
standards:
- POSIX
Expand Down
7 changes: 7 additions & 0 deletions libc/src/unistd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.getcwd
)

add_entrypoint_object(
gethostname
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.gethostname
)

add_entrypoint_object(
getpid
ALIAS
Expand Down
22 changes: 22 additions & 0 deletions libc/src/unistd/gethostname.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Implementation header for gethostname -------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_UNISTD_GETHOSTNAME_H
#define LLVM_LIBC_SRC_UNISTD_GETHOSTNAME_H

#include "hdr/types/size_t.h"
#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

int gethostname(char *name, size_t len);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_UNISTD_GETHOSTNAME_H
16 changes: 16 additions & 0 deletions libc/src/unistd/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ add_entrypoint_object(
libc.src.errno.errno
)

add_entrypoint_object(
gethostname
SRCS
gethostname.cpp
HDRS
../gethostname.h
DEPENDS
libc.hdr.types.size_t
libc.include.sys_syscall
libc.include.sys_utsname
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
libc.src.string.strlen
libc.src.string.strncpy
)

add_entrypoint_object(
geteuid
SRCS
Expand Down
60 changes: 60 additions & 0 deletions libc/src/unistd/linux/gethostname.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//===-- Linux implementation of gethostname -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/unistd/gethostname.h"

#include "hdr/types/size_t.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/__support/macros/config.h"

#include "src/errno/libc_errno.h"
#include "src/string/strlen.h"
#include "src/string/strncpy.h"

#include <sys/syscall.h> // For syscall numbers.
#include <sys/utsname.h>

namespace LIBC_NAMESPACE_DECL {

// Matching the behavior of glibc version 2.2 and later.
// Copies up to len bytes from the returned nodename field into name.
LLVM_LIBC_FUNCTION(int, gethostname, (char *name, size_t len)) {
// Check for invalid pointer
if (name == nullptr) {
libc_errno = EFAULT;
return -1;
}

struct utsname unameData;
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_uname, &unameData);

// Checks if the length of the nodename was greater than or equal to len, and
// if it is, then the function returns -1 with errno set to ENAMETOOLONG. In
// this case, a terminating null byte is not included in the returned name.
if (strlen(unameData.nodename) >= len) {
strncpy(name, unameData.nodename, len);
libc_errno = ENAMETOOLONG;
return -1;
}

// If the size of the array name is not large enough (less than the size of
// nodename with null termination), then anything might happen. In this case,
// what happens to the array name will be determined by the implementation of
// LIBC_NAMESPACE_DECL::strncpy
strncpy(name, unameData.nodename, len);

if (ret < 0) {
libc_errno = static_cast<int>(-ret);
return -1;
}

return 0;
}

} // namespace LIBC_NAMESPACE_DECL
11 changes: 11 additions & 0 deletions libc/test/src/unistd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,17 @@ add_libc_unittest(
libc.src.unistd.unlinkat
)

add_libc_unittest(
gethostname_test
SUITE
libc_unistd_unittests
SRCS
gethostname_test.cpp
DEPENDS
libc.src.unistd.gethostname
libc.src.errno.errno
)

add_libc_unittest(
getpid_test
SUITE
Expand Down
28 changes: 28 additions & 0 deletions libc/test/src/unistd/gethostname_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- Unittests for gethostname -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/errno/libc_errno.h"
#include "src/unistd/gethostname.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcGetHostNameTest, GetCurrHostName) {
char hostbuffer[1024];
int ret = LIBC_NAMESPACE::gethostname(hostbuffer, sizeof(hostbuffer));
ASSERT_NE(ret, -1);
ASSERT_ERRNO_SUCCESS();

ret = LIBC_NAMESPACE::gethostname(hostbuffer, 0);
ASSERT_EQ(ret, -1);
ASSERT_ERRNO_EQ(ENAMETOOLONG);

// test for invalid pointer
char *nptr = nullptr;
ret = LIBC_NAMESPACE::gethostname(nptr, 1);
ASSERT_EQ(ret, -1);
ASSERT_ERRNO_EQ(EFAULT);
}
Loading