본문 바로가기

Learning/└◆Reversing

[참고] Signal에 대해서(시그널 함수)

시그널(signal)에 대해서

 

 

시그널에 대한 정보 확인

 

@linux200

 

# whatis signal

signal             (2) - ANSI C signal handling

signal             (3p) - signal management

signal             (7) - list of available signals

signal.h [signal]  (0p) - signals

 

 


# man 7 signal

NAME

       signal - list of available signals


DESCRIPTION

       Linux  supports both POSIX reliable signals (hereinafter

       "standard signals") and POSIX real-time signals.


   Signal Dispositions

       Each signal has a current disposition, which  determines

       how the process behaves when it is delivered the signal.


       The entries in the "Action" column of the  tables  below

       specify the default disposition for each signal, as fol-

       lows:


       Term   Default action is to terminate the process.


       Ign    Default action is to ignore the signal.


       Core   Default action is to terminate  the  process  and

              dump core (see core(5)).


       Stop   Default action is to stop the process.


       Cont   Default  action  is to continue the process if it

              is currently stopped.

..... (중략) .....

       Signal     Value     Action   Comment

       -------------------------------------------------------------------------

       SIGHUP        1       Term    Hangup detected on controlling terminal

                                     or death of controlling process

       SIGINT        2       Term    Interrupt from keyboard

       SIGQUIT       3       Core    Quit from keyboard

       SIGILL        4       Core    Illegal Instruction

       SIGABRT       6       Core    Abort signal from abort(3)

       SIGFPE        8       Core    Floating point exception

       SIGKILL       9       Term    Kill signal

       SIGSEGV      11       Core    Invalid memory reference

       SIGPIPE      13       Term    Broken pipe: write to pipe with no readers

       SIGALRM      14       Term    Timer signal from alarm(2)

       SIGTERM      15       Term    Termination signal

       SIGUSR1   30,10,16    Term    User-defined signal 1

       SIGUSR2   31,12,17    Term    User-defined signal 2

       SIGCHLD   20,17,18    Ign     Child stopped or terminated

       SIGCONT   19,18,25    Cont    Continue if stopped

       SIGSTOP   17,19,23    Stop    Stop process

       SIGTSTP   18,20,24    Stop    Stop typed at tty

       SIGTTIN   21,21,26    Stop    tty input for background process

       SIGTTOU   22,22,27    Stop    tty output for background process


term- 종료 core- core생성 등등..

..... (중략) .....

 

 


운영체제에 미리 설정되어있는 목록을 볼 수 있다.

# 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     16) SIGSTKFLT

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      34) SIGRTMIN

35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4

39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8

43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12

47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 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


 

 

# cd /usr/include

# find . -name signal.h -type f

 

./linux/signal.h

./asm-generic/signal.h

./asm/signal.h

./signal.h

./sys/signal.h

 

 

# cat /usr/include/asm/signal.h

..... (중략) .....

#define SIGHUP           1

#define SIGINT           2

#define SIGQUIT          3

#define SIGILL           4

#define SIGTRAP          5

#define SIGABRT          6

#define SIGIOT           6

#define SIGBUS           7

#define SIGFPE           8

#define SIGKILL          9

#define SIGUSR1         10

#define SIGSEGV         11

#define SIGUSR2         12

#define SIGPIPE         13

#define SIGALRM         14

#define SIGTERM         15

#define SIGSTKFLT       16

#define SIGCHLD         17

#define SIGCONT         18

#define SIGSTOP         19

#define SIGTSTP         20

#define SIGTTIN         21

#define SIGTTOU         22

#define SIGURG          23

#define SIGXCPU         24

#define SIGXFSZ         25

#define SIGVTALRM       26

#define SIGPROF         27

#define SIGWINCH        28

#define SIGIO           29

#define SIGPOLL         SIGIO

/*

#define SIGLOST         29

*/

#define SIGPWR          30

#define SIGSYS          31

#define SIGUNUSED       31


/* These should not be considered constants from userland.  */

#define SIGRTMIN        32

#define SIGRTMAX        _NSIG

..... (중략) .....

 

  


==============================================================================



시그널 함수(signal())

시그널을 제어하기 위해서 사용한다. 프로그래머가 시그널 메뉴얼대로 운영하도록 선언 되어있는것을 제어하기 위해서

 

 

# man signal (# man 2 singal)


NAME

       signal - ANSI C signal handling


SYNOPSIS

       #include <signal.h>


       typedef void (*sighandler_t)(int);


       sighandler_t signal(int signum, sighandler_t handler);


DESCRIPTION

       The  signal()  system call installs a new signal handler

       for the signal with number signum.  The  signal  handler

       is set to sighandler which may be a user specified func-

       tion, or either SIG_IGN or SIG_DFL.


       Upon arrival of a signal with number signum the  follow-

       ing  happens.   If  the  corresponding handler is set to

       SIG_IGN, then the signal is ignored.  If the handler  is

       set  to SIG_DFL, then the default action associated with

       the signal (see signal(7)) occurs.  Finally, if the han-

       dler  is  set to a function sighandler then first either

       the handler is reset to SIG_DFL  or  an  implementation-

       dependent  blocking  of the signal is performed and next

       sighandler is called with argument signum.


       Using a signal handler function for a signal  is  called

       "catching  the signal".  The signals SIGKILL and SIGSTOP

       cannot be caught or ignored.


 

 

 

시그널 함수 사용법


-----------------------------------------------------------------------------

함수 사용법                                 함수 사용예                  설명

-----------------------------------------------------------------------------

signal(시그널번호, SIG_DFL)       signal(SIGINT, SIG_DFL)    SIGINT 시그널 실행

signal(시그널번호, SIG_IGN)       signal(SIGQUIT, SIG_IGN)  SIGQUIT 시그널 무시

signal(시그널번호, handler함수)  signal(SIGINT, handler)     SIGINT(CTRL + C)가 입력되면 

                                                                                     handler() 함수를 실행

-----------------------------------------------------------------------------

무언가를 설치할 때 설치가 끝나면 설치시에 생성된 임시파일이 삭제되는데 중간에 멈추게 되면 임시파일이

삭제되지 않고 남게 된다. 임시파일은 중요한 정보를 담고 있기 때문에 handler함수를 사용해 중간에 끊기게 되면

임시파일들을 모두 삭제하는 동작을 해야한다.

 


 

(linux200)



시그널(signal)?

하나의 프로세스가 다른프로세에게 보내는 비동기적(명령어쳤을 때) 알림 이벤트 메세지

signal 1, 2, 6, 8, 9, 15 정도는 필수적으로 암기


# kill [-1 | -2 | -9 | -15] PID PID

signal 종류

----------------------------------                     (반영시키기 위해)

1  SIGHUP   HangUp signal, process restart   # kill -1 PID (프로세스 Restart)

SIGINT    Interrupt signal                          # kill -2 PID (인터럽트 걸려서 종료)

9  SIGKILL   force signal                              # kill -9 PID (강제종료)

15 SIGTERM termination(default)                   # kill -15 PID (정상종료)

----------------------------------

 

 

 

[실습] signal(-1/-9/-15) 간단한 실습

 

# pgrep -lf sendmail

 

4462 sendmail: accepting connections

4472 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue

 

 

# kill -15 4462

# pgrep -lf sendmail

 

4472 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue

 

 

# service sendmail restart

# pgrep -lf sendmail

 

11130 sendmail: accepting connections

11140 sendmail: Queue runner@01:00:00 for /var/spool/clientmque

 

 

# kill -9 11130

# pgrep -lf sendmail

 

11140 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue

 

 

# service sendmail restart

# pgrep -lf sendmail

 

11200 sendmail: accepting connections

11210 sendmail: Queue runner@01:00:00 for /var/spool/clientmque

 

 

# kill -1 11200

# pgrep -lf sendmail

=> 종료되었다가 다시 뜨면서 새로운 PID를 할당받은것을 알 수 있다.

11223 sendmail: accepting connections

11210 sendmail: Queue runner@01:00:00 for /var/spool/clientmqueue

 

 

 

 

[실습] signal() 함수 사용법

# cd /test && rm -rf /test/*

# vi signal.c

 

#include<stdio.h>

#include<signal.h>

#include<unistd.h>

 

/* CTRL + C function */

void sigint_handler(int signo)

{

     printf("received %d\n", signo);

     signal(SIGINT, SIG_DFL); /* signal excute */

}      운영체제가 정의한대로 정상동작

 

/* CTRL + Z function */

void sigtstp_handler(int signo)

{

     printf("received %d\n", signo);

     signal(SIGTSTP, SIG_IGN); /* signal not excute */

}

 

/* CTRL + \ function */

void sigquit_handler(int signo)

{

     printf("received %d\n", signo);

     signal(SIGQUIT, SIG_DFL); /* signal excute */

}

 

int main(void)

{

     if (signal(SIGINT, sigint_handler) == SIG_ERR)

     {

          printf("\ncan't catch signal\n");

     }

 

     if(signal(SIGTSTP, sigtstp_handler) == SIG_ERR)

     {

          printf("\ncan't catch signal\n");

     }

     if(signal(SIGQUIT, sigquit_handler) == SIG_ERR)

     {

          printf("\ncan't catch signal\n");

     }

     while(1)

          sleep(1);

 

     return 0;

}

 

 

# gcc -o signal signal.c

# ./signal

<CTRL + C>

received 2

<CTRL + \>

received 3

<CTRL + C>

 

# ./signal

<CTRL + C>

received 2

<CTRL + \>

received 3

<CTRL + Z>

received 20

<CTRL + C>

 

# ./signal

<CTRL + Z>

received 20

<CTRL + C>

received 2

<CTRL + \>

received 3

<CTRL + C>

 

# stty -a (# stty --all)

 

speed 38400 baud; rows 33; columns 71; line = 0;

intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?;

eol2 = M-^?; swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;

werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;

-parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts -cdtrdsr

-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon

-ixoff -iuclc ixany imaxbel iutf8

opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0

isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop

-echoprt echoctl echoke

^C는 <컨트롤 + C>를 뜻한다.