-
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 all 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,21 @@ | ||
| //===-- 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 "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,53 @@ | ||
| //===-- 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/libc_errno.h" | ||
| #include "src/__support/macros/config.h" | ||
| #include "src/string/string_utils.h" | ||
|
|
||
| #include <sys/syscall.h> // For syscall numbers. | ||
| #include <sys/utsname.h> | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| LLVM_LIBC_FUNCTION(int, gethostname, (char *name, size_t size)) { | ||
| // Check for invalid pointer | ||
| if (name == nullptr) { | ||
| libc_errno = EFAULT; | ||
| return -1; | ||
| } | ||
|
|
||
| // Because there is no SYS_gethostname syscall, we use uname to get the | ||
| // hostname. | ||
| utsname unameData; | ||
| int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_uname, &unameData); | ||
| if (ret < 0) { | ||
| libc_errno = static_cast<int>(-ret); | ||
| return -1; | ||
| } | ||
|
|
||
| // Guarantee that the name will be null terminated. | ||
| // The amount of bytes copied is min(size + 1, strlen(nodename) + 1) | ||
| // +1 to account for the null terminator (the last copied byte is a NULL). | ||
| internal::strlcpy(name, unameData.nodename, size + 1); | ||
|
|
||
| // Checks if the length of the hostname was greater than or equal to size | ||
| if (internal::string_length(unameData.nodename) >= size) { | ||
| libc_errno = ENAMETOOLONG; | ||
| return -1; | ||
| } | ||
|
|
||
| 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,31 @@ | ||
| //===-- 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/unistd/gethostname.h" | ||
|
|
||
| #include "test/UnitTest/ErrnoCheckingTest.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| using LlvmLibcGetHostNameTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest; | ||
|
|
||
| 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.