IOCTL(2)
π IOCTL β control device π
okay bestie, buckle up, because we're diving into a system call from the System Calls Manual and it's giving main character energy fr fr. no cap, this one is iconic. π
β¨ NAME β¨
ioctl β control device ποΈ
π LIBRARY π
Standard C Library (libc, -lc) π«Ά
πΈ SYNOPSIS πΈ
#include <sys/ioctl.h>
int
ioctl(int fd, unsigned long request, ...);
π DESCRIPTION π
so like. the ioctl() system call manipulates the underlying device parameters of special files. literally that's its whole job and it ate. π
in particular, many operating characteristics of character special files (e.g. terminals) may be controlled with ioctl() requests. the argument fd must be an open file descriptor. periodt. π₯
the third argument to ioctl() is traditionally named char *argp. however, most uses of ioctl() require the third argument to be a caddr_t or an int. the way that tradition and reality are just not the same thing π
an ioctl() request has encoded in it whether the argument is an βinβ argument or βoutβ argument, and the size of the argument argp in bytes. macros and defines used in specifying an ioctl request are located in the file <sys/ioctl.h>. iykyk β¨
π¦ GENERIC IOCTLS π¦
some generic ioctls are not implemented for all types of file descriptors. these include:
FIONREADintβ get the number of bytes that are immediately available for reading. πFIONWRITEintβ get the number of bytes in the descriptor's send queue. these bytes are data which has been written to the descriptor but which are being held by the kernel for further processing. the nature of the required processing depends on the underlying device. for TCP sockets, these bytes have not yet been acknowledged by the other side of the connection. π (rude of them tbh)FIONSPACEintβ get the free space in the descriptor's send queue. this value is the size of the send queue minus the number of bytes being held in the queue. Note: while this value represents the number of bytes that may be added to the queue, other resource limitations may cause a write not larger than the send queue's space to be blocked. one such limitation would be a lack of network buffers for a write to a network connection. π
π RETURN VALUES π
if an error has occurred, a value of -1 is returned and errno is set to indicate the error. it's giving "we tried, we failed, check errno bestie" π
π ERRORS π
the ioctl() system call will fail if:
[EBADF]β thefdargument is not a valid descriptor. π«[ENOTTY]β thefdargument is not associated with a character special device. π[ENOTTY]β the specifiedrequestdoes not apply to the kind of object that the descriptorfdreferences. (yes it'sENOTTYtwice, they're different problems, slay) β¨[EINVAL]β therequestorargpargument is not valid. π€[EFAULT]β theargpargument points outside the process's allocated address space. π³οΈ
not me listing ENOTTY two whole times. the kernel said "one error code, two crimes." iconic behavior. π
π SEE ALSO π
execve(2), fcntl(2), intro(4), tty(4) π«Ά
β HISTORY β
the ioctl() function appeared in Version 7 AT&T UNIX. ancient. legendary. the blueprint. π
and that's the whole page, bestie!! this beauty came from the Debian crew, dated September 11, 2013 β so if you ever need to cite your sources, that's the vibe. now go forth and control some devices πβ¨π¦
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1980, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms