github.com/jk-he/cni@v0.8.1/pkg/invoke/raw_exec.go (about) 1 // Copyright 2016 CNI authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package invoke 16 17 import ( 18 "bytes" 19 "context" 20 "encoding/json" 21 "fmt" 22 "io" 23 "os/exec" 24 "strings" 25 "time" 26 27 "github.com/containernetworking/cni/pkg/types" 28 ) 29 30 type RawExec struct { 31 Stderr io.Writer 32 } 33 34 func (e *RawExec) ExecPlugin(ctx context.Context, pluginPath string, stdinData []byte, environ []string) ([]byte, error) { 35 stdout := &bytes.Buffer{} 36 stderr := &bytes.Buffer{} 37 c := exec.CommandContext(ctx, pluginPath) 38 c.Env = environ 39 c.Stdin = bytes.NewBuffer(stdinData) 40 c.Stdout = stdout 41 c.Stderr = stderr 42 43 // Retry the command on "text file busy" errors 44 for i := 0; i <= 5; i++ { 45 err := c.Run() 46 47 // Command succeeded 48 if err == nil { 49 break 50 } 51 52 // If the plugin is currently about to be written, then we wait a 53 // second and try it again 54 if strings.Contains(err.Error(), "text file busy") { 55 time.Sleep(time.Second) 56 continue 57 } 58 59 // All other errors except than the busy text file 60 return nil, e.pluginErr(err, stdout.Bytes(), stderr.Bytes()) 61 } 62 63 // Copy stderr to caller's buffer in case plugin printed to both 64 // stdout and stderr for some reason. Ignore failures as stderr is 65 // only informational. 66 if e.Stderr != nil && stderr.Len() > 0 { 67 _, _ = stderr.WriteTo(e.Stderr) 68 } 69 return stdout.Bytes(), nil 70 } 71 72 func (e *RawExec) pluginErr(err error, stdout, stderr []byte) error { 73 emsg := types.Error{} 74 if len(stdout) == 0 { 75 if len(stderr) == 0 { 76 emsg.Msg = fmt.Sprintf("netplugin failed with no error message: %v", err) 77 } else { 78 emsg.Msg = fmt.Sprintf("netplugin failed: %q", string(stderr)) 79 } 80 } else if perr := json.Unmarshal(stdout, &emsg); perr != nil { 81 emsg.Msg = fmt.Sprintf("netplugin failed but error parsing its diagnostic message %q: %v", string(stdout), perr) 82 } 83 return &emsg 84 } 85 86 func (e *RawExec) FindInPath(plugin string, paths []string) (string, error) { 87 return FindInPath(plugin, paths) 88 }