SWAPCONTEXT(2)

πŸ’– SWAPCONTEXT(2) πŸ’–

okay bestie, welcome to the Linux 2.6 Programmer's Guide section of the man pages β€” today we're doing swapcontext(2) and honestly? it's giving "quiet little syscall that's lowkey carrying your whole coroutine situation." ✨


✨ NAME ✨

swapcontext β€” swap out old context with new context πŸ¦‹


πŸŽ€ SYNOPSIS πŸŽ€

int sys_swapcontext (struct ucontext *old_ctx, struct ucontext *new_ctx,
                     int r5, int r6, int r7, int r8,
                     struct pt_regs *regs);

πŸ’… DESCRIPTION πŸ’…

so here's the vibe: swapcontext swaps out the context old_ctx with the new context new_ctx. that's it, that's the whole job, she's a one-trick pony and she EATS πŸ’…

now about those little int parameters β€” r5, r6, r7, r8 β€” they have no place in the system call functionality. like, they're literally just there. not doing anything. main character energy with zero lines, honestly. πŸ™ˆ

and then there's regs, which indicates the current user register values from the user stack. she's the one actually holding information, periodt. ⭐


🌈 RETURN VALUE 🌈

swapcontext returns 0 on success πŸŽ‰ β€” icon behaviour, no notes.

otherwise? she returns one of the errors listed down in the Errors section. so yeah, if you get a nonzero return, go read the next section, bestie, it's right there for you. 😭


πŸ’€ ERRORS πŸ’€

  • -EFAULT β€” swapcontext could not verify that the memory area pointed to by old_ctx or new_ctx was accessible for the operation. basically the kernel looked at your pointer and went "girl, that's not yours." 🫢
  • -SIGSEGV β€” a fault occurred when the context was being copied into the kernel's image of the user's registers. this should only occur in an out-of-memory situation. > ⚠️ heads up: OOM territory, that's the only time this should be showing up, so if you're seeing it and you're not out of memory, that's a whole different conversation.

πŸ¦‹ SEE ALSO πŸ¦‹

go read those next, they're the rest of the squad πŸ’œ


πŸ‘‘ AUTHOR πŸ‘‘

Niki Rahimi β€” thank you for your service, legend πŸ«‘πŸ’–


and that's the whole page, bestie!! this one's from the Linux 2.6 days, dated 2004-March-12 🌸 so yeah she's an elder but she's still iconic and we respect her. mwah, go swap some contexts responsibly πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from
  • centos-stream 10: man-pages 6.06-13.el10 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND LicenseRef-Fedora-Public-Domain AND LicenseRef-Fedora-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • centos-stream 9: man-pages 6.04-10.el9 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND LicenseRef-Fedora-Public-Domain AND LicenseRef-Fedora-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • fedora 43: man-pages 6.13-2.fc43 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND LicenseRef-Fedora-Public-Domain AND LicenseRef-Fedora-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • fedora 43: man-pages 6.9.1-5.fc43 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND LicenseRef-Fedora-Public-Domain AND LicenseRef-Fedora-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • fedora 44: man-pages 6.13-3.fc44 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND LicenseRef-Fedora-Public-Domain AND LicenseRef-Fedora-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms