github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/signals_windows.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package containerd
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  	"strings"
    23  	"syscall"
    24  
    25  	"golang.org/x/sys/windows"
    26  )
    27  
    28  var signalMap = map[string]syscall.Signal{
    29  	"HUP":    syscall.Signal(windows.SIGHUP),
    30  	"INT":    syscall.Signal(windows.SIGINT),
    31  	"QUIT":   syscall.Signal(windows.SIGQUIT),
    32  	"SIGILL": syscall.Signal(windows.SIGILL),
    33  	"TRAP":   syscall.Signal(windows.SIGTRAP),
    34  	"ABRT":   syscall.Signal(windows.SIGABRT),
    35  	"BUS":    syscall.Signal(windows.SIGBUS),
    36  	"FPE":    syscall.Signal(windows.SIGFPE),
    37  	"KILL":   syscall.Signal(windows.SIGKILL),
    38  	"SEGV":   syscall.Signal(windows.SIGSEGV),
    39  	"PIPE":   syscall.Signal(windows.SIGPIPE),
    40  	"ALRM":   syscall.Signal(windows.SIGALRM),
    41  	"TERM":   syscall.Signal(windows.SIGTERM),
    42  }
    43  
    44  // ParseSignal parses a given string into a syscall.Signal
    45  // the rawSignal can be a string with "SIG" prefix,
    46  // or a signal number in string format.
    47  func ParseSignal(rawSignal string) (syscall.Signal, error) {
    48  	s, err := strconv.Atoi(rawSignal)
    49  	if err == nil {
    50  		sig := syscall.Signal(s)
    51  		for _, msig := range signalMap {
    52  			if sig == msig {
    53  				return sig, nil
    54  			}
    55  		}
    56  		return -1, fmt.Errorf("unknown signal %q", rawSignal)
    57  	}
    58  	signal, ok := signalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")]
    59  	if !ok {
    60  		return -1, fmt.Errorf("unknown signal %q", rawSignal)
    61  	}
    62  	return signal, nil
    63  }