-
Notifications
You must be signed in to change notification settings - Fork 5k
vfkit: More robust state management #20506
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10ea7ca
Add process package
nirs f568744
vfkit: Use process pidfile helpers
nirs 0836655
vfkit: Simpler and more robust GetState()
nirs a51c3fe
vfkit: Use process.Exists()
nirs 233c0ff
vfkit: More robust Stop()
nirs 6319ed1
vfkit: More robust Kill()
nirs 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 hidden or 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 hidden or 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,102 @@ | ||
/* | ||
Copyright 2025 The Kubernetes Authors All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package process | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/mitchellh/go-ps" | ||
) | ||
|
||
const pidfileMode = 0o600 | ||
|
||
// WritePidfile writes pid to path. | ||
func WritePidfile(path string, pid int) error { | ||
data := fmt.Sprintf("%d", pid) | ||
return os.WriteFile(path, []byte(data), pidfileMode) | ||
} | ||
|
||
// ReadPid reads a pid from path. | ||
func ReadPidfile(path string) (int, error) { | ||
data, err := os.ReadFile(path) | ||
if err != nil { | ||
// Pass os.ErrNotExist | ||
return -1, err | ||
} | ||
s := strings.TrimSpace(string(data)) | ||
pid, err := strconv.Atoi(s) | ||
if err != nil { | ||
return -1, fmt.Errorf("invalid pid %q: %s", s, err) | ||
} | ||
return pid, nil | ||
} | ||
|
||
// Exists tells if a process matching pid and executable name exist. Executable is | ||
// not the path to the executable. | ||
func Exists(pid int, executable string) (bool, error) { | ||
// Fast path if pid does not exist. | ||
exists, err := pidExists(pid) | ||
if err != nil { | ||
return true, err | ||
} | ||
if !exists { | ||
return false, nil | ||
} | ||
|
||
// Slow path if pid exist, depending on the platform. On windows and darwin | ||
// this fetch all processes from the krenel and find a process with pid. On | ||
// linux this reads /proc/pid/stat | ||
entry, err := ps.FindProcess(pid) | ||
if err != nil { | ||
return true, err | ||
} | ||
if entry == nil { | ||
return false, nil | ||
} | ||
return entry.Executable() == executable, nil | ||
} | ||
|
||
// Terminate a process with pid and matching name. Returns os.ErrProcessDone if | ||
// the process does not exist, or nil if termiation was requested. Caller need | ||
// to wait until the process disappears. | ||
func Terminate(pid int, executable string) error { | ||
exists, err := Exists(pid, executable) | ||
if err != nil { | ||
return err | ||
} | ||
if !exists { | ||
return os.ErrProcessDone | ||
} | ||
return terminatePid(pid) | ||
} | ||
|
||
// Kill a process with pid matching executable name. Returns os.ErrProcessDone | ||
// if the process does not exist or nil the kill was requested. Caller need to | ||
// wait until the process disappears. | ||
func Kill(pid int, executable string) error { | ||
exists, err := Exists(pid, executable) | ||
if err != nil { | ||
return err | ||
} | ||
if !exists { | ||
return os.ErrProcessDone | ||
} | ||
return killPid(pid) | ||
} |
This file contains hidden or 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,53 @@ | ||
//go:build !windows | ||
|
||
/* | ||
Copyright 2025 The Kubernetes Authors All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package process | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
func pidExists(pid int) (bool, error) { | ||
// Never fails and we get a process in "done" state that returns | ||
// os.ErrProcessDone from Signal or Wait. | ||
process, err := os.FindProcess(pid) | ||
if err != nil { | ||
return true, err | ||
} | ||
if process.Signal(syscall.Signal(0)) == os.ErrProcessDone { | ||
return false, nil | ||
} | ||
return true, nil | ||
} | ||
|
||
func terminatePid(pid int) error { | ||
p, err := os.FindProcess(pid) | ||
if err != nil { | ||
return err | ||
} | ||
return p.Signal(syscall.SIGTERM) | ||
} | ||
|
||
func killPid(pid int) error { | ||
p, err := os.FindProcess(pid) | ||
if err != nil { | ||
return err | ||
} | ||
return p.Kill() | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.