Skip to content

Commit 5ad347d

Browse files
authored
[libc][POSIX][unistd] Implement gethostname (#128142)
The implementation is based on the specifications from: https://man7.org/linux/man-pages/man2/gethostname.2.html Closes #126602 --------- Signed-off-by: ZakyHermawan <[email protected]>
1 parent 154e063 commit 5ad347d

File tree

10 files changed

+148
-0
lines changed

10 files changed

+148
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ set(TARGET_LIBC_ENTRYPOINTS
332332
libc.src.unistd.getcwd
333333
libc.src.unistd.getentropy
334334
libc.src.unistd.geteuid
335+
libc.src.unistd.gethostname
335336
libc.src.unistd.getpid
336337
libc.src.unistd.getppid
337338
libc.src.unistd.getsid

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ set(TARGET_LIBC_ENTRYPOINTS
336336
libc.src.unistd.getcwd
337337
libc.src.unistd.getentropy
338338
libc.src.unistd.geteuid
339+
libc.src.unistd.gethostname
339340
libc.src.unistd.getpid
340341
libc.src.unistd.getppid
341342
libc.src.unistd.getsid

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ set(TARGET_LIBC_ENTRYPOINTS
338338
libc.src.unistd.getcwd
339339
libc.src.unistd.getentropy
340340
libc.src.unistd.geteuid
341+
libc.src.unistd.gethostname
341342
libc.src.unistd.getpid
342343
libc.src.unistd.getppid
343344
libc.src.unistd.getsid

libc/include/unistd.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ functions:
141141
return_type: uid_t
142142
arguments:
143143
- type: void
144+
- name: gethostname
145+
standards:
146+
- POSIX
147+
return_type: int
148+
arguments:
149+
- type: char *
150+
- type: size_t
144151
- name: getopt
145152
standards:
146153
- POSIX

libc/src/unistd/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ add_entrypoint_object(
111111
.${LIBC_TARGET_OS}.getcwd
112112
)
113113

114+
add_entrypoint_object(
115+
gethostname
116+
ALIAS
117+
DEPENDS
118+
.${LIBC_TARGET_OS}.gethostname
119+
)
120+
114121
add_entrypoint_object(
115122
getpid
116123
ALIAS

libc/src/unistd/gethostname.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for gethostname -------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_UNISTD_GETHOSTNAME_H
10+
#define LLVM_LIBC_SRC_UNISTD_GETHOSTNAME_H
11+
12+
#include "hdr/types/size_t.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
int gethostname(char *name, size_t len);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_UNISTD_GETHOSTNAME_H

libc/src/unistd/linux/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@ add_entrypoint_object(
194194
libc.src.errno.errno
195195
)
196196

197+
add_entrypoint_object(
198+
gethostname
199+
SRCS
200+
gethostname.cpp
201+
HDRS
202+
../gethostname.h
203+
DEPENDS
204+
libc.hdr.types.size_t
205+
libc.include.sys_syscall
206+
libc.include.sys_utsname
207+
libc.src.__support.OSUtil.osutil
208+
libc.src.errno.errno
209+
)
210+
197211
add_entrypoint_object(
198212
geteuid
199213
SRCS
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//===-- Linux implementation of gethostname -------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/unistd/gethostname.h"
10+
11+
#include "hdr/types/size_t.h"
12+
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
13+
#include "src/__support/common.h"
14+
#include "src/__support/libc_errno.h"
15+
#include "src/__support/macros/config.h"
16+
#include "src/string/string_utils.h"
17+
18+
#include <sys/syscall.h> // For syscall numbers.
19+
#include <sys/utsname.h>
20+
21+
namespace LIBC_NAMESPACE_DECL {
22+
23+
LLVM_LIBC_FUNCTION(int, gethostname, (char *name, size_t size)) {
24+
// Check for invalid pointer
25+
if (name == nullptr) {
26+
libc_errno = EFAULT;
27+
return -1;
28+
}
29+
30+
// Because there is no SYS_gethostname syscall, we use uname to get the
31+
// hostname.
32+
utsname unameData;
33+
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_uname, &unameData);
34+
if (ret < 0) {
35+
libc_errno = static_cast<int>(-ret);
36+
return -1;
37+
}
38+
39+
// Guarantee that the name will be null terminated.
40+
// The amount of bytes copied is min(size + 1, strlen(nodename) + 1)
41+
// +1 to account for the null terminator (the last copied byte is a NULL).
42+
internal::strlcpy(name, unameData.nodename, size + 1);
43+
44+
// Checks if the length of the hostname was greater than or equal to size
45+
if (internal::string_length(unameData.nodename) >= size) {
46+
libc_errno = ENAMETOOLONG;
47+
return -1;
48+
}
49+
50+
return 0;
51+
}
52+
53+
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/unistd/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,18 @@ add_libc_unittest(
408408
libc.test.UnitTest.ErrnoSetterMatcher
409409
)
410410

411+
add_libc_unittest(
412+
gethostname_test
413+
SUITE
414+
libc_unistd_unittests
415+
SRCS
416+
gethostname_test.cpp
417+
DEPENDS
418+
libc.src.unistd.gethostname
419+
libc.src.errno.errno
420+
libc.test.UnitTest.ErrnoCheckingTest
421+
)
422+
411423
add_libc_unittest(
412424
getpid_test
413425
SUITE
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===-- Unittests for gethostname -----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/unistd/gethostname.h"
10+
11+
#include "test/UnitTest/ErrnoCheckingTest.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
using LlvmLibcGetHostNameTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
15+
16+
TEST(LlvmLibcGetHostNameTest, GetCurrHostName) {
17+
char hostbuffer[1024];
18+
int ret = LIBC_NAMESPACE::gethostname(hostbuffer, sizeof(hostbuffer));
19+
ASSERT_NE(ret, -1);
20+
ASSERT_ERRNO_SUCCESS();
21+
22+
ret = LIBC_NAMESPACE::gethostname(hostbuffer, 0);
23+
ASSERT_EQ(ret, -1);
24+
ASSERT_ERRNO_EQ(ENAMETOOLONG);
25+
26+
// test for invalid pointer
27+
char *nptr = nullptr;
28+
ret = LIBC_NAMESPACE::gethostname(nptr, 1);
29+
ASSERT_EQ(ret, -1);
30+
ASSERT_ERRNO_EQ(EFAULT);
31+
}

0 commit comments

Comments
 (0)