Skip to content

[libc] Add basic ioctl function #97509

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -206,6 +206,9 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdio.vsnprintf
libc.src.stdio.vsprintf

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

# sys/mman.h entrypoints
libc.src.sys.mman.madvise
libc.src.sys.mman.mincore
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 @@ -226,6 +226,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.mincore
Expand Down
8 changes: 7 additions & 1 deletion libc/spec/posix.td
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,13 @@ def POSIX : StandardSpec<"POSIX"> {
], // Macros
[], // Types
[], // Enumerations
[] // Functions
[
FunctionSpec<
"ioctl",
RetValSpec<IntType>,
[ArgSpec<IntType>, ArgSpec<IntType>, ArgSpec<VarArgType>]
>
] // Functions
>;

HeaderSpec Spawn = HeaderSpec<
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
10 changes: 10 additions & 0 deletions libc/src/sys/ioctl/CMakeLists.txt
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
)
18 changes: 18 additions & 0 deletions libc/src/sys/ioctl/ioctl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- 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

namespace LIBC_NAMESPACE {

int ioctl(int fd, int req, ...);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_SYS_IOCTL_IOCTL_H
12 changes: 12 additions & 0 deletions libc/src/sys/ioctl/linux/CMakeLists.txt
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
)
24 changes: 24 additions & 0 deletions libc/src/sys/ioctl/linux/ioctl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdarg.h>
#include <sys/syscall.h> // For syscall numbers.

#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/common.h"
#include "src/sys/ioctl/ioctl.h"

#include "src/errno/libc_errno.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, ioctl, (int fd, int req, ...)) {
void *arg;
va_list ap;
va_start(ap, req);
arg = va_arg(ap, void *);
va_end(ap);
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, req, arg);
// FIXME(@izaakschroeder): There is probably more to do here.
// See: https://github.com/kraj/musl/blob/kraj/master/src/misc/ioctl.c
return ret;
}

} // namespace LIBC_NAMESPACE
Loading