Skip to content

Commit 134f7a7

Browse files
authored
Merge pull request #5007 from fidencio/wip/allow-shimv2-to-also-be-loaded-from-an-arbitrary-path
v2, util: Take the full binary path when starting the shimv2 process
2 parents 52a6021 + d80dbda commit 134f7a7

1 file changed

Lines changed: 36 additions & 15 deletions

File tree

runtime/v2/shim/util.go

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,28 @@ func Command(ctx context.Context, runtime, containerdAddress, containerdTTRPCAdd
6464
cmdPath = cmdPathI.(string)
6565
} else {
6666
var lerr error
67-
if cmdPath, lerr = exec.LookPath(name); lerr != nil {
68-
if eerr, ok := lerr.(*exec.Error); ok {
69-
if eerr.Err == exec.ErrNotFound {
70-
// LookPath only finds current directory matches based on
71-
// the callers current directory but the caller is not
72-
// likely in the same directory as the containerd
73-
// executables. Instead match the calling binaries path
74-
// (containerd) and see if they are side by side. If so
75-
// execute the shim found there.
76-
testPath := filepath.Join(filepath.Dir(self), name)
77-
if _, serr := os.Stat(testPath); serr == nil {
78-
cmdPath = testPath
79-
}
80-
if cmdPath == "" {
81-
return nil, errors.Wrapf(os.ErrNotExist, "runtime %q binary not installed %q", runtime, name)
67+
binaryPath := BinaryPath(runtime)
68+
if _, serr := os.Stat(binaryPath); serr == nil {
69+
cmdPath = binaryPath
70+
}
71+
72+
if cmdPath == "" {
73+
if cmdPath, lerr = exec.LookPath(name); err != nil {
74+
if eerr, ok := lerr.(*exec.Error); ok {
75+
if eerr.Err == exec.ErrNotFound {
76+
// LookPath only finds current directory matches based on
77+
// the callers current directory but the caller is not
78+
// likely in the same directory as the containerd
79+
// executables. Instead match the calling binaries path
80+
// (containerd) and see if they are side by side. If so
81+
// execute the shim found there.
82+
testPath := filepath.Join(filepath.Dir(self), name)
83+
if _, serr := os.Stat(testPath); serr == nil {
84+
cmdPath = testPath
85+
}
86+
if cmdPath == "" {
87+
return nil, errors.Wrapf(os.ErrNotExist, "runtime %q binary not installed %q", runtime, name)
88+
}
8289
}
8390
}
8491
}
@@ -123,6 +130,20 @@ func BinaryName(runtime string) string {
123130
return fmt.Sprintf(shimBinaryFormat, parts[len(parts)-2], parts[len(parts)-1])
124131
}
125132

133+
// BinaryPath returns the full path for the shim binary from the runtime name,
134+
// empty string returns means runtime name is invalid
135+
func BinaryPath(runtime string) string {
136+
dir := filepath.Dir(runtime)
137+
binary := BinaryName(runtime)
138+
139+
path, err := filepath.Abs(filepath.Join(dir, binary))
140+
if err != nil {
141+
return ""
142+
}
143+
144+
return path
145+
}
146+
126147
// Connect to the provided address
127148
func Connect(address string, d func(string, time.Duration) (net.Conn, error)) (net.Conn, error) {
128149
return d(address, 100*time.Second)

0 commit comments

Comments
 (0)