-
Notifications
You must be signed in to change notification settings - Fork 330
activation: allow more control over socket-activated file descriptors #441
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
Open
percivalalb
wants to merge
9
commits into
coreos:main
Choose a base branch
from
percivalalb:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+266
−26
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
420a920
Allow more control over file descriptors
percivalalb 1a14aa1
Set unsetEnv default to true to match previous behaviour
percivalalb a7b9d74
Use custom error struct instead of fmt wrapped error
percivalalb 9697a28
Return fd close error & close dev null file on error
percivalalb 9e79ff5
Add docs to options.go file
percivalalb 880388f
Restore old function signatures, add WithOptions variants
601d967
Make error type private, fix fd idx not being in error message
7534ea3
Add copyright notice
49548cb
Remove redundant build line
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright 2015 CoreOS, Inc. | ||
| // | ||
| // 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 activation | ||
|
|
||
| // Method decides what happens to the file descriptors that are passed in by systemd. | ||
| type Method int | ||
|
|
||
| const ( | ||
| // ConsumeFiles is the default, and removes the original file descriptors passed in by | ||
| // systemd. This means that new file descriptors created by the program may use the | ||
| // file descriptors indices. | ||
| ConsumeFiles Method = iota | ||
|
|
||
| // ReserveFiles stores placeholder file descriptors, which point to /dev/null. This | ||
| // stops new file descriptors consuming the indices. | ||
| ReserveFiles | ||
|
|
||
| // CloneFiles duplicates and leaves the original file descriptors in tack. This | ||
| // stops new file descriptors consuming the indices like [ReserveFiles] but | ||
| // consider the possible secuirty risk of leaving the sockets exposed. | ||
| ConserveFiles | ||
| ) |
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,68 @@ | ||
| // Copyright 2015 CoreOS, Inc. | ||
| // | ||
| // 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. | ||
|
|
||
| //go:build !windows | ||
|
|
||
| package activation | ||
|
|
||
| import ( | ||
| "os" | ||
| "strconv" | ||
| "syscall" | ||
| ) | ||
|
|
||
| func (m Method) Apply(f *os.File) error { | ||
| saveFd := int(f.Fd()) // get the idx before being closed. | ||
|
|
||
| switch m { | ||
| case ConsumeFiles: | ||
| return f.Close() | ||
| case ReserveFiles: | ||
| devNull, err := os.OpenFile(os.DevNull, os.O_RDWR, 0o666) | ||
| if err != nil { | ||
| return errorDevNullSetup{err: err, fd: saveFd} | ||
| } | ||
|
|
||
| nullFd := int(devNull.Fd()) | ||
|
|
||
| // "If oldfd equals newfd, then dup3() fails with the error EINVAL." | ||
| if saveFd == nullFd { | ||
| syscall.CloseOnExec(nullFd) | ||
| } else { | ||
| // "makes newfd be the copy of oldfd, closing newfd first if necessary" | ||
| if err := syscall.Dup3(nullFd, saveFd, syscall.O_CLOEXEC); err != nil { | ||
| devNull.Close() // on an error tidy up. | ||
|
|
||
| return errorDevNullSetup{err: err, fd: saveFd} | ||
| } | ||
| } | ||
| case ConserveFiles: | ||
| // no action | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| type errorDevNullSetup struct { | ||
| fd int | ||
| err error | ||
| } | ||
|
|
||
| func (e errorDevNullSetup) Error() string { | ||
| return "setting up " + strconv.Itoa(e.fd) + " fd to /dev/null: " + e.err.Error() | ||
| } | ||
|
|
||
| func (e errorDevNullSetup) Unwrap() error { | ||
| return e.err | ||
| } |
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,21 @@ | ||
| // Copyright 2015 CoreOS, Inc. | ||
| // | ||
| // 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 activation | ||
|
|
||
| import "os" | ||
|
|
||
| func (m Method) Apply(f *os.File) error { | ||
| 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
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,48 @@ | ||
| // Copyright 2015 CoreOS, Inc. | ||
| // | ||
| // 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 activation | ||
|
|
||
| type options struct { | ||
| unsetEnv bool | ||
| method Method | ||
| } | ||
|
|
||
| // apply applies option functions to the options struct. | ||
| func (o *options) apply(opts ...option) { | ||
| for _, opt := range opts { | ||
| opt(o) | ||
| } | ||
| } | ||
|
|
||
| type option func(*options) | ||
|
|
||
| // UnsetEnv controls if the LISTEN_PID, LISTEN_FDS & LISTEN_FDNAMES environment | ||
| // variables are unset. | ||
| // | ||
| // This is useful to avoid clashes in fd usage and to avoid leaking environment | ||
| // flags to child processes. | ||
| func UnsetEnv(f bool) option { | ||
| return func(o *options) { | ||
| o.unsetEnv = f | ||
| } | ||
| } | ||
|
|
||
| // UseMethod chooses the [Method] applied to the file descriptor passed in by | ||
| // systemd socket activation. | ||
| func UseMethod(m Method) option { | ||
| return func(o *options) { | ||
| o.method = m | ||
| } | ||
| } |
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
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.
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.
Any thoughts on how to handle the error from this? Previously this was a
f.Close()and the error was ignored (would linux ever return an error anyway?). Now a few more things could happen - e.g. setting up the/dev/nullstuff.