-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow more control over file descriptors
- Loading branch information
1 parent
7d375ec
commit cbfc7a9
Showing
6 changed files
with
166 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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,54 @@ | ||
// 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 | ||
// +build !windows | ||
|
||
package activation | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
func (m Method) Apply(f *os.File) error { | ||
saveFd := int(f.Fd()) // get the idx before being closed. | ||
|
||
switch m { | ||
case ConsumeFiles: | ||
f.Close() | ||
case ReserveFiles: | ||
devNull, err := os.OpenFile(os.DevNull, os.O_RDWR, 0755) | ||
if err != nil { | ||
return fmt.Errorf("accessing /dev/null: %w", err) | ||
} | ||
|
||
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 { | ||
return fmt.Errorf("setting %d fd to /dev/null: %w", saveFd, err) | ||
} | ||
} | ||
case ConserveFiles: | ||
// no action | ||
} | ||
|
||
return nil | ||
} |
This file contains 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 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 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,30 @@ | ||
package activation | ||
|
||
type options struct { | ||
unsetEnv bool | ||
method Method | ||
} | ||
|
||
type option func(*options) | ||
|
||
func UnsetEnv(f bool) option { | ||
return func(o *options) { | ||
o.unsetEnv = f | ||
} | ||
} | ||
|
||
func UseMethod(m Method) option { | ||
return func(o *options) { | ||
o.method = m | ||
} | ||
} | ||
|
||
func Options(opts ...option) *options { | ||
o := &options{} | ||
|
||
for _, opt := range opts { | ||
opt(o) | ||
} | ||
|
||
return o | ||
} |
This file contains 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