Section 5.13. Signals


5.13. Signals

A signal is an indication 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. This section compares and contrasts signal support between Linux 2.6 and AIX.

5.13.1. Signal Actions

Each 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 is terminated.

  • Process Coredump. Process is terminated and coredump may be produced.[19]

    [19] In certain Linux systems, coredumping is turned off by default. To turn it on, use the ulimit c command.

  • Process Stopped. Process is stopped.

  • Process Ignore Signal. Process ignores the 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

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

This section defines the signal() APIs available in GNU/Linux and the differences compared to AIX.

5.13.2.1. AIX Prototype

#include <signal.h> int sigaction (int Signal, struct sigaction *Action, struct sigaction *OAction); int sigprocmask (int How, const sigset_t *Set, sigset *OSet); int sigpending (sigset_t *Set); int sigsuspend (const sigset_t *SignalMask); int sigpause (int SignalMask); 


5.13.2.2. Linux Prototype

#include <signal.h> int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact); int sigprocmask(int how, const sigset_t *set, sigset_t *oldset); int sigpending(sigset_t *set); int sigsuspend(const sigset_t *mask); int sigpause(int sigmask); 


5.13.2.3. Detail Comparison

**Not Compatible**

Signal values are different between AIX and Linux.

The remaining documentation is taken from the Linux man page.

The sigaction system call is used to change the action taken by a process on receipt of a specific signal.

signum specifies the signal and can be any valid signal except SIGKILL and SIGSTOP.

If act is non-null, the new action for signal signum is installed from act. If oldact is non-null, the previous action is saved in oldact.

The sigaction structure is defined as something like this:

struct sigaction {  void (*sa_handler)(int);  void (*sa_sigaction)(int, siginfo_t *, void *);  sigset_t sa_mask;  int sa_flags;  void (*sa_restorer)(void); } 


On some architectures, a union is involved; do not assign to both sa_handler and sa_sigaction.

The sa_restorer element is obsolete and should not be used. POSIX does not specify an sa_restorer element.

sa_handler specifies the action to be associated with signum and may be SIG_DFL for the default action, SIG_IGN to ignore this signal, or a pointer to a signal-handling function.

sa_mask gives a mask of signals that should be blocked during execution of the signal handler. In addition, the signal that triggered the handler will be blocked, unless the SA_NODEFER or SA_NOMASK flags are used.

sa_flags specifies a set of flags that modify the behavior of the signal-handling process. It is formed by the bitwise OR of zero or more of the following:

  • SA_NOCLDSTOP

    If signum is SIGCHLD, do not receive notification when child processes stop (that is, when child processes receive one of SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU).

  • SA_ONESHOT or SA_RESETHAND

    Restore the signal action to the default state once the signal handler has been called.

  • SA_ONSTACK

    Call the signal handler on an alternate signal stack provided by sigaltstack. If an alternate stack is not available, the default stack will be used.

  • SA_RESTART

    Provide behavior compatible with BSD signal semantics by making certain system calls restartable across signals.

  • SA_NOMASK or SA_NODEFER

    Do not prevent the signal from being received from within its own signal handler.

  • SA_SIGINFO

    The signal handler takes three arguments, not one. In this case, sa_sigaction should be set rather than sa_handler. (The sa_sigaction field was added in Linux 2.1.86.)

The siginfo_t parameter to sa_sigaction is a struct with the following elements:

siginfo_t {  int si_signo;      /* Signal number */  int si_errno;      /* An errno value */  int si_code;       /* Signal code */  pid_t si_pid;      /* Sending process ID */  uid_t si_uid;      /* Real user ID of sending process */  int si_status;     /* Exit value or signal */  clock_t si_utime;  /* User time consumed */  clock_t si_stime;  /* System time consumed */  sigval_t si_value; /* Signal value */  int si_int;         /* POSIX.1b signal */  void * si_ptr;      /* POSIX.1b signal */  void * si_addr;    /* Memory location which caused fault */  int si_band;       /* Band event */  int si_fd;          /* File descriptor */ } 


si_signo, si_errno, and si_code are defined for all signals. The rest of the struct may be a union, so one should only read the fields that are meaningful for the given signal. Kill, POSIX.1b signals, and SIGCHLD fill in si_pid and si_uid. SIGCHLD also fills in si_status, si_utime, and si_stime. si_int and si_ptr are specified by the sender of the POSIX.1b signal. SIGILL, SIGFPE, SIGSEGV, and SIGBUS fill in si_addr with the address of the fault. SIGPOLL fills in si_band and si_fd.

si_code indicates why this signal was sent. It is a value, not a bitmask. Table 5-8 lists the values possible for any signal.

Table 5-8. Values of si_code as Implemented in Linux 2.6

Signal

Linux si_code[20]

Description

SIGILL

ILL_ILLOPC

Illegal opcode.

 

ILL_ILLOPN

Illegal operand.

 

ILL_ILLADR

Illegal addressing mode.

 

ILL_ILLTRP

Illegal trap.

 

ILL_PRVOPC

Privileged opcode.

 

ILL_PRVREG

Privileged register.

 

ILL_COPROC

Coprocessor error.

 

ILL_BADSTK

Internal stack error.

  

Unknown error.

  

Break instruction trap.

SIGFPE

FPE_INTDIV

Integer divide by 0.

 

FPE_INTOVF

Integer overflow.

 

FPE_FLTDIV

Floating-point divide by 0.

 

FPE_FLTOVF

Floating-point overflow.

 

FPE_FLTUND

Floating-point underflow.

 

FPE_FLTRES

Floating-point invalid operation.

 

FPE_FLTINV

Subscript out of range.

SIGSEGV

SEGV_MAPERR

Address not mapped to object.

 

SEGV_ACCERR

Invalid permissions for mapped object.

SIGBUS

BUS_ADRALN

Bus error.

 

BUS_ADRERR

 
 

BUS_OBJERR

 

SIGTRAP

trAP_BRKPT

Process breakpoint.

 

TRAP_TRACE

Process trace trap.

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.


[20] Referenced from bits/sigingo.h

The sigprocmask call is used to change the list of currently blocked signals. The behavior of the call is dependent on the value of how, as follows:

  • SIG_BLOCK. The set of blocked signals is the union of the current set and the set argument.

  • SIG_UNBLOCK. The signals in set are removed from the current set of blocked signals. It is legal to attempt to unblock a signal that is not blocked.

  • SIG_SETMASK. The set of blocked signals is set to the argument set.

If oldset is non-null, the previous value of the signal mask is stored in oldset.

The sigpending call allows the examination of pending signals (ones that have been raised while blocked). The signal mask of pending signals is stored in set.

The sigsuspend call temporarily replaces the signal mask for the process with that given by mask and then suspends the process until a signal is received.

5.13.2.4. Return Value

The functions sigaction, sigprocmask, and sigpending return 0 on success and 1 on error. The function sigsuspend always returns 1, normally with the error EINTR.

5.13.2.5. Errors
  • EINVAL. An invalid signal was specified. This will also be generated if an attempt is made to change the action for SIGKILL or SIGSTOP, which cannot be caught.

  • EFAULT. act, oldact, set, oldset, or mask point to memory that is not a valid part of the process address space.

  • EINTR. System call was interrupted.

5.13.2.6. Signal Values

Signal values are defined differently between Linux and AIX. Linux signals are essentially a fully contained subset of AIX signals. The only exception is SIGSTKFLT. No code was found in the Linux kernel to implement the SIGSTKFLT signal. Table 5-9 shows the differences in signal values between AIX and Linux.

Table 5-9. Linux and AIX Signal Values

Signal

Linux

AIX

SIGHUP

1

1

SIGINT

2

2

SIGQUIT

3

3

SIGILL

4

4

SIGTRAP

5

5

SIGABRT

6

6

SIGIOT

6

6

SIGBUS

7

10

SIGFPE

8

8

SIGKILL

9

9

SIGUSR1

10

30

SIGSEGV

11

11

SIGUSR2

12

31

SIGPIPE

13

13

SIGALRM

14

14

SIGTERM

15

15

SIGSTKFLT

16

Undefined

SIGCLD

17

20

SIGCHLD

17

20

SIGCONT

18

19

SIGSTOP

19

17

SIGTSTP

20

18

SIGTTI

21

21

SIGTTOU

22

22

SIGURG

23

16

SIGXCPU

24

24

SIGXFSZ

25

25

SIGVTALRM

26

34

SIGPROF

27

32

SIGWINCH

28

28

SIGPOLL

29

23

SIGIO

29

23

SIGPWR

30

29

SIGSYS

31

12

_NSIG

64

Undefined


Implementation of correct and reliable signals has been in place for many years now, where an installed signal handler remains persistent and is not reset by the kernel. The POSIX standards provided a fairly well-defined set of interfaces for using signals in code, and today the Linux implementation of signals is fully POSIX-compliant.




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