-
Couldn't load subscription status.
- Fork 15k
[libc][POSIX][unistd] Implement gethostname #128142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c44b475
Implement gethostname
ZakyHermawan 89494d2
style: fix clang-format issues
ZakyHermawan 0cedfc2
change len to size, use internal implementation instead of calling ot…
ZakyHermawan c23492b
remove unnecessary strlen and strncpy from CMakeLists.txt
ZakyHermawan 0e56949
fix clang-format
ZakyHermawan 549c8d9
Merge branch 'main' into implement-gethostname
ZakyHermawan aeb9ebe
fix error caused by errno update
ZakyHermawan 9e141d5
fix formatting
ZakyHermawan 32f3fc7
remove unnecessary includes
ZakyHermawan fb720b0
[NFC] remove unnecessary struct keyword
ZakyHermawan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
ZakyHermawan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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; | ||
ZakyHermawan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_uname, &unameData); | ||
ZakyHermawan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // 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) { | ||
ZakyHermawan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| strncpy(name, unameData.nodename, len); | ||
ZakyHermawan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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; | ||
| } | ||
ZakyHermawan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.