github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/sysutil/sysutil.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 // Package sysutil is a cross-platform compatibility layer on top of package 12 // syscall. It exposes APIs for common operations that require package syscall 13 // and re-exports several symbols from package syscall that are known to be 14 // safe. Using package syscall directly from other packages is forbidden. 15 package sysutil 16 17 import ( 18 "os" 19 "os/exec" 20 "os/signal" 21 "syscall" 22 23 "github.com/cockroachdb/errors" 24 ) 25 26 // Signal is syscall.Signal. 27 type Signal = syscall.Signal 28 29 // Errno is syscall.Errno. 30 type Errno = syscall.Errno 31 32 // Exported syscall.Errno constants. 33 const ( 34 ECONNRESET = syscall.ECONNRESET 35 ECONNREFUSED = syscall.ECONNREFUSED 36 ) 37 38 // FSInfo describes a filesystem. It is returned by StatFS. 39 type FSInfo struct { 40 FreeBlocks int64 41 AvailBlocks int64 42 TotalBlocks int64 43 BlockSize int64 44 } 45 46 // ExitStatus returns the exit status contained within an exec.ExitError. 47 func ExitStatus(err *exec.ExitError) int { 48 // err.Sys() is of type syscall.WaitStatus on all supported platforms. 49 // syscall.WaitStatus has a different type on Windows, but that type has an 50 // ExitStatus method with an identical signature, so no need for conditional 51 // compilation. 52 return err.Sys().(syscall.WaitStatus).ExitStatus() 53 } 54 55 const refreshSignal = syscall.SIGHUP 56 57 // RefreshSignaledChan returns a channel that will receive an os.Signal whenever 58 // the process receives a "refresh" signal (currently SIGHUP). A refresh signal 59 // indicates that the user wants to apply nondisruptive updates, like reloading 60 // certificates and flushing log files. 61 // 62 // On Windows, the returned channel will never receive any values, as Windows 63 // does not support signals. Consider exposing a refresh trigger through other 64 // means if Windows support is important. 65 func RefreshSignaledChan() <-chan os.Signal { 66 ch := make(chan os.Signal, 1) 67 signal.Notify(ch, refreshSignal) 68 return ch 69 } 70 71 // IsErrConnectionReset returns true if an 72 // error is a "connection reset by peer" error. 73 func IsErrConnectionReset(err error) bool { 74 return errors.Is(err, syscall.ECONNRESET) 75 } 76 77 // IsErrConnectionRefused returns true if an error is a "connection refused" error. 78 func IsErrConnectionRefused(err error) bool { 79 return errors.Is(err, syscall.ECONNREFUSED) 80 }