Blame man2/sigreturn.2

Packit 7cfc04
.\" Copyright (C) 2008, 2014, Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" Created   Sat Aug 21 1995     Thomas K. Dyas <tdyas@eden.rutgers.edu>
Packit 7cfc04
.\" Modified Tue Oct 22 22:09:03 1996 by Eric S. Raymond <esr@thyrsus.com>
Packit 7cfc04
.\" 2008-06-26, mtk, added some more detail on the work done by sigreturn()
Packit 7cfc04
.\" 2014-12-05, mtk, rewrote all of the rest of the original page
Packit 7cfc04
.\"
Packit 7cfc04
.TH SIGRETURN 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
sigreturn, rt_sigreturn \- return from signal handler and cleanup stack frame
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.BI "int sigreturn(...);"
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
If the Linux kernel determines that an unblocked
Packit 7cfc04
signal is pending for a process, then,
Packit 7cfc04
at the next transition back to user mode in that process
Packit 7cfc04
(e.g., upon return from a system call or
Packit 7cfc04
when the process is rescheduled onto the CPU),
Packit 7cfc04
it creates a new frame on the user-space stack where it
Packit 7cfc04
saves various pieces of process context
Packit 7cfc04
(processor status word, registers, signal mask, and signal stack settings).
Packit 7cfc04
.\" See arch/x86/kernel/signal.c::__setup_frame() [in 3.17 source code]
Packit 7cfc04
.PP
Packit 7cfc04
The kernel also arranges that, during the transition back to user mode,
Packit 7cfc04
the signal handler is called, and that, upon return from the handler,
Packit 7cfc04
control passes to a piece of user-space code commonly called
Packit 7cfc04
the "signal trampoline".
Packit 7cfc04
The signal trampoline code in turn calls
Packit 7cfc04
.BR sigreturn ().
Packit 7cfc04
.PP
Packit 7cfc04
This
Packit 7cfc04
.BR sigreturn ()
Packit 7cfc04
call undoes everything that was
Packit 7cfc04
done\(emchanging the process's signal mask, switching signal stacks (see
Packit 7cfc04
.BR sigaltstack "(2))\(emin "
Packit 7cfc04
order to invoke the signal handler.
Packit 7cfc04
Using the information that was earlier saved on the user-space stack
Packit 7cfc04
.BR sigreturn ()
Packit 7cfc04
restores the process's signal mask, switches stacks,
Packit 7cfc04
and restores the process's context
Packit 7cfc04
(processor flags and registers,
Packit 7cfc04
including the stack pointer and instruction pointer),
Packit 7cfc04
so that the process resumes execution
Packit 7cfc04
at the point where it was interrupted by the signal.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
.BR sigreturn ()
Packit 7cfc04
never returns.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
Many UNIX-type systems have a
Packit 7cfc04
.BR sigreturn ()
Packit 7cfc04
system call or near equivalent.
Packit 7cfc04
However, this call is not specified in POSIX,
Packit 7cfc04
and details of its behavior vary across systems.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
.BR sigreturn ()
Packit 7cfc04
exists only to allow the implementation of signal handlers.
Packit 7cfc04
It should
Packit 7cfc04
.B never
Packit 7cfc04
be called directly.
Packit 7cfc04
(Indeed, a simple
Packit 7cfc04
.BR sigreturn ()
Packit 7cfc04
.\" See sysdeps/unix/sysv/linux/sigreturn.c and
Packit 7cfc04
.\" signal/sigreturn.c in the glibc source
Packit 7cfc04
wrapper in the GNU C library simply returns -1, with
Packit 7cfc04
.I errno
Packit 7cfc04
set to
Packit 7cfc04
.BR ENOSYS .)
Packit 7cfc04
Details of the arguments (if any) passed to
Packit 7cfc04
.BR sigreturn ()
Packit 7cfc04
vary depending on the architecture.
Packit 7cfc04
(On some architectures, such as x86-64,
Packit 7cfc04
.BR sigreturn ()
Packit 7cfc04
takes no arguments, since all of the information that it requires
Packit 7cfc04
is available in the stack frame that was previously created by the
Packit 7cfc04
kernel on the user-space stack.)
Packit 7cfc04
.PP
Packit 7cfc04
Once upon a time, UNIX systems placed the signal trampoline code
Packit 7cfc04
onto the user stack.
Packit 7cfc04
Nowadays, pages of the user stack are protected so as to
Packit 7cfc04
disallow code execution.
Packit 7cfc04
Thus, on contemporary Linux systems, depending on the architecture,
Packit 7cfc04
the signal trampoline code lives either in the
Packit 7cfc04
.BR vdso (7)
Packit 7cfc04
or in the C library.
Packit 7cfc04
In the latter case,
Packit 7cfc04
.\" See, for example, sysdeps/unix/sysv/linux/i386/sigaction.c and
Packit 7cfc04
.\" sysdeps/unix/sysv/linux/x86_64/sigaction.c in the glibc (2.20) source.
Packit 7cfc04
the C library's
Packit 7cfc04
.BR sigaction (2)
Packit 7cfc04
wrapper function informs the kernel of the location of the trampoline code
Packit 7cfc04
by placing its address in the
Packit 7cfc04
.I sa_restorer
Packit 7cfc04
field of the
Packit 7cfc04
.I sigaction
Packit 7cfc04
structure,
Packit 7cfc04
and sets the
Packit 7cfc04
.BR SA_RESTORER
Packit 7cfc04
flag in the
Packit 7cfc04
.IR sa_flags
Packit 7cfc04
field.
Packit 7cfc04
.PP
Packit 7cfc04
The saved process context information is placed in a
Packit 7cfc04
.I ucontext_t
Packit 7cfc04
structure (see
Packit 7cfc04
.IR <sys/ucontext.h> ).
Packit 7cfc04
That structure is visible within the signal handler
Packit 7cfc04
as the third argument of a handler established via
Packit 7cfc04
.BR sigaction (2)
Packit 7cfc04
with the
Packit 7cfc04
.BR SA_SIGINFO
Packit 7cfc04
flag.
Packit 7cfc04
.PP
Packit 7cfc04
On some other UNIX systems,
Packit 7cfc04
the operation of the signal trampoline differs a little.
Packit 7cfc04
In particular, on some systems, upon transitioning back to user mode,
Packit 7cfc04
the kernel passes control to the trampoline (rather than the signal handler),
Packit 7cfc04
and the trampoline code calls the signal handler (and then calls
Packit 7cfc04
.BR sigreturn ()
Packit 7cfc04
once the handler returns).
Packit 7cfc04
.\"
Packit 7cfc04
.SS C library/kernel differences
Packit 7cfc04
The original Linux system call was named
Packit 7cfc04
.BR sigreturn ().
Packit 7cfc04
However, with the addition of real-time signals in Linux 2.2,
Packit 7cfc04
a new system call,
Packit 7cfc04
.BR rt_sigreturn ()
Packit 7cfc04
was added to support an enlarged
Packit 7cfc04
.IR sigset_t
Packit 7cfc04
type.
Packit 7cfc04
The GNU C library
Packit 7cfc04
hides these details from us, transparently employing
Packit 7cfc04
.BR rt_sigreturn ()
Packit 7cfc04
when the kernel provides it.
Packit 7cfc04
.\"
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR kill (2),
Packit 7cfc04
.BR restart_syscall (2),
Packit 7cfc04
.BR sigaltstack (2),
Packit 7cfc04
.BR signal (2),
Packit 7cfc04
.BR getcontext (3),
Packit 7cfc04
.BR signal (7),
Packit 7cfc04
.BR vdso (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.