signal

package
v1.2.55 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpPreviousStacktrace

func DumpPreviousStacktrace()

DumpPreviousStacktrace dumps the previous human readable stacktrace to fd, which is set by SetSignalDumpToFd.

func DumpSignalTo

func DumpSignalTo(fd int)

DumpSignalTo redirects log to fd, -1 if not set; muted if < 0.

func DumpStacktraceTo

func DumpStacktraceTo(name string)

DumpStacktraceTo set dump file path of stacktrace when signal is triggered "*.stacktrace.dump" under a temp dir if not set.

func Notify

func Notify(c chan<- os.Signal, sigs ...os.Signal)

Notify act as signal.Notify, which invokes the Go signal handler. https://godoc.org/os/signal#hdr-Go_programs_that_use_cgo_or_SWIG Notify must be called again when one sig is called on windows system as windows is based on signal(), which will reset sig's handler to SIG_DFL before sig's handler is called While unix-like os will remain sig's handler always.

Example
package main

import (
	"fmt"
	"os"
	"runtime"
	"sync"
	"syscall"
	"time"

	signal_ "github.com/searKing/golang/go/os/signal"
)

func main() {
	signal_.DumpSignalTo(syscall.Stderr)
	signal_.SetSigInvokeChain(syscall.SIGUSR1, syscall.SIGUSR2, 0, syscall.SIGINT)

	// Set up channel on which to send signal notifications.
	// We must use a buffered channel or risk missing the signal
	// if we're not ready to receive when the signal is sent.
	c := make(chan os.Signal, 1)
	//signal.Notify(c, syscall.SIGINT, syscall.SIGSEGV)
	signal_.Notify(c, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGINT)

	// simulate to send a SIGINT to this example test
	go func() {
		_ = syscall.Kill(syscall.Getpid(), syscall.SIGINT)
	}()

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		runtime.LockOSThread()
		defer runtime.UnlockOSThread()
		defer wg.Done()
		for {
			// Block until a signal is received.
			select {
			case s, ok := <-c:
				if !ok {
					return
				}
				//signal.Stop(c)
				//close(c)
				if s == syscall.SIGUSR1 {
					_, _ = fmt.Fprintf(os.Stderr, "Previous run crashed:\n%s\n", signal_.PreviousStacktrace())
					_ = syscall.Kill(syscall.Getpid(), syscall.SIGUSR2)
				} else if s != syscall.SIGUSR2 {
					fmt.Printf("Got signal: %s\n", s)

					// just in case of windows os system, which is based on signal() in C language
					// you can comment below out on unix-like os system.
					signal_.Notify(c, s)
				}
				if s == syscall.SIGINT {
					return
				}

			case <-time.After(time.Minute):
				_, _ = fmt.Fprintf(os.Stderr, "time overseed:\n")
				return
			}
			// set os.Stderr to pass test, for the stacktrace is random.
			// stderr prints something like:
			// Signal received(2).
			// Stacktrace dumped to file: stacktrace.dump.
			// Previous run crashed:
			//  0# searking::SignalHandler::operator()(int, __siginfo*, void*) in /private/var/folders/12/870qx8rd0_d96nt6g078wp080000gn/T/___ExampleSignalAction_in_github_com_searKing_golang_go_os_signal
			//  1# _sigtramp in /usr/lib/system/libsystem_platform.dylib

		}
	}()
	wg.Wait()

}
Output:

Got signal: interrupt

func PreviousStacktrace

func PreviousStacktrace() string

PreviousStacktrace returns a human readable stacktrace

func SetSigInvokeChain

func SetSigInvokeChain(to os.Signal, wait os.Signal, sleepInSeconds int, froms ...os.Signal)

SetSigInvokeChain sets a rule to raise signal to {to} and wait until {wait}, done with sleep {sleepInSeconds}s

func Signum

func Signum(sig os.Signal) int

Types

type OnSignalHandler

type OnSignalHandler interface {
	OnSignal(signum os.Signal)
}

type OnSignalHandlerFunc

type OnSignalHandlerFunc func(signum os.Signal)

func (OnSignalHandlerFunc) OnSignal

func (f OnSignalHandlerFunc) OnSignal(signum os.Signal)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL