forked from runabol/tork
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
665 additions
and
33 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
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
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
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,35 @@ | ||
//go:build linux | ||
|
||
package reexec // import "github.com/docker/docker/pkg/reexec" | ||
|
||
import ( | ||
"os/exec" | ||
"syscall" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// Self returns the path to the current process's binary. | ||
// Returns "/proc/self/exe". | ||
func Self() string { | ||
return "/proc/self/exe" | ||
} | ||
|
||
// Command returns *exec.Cmd which has Path as current binary. Also it setting | ||
// SysProcAttr.Pdeathsig to SIGTERM. | ||
// This will use the in-memory version (/proc/self/exe) of the current binary, | ||
// it is thus safe to delete or replace the on-disk binary (os.Args[0]). | ||
// | ||
// As SysProcAttr.Pdeathsig is set, the signal will be sent to the process when | ||
// the OS thread which created the process dies. It is the caller's | ||
// responsibility to ensure that the creating thread is not terminated | ||
// prematurely. See https://go.dev/issue/27505 for more details. | ||
func Command(args ...string) *exec.Cmd { | ||
return &exec.Cmd{ | ||
Path: Self(), | ||
Args: args, | ||
SysProcAttr: &syscall.SysProcAttr{ | ||
Pdeathsig: unix.SIGTERM, | ||
}, | ||
} | ||
} |
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,23 @@ | ||
//go:build freebsd || darwin | ||
|
||
package reexec // import "github.com/docker/docker/pkg/reexec" | ||
|
||
import ( | ||
"os/exec" | ||
) | ||
|
||
// Self returns the path to the current process's binary. | ||
// Uses os.Args[0]. | ||
func Self() string { | ||
return naiveSelf() | ||
} | ||
|
||
// Command returns *exec.Cmd which has Path as current binary. | ||
// For example if current binary is "docker" at "/usr/bin/", then cmd.Path will | ||
// be set to "/usr/bin/docker". | ||
func Command(args ...string) *exec.Cmd { | ||
return &exec.Cmd{ | ||
Path: Self(), | ||
Args: args, | ||
} | ||
} |
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 && !windows && !freebsd && !darwin | ||
// +build !linux,!windows,!freebsd,!darwin | ||
|
||
package reexec // import "github.com/docker/docker/pkg/reexec" | ||
|
||
import ( | ||
"os/exec" | ||
) | ||
|
||
func Self() string { | ||
return "" | ||
} | ||
|
||
// Command is unsupported on operating systems apart from Linux, Windows, and Darwin. | ||
func Command(args ...string) *exec.Cmd { | ||
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,21 @@ | ||
package reexec // import "github.com/docker/docker/pkg/reexec" | ||
|
||
import ( | ||
"os/exec" | ||
) | ||
|
||
// Self returns the path to the current process's binary. | ||
// Uses os.Args[0]. | ||
func Self() string { | ||
return naiveSelf() | ||
} | ||
|
||
// Command returns *exec.Cmd which has Path as current binary. | ||
// For example if current binary is "docker.exe" at "C:\", then cmd.Path will | ||
// be set to "C:\docker.exe". | ||
func Command(args ...string) *exec.Cmd { | ||
return &exec.Cmd{ | ||
Path: Self(), | ||
Args: args, | ||
} | ||
} |
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,52 @@ | ||
package reexec // import "github.com/docker/docker/pkg/reexec" | ||
|
||
import ( | ||
"os" | ||
"os/exec" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func init() { | ||
Register("reexec", func() { | ||
panic("Return Error") | ||
}) | ||
Init() | ||
} | ||
|
||
func TestRegister(t *testing.T) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
assert.Equal(t, `reexec func already registered under name "reexec"`, r) | ||
} | ||
}() | ||
Register("reexec", func() {}) | ||
} | ||
|
||
func TestCommand(t *testing.T) { | ||
cmd := Command("reexec") | ||
w, err := cmd.StdinPipe() | ||
assert.NilError(t, err, "Error on pipe creation: %v", err) | ||
defer w.Close() | ||
|
||
err = cmd.Start() | ||
assert.NilError(t, err, "Error on re-exec cmd: %v", err) | ||
err = cmd.Wait() | ||
assert.Error(t, err, "exit status 2") | ||
} | ||
|
||
func TestNaiveSelf(t *testing.T) { | ||
if os.Getenv("TEST_CHECK") == "1" { | ||
os.Exit(2) | ||
} | ||
cmd := exec.Command(naiveSelf(), "-test.run=TestNaiveSelf") | ||
cmd.Env = append(os.Environ(), "TEST_CHECK=1") | ||
err := cmd.Start() | ||
assert.NilError(t, err, "Unable to start command") | ||
err = cmd.Wait() | ||
assert.Error(t, err, "exit status 2") | ||
|
||
os.Args[0] = "mkdir" | ||
assert.Check(t, naiveSelf() != os.Args[0]) | ||
} |
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,51 @@ | ||
// Package reexec facilitates the busybox style reexec of the docker binary that | ||
// we require because of the forking limitations of using Go. Handlers can be | ||
// registered with a name and the argv 0 of the exec of the binary will be used | ||
// to find and execute custom init paths. | ||
package reexec // import "github.com/docker/docker/pkg/reexec" | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
) | ||
|
||
var registeredInitializers = make(map[string]func()) | ||
|
||
// Register adds an initialization func under the specified name | ||
func Register(name string, initializer func()) { | ||
if _, exists := registeredInitializers[name]; exists { | ||
panic(fmt.Sprintf("reexec func already registered under name %q", name)) | ||
} | ||
|
||
registeredInitializers[name] = initializer | ||
} | ||
|
||
// Init is called as the first part of the exec process and returns true if an | ||
// initialization function was called. | ||
func Init() bool { | ||
initializer, exists := registeredInitializers[os.Args[0]] | ||
if exists { | ||
initializer() | ||
|
||
return true | ||
} | ||
return false | ||
} | ||
|
||
func naiveSelf() string { | ||
name := os.Args[0] | ||
if filepath.Base(name) == name { | ||
if lp, err := exec.LookPath(name); err == nil { | ||
return lp | ||
} | ||
} | ||
// handle conversion of relative paths to absolute | ||
if absName, err := filepath.Abs(name); err == nil { | ||
return absName | ||
} | ||
// if we couldn't get absolute name, return original | ||
// (NOTE: Go only errors on Abs() if os.Getwd fails) | ||
return name | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package runtime | ||
package docker | ||
|
||
import ( | ||
"archive/tar" | ||
|
Oops, something went wrong.