github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/runtime/v2/shim/util_windows.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package shim 18 19 import ( 20 "context" 21 "net" 22 "os" 23 "syscall" 24 "time" 25 26 winio "github.com/Microsoft/go-winio" 27 "github.com/pkg/errors" 28 ) 29 30 const shimBinaryFormat = "containerd-shim-%s-%s.exe" 31 32 func getSysProcAttr() *syscall.SysProcAttr { 33 return nil 34 } 35 36 // AnonReconnectDialer returns a dialer for an existing npipe on containerd reconnection 37 func AnonReconnectDialer(address string, timeout time.Duration) (net.Conn, error) { 38 ctx, cancel := context.WithTimeout(context.Background(), timeout) 39 defer cancel() 40 41 c, err := winio.DialPipeContext(ctx, address) 42 if os.IsNotExist(err) { 43 return nil, errors.Wrap(os.ErrNotExist, "npipe not found on reconnect") 44 } else if err == context.DeadlineExceeded { 45 return nil, errors.Wrapf(err, "timed out waiting for npipe %s", address) 46 } else if err != nil { 47 return nil, err 48 } 49 return c, nil 50 } 51 52 // AnonDialer returns a dialer for a npipe 53 func AnonDialer(address string, timeout time.Duration) (net.Conn, error) { 54 ctx, cancel := context.WithTimeout(context.Background(), timeout) 55 defer cancel() 56 57 // If there is nobody serving the pipe we limit the timeout for this case to 58 // 5 seconds because any shim that would serve this endpoint should serve it 59 // within 5 seconds. 60 serveTimer := time.NewTimer(5 * time.Second) 61 defer serveTimer.Stop() 62 for { 63 c, err := winio.DialPipeContext(ctx, address) 64 if err != nil { 65 if os.IsNotExist(err) { 66 select { 67 case <-serveTimer.C: 68 return nil, errors.Wrap(os.ErrNotExist, "pipe not found before timeout") 69 default: 70 // Wait 10ms for the shim to serve and try again. 71 time.Sleep(10 * time.Millisecond) 72 continue 73 } 74 } else if err == context.DeadlineExceeded { 75 return nil, errors.Wrapf(err, "timed out waiting for npipe %s", address) 76 } 77 return nil, err 78 } 79 return c, nil 80 } 81 }