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

Use unique pid file for osquery per launcher run #1657

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 7 additions & 4 deletions ee/agent/timemachine/timemachine_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package timemachine

import (
"context"
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"testing"
"time"

"github.com/kolide/kit/ulid"
"github.com/kolide/launcher/ee/agent/types/mocks"
"github.com/kolide/launcher/ee/allowedcmd"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -52,10 +54,11 @@ func TestAddExclusions(t *testing.T) {
"osquery.autoload": true,
"osquery.db/": true,
"osquery.pid": true,
"osquery.sock": true,
"osquery.sock.51807": true,
"osquery.sock.63071": true,
"osqueryd-tuf/": true,
fmt.Sprintf("osquery-%s.pid", ulid.New()): true,
"osquery.sock": true,
"osquery.sock.51807": true,
"osquery.sock.63071": true,
"osqueryd-tuf/": true,

// these should NOT be excluded
"launcher-tuf/": false,
Expand Down
5 changes: 4 additions & 1 deletion pkg/osquery/runtime/osqueryinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sync"
"time"

"github.com/kolide/kit/ulid"
"github.com/kolide/launcher/ee/agent"
"github.com/kolide/launcher/ee/agent/types"
"github.com/kolide/launcher/pkg/backoff"
Expand Down Expand Up @@ -392,8 +393,10 @@ func calculateOsqueryPaths(opts osqueryOptions) (*osqueryFilePaths, error) {

extensionAutoloadPath := filepath.Join(opts.rootDirectory, "osquery.autoload")

// We want to use a unique pidfile per launcher run to avoid file locking issues.
// See: https://github.com/kolide/launcher/issues/1599
osqueryFilePaths := &osqueryFilePaths{
pidfilePath: filepath.Join(opts.rootDirectory, "osquery.pid"),
pidfilePath: filepath.Join(opts.rootDirectory, fmt.Sprintf("osquery-%s.pid", ulid.New())),
databasePath: filepath.Join(opts.rootDirectory, "osquery.db"),
augeasPath: filepath.Join(opts.rootDirectory, "augeas-lenses"),
extensionSocketPath: extensionSocketPath,
Expand Down
18 changes: 18 additions & 0 deletions pkg/osquery/runtime/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,24 @@ func (r *Runner) launchOsqueryInstance() error {
return o.doneCtx.Err()
})

// Clean up PID file on shutdown
o.errgroup.Go(func() error {
defer r.slogger.Log(ctx, slog.LevelInfo,
"exiting errgroup",
"errgroup", "cleanup PID file",
)

<-o.doneCtx.Done()
if err := os.Remove(paths.pidfilePath); err != nil {
r.slogger.Log(ctx, slog.LevelInfo,
"could not remove PID file",
"pid_file", paths.pidfilePath,
"err", err,
)
}
return o.doneCtx.Err()
})

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/osquery/runtime/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestCalculateOsqueryPaths(t *testing.T) {
func TestCreateOsqueryCommand(t *testing.T) {
t.Parallel()
paths := &osqueryFilePaths{
pidfilePath: "/foo/bar/osquery.pid",
pidfilePath: "/foo/bar/osquery-abcd.pid",
directionless marked this conversation as resolved.
Show resolved Hide resolved
databasePath: "/foo/bar/osquery.db",
extensionSocketPath: "/foo/bar/osquery.sock",
extensionAutoloadPath: "/foo/bar/osquery.autoload",
Expand Down
Loading