-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[libc] add ioctl #141393
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
[libc] add ioctl #141393
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5fad00a
[libc] add ioctl
cowtoolz 51499ce
fix ioctl linux interior return value documentation
cowtoolz e749455
Update libc/src/sys/ioctl/linux/ioctl.cpp
cowtoolz 25fbc06
replace ioctl syscalls with internal ioctl
cowtoolz 8083813
Revert "replace ioctl syscalls with internal ioctl"
cowtoolz b5b86c2
Update libc/test/src/sys/ioctl/linux/ioctl_test.cpp
cowtoolz 759e2f2
fix bad includes and missing macro for ioctl test
cowtoolz 98ef42e
various improvements to ioctl tests
cowtoolz 3cfd2f8
fix missing include
cowtoolz aed221e
fix incorrect type for assertion param
cowtoolz d628541
add missing test dependencies to cmakelists
cowtoolz 924ed4c
fix inappropriate ioctl for device in ioctl tests
cowtoolz e46974b
test cleanup
cowtoolz c0fed59
add ioctl wrapper header
cowtoolz 3a71160
add ioctl wrapper header
cowtoolz 94d3491
fix ioctl test file creation
cowtoolz 8252739
remove testdata directory
cowtoolz d25618f
run clang-format
cowtoolz 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 @@ | ||
//===-- Definition of macros from sys/ioctl.h -----------------------------===// | ||
// | ||
// 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_HDR_SYS_IOCTL_MACROS_H | ||
#define LLVM_LIBC_HDR_SYS_IOCTL_MACROS_H | ||
|
||
#ifdef LIBC_FULL_BUILD | ||
|
||
#include "include/llvm-libc-macros/sys-ioctl-macros.h" | ||
|
||
#else // Overlay mode | ||
|
||
#include <sys/ioctl.h> | ||
|
||
#endif // LLVM_LIBC_FULL_BUILD | ||
|
||
#endif // LLVM_LIBC_HDR_SYS_IOCTL_MACROS_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
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,10 @@ | ||
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) | ||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) | ||
endif() | ||
|
||
add_entrypoint_object( | ||
ioctl | ||
ALIAS | ||
DEPENDS | ||
.${LIBC_TARGET_OS}.ioctl | ||
) |
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,20 @@ | ||
//===-- Implementation header for ioctl ---------------------------*-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_SYS_IOCTL_IOCTL_H | ||
#define LLVM_LIBC_SRC_SYS_IOCTL_IOCTL_H | ||
|
||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
int ioctl(int fd, unsigned long request, ...); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_SYS_IOCTL_IOCTL_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
add_entrypoint_object( | ||
ioctl | ||
SRCS | ||
ioctl.cpp | ||
HDRS | ||
../ioctl.h | ||
DEPENDS | ||
libc.include.sys_ioctl | ||
libc.include.sys_syscall | ||
libc.src.__support.OSUtil.osutil | ||
libc.src.errno.errno | ||
) |
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,36 @@ | ||
//===---------- Linux implementation of the ioctl function ----------------===// | ||
// | ||
// 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/sys/ioctl/ioctl.h" | ||
|
||
#include "src/__support/OSUtil/syscall.h" // For internal syscall function. | ||
#include "src/__support/common.h" | ||
#include "src/errno/libc_errno.h" | ||
#include <stdarg.h> | ||
#include <sys/syscall.h> // For syscall numbers. | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(int, ioctl, (int fd, unsigned long request, ...)) { | ||
va_list vargs; | ||
va_start(vargs, request); | ||
void *data_pointer = va_arg(vargs, void *); | ||
cowtoolz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int ret = | ||
LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, request, data_pointer); | ||
va_end(vargs); | ||
|
||
// Some ioctls can be expected to return positive values | ||
if (ret >= 0) | ||
return ret; | ||
|
||
// If there is an error, errno is set and -1 is returned. | ||
libc_errno = -ret; | ||
return -1; | ||
} | ||
|
||
} // 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,3 @@ | ||
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}) | ||
add_subdirectory(${LIBC_TARGET_OS}) | ||
endif() |
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,17 @@ | ||
add_custom_target(libc_sys_ioctl_unittests) | ||
|
||
add_libc_unittest( | ||
ioctl_test | ||
SUITE | ||
libc_sys_ioctl_unittests | ||
SRCS | ||
ioctl_test.cpp | ||
DEPENDS | ||
libc.hdr.ioctl_macros | ||
libc.src.sys.ioctl.ioctl | ||
libc.src.errno.errno | ||
libc.src.fcntl.open | ||
libc.src.unistd.close | ||
libc.src.unistd.read | ||
libc.src.unistd.write | ||
) |
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,75 @@ | ||
//===-- Unittests for ioctl -----------------------------------------------===// | ||
// | ||
// 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/fcntl/open.h" | ||
#include "src/sys/ioctl/ioctl.h" | ||
#include "src/unistd/close.h" | ||
#include "src/unistd/read.h" | ||
#include "src/unistd/write.h" | ||
|
||
#include "test/UnitTest/ErrnoSetterMatcher.h" | ||
cowtoolz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include "test/UnitTest/Test.h" | ||
|
||
#include "hdr/sys_stat_macros.h" | ||
|
||
#include "hdr/sys_ioctl_macros.h" | ||
|
||
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; | ||
|
||
TEST(LlvmLibcSysIoctlTest, InvalidCommandAndFIONREAD) { | ||
LIBC_NAMESPACE::libc_errno = 0; | ||
|
||
// Setup the test file | ||
constexpr const char *TEST_FILE_NAME = "ioctl.test"; | ||
constexpr const char TEST_MSG[] = "ioctl test"; | ||
constexpr int TEST_MSG_SIZE = sizeof(TEST_MSG) - 1; | ||
auto TEST_FILE = libc_make_test_file_path(TEST_FILE_NAME); | ||
int new_test_file_fd = LIBC_NAMESPACE::open( | ||
TEST_FILE, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); | ||
ASSERT_THAT( | ||
(int)LIBC_NAMESPACE::write(new_test_file_fd, TEST_MSG, TEST_MSG_SIZE), | ||
Succeeds(TEST_MSG_SIZE)); | ||
ASSERT_ERRNO_SUCCESS(); | ||
ASSERT_THAT(LIBC_NAMESPACE::close(new_test_file_fd), Succeeds(0)); | ||
ASSERT_ERRNO_SUCCESS(); | ||
|
||
// Reopen the file for testing | ||
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_RDONLY); | ||
ASSERT_ERRNO_SUCCESS(); | ||
ASSERT_GT(fd, 0); | ||
|
||
// FIONREAD reports the number of available bytes to read for the passed fd | ||
// This will report the full size of the file, as we haven't read anything yet | ||
int n = -1; | ||
int ret = LIBC_NAMESPACE::ioctl(fd, FIONREAD, &n); | ||
ASSERT_ERRNO_SUCCESS(); | ||
ASSERT_GT(ret, -1); | ||
ASSERT_EQ(n, TEST_MSG_SIZE); | ||
|
||
// But if we read some bytes... | ||
constexpr int READ_COUNT = 5; | ||
char read_buffer[READ_COUNT]; | ||
ASSERT_THAT((int)LIBC_NAMESPACE::read(fd, read_buffer, READ_COUNT), | ||
Succeeds(READ_COUNT)); | ||
|
||
// ... n should have decreased by the number of bytes we've read | ||
int n_after_reading = -1; | ||
ret = LIBC_NAMESPACE::ioctl(fd, FIONREAD, &n_after_reading); | ||
ASSERT_ERRNO_SUCCESS(); | ||
ASSERT_GT(ret, -1); | ||
ASSERT_EQ(n - READ_COUNT, n_after_reading); | ||
|
||
// 0xDEADBEEF is just a random nonexistent command; | ||
// calling this should always fail with ENOTTY | ||
ret = LIBC_NAMESPACE::ioctl(fd, 0xDEADBEEF, NULL); | ||
ASSERT_ERRNO_EQ(ENOTTY); | ||
ASSERT_EQ(ret, -1); | ||
|
||
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); | ||
} |
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.