File: signal_linux.go

package info (click to toggle)
singularity-container 4.0.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,672 kB
  • sloc: asm: 3,857; sh: 2,125; ansic: 1,677; awk: 414; makefile: 110; python: 99
file content (74 lines) | stat: -rw-r--r-- 1,818 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2018-2019, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package signal

/*
#include <signal.h>

void raiseSignal(int sig) {
	signal(sig, SIG_DFL);
	raise(sig);
}
*/
import "C"

import (
	"fmt"
	"strconv"
	"strings"

	"golang.org/x/sys/unix"
)

// similarSignals maps similar signals not handled
// by unix package.
var similarSignals = map[string]string{
	"SIGIOT":  "SIGABRT",
	"SIGCLD":  "SIGCHLD",
	"SIGPOLL": "SIGIO",
}

// Convert converts a signal string to corresponding signal number
func Convert(sig string) (unix.Signal, error) {
	sigStr := strings.ToUpper(sig)

	if !strings.HasPrefix(sigStr, "SIG") {
		sigStr = "SIG" + sigStr
	}
	if s, ok := similarSignals[sigStr]; ok {
		sigStr = s
	}

	sigNum := unix.SignalNum(sigStr)
	if sigNum != 0 {
		return sigNum, nil
	}

	sigConv, err := strconv.ParseInt(sig, 10, 32)
	if err != nil {
		return sigNum, fmt.Errorf("%s is not a number", sig)
	}

	sigName := unix.SignalName(unix.Signal(sigConv))
	sigNum = unix.SignalNum(sigName)
	if sigNum == 0 {
		return sigNum, fmt.Errorf("can't convert %s to signal number", sig)
	}

	return sigNum, nil
}

// Raise sends a signal to the current process and ensure the
// current signal handler is set to its default handler for the
// corresponding signal. It allows to send signals like SIGABRT
// without triggering Go core dump handler.
func Raise(sig unix.Signal) {
	// signal handlers like the one for SIGABRT can't be reset
	// easily from Go without using rt_sigaction syscall implying
	// to defines sigaction structure for various architecture so
	// let's proceed with C
	C.raiseSignal(C.int(int(sig)))
}