- containerd/fifo#17 Expose underlying file's `SyscallConn` method
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
| ... | ... |
@@ -120,7 +120,7 @@ google.golang.org/genproto 694d95ba50e67b2e363f3483057d |
| 120 | 120 |
|
| 121 | 121 |
# containerd |
| 122 | 122 |
github.com/containerd/containerd ceba56893a76f22cf0126c46d835c80fb3833408 |
| 123 |
-github.com/containerd/fifo 3d5202aec260678c48179c56f40e6f38a095738c |
|
| 123 |
+github.com/containerd/fifo a9fb20d87448d386e6d50b1f2e1fa70dcf0de43c |
|
| 124 | 124 |
github.com/containerd/continuity 004b46473808b3e7a4a3049c20e4376c91eb966d |
| 125 | 125 |
github.com/containerd/cgroups 4994991857f9b0ae8dc439551e8bebdbb4bf66c1 |
| 126 | 126 |
github.com/containerd/console c12b1e7919c14469339a5d38f2f8ed9b64a9de23 |
| 127 | 127 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,116 @@ |
| 0 |
+// +build go1.12 |
|
| 1 |
+ |
|
| 2 |
+/* |
|
| 3 |
+ Copyright The containerd Authors. |
|
| 4 |
+ |
|
| 5 |
+ Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 6 |
+ you may not use this file except in compliance with the License. |
|
| 7 |
+ You may obtain a copy of the License at |
|
| 8 |
+ |
|
| 9 |
+ http://www.apache.org/licenses/LICENSE-2.0 |
|
| 10 |
+ |
|
| 11 |
+ Unless required by applicable law or agreed to in writing, software |
|
| 12 |
+ distributed under the License is distributed on an "AS IS" BASIS, |
|
| 13 |
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 14 |
+ See the License for the specific language governing permissions and |
|
| 15 |
+ limitations under the License. |
|
| 16 |
+*/ |
|
| 17 |
+ |
|
| 18 |
+package fifo |
|
| 19 |
+ |
|
| 20 |
+import ( |
|
| 21 |
+ "syscall" |
|
| 22 |
+ |
|
| 23 |
+ "github.com/pkg/errors" |
|
| 24 |
+) |
|
| 25 |
+ |
|
| 26 |
+// SyscallConn provides raw access to the fifo's underlying filedescrptor. |
|
| 27 |
+// See syscall.Conn for guarentees provided by this interface. |
|
| 28 |
+func (f *fifo) SyscallConn() (syscall.RawConn, error) {
|
|
| 29 |
+ // deterministic check for closed |
|
| 30 |
+ select {
|
|
| 31 |
+ case <-f.closed: |
|
| 32 |
+ return nil, errors.New("fifo closed")
|
|
| 33 |
+ default: |
|
| 34 |
+ } |
|
| 35 |
+ |
|
| 36 |
+ select {
|
|
| 37 |
+ case <-f.closed: |
|
| 38 |
+ return nil, errors.New("fifo closed")
|
|
| 39 |
+ case <-f.opened: |
|
| 40 |
+ return f.file.SyscallConn() |
|
| 41 |
+ default: |
|
| 42 |
+ } |
|
| 43 |
+ |
|
| 44 |
+ // Not opened and not closed, this means open is non-blocking AND it's not open yet |
|
| 45 |
+ // Use rawConn to deal with non-blocking open. |
|
| 46 |
+ rc := &rawConn{f: f, ready: make(chan struct{})}
|
|
| 47 |
+ go func() {
|
|
| 48 |
+ select {
|
|
| 49 |
+ case <-f.closed: |
|
| 50 |
+ return |
|
| 51 |
+ case <-f.opened: |
|
| 52 |
+ rc.raw, rc.err = f.file.SyscallConn() |
|
| 53 |
+ close(rc.ready) |
|
| 54 |
+ } |
|
| 55 |
+ }() |
|
| 56 |
+ |
|
| 57 |
+ return rc, nil |
|
| 58 |
+} |
|
| 59 |
+ |
|
| 60 |
+type rawConn struct {
|
|
| 61 |
+ f *fifo |
|
| 62 |
+ ready chan struct{}
|
|
| 63 |
+ raw syscall.RawConn |
|
| 64 |
+ err error |
|
| 65 |
+} |
|
| 66 |
+ |
|
| 67 |
+func (r *rawConn) Control(f func(fd uintptr)) error {
|
|
| 68 |
+ select {
|
|
| 69 |
+ case <-r.f.closed: |
|
| 70 |
+ return errors.New("control of closed fifo")
|
|
| 71 |
+ case <-r.ready: |
|
| 72 |
+ } |
|
| 73 |
+ |
|
| 74 |
+ if r.err != nil {
|
|
| 75 |
+ return r.err |
|
| 76 |
+ } |
|
| 77 |
+ |
|
| 78 |
+ return r.raw.Control(f) |
|
| 79 |
+} |
|
| 80 |
+ |
|
| 81 |
+func (r *rawConn) Read(f func(fd uintptr) (done bool)) error {
|
|
| 82 |
+ if r.f.flag&syscall.O_WRONLY > 0 {
|
|
| 83 |
+ return errors.New("reading from write-only fifo")
|
|
| 84 |
+ } |
|
| 85 |
+ |
|
| 86 |
+ select {
|
|
| 87 |
+ case <-r.f.closed: |
|
| 88 |
+ return errors.New("reading of a closed fifo")
|
|
| 89 |
+ case <-r.ready: |
|
| 90 |
+ } |
|
| 91 |
+ |
|
| 92 |
+ if r.err != nil {
|
|
| 93 |
+ return r.err |
|
| 94 |
+ } |
|
| 95 |
+ |
|
| 96 |
+ return r.raw.Read(f) |
|
| 97 |
+} |
|
| 98 |
+ |
|
| 99 |
+func (r *rawConn) Write(f func(fd uintptr) (done bool)) error {
|
|
| 100 |
+ if r.f.flag&(syscall.O_WRONLY|syscall.O_RDWR) == 0 {
|
|
| 101 |
+ return errors.New("writing to read-only fifo")
|
|
| 102 |
+ } |
|
| 103 |
+ |
|
| 104 |
+ select {
|
|
| 105 |
+ case <-r.f.closed: |
|
| 106 |
+ return errors.New("writing to a closed fifo")
|
|
| 107 |
+ case <-r.ready: |
|
| 108 |
+ } |
|
| 109 |
+ |
|
| 110 |
+ if r.err != nil {
|
|
| 111 |
+ return r.err |
|
| 112 |
+ } |
|
| 113 |
+ |
|
| 114 |
+ return r.raw.Write(f) |
|
| 115 |
+} |
| ... | ... |
@@ -1,6 +1,7 @@ |
| 1 | 1 |
### fifo |
| 2 | 2 |
|
| 3 | 3 |
[](https://travis-ci.org/containerd/fifo) |
| 4 |
+[](https://codecov.io/gh/containerd/fifo) |
|
| 4 | 5 |
|
| 5 | 6 |
Go package for handling fifos in a sane way. |
| 6 | 7 |
|
| ... | ... |
@@ -30,3 +31,14 @@ func (f *fifo) Write(b []byte) (int, error) |
| 30 | 30 |
// before open(2) has returned and fifo was never opened. |
| 31 | 31 |
func (f *fifo) Close() error |
| 32 | 32 |
``` |
| 33 |
+ |
|
| 34 |
+## Project details |
|
| 35 |
+ |
|
| 36 |
+The fifo is a containerd sub-project, licensed under the [Apache 2.0 license](./LICENSE). |
|
| 37 |
+As a containerd sub-project, you will find the: |
|
| 38 |
+ |
|
| 39 |
+ * [Project governance](https://github.com/containerd/project/blob/master/GOVERNANCE.md), |
|
| 40 |
+ * [Maintainers](https://github.com/containerd/project/blob/master/MAINTAINERS), |
|
| 41 |
+ * and [Contributing guidelines](https://github.com/containerd/project/blob/master/CONTRIBUTING.md) |
|
| 42 |
+ |
|
| 43 |
+information in our [`containerd/project`](https://github.com/containerd/project) repository. |