mirror of
https://github.com/rclone/rclone.git
synced 2024-11-25 09:41:44 +08:00
6b7d7d0441
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>
42 lines
782 B
Go
42 lines
782 B
Go
package atexit
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/rclone/rclone/lib/exitcode"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type fakeSignal struct{}
|
|
|
|
func (*fakeSignal) String() string {
|
|
return "fake"
|
|
}
|
|
|
|
func (*fakeSignal) Signal() {
|
|
}
|
|
|
|
var _ os.Signal = (*fakeSignal)(nil)
|
|
|
|
func TestExitCode(t *testing.T) {
|
|
switch runtime.GOOS {
|
|
case "windows", "plan9":
|
|
for _, i := range []os.Signal{
|
|
os.Interrupt,
|
|
os.Kill,
|
|
} {
|
|
assert.Equal(t, exitCode(i), exitcode.UncategorizedError)
|
|
}
|
|
|
|
default:
|
|
// SIGINT (2) and SIGKILL (9) are portable numbers specified by POSIX.
|
|
assert.Equal(t, exitCode(os.Interrupt), 128+2)
|
|
assert.Equal(t, exitCode(os.Kill), 128+9)
|
|
}
|
|
|
|
// Never a real signal
|
|
assert.Equal(t, exitCode(&fakeSignal{}), exitcode.UncategorizedError)
|
|
}
|