diff --git a/ci/go-no-os-exit.sh b/ci/go-no-os-exit.sh deleted file mode 100755 index 5f0f98436a..0000000000 --- a/ci/go-no-os-exit.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash -# Copyright (c) 2018 Intel Corporation -# -# SPDX-License-Identifier: Apache-2.0 -# -# Check there are no os.Exit() calls creeping into the code -# We don't use that exit path in the Kata codebase. - -# Allow the path to check to be over-ridden. -# Default to the current directory. -go_packages=${1:-.} - -echo "Checking for no os.Exit() calls for package [${go_packages}]" - -candidates=`go list -f '{{.Dir}}/*.go' $go_packages` -for f in $candidates; do - filename=`basename $f` - # skip all go test files - [[ $filename == *_test.go ]] && continue - # skip exit.go where, the only file we should call os.Exit() from. - [[ $filename == "exit.go" ]] && continue - files="$f $files" -done - -[ -z "$files" ] && echo "No files to check, skipping" && exit 0 - -if egrep -n '\' $files; then - echo "Direct calls to os.Exit() are forbidden, please use exit() so atexit() works" - exit 1 -fi diff --git a/src/runtime/Makefile b/src/runtime/Makefile index f938981e8c..5bfc803f13 100644 --- a/src/runtime/Makefile +++ b/src/runtime/Makefile @@ -573,7 +573,6 @@ $(MONITOR_OUTPUT): $(SOURCES) $(GENERATED_FILES) $(MAKEFILE_LIST) .git-commit .PHONY: \ check \ - check-go-static \ coverage \ default \ install \ @@ -595,7 +594,7 @@ $(GENERATED_FILES): %: %.in $(MAKEFILE_LIST) VERSION .git-commit generate-config: $(CONFIGS) -check: check-go-static +check: test: install-hook go-test @@ -610,10 +609,6 @@ go-test: $(GENERATED_FILES) go clean -testcache go test -v -mod=vendor ./... -check-go-static: - $(QUIET_CHECK)../../ci/go-no-os-exit.sh ./cmd/kata-runtime - $(QUIET_CHECK)../../ci/go-no-os-exit.sh ./virtcontainers - coverage: go test -v -mod=vendor -covermode=atomic -coverprofile=coverage.txt ./... go tool cover -html=coverage.txt -o coverage.html diff --git a/src/runtime/cmd/kata-runtime/exit.go b/src/runtime/cmd/kata-runtime/exit.go deleted file mode 100644 index 4a95ebe0de..0000000000 --- a/src/runtime/cmd/kata-runtime/exit.go +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2017 Intel Corporation -// -// SPDX-License-Identifier: Apache-2.0 -// - -package main - -import "os" - -var atexitFuncs []func() - -var exitFunc = os.Exit - -// atexit registers a function f that will be run when exit is called. The -// handlers so registered will be called the in reverse order of their -// registration. -func atexit(f func()) { - atexitFuncs = append(atexitFuncs, f) -} - -// exit calls all atexit handlers before exiting the process with status. -func exit(status int) { - for i := len(atexitFuncs) - 1; i >= 0; i-- { - f := atexitFuncs[i] - f() - } - exitFunc(status) -} diff --git a/src/runtime/cmd/kata-runtime/exit_test.go b/src/runtime/cmd/kata-runtime/exit_test.go deleted file mode 100644 index 59913762bd..0000000000 --- a/src/runtime/cmd/kata-runtime/exit_test.go +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2017 Intel Corporation -// -// SPDX-License-Identifier: Apache-2.0 -// - -package main - -import ( - "os" - "testing" - - "github.com/stretchr/testify/assert" -) - -var testFoo string - -func testFunc() { - testFoo = "bar" -} - -func TestExit(t *testing.T) { - assert := assert.New(t) - - var testExitStatus int - exitFunc = func(status int) { - testExitStatus = status - } - - defer func() { - exitFunc = os.Exit - }() - - // test with no atexit functions added. - exit(1) - assert.Equal(testExitStatus, 1) - - // test with a function added to the atexit list. - atexit(testFunc) - exit(0) - assert.Equal(testFoo, "bar") - assert.Equal(testExitStatus, 0) -} diff --git a/src/runtime/cmd/kata-runtime/main.go b/src/runtime/cmd/kata-runtime/main.go index 23cd3d07de..845887ad85 100644 --- a/src/runtime/cmd/kata-runtime/main.go +++ b/src/runtime/cmd/kata-runtime/main.go @@ -33,6 +33,8 @@ import ( // arch is the architecture for the running program const arch = goruntime.GOARCH +var exitFunc = os.Exit + var usage = fmt.Sprintf(`%s runtime %s is a command line program for running applications packaged @@ -328,7 +330,7 @@ func handleShowConfig(context *cli.Context) { fmt.Fprintf(defaultOutputFile, "%s\n", file) } - exit(0) + exitFunc(0) } } @@ -448,7 +450,7 @@ func userWantsUsage(context *cli.Context) bool { func fatal(err error) { kataLog.Error(err) fmt.Fprintln(defaultErrorFile, err) - exit(1) + exitFunc(1) } type fatalWriter struct { diff --git a/src/runtime/cmd/kata-runtime/main_test.go b/src/runtime/cmd/kata-runtime/main_test.go index c90b9338d0..f8dab195e9 100644 --- a/src/runtime/cmd/kata-runtime/main_test.go +++ b/src/runtime/cmd/kata-runtime/main_test.go @@ -93,7 +93,7 @@ func TestMain(m *testing.M) { if path.Base(os.Args[0]) == katautils.NAME+".coverage" || path.Base(os.Args[0]) == katautils.NAME { main() - exit(0) + exitFunc(0) } runUnitTests(m)