6.11. SignalsA signal indicates that an event has occurred. Typically, a process that receives a signal handles that signal through either a default mechanism or a signal handler. POSIX defines a set of signals and signal handling functions that are implemented in Linux. 6.11.1. Signal ActionsEvery signal has an associated default action associated with it. A default action is what the Linux kernel does for the process when a signal arrives for that process. The default actions are as follows:
A process can also install a signal handler to run when a signal is delivered to it. The signal handler takes the place of the default action associated with each signal. Signals occur as a consequence of an event. Here are some examples that can cause a signal to be sent to a process:
6.11.2. Simple SignalsThe simplest signal-handling function Linux supports is the original ISO C standard signal() API. It allows the calling process to change the action for a particular signal to either ignore or restore the default signal action or install a function that is called when that particular signal is received by the process. Here is the definition of the signal() API as defined by the GNU/Linux signal(2) man page:
Here is how the HP-UX man page for signal(2) defines signal(): #include <signal.h> void (*signal(int sig, void (*func)(int)))(int); Although both Linux and HP-UX may seem to support both signal APIs, each implementation is different. Linux implements the BSD behavior; HP-UX implements UNIX V7 or System V behavior. Example 6-9 demonstrates both behaviors. Example 6-9. Listing of signal_1.c
Compile and run on Linux: $ c signal_1.c -o signal_1 $ signal_1 In handler <- Ctl-c In handler <- Ctl-c In handler <- Ctl-c Ctl-\ (to kill the process) Quit (core dumped) The BSD behavior as implemented on Linux reinstalls the signal handler when the signal is received. This is why the handler is always called whenever the user presses Ctrl-c. Compile and run on HP-UX: $ aCC signal_1.c -o signal_1 $ ./signal_1 In handler <- Ctl-c <- Ctl-c $ Program is terminated Traditional System V semantics of the signal API returns the signal action to the default action after the signal is handled. Thus, a second Ctl-c sent to the process results in process termination. HP-UX provides a compatibility function to the BSD signal semantics through the bsd_signal() API.[39]
6.11.3. Signals SentAsynchronous signals sent by the kernel to a process as a result of an event may also differ in Linux compared to HP-UX. Example 6-10 illustrates this. Example 6-10. Listing of coredump_1.c
Compile and run on Linux: $ gcc coredump_1.c -o coredump_1 -D_MAKE_CORE $ ./coredump_1 Segmentation fault (core dumped) Compile and run on HP-UX $ aCC coredump_1.c -o coredump_1 -D_MAKE_CORE $ ./coredump_1 Bus error(coredump) Although both programs resulted in process termination, the signal received by both processes differed. The Linux kernel sent a SIGSEGV; HP-UX sent a SIGBUS. 6.11.4. Signal Support in LinuxTo get a list of supported signals in Linux, issue a kill -l (the letter l ) command: $ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 35) SIGRTMIN 36) SIGRTMIN+1 37) SIGRTMIN+2 38) SIGRTMIN+3 39) SIGRTMIN+4 40) SIGRTMIN+5 41) SIGRTMIN+6 42) SIGRTMIN+7 43) SIGRTMIN+8 44) SIGRTMIN+9 45) SIGRTMIN+10 46) SIGRTMIN+11 47) SIGRTMIN+12 48) SIGRTMIN+13 49) SIGRTMIN+14 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX This particular Linux implementation lists support for 30 non-real-time signals and 30 real-time signals. Issuing the same command on an HP-UX v11 machine yields the following: $ kill -l 1) HUP 2) INT 3) QUIT ... other signal output deleted ... 37) RTMIN 38) RTMIN+1 39) RTMIN+2 40) RTMIN+3 41) RTMAX-3 42) RTMAX-2 43) RTMAX-1 44) RTMAX Although HP-UX supports most if not all of the POSIX-defined signals, it only supports eight real-time signals. 6.11.5. POSIX Signal-Catching FunctionLinux provides the POSIX signal handling API sigaction(). Here is sigaction as defined by the POSIX standard: #include <signal.h> int sigaction(int sig, const struct sigaction *restrict act, struct sigaction *restrict oact); The sigaction structure defined in signal.h follows the POSIX definition, as follows: typedef struct { void (*sa_handler)(int) /* Pointer to a signal-catching * function or one of the macros. * SIG_IGN or SIG_DFL. */ sigset_t sa_mask /* Set of signals to be blocked * during execution of the signal * handling function. */ int sa_flags /* Special flags. */ void (*sa_sigaction)(int, siginfo_t *, void *) /* Pointer to a signal-catching * function. */ } sigaction_t; The definition of the sa_sigaction function within the sigaction_t structure uses an argument that is of type siginfo_t, which is a structure that can be used to get more information about the signal's context. Here is a simplified version of the siginfo_t structure as defined in bits/siginfo.h in Linux: typedef struct { int si_signo /* Signal number. */ int si_errno /* If non-zero, an errno value is associated * with this signal, as defined in <errno.h> . */ int si_code /* Signal code. */ pid_t si_pid /* Sending process ID. */ uid_t si_uid /* Real user ID of sending * process. */ sigval si_value /* Signal value. */ } siginfo_t; 6.11.6. Signal InformationInformation about how and why a signal was generated is called the signal's context or information. Within the siginfo_t structure, the following members, si_signo and si_code, can be used to obtain more information about the signal. For code examples that show how to use siginfo_t, refer to Linux Application Development, by Johnson and Troan. Table 6-18 compares Linux and HP-UX implementations of the si_signo (Signal) and si_code values. Blank lines in the HP-UX and Linux columns mean the si code is not supported on the platforms.
6.11.7. Signal Default ActionsLinux implements both POSIX real-time and non-real-time signals. As discussed earlier in this chapter, each signal has its own default action. Table 6-19 compares the signal default actions and the corresponding signal value as implemented in both Linux 2.6 and HP-UX v11.
Note In HP-UX, a core file is created only if the following conditions are met:
In Linux, a core file is created in the current directory. On most Linux systems, however, core file creation is disabled by default. To enable core file creation, set a ulimit flag -c to a specified file size limit, as follows: $ ulimit -c 50000 |