CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona,...

29
CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson [email protected]

Transcript of CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona,...

Page 1: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

CSc 352Signal Handling in Unix

Saumya DebrayDept. of Computer Science

The University of Arizona, [email protected]

Page 2: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Signals

• A signal is a mechanism for notifying a program that some event has occurred.– intuition: signal “software interrupt”– when a signal is sent to a program, its normal execution is

interrupted– depending on (1) the state of the program, and (2) the

type of signal, the program may• enter some pre-specified signal handler; or• take some default action.

2

Page 3: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Example Signals (not a complete list)

Signal Name Number DescriptionSIGHUP 1 Hangup (POSIX)SIGINT 2 Terminal interrupt (ANSI)SIGQUIT 3 Terminal quit (POSIX)SIGILL 4 Illegal instruction (ANSI)SIGTRAP 5 Trace trap (POSIX)SIGFPE 8 Floating point exception (ANSI)SIGKILL 9 Kill(can't be caught or ignored) (POSIX)SIGSEGV 11 Invalid memory segment access (ANSI)SIGTERM 15 Termination (ANSI)SIGSTKFLT 16 Stack faultSIGSTOP 19 Stop executing(can't be caught or ignored) (POSIX)SIGPROF 27 Profiling alarm clock (4.2 BSD)SIGWINCH 28 Window size change (4.3 BSD, Sun)SIGPWR 30 Power failure restart (System V)… … …

3

Page 4: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Synchronous vs. Asynchronous Signals

• Synchronous signals:– arise from executing an instruction in the process’s

instruction stream• e.g.: illegal instruction (SIGILL); illegal address (SIGSEGV)

– causes a trap into the OS kernel trap handler• sometimes referred to as “traps”

– directed to the process/thread that executed the instruction

• Asynchronous signals:– source is external to the current execution

• e.g.: profiling clock (SIGPROF); terminal interrupt, ^C (SIGINT)

4

Page 5: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

What’s going on: A high-level view

• Signal sending:– OS kernel updates info

for destination process

• Signal receiving:– kernel forces target

process to handle signal

• Pending signals are sent but not yet received

• A process can block some signals

5

Operating system kernel

processes

devices

signals

interrupts

Page 6: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Dealing with Signals

• The way in which a process deals with a given signal is called the disposition of that signal in that process.

• Possible dispositions:– ignore– default

• different for different signals

– programmer-specified handler

• Exceptions: SIGKILL and SIGSTOP– defaults cannot be changed for these

6

Page 7: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Specifying a Signal Handler

7

Page 8: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Specifying a Signal Handler

8

changes the signal handler

Page 9: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Specifying a Signal Handler

9

signal number(can’t be SIGKILL or SIGSTOP)

Page 10: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Specifying a Signal Handler

10

info about new handler)

Page 11: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Specifying a Signal Handler

11

if non-NULL,where to saveold handler info

Page 12: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Specifying a Signal Handler

12

pointer to handler function:

void handler(int signal_num)or SIG_DFL or SIG_IGN

Page 13: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Specifying a Signal Handler

13

specifies handler if sa_flagscontains SA_SIGINFO

Page 14: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Specifying a Signal Handler

14

specifies signals that should be blocked while the handler is executing

Page 15: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

A Simple Example

15

structure to hold info about handler

Page 16: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

A Simple Example

16

signal handler function

Page 17: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

A Simple Example

17

specifying the handler

Page 18: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

A Simple Example

18

didn’t change the default signal handler

NULL pointer dereference produces a Segmentation fault

Page 19: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

A Simple Example

19

signal handler changed

but why does it get executed over and over?

Page 20: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Behind the scenes of a SIGSEGV

• When a program tries to access a bad address:– execution traps into the OS kernel– if no handler is specified:

• kernel invokes the default handler • default handler prints out “Segmentation fault” and kills the

process

– if a handler is specified:• kernel executes the handler

– the expectation is that the handler fixes the problem

• restarts the offending operation– this allows programmer-controlled recovery from errors

20

Page 21: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Another Example: weird factorial

21

no base case???

Page 22: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

weird factorial: cont’d

22

j = x = x * yi = 0

y-1

Page 23: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

weird factorial: cont’d

23

j = 1 * 2 * … * n = n!

Page 24: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

weird factorial: cont’d

24

signal handler for SIGSEGV

(signal raised by dereferencing a bad pointerin factorial() )

sets k = n!prints out k = n! and exits

Page 25: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Sending signals

• A program can send a signal to another program using the kill() system call:

int kill(pid_t pid, int sig)

sends the signal number sig to process pid(see /usr/include/asm-generic/signal.h)

• A user can send a signal from the command line using the kill command:

kill –N pidE.g., “kill -9 pid” (9 = SIGKILL)

25

Page 26: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Asynchronous signals

26

SIGINT will be handled by my_handler()

send SIGINT to process with process-id = target

Page 27: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Asynchronous signals – cont’d

27

Page 28: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Blocking signals

• Each process has a list (bit-vector) of blocked signals– when a signal is blocked, it remains pending

• Related system calls:– sigprocmask(2) system call changes the list of blocked

signals– sigpending(2) shows which signals are (blocked and)

pending– sigsuspend(2) suspends the calling process until the

specified signal is received

28

Page 29: CSc 352 Signal Handling in Unix Saumya Debray Dept. of Computer Science The University of Arizona, Tucson debray@cs.arizona.edu.

Signal voodoo

• When a process forks off a child, it may want to hear back about how things went– when the child process exits, it becomes a zombie until its

exit status is reported to the parent process– when something interesting happens to the child (it exits,

crashes, stops, etc.), a SIGCHLD signal is sent to its parent– the parent can use the wait() system call (or variants) to

find the child’s exit status

• If the parent is not interested, it can use sigaction() to explicitly specify that SIGCHLD should be ignored

29