rclone/lib/atexit/atexit_unix.go
Michael Hanselmann 6b7d7d0441 atexit: Terminate with non-zero status after receiving signal
When rclone received a SIGINT (Ctrl+C) or SIGTERM signal while an atexit
function is registered it always terminated with status code 0. Unix
convention is to exit with a non-zero status code. Often it's
`128 + int(signum), but at least not zero.

With this change fatal signals handled by the `atexit` package cause
a non-zero exit code. On Unix systems it's `128 + int(signum)` while
on other systems, such as Windows, it's always 2 ("error not otherwise
categorised").

Resolves #5437.

Signed-off-by: Michael Hanselmann <public@hansmi.ch>
2021-07-07 17:59:26 +01:00

25 lines
636 B
Go

//+build !windows,!plan9
package atexit
import (
"os"
"syscall"
"github.com/rclone/rclone/lib/exitcode"
)
var exitSignals = []os.Signal{syscall.SIGINT, syscall.SIGTERM} // Not syscall.SIGQUIT as we want the default behaviour
// exitCode calculates the exit code for the given signal. Many Unix programs
// exit with 128+signum if they handle signals. Most shell also implement the
// same convention if a program is terminated by an uncaught and/or fatal
// signal.
func exitCode(sig os.Signal) int {
if real, ok := sig.(syscall.Signal); ok && int(real) > 0 {
return 128 + int(real)
}
return exitcode.UncategorizedError
}