Section 6.11. Signals


6.11. Signals

A 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 Actions

Every 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:

  • Process termination

  • Process core dump[37]

    [37] In certain Linux systems, core dumping is turned off by default. To turn it on, use the ulimit -c command.

  • Process stopped

  • Process ignore signal

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:

  • A program error such as dividing by 0 or accessing an invalid address

  • A child process terminating

  • A timer expiring

  • A user requesting to interrupt or terminate a process

6.11.2. Simple Signals

The 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:

[View full width]

#include <signal.h> typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler);[38]


[38] The name sighandler_t for this data type is a GNU extension.

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

#include <string.h> #include <unistd.h> #include <signal.h> void handler (int signum) {   char buf[100];   strcpy (buf, "In handler\n");   buf[strlen(buf)+1] = '\0';   write (2, buf, strlen(buf)); } int main() {   signal(SIGINT, handler);   for(;;)     sleep(1);   return (0); } 

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]

[39] HP-UX man pages say this function may not be supported in the future.

6.11.3. Signals Sent

Asynchronous 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

#include <stdio.h> int func2(void *arg) {    return (5); } int (*fp)(void *); int func1() { #ifndef _MAKE_CORE    fp = func2; #endif    return (fp(NULL)); } int main() {    printf ("return from func1 = %d\n", func1()); } 

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 Linux

To 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 Function

Linux 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 Information

Information 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.

Table 6-18. Values of si_code as Implemented in Linux 2.6 and HP-UX v11

Signal

Linux si_code[40]

HP-UX si_code[41]

Description

SIGILL

ILL_ILLOPC

ILL_ILLOPC

Illegal opcode

 

ILL_ILLOPN

 

Illegal operand

 

ILL_ILLADR

 

Illegal addressing mode

 

ILL_ILLTRP

 

Illegal trap

 

ILL_PRVOPC

ILL_PRVOPC

Privileged opcode

 

ILL_PRVREG

ILL_PRVREG

Privileged register

 

ILL_COPROC

 

Coprocessor error

 

ILL_BADSTK

 

Internal stack error

  

ILL_UNKNOWN

Unknown error

  

ILL_ILLBRKINST

Break instruction trap

SIGFPE

FPE_INTDIV

 

Integer divide by 0

 

FPE_INTOVF

 

Integer overflow

 

FPE_FLTDIV

 

Floating point divide by 0

 

FPE_FLTOVF

FPE_FLTOVF

Floating point overflow

 

FPE_FLTUND

 

Floating point underflow

 

FPE_FLTRES

 

Floating point invalid operation

 

FPE_FLTINV

 

Subscript out of range

  

FPE_COND

HP conditional trap

  

FPE_ASSISTX

HP assist exception trap

  

FPE_ASSISTEM

HP assist exception trap

  

FPE_UNKNOWN

Unknown error

SIGSEGV

SEGV_MAPERR

 

Address not mapped to object

 

SEGV_ACCERR

 

Invalid permissions for mapped object

  

SEGV_UNKNOWN

Unknown error

SIGBUS

BUS_ADRALN

 

Bus error

 

BUS_ADRERR

  
 

BUS_OBJERR

  
  

BUS_UNKNOWN

Unknown error

SIGTRAP

trAP_BRKPT

 

Process breakpoint

 

trAP_TRACE

 

Process trace trap

  

trAP_UNKNOWN

Unknown error

SIGCHLD

CLD_EXITED

 

Child has exited

 

CLD_KILLED

 

Child was killed

 

CLD_DUMPED

 

Child terminated abnormally

 

CLD_TRAPPED

 

Traced child has trapped

 

CLD_STOPPED

 

Child has stopped

 

CLD_CONTINUED

 

Stopped child has continued

SIGPOLL

POLL_IN

 

Data input available

 

POLL_OUT

 

Output buffers available

 

POLL_MSG

 

Input message available

 

POLL_ERR

 

I/O error

 

POLL_PRI

 

High-priority input available

 

POLL_HUP

 

Device disconnected


[40] Referenced from bits/sigingo.h

[41] Referenced from sys/siginfo.h HP-UX v11

6.11.7. Signal Default Actions

Linux 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.

Table 6-19. Signal Table for HP-UX and Linux Systems

Signal

HP-UX Default Action[42]

HP-UX Signal Number

Linux Default Action[43]

Linux Signal Number

SIGABRT

Abnormal termination of the process. Core dump.

6

Core dump

6

SIGALRM

The process is terminated. Status is made available to wait() and waitpid().

14

Terminate

14

SIGFPE

Abnormal termination of the process. Core dump.

8

Core dump

8

SIGHUP

The process is terminated. Status is made available to wait() and waitpid().

1

Terminate

1

SIGILL

Abnormal termination of the process. Core dump.

4

Core dump

4

SIGINT

The process is terminated. Status is made available to wait() and waitpid().

2

Terminate

2

SIGKILL

The process is terminated. Status is made available to wait() and waitpid().

9

Terminate (always)

9

SIGPIPE

The process is terminated. Status is made available to wait() and waitpid().

13

Terminate

13

SIGQUIT

Abnormal termination of the process. Core dump.

3

Core dump

3

SIGSEGV

Abnormal termination of the process. Core dump.

11

Core dump

11

SIGTERM

The process is terminated. Status is made available to wait() and waitpid().

15

Terminate

15

SIGUSR1

The process is terminated. Status is made available to wait() and waitpid().

16

Terminate

10

SIGUSR2

The process is terminated. Status is made available to wait() and waitpid().

17

Terminate

12

SIGCHLD

Ignore the signal.

18

Ignore

17

SIGCONT

Continue the process if it is stopped; otherwise, ignore the signal.

26

Ignore

18

SIGSTOP

Stop the process.

24

Stop (always)

19

SIGTSTP

Stop the process.

25

Stop

20

SIGTTIN

Stop the process.

27

Stop

21

SIGTTOU

Stop the process.

28

Stop

22

SIGBUS

Abnormal termination of the process. Core dump.

10

Core dump

7

SIGPOLL[44]

The process is terminated.Status is made available to wait() and waitpid().

22

 

29

SIGPROF

The process is terminated. Status is made available to wait() and waitpid().

21

Terminate

27

SIGSYS

Abnormal termination of the process. Core dump.

12

Core dump

31

SIGTRAP

Abnormal termination of the process. Core dump.

5

Core dump

5

SIGURG

Ignore the signal.

29

Ignore

23

SIGVTALRM

The process is terminated. Status is made available to wait() and waitpid().

20

Terminate

26

SIGXCPU

Abnormal termination of the process. Core dump.

33

Core dump

24

SIGXFSZ

Abnormal termination of the process. Core dump.

34

Core dump

25

SIGRTMIN

The process is terminated. Status is made available to wait() and waitpid().

37

Terminate

35

SIGRTMAX

The process is terminated. Status is made available to wait() and waitpid().

44

Terminate

64


[42] HP-UX signal(5) man pages HP-UX 11i version 2: August 2003

[43] This was taken from the comment section in linux/kernel/signal.c version 2.6.10.

[44] Equivalent to SIGIO in both HP-UX and Linux

Note

In HP-UX, a core file is created only if the following conditions are met:

  • The real user ID and the effective user ID of the receiving process are equal.

  • The real group ID and the effective group ID of the receiving process are equal.

  • A regular file named core does not exist in the current directory, or if it exists, it is writable.


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 





UNIX to Linux Porting. A Comprehensive Reference
UNIX to Linux Porting: A Comprehensive Reference
ISBN: 0131871099
EAN: 2147483647
Year: 2004
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net