Skip to content

[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 18 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 2 additions & 7 deletions libc/src/termios/linux/tcdrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <sys/syscall.h> // For syscall numbers
#include <termios.h>

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, tcdrain, (int fd)) {
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCSBRK, 1);
if (ret < 0) {
libc_errno = -ret;
return -1;
}
return 0;
return LIBC_NAMESPACE::ioctl(fd, TCSBRK, 1);
}

} // namespace LIBC_NAMESPACE_DECL
9 changes: 2 additions & 7 deletions libc/src/termios/linux/tcflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <sys/syscall.h> // For syscall numbers
#include <termios.h>

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, tcflow, (int fd, int action)) {
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCXONC, action);
if (ret < 0) {
libc_errno = -ret;
return -1;
}
return 0;
return LIBC_NAMESPACE::ioctl(fd, TCXONC, action);
}

} // namespace LIBC_NAMESPACE_DECL
10 changes: 2 additions & 8 deletions libc/src/termios/linux/tcflush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,14 @@
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <sys/syscall.h> // For syscall numbers
#include <termios.h>

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, tcflush, (int fd, int queue_selector)) {
int ret =
LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCFLSH, queue_selector);
if (ret < 0) {
libc_errno = -ret;
return -1;
}
return 0;
return LIBC_NAMESPACE::ioctl(fd, TCFLSH, queue_selector);
}

} // namespace LIBC_NAMESPACE_DECL
9 changes: 4 additions & 5 deletions libc/src/termios/linux/tcgetattr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <sys/syscall.h> // For syscall numbers
#include <termios.h>

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, tcgetattr, (int fd, struct termios *t)) {
LIBC_NAMESPACE::kernel_termios kt;
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCGETS, &kt);
if (ret < 0) {
libc_errno = -ret;
int ret = LIBC_NAMESPACE::ioctl(fd, TCGETS, &kt);
if (ret < 0)
return -1;
}

t->c_iflag = kt.c_iflag;
t->c_oflag = kt.c_oflag;
t->c_cflag = kt.c_cflag;
Expand Down
9 changes: 4 additions & 5 deletions libc/src/termios/linux/tcgetsid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <sys/syscall.h> // For syscall numbers
#include <termios.h>

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(pid_t, tcgetsid, (int fd)) {
pid_t sid;
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TIOCGSID, &sid);
if (ret < 0) {
libc_errno = -ret;
int ret = LIBC_NAMESPACE::ioctl(fd, TIOCGSID, &sid);
if (ret < 0)
return -1;
}
return sid;
}

Expand Down
9 changes: 2 additions & 7 deletions libc/src/termios/linux/tcsendbreak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <sys/syscall.h> // For syscall numbers
#include <termios.h>

Expand All @@ -23,12 +23,7 @@ LLVM_LIBC_FUNCTION(pid_t, tcsendbreak, (int fd, int /* unused duration */)) {
// POSIX leaves the behavior for non-zero duration implementation dependent.
// Which means that the behavior can be the same as it is when duration is
// zero. So, we just pass zero to the syscall.
int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TCSBRK, 0);
if (ret < 0) {
libc_errno = -ret;
return -1;
}
return 0;
return LIBC_NAMESPACE::ioctl(SYS_ioctl, fd, TCSBRK, 0);
}

} // namespace LIBC_NAMESPACE_DECL
9 changes: 2 additions & 7 deletions libc/src/termios/linux/tcsetattr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "src/__support/macros/config.h"
#include "src/errno/libc_errno.h"

#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <asm/ioctls.h> // Safe to include without the risk of name pollution.
#include <sys/syscall.h> // For syscall numbers
#include <termios.h>

Expand Down Expand Up @@ -52,12 +52,7 @@ LLVM_LIBC_FUNCTION(int, tcsetattr,
kt.c_cc[i] = 0;
}

int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, cmd, &kt);
if (ret < 0) {
libc_errno = -ret;
return -1;
}
return 0;
return LIBC_NAMESPACE::ioctl(fd, cmd, &kt);
}

} // namespace LIBC_NAMESPACE_DECL
4 changes: 1 addition & 3 deletions libc/src/unistd/linux/isatty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ LLVM_LIBC_FUNCTION(int, isatty, (int fd)) {
int line_d_val = INIT_VAL;
// This gets the line dicipline of the terminal. When called on something that
// isn't a terminal it doesn't change line_d_val and returns -1.
int result =
LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, TIOCGETD, &line_d_val);
int result = LIBC_NAMESPACE::ioctl(fd, TIOCGETD, &line_d_val);
if (result == 0)
return 1;

libc_errno = -result;
return 0;
}

Expand Down