SWAPON(2)

πŸ’… SWAPON(2) πŸ’… β€” from the System Calls Manual, bestie

okay so literally put your phone down for two secs because we're doing a whole system call today and it's giving main character energy. this is SWAPON(2), it lives in the System Calls Manual, and it's about swapon and swapoff β€” the two lil' functions that control devices for interleaved paging/swapping. yes bestie, paging AND swapping. both. iconic. 🫢


πŸ¦‹ NAME πŸ¦‹

swapon, swapoff β€” control devices for interleaved paging/swapping


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc). yeah she's linked against -lc, no cap πŸ’–


✨ SYNOPSIS ✨

#include <unistd.h>

int
swapon(const char *special);

int
swapoff(const char *special);

like, that's it. two functions. two ints. so clean ✨


🌸 DESCRIPTION 🌸

The swapon() system call makes the block device special available to the system for allocation for paging and swapping. the way that it just hands the whole device over… πŸ’– The names of potentially available devices are known to the system and defined at system configuration time. The size of the swap area on special is calculated at the time the device is first made available for swapping. like, first time only. that's when the math happens πŸ“βœ¨

The swapoff() system call disables paging and swapping on the given device. All associated swap metadata are deallocated, and the device is made available for other purposes. she's freed. she's unbothered. she's available for other purposes πŸ’…


🌈 RETURN VALUES 🌈

If an error has occurred, a value of -1 is returned and errno is set to indicate the error. so if you see -1, that's not a vibe, that's an error code bestie 😭 go read errno.


πŸ”₯ ERRORS πŸ”₯

Both swapon() and swapoff() can fail if:

  • [ENOTDIR] β€” A component of the path prefix is not a directory. rude of it honestly πŸ™ˆ
  • [ENAMETOOLONG] β€” A component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters. that's a LOT of characters, like calm down βœ‹
  • [ENOENT] β€” The named device does not exist. it's giving nothing. it's giving nowhere πŸ«₯
  • [EACCES] β€” Search permission is denied for a component of the path prefix. locked out πŸ’€
  • [ELOOP] β€” Too many symbolic links were encountered in translating the pathname. symlink chain that never ends, no ma'am πŸŒ€
  • [EPERM] β€” The caller is not the super-user. bestie you are NOT root. sit down πŸ’…
  • [EFAULT] β€” The special argument points outside the process's allocated address space. that pointer is in the void, babes πŸ‘»

Additionally, swapon() can fail for the following reasons:

  • [ENOTBLK] β€” The special argument is not a block device. wrong kind of device, she said no πŸ’”
  • [EBUSY] β€” The device specified by special has already been made available for swapping. she's already booked and busy ✨
  • [ENXIO] β€” The major device number of special is out of range (this indicates no device driver exists for the associated hardware). no driver, no service 🚫
  • [EIO] β€” An I/O error occurred while opening the swap device. the hardware said "i'm tired" 😭
  • [EINTEGRITY] β€” Corrupted data was detected while reading from the file system to open the swap device. corrupted data detected, that's a whole crime scene 🚨

Lastly, swapoff() can fail if:

  • [EINVAL] β€” The system is not currently swapping to special. you can't turn off something that was never on, bestie 🫠
  • [ENOMEM] β€” Not enough virtual memory is available to safely disable paging and swapping to the given device. not enough virtual memory to safely do the thing β€” and yes, "safely" is doing a LOT of work in that sentence, so take it seriously πŸ”₯

πŸ‘‘ SEE ALSO πŸ‘‘

config(8), swapon(8), sysctl(8) β€” go read these next, iykyk πŸ“–βœ¨


πŸ’œ HISTORY πŸ’œ

The swapon() system call appeared in 4.0BSD. The swapoff() system call appeared in FreeBSD 5.0. legacy behavior honestly, we love a glow-up πŸ’–


okay that's the whole page bestie, you ATE it πŸ“ this one's from the Debian crew, dated March 30, 2020 β€” go forth and swap responsibly, mwah πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from