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:

  • FIONREAD int β€” get the number of bytes that are immediately available for reading. πŸ‘€
  • FIONWRITE int β€” 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)
  • FIONSPACE int β€” 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] β€” the fd argument is not a valid descriptor. 🚫
  • [ENOTTY] β€” the fd argument is not associated with a character special device. πŸ’€
  • [ENOTTY] β€” the specified request does not apply to the kind of object that the descriptor fd references. (yes it's ENOTTY twice, they're different problems, slay) ✨
  • [EINVAL] β€” the request or argp argument is not valid. 😀
  • [EFAULT] β€” the argp argument 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