-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NixOS support] Run patchelf after autoupdate download (#1468)
- Loading branch information
1 parent
e21f39f
commit 54774b3
Showing
8 changed files
with
126 additions
and
9 deletions.
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