-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Write pidfile atomically #11983
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
Write pidfile atomically #11983
Changes from all commits
Commits
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
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,73 @@ | ||
// Copyright 2025 The gVisor 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 cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
) | ||
|
||
// WritePidFile writes pid file atomically if possible. | ||
func WritePidFile(path string, pid int) error { | ||
pidStr := []byte(strconv.Itoa(pid)) | ||
|
||
st, err := os.Stat(path) | ||
if err == nil && !st.Mode().IsRegular() { | ||
// If not regular file, write in place. | ||
if err := os.WriteFile(path, pidStr, 0644); err != nil { | ||
return fmt.Errorf("failed to write pid file %s: %w", path, err) | ||
} | ||
return nil | ||
} | ||
if err != nil && !os.IsNotExist(err) { | ||
return fmt.Errorf("stat file %s failed: %w", path, err) | ||
} | ||
|
||
// Otherwise write using temp file to make write atomic. | ||
dir := filepath.Dir(path) | ||
tempFile, err := os.CreateTemp(dir, "pid-tmp-*") | ||
if err != nil { | ||
return fmt.Errorf("failed to create temp pid file in dir %s: %w", dir, err) | ||
} | ||
|
||
tempFileRenamed := false | ||
defer func(tempFile *os.File) { | ||
_ = tempFile.Close() | ||
if !tempFileRenamed { | ||
_ = os.Remove(tempFile.Name()) | ||
} | ||
}(tempFile) | ||
|
||
if err := os.Chmod(tempFile.Name(), 0644); err != nil { | ||
return fmt.Errorf("failed to chmod pid file %s: %w", tempFile.Name(), err) | ||
} | ||
|
||
if _, err := tempFile.Write(pidStr); err != nil { | ||
return fmt.Errorf("failed to write pid file %s: %w", tempFile.Name(), err) | ||
} | ||
|
||
if err := tempFile.Close(); err != nil { | ||
return fmt.Errorf("failed to close temp pid file %s: %w", tempFile.Name(), err) | ||
} | ||
|
||
if err := os.Rename(tempFile.Name(), path); err != nil { | ||
return fmt.Errorf("failed to rename temp pid file %s -> %s: %w", tempFile.Name(), path, err) | ||
} | ||
tempFileRenamed = true | ||
|
||
return nil | ||
} |
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,64 @@ | ||
// Copyright 2025 The gVisor 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 cmd | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func TestWritePidFile(t *testing.T) { | ||
tempDir, err := os.MkdirTemp("", "test-*") | ||
if err != nil { | ||
t.Fatalf("failed to create temp dir: %v", err) | ||
} | ||
defer func() { | ||
_ = os.RemoveAll(tempDir) | ||
}() | ||
|
||
t.Run("Write new file", func(t *testing.T) { | ||
path := filepath.Join(tempDir, "test-new.pid") | ||
if err := WritePidFile(path, 17); err != nil { | ||
t.Fatalf("failed to write pid file: %v", err) | ||
} | ||
|
||
pidStr, err := os.ReadFile(path) | ||
if err != nil { | ||
t.Fatalf("failed to read pid file: %v", err) | ||
} | ||
if string(pidStr) != "17" { | ||
t.Fatalf("pid file did not contain pid '17'") | ||
} | ||
}) | ||
|
||
t.Run("Overwrite existing file", func(t *testing.T) { | ||
path := filepath.Join(tempDir, "test-overwrite.pid") | ||
if err := os.WriteFile(path, []byte("11"), 0600); err != nil { | ||
t.Fatalf("failed to write pid file: %v", err) | ||
} | ||
|
||
if err := WritePidFile(path, 19); err != nil { | ||
t.Fatalf("failed to overwrite write pid file: %v", err) | ||
} | ||
pidStr, err := os.ReadFile(path) | ||
if err != nil { | ||
t.Fatalf("failed to read pid file: %v", err) | ||
} | ||
if string(pidStr) != "19" { | ||
t.Fatalf("pid file did not contain pid '19'") | ||
} | ||
}) | ||
} |
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.