Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap main and remove os.Exit calls so all deferred functions will execute #1693

Merged
merged 18 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ linters-settings:
forbid:
- p: ^exec\.Command.*$
msg: use ee/allowedcmd functions instead
- p: ^os\.Exit.*$
directionless marked this conversation as resolved.
Show resolved Hide resolved
msg: do not use os.Exit so that launcher can shut down gracefully
sloglint:
kv-only: true
context-only: true
Expand Down
49 changes: 33 additions & 16 deletions cmd/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ import (
)

func main() {
if err := runMain(); err != nil {
os.Exit(1) //nolint:forbidigo // Our only allowed usages of os.Exit are in this function
}
os.Exit(0) //nolint:forbidigo // Our only allowed usages of os.Exit are in this function
}

func runMain() error {
systemSlogger, logCloser, err := multislogger.SystemSlogger()
if err != nil {
fmt.Fprintf(os.Stderr, "error creating system logger: %v\n", err)
os.Exit(1)
return fmt.Errorf("creating system logger: %w", err)
}
defer logCloser.Close()

Expand Down Expand Up @@ -62,22 +69,31 @@ func main() {
// fork-bombing itself. This is an ENV, because there's no
// good way to pass it through the flags.
if !env.Bool("LAUNCHER_SKIP_UPDATES", false) && !inBuildDir {
runNewerLauncherIfAvailable(ctx, systemSlogger.Logger)
if err := runNewerLauncherIfAvailable(ctx, systemSlogger.Logger); err != nil {
return fmt.Errorf("running newer version of launcher: %w", err)
RebeccaMahany marked this conversation as resolved.
Show resolved Hide resolved
}
}

// if the launcher is being ran with a positional argument,
// handle that argument. Fall-back to running launcher
if len(os.Args) > 1 && !strings.HasPrefix(os.Args[1], `-`) {
if err := runSubcommands(systemSlogger); err != nil {
logutil.Fatal(logger, "err", fmt.Errorf("run with positional args: %w", err))
systemSlogger.Log(ctx, slog.LevelInfo,
"running with positional args",
"err", err,
)
return fmt.Errorf("running with positional args: %w", err)
}
os.Exit(0)
return nil
}

opts, err := launcher.ParseOptions("", os.Args[1:])
if err != nil {
if launcher.IsInfoCmd(err) {
return nil
}
level.Info(logger).Log("err", err)
os.Exit(1)
return fmt.Errorf("parsing options: %w", err)
RebeccaMahany marked this conversation as resolved.
Show resolved Hide resolved
}

// recreate the logger with the appropriate level.
Expand Down Expand Up @@ -121,14 +137,16 @@ func main() {
ctx = ctxlog.NewContext(ctx, logger)

if err := runLauncher(ctx, cancel, slogger, systemSlogger, opts); err != nil {
if tuf.IsLauncherReloadNeededErr(err) {
level.Debug(logger).Log("msg", "runLauncher exited to run newer version of launcher", "err", err.Error())
runNewerLauncherIfAvailable(ctx, slogger.Logger)
} else {
if !tuf.IsLauncherReloadNeededErr(err) {
level.Debug(logger).Log("msg", "run launcher", "stack", fmt.Sprintf("%+v", err))
logutil.Fatal(logger, err, "run launcher")
return fmt.Errorf("running launcher: %w", err)
}
level.Debug(logger).Log("msg", "runLauncher exited to run newer version of launcher", "err", err.Error())
if err := runNewerLauncherIfAvailable(ctx, slogger.Logger); err != nil {
return fmt.Errorf("running newer version of launcher: %w", err)
}
}
return nil
}

func runSubcommands(systemMultiSlogger *multislogger.MultiSlogger) error {
Expand Down Expand Up @@ -174,7 +192,7 @@ func runSubcommands(systemMultiSlogger *multislogger.MultiSlogger) error {

// runNewerLauncherIfAvailable checks the autoupdate library for a newer version
// of launcher than the currently-running one. If found, it will exec that version.
func runNewerLauncherIfAvailable(ctx context.Context, slogger *slog.Logger) {
func runNewerLauncherIfAvailable(ctx context.Context, slogger *slog.Logger) error {
newerBinary, err := latestLauncherPath(ctx, slogger)
if err != nil {
slogger.Log(ctx, slog.LevelError,
Expand All @@ -185,15 +203,15 @@ func runNewerLauncherIfAvailable(ctx context.Context, slogger *slog.Logger) {
// Fall back to legacy autoupdate library
newerBinary, err = autoupdate.FindNewestSelf(ctx)
if err != nil {
return
return nil
}
}

if newerBinary == "" {
slogger.Log(ctx, slog.LevelInfo,
"nothing newer",
)
return
return nil
}

slogger.Log(ctx, slog.LevelInfo,
Expand All @@ -208,14 +226,14 @@ func runNewerLauncherIfAvailable(ctx context.Context, slogger *slog.Logger) {
"new_binary", newerBinary,
"err", err,
)
os.Exit(1)
return fmt.Errorf("execing newer version of launcher: %w", err)
}

slogger.Log(ctx, slog.LevelError,
"execing newer version of launcher exited unexpectedly without error",
"new_binary", newerBinary,
)
os.Exit(1)
return errors.New("execing newer version of launcher exited unexpectedly without error")
}

// latestLauncherPath looks for the latest version of launcher in the new autoupdate library,
Expand Down Expand Up @@ -261,6 +279,5 @@ func runVersion(_ *multislogger.MultiSlogger, args []string) error {
version.PrintFull()
detachConsole()

os.Exit(0)
return nil
}
11 changes: 3 additions & 8 deletions cmd/launcher/svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
package main

import (
"fmt"
"os"
"errors"

"github.com/kolide/launcher/pkg/log/multislogger"
)

func runWindowsSvc(_ *multislogger.MultiSlogger, _ []string) error {
fmt.Println("This isn't windows")
os.Exit(1)
return nil
return errors.New("this is not windows")
}

func runWindowsSvcForeground(_ *multislogger.MultiSlogger, _ []string) error {
fmt.Println("This isn't windows")
os.Exit(1)
return nil
return errors.New("this is not windows")
}
21 changes: 16 additions & 5 deletions cmd/launcher/svc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func runWindowsSvc(systemSlogger *multislogger.MultiSlogger, args []string) erro
"error parsing options",
"err", err,
)
os.Exit(1)
return fmt.Errorf("parsing options: %w", err)
}

localSlogger := multislogger.New()
Expand Down Expand Up @@ -152,7 +152,7 @@ func runWindowsSvcForeground(systemSlogger *multislogger.MultiSlogger, args []st
opts, err := launcher.ParseOptions("", os.Args[2:])
if err != nil {
level.Info(logger).Log("err", err)
os.Exit(1)
return fmt.Errorf("parsing options: %w", err)
}

// set extra debug options
Expand Down Expand Up @@ -183,6 +183,8 @@ func (w *winSvc) Execute(args []string, r <-chan svc.ChangeRequest, changes chan

ctx = ctxlog.NewContext(ctx, w.logger)

runLauncherResults := make(chan struct{}, 0)

go func() {
err := runLauncher(ctx, cancel, w.slogger, w.systemSlogger, w.opts)
if err != nil {
Expand All @@ -192,8 +194,8 @@ func (w *winSvc) Execute(args []string, r <-chan svc.ChangeRequest, changes chan
"stack_trace", fmt.Sprintf("%+v", errors.WithStack(err)),
)
changes <- svc.Status{State: svc.Stopped, Accepts: cmdsAccepted}
// Launcher is already shut down -- fully exit so that the service manager can restart the service
os.Exit(1)
// Launcher is already shut down -- send signal to fully exit so that the service manager can restart the service
runLauncherResults <- struct{}{}
}

// If we get here, it means runLauncher returned nil. If we do
Expand All @@ -204,7 +206,7 @@ func (w *winSvc) Execute(args []string, r <-chan svc.ChangeRequest, changes chan
"runLauncher exited cleanly",
)
changes <- svc.Status{State: svc.Stopped, Accepts: cmdsAccepted}
os.Exit(0)
runLauncherResults <- struct{}{}
}()

for {
Expand All @@ -231,6 +233,15 @@ func (w *winSvc) Execute(args []string, r <-chan svc.ChangeRequest, changes chan
"change_request", fmt.Sprintf("%+v", c),
)
}
case <-runLauncherResults:
RebeccaMahany marked this conversation as resolved.
Show resolved Hide resolved
w.systemSlogger.Log(ctx, slog.LevelInfo,
"shutting down service after launcher shutdown",
)
changes <- svc.Status{State: svc.StopPending}
cancel()
time.Sleep(2 * time.Second) // give rungroups enough time to shut down
changes <- svc.Status{State: svc.Stopped, Accepts: cmdsAccepted}
return ssec, errno
}
}
}
6 changes: 3 additions & 3 deletions cmd/package-builder/package-builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func usage() {
func main() {
if len(os.Args) < 2 {
usage()
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit outside of launcher
}

var run func([]string) error
Expand All @@ -331,12 +331,12 @@ func main() {
run = runListTargets
default:
usage()
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit outside of launcher
}

if err := run(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit outside of launcher
}
}

Expand Down
10 changes: 5 additions & 5 deletions ee/agent/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func TestMain(m *testing.M) {
downloadDir, err := os.MkdirTemp("", "osquery-runsimple")
if err != nil {
fmt.Printf("failed to make temp dir for test osquery binary: %v", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
}
defer os.RemoveAll(downloadDir)

target := packaging.Target{}
if err := target.PlatformFromString(runtime.GOOS); err != nil {
fmt.Printf("error parsing platform %s: %v", runtime.GOOS, err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand All @@ -49,7 +49,7 @@ func TestMain(m *testing.M) {
dlPath, err := packaging.FetchBinary(ctx, downloadDir, "osqueryd", target.PlatformBinaryName("osqueryd"), "nightly", target)
if err != nil {
fmt.Printf("error fetching binary osqueryd binary: %v", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
}

testOsqueryBinary = filepath.Join(downloadDir, filepath.Base(dlPath))
Expand All @@ -59,12 +59,12 @@ func TestMain(m *testing.M) {

if err := fsutil.CopyFile(dlPath, testOsqueryBinary); err != nil {
fmt.Printf("error copying osqueryd binary: %v", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
}

// Run the tests
retCode := m.Run()
os.Exit(retCode)
os.Exit(retCode) //nolint:forbidigo // Fine to use os.Exit inside tests
}

func TestDetectAndRemediateHardwareChange(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions ee/agent/storage/ci/keyvalue_store_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func SetupDB(t *testing.T) *bbolt.DB {
dbDir, err = os.MkdirTemp(os.TempDir(), "storage-bbolt")
if err != nil {
fmt.Println("Failed to create temp dir for bbolt test")
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
}
}

Expand All @@ -52,7 +52,7 @@ func SetupDB(t *testing.T) *bbolt.DB {
} else {
if err != nil {
fmt.Println("Falied to create bolt db")
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
}
}

Expand Down
2 changes: 1 addition & 1 deletion ee/dataflatten/examples/flatten/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func checkError(err error) {
if err != nil {
fmt.Printf("Got Error: %v\nStack:\n%+v\n", err, err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit outside of launcher proper
}
}

Expand Down
6 changes: 3 additions & 3 deletions ee/tuf/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ func TestHelperProcess(t *testing.T) {
case "sleep":
time.Sleep(10 * time.Second)
case "exit0":
os.Exit(0)
os.Exit(0) //nolint:forbidigo // Fine to use os.Exit inside tests
case "exit1":
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit inside tests
case "exit2":
os.Exit(2)
os.Exit(2) //nolint:forbidigo // Fine to use os.Exit inside tests
}

// default behavior nothing
Expand Down
14 changes: 7 additions & 7 deletions ee/ui/assets/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
)
if err := flagset.Parse(os.Args[1:]); err != nil {
level.Error(logger).Log("msg", "error parsing flags", "err", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit outside of launcher proper
}

// relevel with the debug flag
Expand All @@ -61,7 +61,7 @@ func main() {
}
}
if missingOpt {
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit outside of launcher proper
}

inDir = *flIndir
Expand All @@ -72,7 +72,7 @@ func main() {
files, err := filepath.Glob(inDir + "/*.png")
if err != nil {
level.Error(logger).Log("msg", "error globbing input files", "error", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit outside of launcher proper
}
for _, file := range files {
file = filepath.Base(file)
Expand All @@ -82,7 +82,7 @@ func main() {

if dir, err := os.MkdirTemp("", "icon-generator"); err != nil {
level.Error(logger).Log("msg", "error making tmpdir", "err", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit outside of launcher proper
} else {
tmpDir = dir
}
Expand All @@ -95,21 +95,21 @@ func main() {
"msg", "error generating ico",
"name", name,
"err", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit outside of launcher proper
}

if err := generatePng(ctx, logger, name); err != nil {
level.Error(logger).Log(
"msg", "error generating png",
"name", name,
"err", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit outside of launcher proper
}
}

if err := generateAssetGo(ctx, logger); err != nil {
level.Error(logger).Log("msg", "error expanding template", "error", err)
os.Exit(1)
os.Exit(1) //nolint:forbidigo // Fine to use os.Exit outside of launcher proper
}

}
Expand Down
Loading
Loading