Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 5: Locking and Interprocess Communication


Although signal handling takes place in the kernel, the installed signal handlers run in user
mode — otherwise, it would be very easy to introduce malicious or faulty code into the kernel and
undermine the system security mechanisms. Generally, signal handlers use the user mode stack of
the process in question. However, POSIX mandates the option of running signal handlers on a stack
set up specifically for this purpose (using thesigaltstacksystem call). The address and size of this
additional stack (which must be explicitly allocated by the user application) are held insas_ss_spand
sas_ss_size, respectively.^13

Thesighandelement with the following structure is used to manage information on the signal handlers
installed. The underlying structure is essentially defined as follows:

<sched.h>
struct sighand_struct {
atomic_t count;
struct k_sigaction action[_NSIG];
};

countholds the number of processes that share the instance.AsdescribedinChapter2,itispossibleto
specify in thecloneoperation that parent and child process share the same signal handler so that there
is no need to copy the data structure.

The installed signal handlers are held in theactionarray that has_NSIGelements._NSIGspecifies
the number of different signals that can be handled. This figure is 64 on most platforms, but there are
exceptions — Mips, for instance, which supports 128 signals.

Each element contains an instance of thek_sigactionstructure to specify the properties of a signal
as seen by the kernel. On some platforms, the kernel has more information on signal handlers than
is available for userspace applications. Normally,k_sigactionhas a single element that includes the
familiarsigactionstructure.

<asm-arch/signal.h>
struct k_sigaction {
struct sigaction sa;
};

If no user-defined handler routine is installed for a signal (this means that the default routine is used
instead), thesa.sa_handlerflag is set toSIG_DFL. In this case, the kernel performs one of four standard
actions depending on the signal type:

❑ Ignore— Nothing happens.
❑ Terminate— Terminates the process or process group.
❑ Stop— Places the process in theTASK_STOPPEDstate.
❑ Core Dump— Creates a core dump of the address space and writes it to a core file for process-
ing, for example, by a debugger.

(^13) Signal handlers that use this stack must be installed using theSA_ONSTACKflag. Since this mechanism is rarely used, I will not
bother discussing it here.

Free download pdf