Skip to content

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

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ set(TARGET_LIBC_ENTRYPOINTS
#libc.src.stdio.scanf
#libc.src.stdio.fscanf

# sys/ioctl.h entrypoints
libc.src.sys.ioctl.ioctl

# sys/mman.h entrypoints
libc.src.sys.mman.madvise
libc.src.sys.mman.mmap
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.scanf
libc.src.stdio.fscanf

# sys/ioctl.h entrypoints
libc.src.sys.ioctl.ioctl

# sys/mman.h entrypoints
libc.src.sys.mman.madvise
libc.src.sys.mman.mmap
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ set(TARGET_LIBC_ENTRYPOINTS
# https://github.com/llvm/llvm-project/issues/80060
# libc.src.sys.epoll.epoll_pwait2

# sys/ioctl.h entrypoints
libc.src.sys.ioctl.ioctl

# sys/mman.h entrypoints
libc.src.sys.mman.madvise
libc.src.sys.mman.mmap
Expand Down
18 changes: 18 additions & 0 deletions libc/spec/linux.td
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ def Linux : StandardSpec<"Linux"> {
[] // Functions
>;

HeaderSpec SysIoctl = HeaderSpec<
"sys/ioctl.h",
[Macro<"MAP_ANONYMOUS">],
[], // Types
[], // Enumerations
[
FunctionSpec<
"ioctl",
RetValSpec<IntType>,
[
ArgSpec<IntType>,
ArgSpec<UnsignedLongType>,
ArgSpec<VoidPtr>,
]
>,
] // Functions
>;

HeaderSpec SysMMan = HeaderSpec<
"sys/mman.h",
[Macro<"MAP_ANONYMOUS">],
Expand Down
1 change: 1 addition & 0 deletions libc/src/sys/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_subdirectory(auxv)
add_subdirectory(epoll)
add_subdirectory(ioctl)
add_subdirectory(mman)
add_subdirectory(random)
add_subdirectory(resource)
Expand Down
12 changes: 12 additions & 0 deletions libc/src/sys/ioctl/CMakeLists.txt
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
)


17 changes: 17 additions & 0 deletions libc/src/sys/ioctl/ioctl.h
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
13 changes: 13 additions & 0 deletions libc/src/sys/ioctl/linux/CMakeLists.txt
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
)

39 changes: 39 additions & 0 deletions libc/src/sys/ioctl/linux/ioctl.cpp
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
// madvise is to be supported on non-linux operating systems also.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: update comment to refer to the correct function instead of madvise.

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
8 changes: 5 additions & 3 deletions libc/test/src/sys/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
add_subdirectory(auxv)
add_subdirectory(epoll)
add_subdirectory(ioctl)
add_subdirectory(mman)
add_subdirectory(prctl)
add_subdirectory(random)
add_subdirectory(resource)
add_subdirectory(select)
Expand All @@ -8,6 +12,4 @@ add_subdirectory(stat)
add_subdirectory(statvfs)
add_subdirectory(utsname)
add_subdirectory(wait)
add_subdirectory(prctl)
add_subdirectory(auxv)
add_subdirectory(epoll)

3 changes: 3 additions & 0 deletions libc/test/src/sys/ioctl/CMakeLists.txt
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()
13 changes: 13 additions & 0 deletions libc/test/src/sys/ioctl/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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
)

27 changes: 27 additions & 0 deletions libc/test/src/sys/ioctl/linux/ioctl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===-- 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/sys/ioctl/ioctl.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/LibcTest.h"
#include "test/UnitTest/Test.h"

#include <sys/syscall.h>
#include <unistd.h>

using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;

TEST(LlvmLibcIoctlTest, InvalidFileDescriptor) {
int fd = 10;
unsigned long request = 10;
int res = LIBC_NAMESPACE::ioctl(fd, request, NULL);
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

EXPECT_THAT(res, Fails(EBADF, -1));
}
Loading