SIGR: Yet Another Simple SIGnal manageR

Signal plays an important role in communication with POSIX system.

For example, what if our code been killed, interrupted, hung on? Generally, the operation system (I am talking about BSD, Unix-like, POSIX system, same in the follow but will omitted), will not really “KILL” it, it will just send a kill signal with id to this process, the this process will process this signal later.

Here is a table to describe the id to default action.

Num | Name | Default Action | Description
1 | SIGHUP | terminate process | terminal line hangup
2 | SIGINT | terminate process | interrupt program
3 | SIGQUIT | create core image | quit program
4 | SIGILL | create core image | illegal instruction
5 | SIGTRAP | create core image | trace trap
6 | SIGABRT | create core image | abort program (formerly SIGIOT)
7 | SIGEMT | create core image | emulate instruction executed
8 | SIGFPE | create core image | floating-point exception
9 | SIGKILL | terminate process | kill program
10 | SIGBUS | create core image | bus error
11 | SIGSEGV | create core image | segmentation violation
12 | SIGSYS | create core image | non-existent system call invoked
13 | SIGPIPE | terminate process | write on a pipe with no reader
14 | SIGALRM | terminate process | real-time timer expired
15 | SIGTERM | terminate process | software termination signal
16 | SIGURG | discard signal | urgent condition present on socket
17 | SIGSTOP | stop process | stop (cannot be caught or ignored)
18 | SIGTSTP | stop process | stop signal generated from keyboard
19 | SIGCONT | discard signal | continue after stop
20 | SIGCHLD | discard signal | child status has changed
21 | SIGTTIN | stop process | background read attempted from control terminal
22 | SIGTTOU | stop process | background write attempted to control terminal
23 | SIGIO | discard signal | I/O is possible on a descriptor (see fcntl(2))
24 | SIGXCPU | terminate process | cpu time limit exceeded (see setrlimit(2))
25 | SIGXFSZ | terminate process | file size limit exceeded (see setrlimit(2))
26 | SIGVTALRM | terminate process | virtual time alarm (see setitimer(2))
27 | SIGPROF | terminate process | profiling timer alarm (see setitimer(2))
28 | SIGWINCH | discard signal | Window size change
29 | SIGINFO | discard signal | status request from keyboard
30 | SIGUSR1 | terminate process | User defined signal 1
31 | SIGUSR2 | terminate process | User defined signal 2

You can also find this table using

$ man 3 signal

According to this table, we can easily find the default action of ‘9’ is ‘terminate process’

If we execute a command like this

$ kill -9

We are not killed this process, we just send a signal to this process, and this process have suicide later in processing this signal.

However, if we are required to process some important action, and a interrupting action comes, what should we do?

The best way, as far as I am concerned, is roll back if finishing will take some time, or save current status to make sure we could continue it once it restarted.

A lot of code provide this signal proces.

In (https://golang.org/), it provides signal as well, which is implemented using channel.

However, what if we have more than one thing to process in different part? A solution could be like this.

We could create an individual service to replace the default action, and then other code could just register/unregister functions by name (which is used to avoid duplicate processing).

Here is the code of my implementation. Please don’t hesitate to give me any feedback if it is possible.

Github:https://github.com/argcv/sigr

A simple usage could be as follow:

package main

import (
"fmt"
"github.com/argcv/sigr"
"time"
)

func main() {
// register a new name
// this is used to
name1 := sigr.RegisterOnStopFuncAutoName(func() {
fmt.Println("Hello, World! #1")
})
name2 := sigr.RegisterOnStopFuncAutoName(func() {
fmt.Println("Hello, World! #2")
})
sigr.RegisterOnStopFunc("customized name", func() {
fmt.Println("Hello, World! #3")
})
fmt.Println("name1:", name1, "name2:", name2)
sigr.RegisterOnStopFunc(name1, func() {
fmt.Println("Hello, World! #1 #overridden")
})
sigr.UnregisterOnStopFunc(name2)
// default: true
sigr.SetQuitDirectyl(true)
// get verbose log
sigr.VerboseLog()
// without verbose log, your printing is still work here
//sigr.NoLog()
// you may type ctrl+C to excute the functions (name1 and "customized name")
time.Sleep(time.Duration(20 * time.Second))
}

The output seems like follow:

$ go run example.go
name1: __auto_1 name2: __auto_2
^C2017/10/26 19:06:17.804737 sigr.go:74: sig: [interrupt] Processing...
2017/10/26 19:06:17.804935 sigr.go:78: Processing task [__auto_1]
Hello, World! #1 #overridden
2017/10/26 19:06:17.804949 sigr.go:78: Processing task [customized name]
Hello, World! #3
2017/10/26 19:06:17.805069 sigr.go:85: sig: [interrupt] Processed, quitting directly
signal: interrupt