-
Notifications
You must be signed in to change notification settings - Fork 5.8k
remove direct dependency on docker/docker #13706
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| Copyright 2025 Docker Compose CLI authors | ||
|
|
||
| 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 pidfile provides helper functions to create and remove PID files. | ||
| // A PID file is usually a file used to store the process ID of a running | ||
| // process. | ||
| // | ||
| // This is a temporary copy of github.com/moby/moby/v2/pkg/pidfile, and | ||
| // should be replaced once pidfile is available as a standalone module. | ||
| package pidfile | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "os" | ||
| "strconv" | ||
| ) | ||
|
|
||
| // Read reads the "PID file" at path, and returns the PID if it contains a | ||
| // valid PID of a running process, or 0 otherwise. It returns an error when | ||
| // failing to read the file, or if the file doesn't exist, but malformed content | ||
| // is ignored. Consumers should therefore check if the returned PID is a non-zero | ||
| // value before use. | ||
| func Read(path string) (pid int, _ error) { | ||
| pidByte, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| pid, err = strconv.Atoi(string(bytes.TrimSpace(pidByte))) | ||
| if err != nil { | ||
| return 0, nil | ||
| } | ||
| if pid != 0 && alive(pid) { | ||
| return pid, nil | ||
| } | ||
| return 0, nil | ||
| } | ||
|
|
||
| // Write writes a "PID file" at the specified path. It returns an error if the | ||
| // file exists and contains a valid PID of a running process, or when failing | ||
| // to write the file. | ||
| func Write(path string, pid int) error { | ||
| if pid < 1 { | ||
| return fmt.Errorf("invalid PID (%d): only positive PIDs are allowed", pid) | ||
| } | ||
| oldPID, err := Read(path) | ||
| if err != nil && !os.IsNotExist(err) { | ||
| return err | ||
| } | ||
| if oldPID != 0 { | ||
| return fmt.Errorf("process with PID %d is still running", oldPID) | ||
| } | ||
| return os.WriteFile(path, []byte(strconv.Itoa(pid)), 0o644) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //go:build !windows | ||
|
|
||
| /* | ||
| Copyright 2025 Docker Compose CLI authors | ||
|
|
||
| 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 pidfile | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
| "runtime" | ||
| "strconv" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| func alive(pid int) bool { | ||
| if pid < 1 { | ||
| return false | ||
| } | ||
| switch runtime.GOOS { | ||
| case "darwin": | ||
| err := unix.Kill(pid, 0) | ||
| return err == nil || errors.Is(err, unix.EPERM) | ||
| default: | ||
| _, err := os.Stat("/proc/" + strconv.Itoa(pid)) | ||
| return err == nil | ||
| } | ||
|
Comment on lines
+34
to
+41
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,38 @@ | ||||||||||||||
| //go:build windows | ||||||||||||||
|
|
||||||||||||||
| /* | ||||||||||||||
| Copyright 2025 Docker Compose CLI authors | ||||||||||||||
| 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 pidfile | ||||||||||||||
|
|
||||||||||||||
| import "golang.org/x/sys/windows" | ||||||||||||||
|
|
||||||||||||||
| func alive(pid int) bool { | ||||||||||||||
| if pid < 1 { | ||||||||||||||
| return false | ||||||||||||||
| } | ||||||||||||||
| h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pid)) | ||||||||||||||
| if err != nil { | ||||||||||||||
| return false | ||||||||||||||
| } | ||||||||||||||
| var c uint32 | ||||||||||||||
| err = windows.GetExitCodeProcess(h, &c) | ||||||||||||||
| _ = windows.CloseHandle(h) | ||||||||||||||
| if err != nil { | ||||||||||||||
| return c == uint32(windows.STATUS_PENDING) | ||||||||||||||
| } | ||||||||||||||
| return true | ||||||||||||||
|
Comment on lines
+35
to
+37
|
||||||||||||||
| return c == uint32(windows.STATUS_PENDING) | |
| } | |
| return true | |
| return false | |
| } | |
| return c == windows.STILL_ACTIVE |
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.
Let's leave that for the module we're moving; ISTR there were some reasons to do this, but would have to check history 😂
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.
Package comment says it provides helper functions to “create and remove PID files”, but this package currently only implements Read and Write (no remove/delete helper). Consider either adding a Remove helper (if intended) or adjusting the package comment to match the actual API surface.