github.com/fafucoder/cilium@v1.6.11/pkg/launcher/launcher.go (about) 1 // Copyright 2017 Authors of Cilium 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 launcher 16 17 import ( 18 "fmt" 19 "io" 20 "os" 21 "os/exec" 22 23 "github.com/cilium/cilium/pkg/lock" 24 "github.com/cilium/cilium/pkg/logging" 25 "github.com/cilium/cilium/pkg/logging/logfields" 26 27 "github.com/sirupsen/logrus" 28 ) 29 30 var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "launcher") 31 32 // Launcher is used to wrap the node executable binary. 33 type Launcher struct { 34 Mutex lock.RWMutex 35 target string 36 args []string 37 process *os.Process 38 stdout io.ReadCloser 39 } 40 41 // Run starts the daemon. 42 func (launcher *Launcher) Run() error { 43 targetName := launcher.GetTarget() 44 cmdStr := fmt.Sprintf("%s %s", targetName, launcher.GetArgs()) 45 cmd := exec.Command(targetName, launcher.GetArgs()...) 46 cmd.Stderr = os.Stderr 47 stdout, _ := cmd.StdoutPipe() 48 if err := cmd.Start(); err != nil { 49 log.WithError(err).WithField("cmd", cmdStr).Error("cmd.Start()") 50 return fmt.Errorf("unable to launch process %s: %s", cmdStr, err) 51 } 52 53 launcher.setProcess(cmd.Process) 54 launcher.setStdout(stdout) 55 56 // Wait for the process to exit in the background to release all 57 // resources 58 go func() { 59 err := cmd.Wait() 60 log.WithFields(logrus.Fields{ 61 "exitCode": err, 62 "cmd": cmdStr, 63 }).Debug("Process exited") 64 }() 65 66 return nil 67 } 68 69 // SetTarget sets the Launcher target. 70 func (launcher *Launcher) SetTarget(target string) { 71 launcher.Mutex.Lock() 72 launcher.target = target 73 launcher.Mutex.Unlock() 74 } 75 76 // GetTarget returns the Launcher target. 77 func (launcher *Launcher) GetTarget() string { 78 launcher.Mutex.RLock() 79 arg := launcher.target 80 launcher.Mutex.RUnlock() 81 return arg 82 } 83 84 // SetArgs sets the Launcher arg. 85 func (launcher *Launcher) SetArgs(args []string) { 86 launcher.Mutex.Lock() 87 launcher.args = args 88 launcher.Mutex.Unlock() 89 } 90 91 // GetArgs returns the Launcher arg. 92 func (launcher *Launcher) GetArgs() []string { 93 launcher.Mutex.RLock() 94 args := launcher.args 95 launcher.Mutex.RUnlock() 96 return args 97 } 98 99 // setProcess sets the internal process with the given process. 100 func (launcher *Launcher) setProcess(proc *os.Process) { 101 launcher.Mutex.Lock() 102 launcher.process = proc 103 launcher.Mutex.Unlock() 104 } 105 106 // GetProcess returns the internal process. 107 func (launcher *Launcher) GetProcess() *os.Process { 108 launcher.Mutex.RLock() 109 proc := launcher.process 110 launcher.Mutex.RUnlock() 111 return proc 112 } 113 114 // setStdout sets the stdout pipe. 115 func (launcher *Launcher) setStdout(stdout io.ReadCloser) { 116 launcher.Mutex.Lock() 117 launcher.stdout = stdout 118 launcher.Mutex.Unlock() 119 } 120 121 // GetStdout gets the stdout pipe. 122 func (launcher *Launcher) GetStdout() io.ReadCloser { 123 launcher.Mutex.RLock() 124 stdout := launcher.stdout 125 launcher.Mutex.RUnlock() 126 return stdout 127 }