-
Notifications
You must be signed in to change notification settings - Fork 103
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
[NixOS support] Run patchelf after autoupdate download #1468
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6aa12ea
Make package for nixos support
RebeccaMahany bb2be51
Add finalize patchelf
RebeccaMahany ece50ed
Merge remote-tracking branch 'upstream/main' into becca/patchelf
RebeccaMahany 76bc55d
Read ELF headers to determine correct interpreter
RebeccaMahany 3d63d77
Read ELF headers to determine correct interpreter
RebeccaMahany 2741611
Add tests, documentation
RebeccaMahany 930bbab
Merge branch 'becca/patchelf' of github.com:RebeccaMahany/launcher in…
RebeccaMahany 325366e
Fix rebase whoops
RebeccaMahany aa6faea
Don't reload launcher if localdev_path is set
RebeccaMahany 50b30be
Use interpreter set by autopatchelfhook
RebeccaMahany fcedb84
Merge remote-tracking branch 'upstream/main' into becca/patchelf
RebeccaMahany File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//go:build linux | ||
// +build linux | ||
|
||
package tuf | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/kolide/launcher/pkg/allowedcmd" | ||
) | ||
|
||
// patchExecutable updates the downloaded binary as necessary for it to be able to | ||
// run on this system. On NixOS, we have to set the interpreter for any non-NixOS | ||
// executable we want to run. | ||
// See: https://unix.stackexchange.com/a/522823 | ||
func patchExecutable(executableLocation string) error { | ||
if !allowedcmd.IsNixOS() { | ||
return nil | ||
} | ||
|
||
interpreter, err := getInterpreter(executableLocation) | ||
if err != nil { | ||
return fmt.Errorf("getting interpreter for %s: %w", executableLocation, err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
cmd, err := allowedcmd.Patchelf(ctx, "--set-interpreter", interpreter, executableLocation) | ||
if err != nil { | ||
return fmt.Errorf("creating patchelf command: %w", err) | ||
} | ||
|
||
if out, err := cmd.CombinedOutput(); err != nil { | ||
return fmt.Errorf("running patchelf: output `%s`, error `%w`", string(out), err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// getInterpreter asks patchelf what the interpreter is for the current running | ||
// executable, assuming that's a reasonable choice given that the current executable | ||
// is able to run. | ||
func getInterpreter(executableLocation string) (string, error) { | ||
currentExecutable, err := os.Executable() | ||
if err != nil { | ||
return "", fmt.Errorf("getting current running executable: %w", err) | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
cmd, err := allowedcmd.Patchelf(ctx, "--print-interpreter", currentExecutable) | ||
if err != nil { | ||
return "", fmt.Errorf("creating patchelf command: %w", err) | ||
} | ||
|
||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return "", fmt.Errorf("running patchelf: output `%s`, error `%w`", string(out), err) | ||
|
||
} | ||
|
||
return strings.TrimSpace(string(out)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package tuf | ||
|
||
func patchExecutable(executableLocation string) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//go:build !linux | ||
// +build !linux | ||
|
||
package tuf | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_patchExecutable(t *testing.T) { | ||
t.Parallel() | ||
|
||
// patchExecutable is a no-op on windows and darwin | ||
require.NoError(t, patchExecutable("")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also use a pointer to a bool. But this is fine