-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Reland: [libc] implement ioctl #85890 #90317
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
Changes from all commits
05a886d
e1a93de
236873d
4f1c749
1578645
e66e13b
de63af0
c02aab6
8cccec9
5100e86
84147f8
49f745a
805266f
40b9b29
d6ce110
7d8d9d7
4e07b1c
7b76aeb
b784f43
0bb8dd7
fd3bad5
0b8f0db
e5e65f8
7b55dee
82e9533
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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 | ||
) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//===-- Implementation header for mmap function -----------------*- 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 | ||
namespace LIBC_NAMESPACE { | ||
|
||
int ioctl(int fd, unsigned long request, ...); | ||
|
||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC_SYS_IOCTL_IOCTL_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
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 | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//===---------- Linux implementation of the POSIX 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 { | ||
|
||
// This function is currently linux only. It has to be refactored suitably if | ||
// ioctl is to be supported on non-linux operating systems also. | ||
LLVM_LIBC_FUNCTION(int, ioctl, (int fd, unsigned long request, ...)) { | ||
va_list ptr_to_memory; | ||
va_start(ptr_to_memory, request); | ||
va_arg(ptr_to_memory, void *); | ||
int ret = | ||
LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, request, ptr_to_memory); | ||
va_end(ptr_to_memory); | ||
|
||
// A negative return value indicates an error with the magnitude of the | ||
// value being the error code. | ||
if (ret < 0) { | ||
libc_errno = -ret; | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
add_custom_target(libc_sys_ioctl_unittests) | ||
|
||
add_libc_unittest( | ||
ioctl_test | ||
SUITE | ||
libc_sys_ioctl_unittests | ||
SRCS | ||
ioctl_test.cpp | ||
DEPENDS | ||
libc.src.sys.ioctl.ioctl | ||
libc.src.errno.errno | ||
libc.src.fcntl.open | ||
libc.src.unistd.close | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//===-- 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/__support/OSUtil/syscall.h" // For internal syscall function. | ||
#include "src/errno/libc_errno.h" | ||
#include "src/fcntl/open.h" | ||
#include "src/sys/ioctl/ioctl.h" | ||
#include "src/unistd/close.h" | ||
#include "test/UnitTest/ErrnoSetterMatcher.h" | ||
#include "test/UnitTest/LibcTest.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
#include <linux/fs.h> | ||
|
||
TEST(LlvmLibcIoctlTest, InvalidFileDescriptor) { | ||
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails; | ||
int fd = 10; | ||
unsigned long request = 10; | ||
int res = LIBC_NAMESPACE::ioctl(fd, request, NULL); | ||
EXPECT_THAT(res, Fails(EBADF, -1)); | ||
} | ||
|
||
TEST(LlvmLibcIoctlTest, ValidFileDescriptor) { | ||
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds; | ||
LIBC_NAMESPACE::libc_errno = 0; | ||
constexpr const char *FILENAME = "ioctl.test"; | ||
auto TEST_FILE = libc_make_test_file_path(FILENAME); | ||
int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
need to include |
||
ASSERT_ERRNO_SUCCESS(); | ||
ASSERT_GT(fd, 0); | ||
int data; | ||
int res = LIBC_NAMESPACE::ioctl(fd, FS_IOC_GETFLAGS, &data); | ||
EXPECT_THAT(res, Succeeds()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the presubmit build failure:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I applied There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks like the error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is perhaps related to the usage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $ lsattr /dev/null
lsattr: Operation not supported While reading flags on /dev/null There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See how libc/test/src/stdio/ tests create temp files. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like it works now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make sure to close the file There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nickdesaulniers nick There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that a linkage failure? Then that would be a missing dependency in cmake for the test. |
||
ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a test that succeeds as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michaelrj-google do you have an idea what test can I use to always guarantee success ? From the other PR @nickdesaulniers nick
told me I dont need to test for this syscall because it is a linux syscall, and the current test file is fine. I tried a test that I expect to succeeds in the PR but that seems to fail in the CI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps
FS_IOC_GETFLAGS
.https://man7.org/linux/man-pages/man2/ioctl_iflags.2.html
That is used as the example for ioctl in Michael Kerrisk's The Linux Programming Interface page 308.