Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package exec |
| 2 | |
| 3 | import ( |
Bogdan Caprita | 66ca353 | 2015-02-05 21:08:10 -0800 | [diff] [blame] | 4 | "bytes" |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 5 | "encoding/binary" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 6 | "errors" |
Cosmos Nicolaou | 1c18c1c | 2014-10-08 16:37:10 -0700 | [diff] [blame] | 7 | "fmt" |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 8 | "io" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 9 | "os" |
| 10 | "os/exec" |
Robert Kroeger | b5d6bda | 2015-01-30 16:10:49 -0800 | [diff] [blame] | 11 | "strconv" |
Cosmos Nicolaou | 1c18c1c | 2014-10-08 16:37:10 -0700 | [diff] [blame] | 12 | "strings" |
Bogdan Caprita | 650b162 | 2014-11-21 15:11:05 -0800 | [diff] [blame] | 13 | "sync" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 14 | "syscall" |
| 15 | "time" |
Cosmos Nicolaou | bfcac5f | 2014-05-22 21:57:35 -0700 | [diff] [blame] | 16 | |
Jiri Simsa | 337af23 | 2015-02-27 14:36:46 -0800 | [diff] [blame] | 17 | "v.io/x/lib/vlog" |
Cosmos Nicolaou | 251a4d8 | 2014-09-30 22:28:45 -0700 | [diff] [blame] | 18 | |
Jiri Simsa | ffceefa | 2015-02-28 11:03:34 -0800 | [diff] [blame^] | 19 | "v.io/x/ref/lib/exec/consts" |
| 20 | "v.io/x/ref/lib/timekeeper" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | var ( |
Cosmos Nicolaou | 1c18c1c | 2014-10-08 16:37:10 -0700 | [diff] [blame] | 24 | ErrAuthTimeout = errors.New("timeout in auth handshake") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 25 | ErrTimeout = errors.New("timeout waiting for child") |
| 26 | ErrSecretTooLarge = errors.New("secret is too large") |
| 27 | ) |
| 28 | |
| 29 | // A ParentHandle is the Parent process' means of managing a single child. |
| 30 | type ParentHandle struct { |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 31 | c *exec.Cmd |
Cosmos Nicolaou | 486d349 | 2014-09-30 22:21:20 -0700 | [diff] [blame] | 32 | config Config |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 33 | secret string |
| 34 | statusRead *os.File |
| 35 | statusWrite *os.File |
| 36 | tk timekeeper.TimeKeeper |
Bogdan Caprita | 650b162 | 2014-11-21 15:11:05 -0800 | [diff] [blame] | 37 | waitDone bool |
| 38 | waitErr error |
| 39 | waitLock sync.Mutex |
Robert Kroeger | b5d6bda | 2015-01-30 16:10:49 -0800 | [diff] [blame] | 40 | callbackPid int |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // ParentHandleOpt is an option for NewParentHandle. |
| 44 | type ParentHandleOpt interface { |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 45 | // ExecParentHandleOpt is a signature 'dummy' method for the |
| 46 | // interface. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 47 | ExecParentHandleOpt() |
| 48 | } |
| 49 | |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 50 | // ConfigOpt can be used to seed the parent handle with a |
| 51 | // config to be passed to the child. |
| 52 | type ConfigOpt struct { |
Cosmos Nicolaou | 486d349 | 2014-09-30 22:21:20 -0700 | [diff] [blame] | 53 | Config |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 54 | } |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 55 | |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 56 | // ExecParentHandleOpt makes ConfigOpt an instance of |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 57 | // ParentHandleOpt. |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 58 | func (ConfigOpt) ExecParentHandleOpt() {} |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 59 | |
| 60 | // SecretOpt can be used to seed the parent handle with a custom secret. |
| 61 | type SecretOpt string |
| 62 | |
| 63 | // ExecParentHandleOpt makes SecretOpt an instance of ParentHandleOpt. |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 64 | func (SecretOpt) ExecParentHandleOpt() {} |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 65 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 66 | // TimeKeeperOpt can be used to seed the parent handle with a custom timekeeper. |
| 67 | type TimeKeeperOpt struct { |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 68 | timekeeper.TimeKeeper |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // ExecParentHandleOpt makes TimeKeeperOpt an instance of ParentHandleOpt. |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 72 | func (TimeKeeperOpt) ExecParentHandleOpt() {} |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 73 | |
| 74 | // NewParentHandle creates a ParentHandle for the child process represented by |
| 75 | // an instance of exec.Cmd. |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 76 | func NewParentHandle(c *exec.Cmd, opts ...ParentHandleOpt) *ParentHandle { |
Cosmos Nicolaou | 486d349 | 2014-09-30 22:21:20 -0700 | [diff] [blame] | 77 | cfg, secret := NewConfig(), "" |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 78 | tk := timekeeper.RealTime() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 79 | for _, opt := range opts { |
| 80 | switch v := opt.(type) { |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 81 | case ConfigOpt: |
| 82 | cfg = v |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 83 | case SecretOpt: |
| 84 | secret = string(v) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 85 | case TimeKeeperOpt: |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 86 | tk = v |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 87 | default: |
| 88 | vlog.Errorf("Unrecognized parent option: %v", v) |
| 89 | } |
| 90 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 91 | return &ParentHandle{ |
Jiri Simsa | 24e87aa | 2014-06-09 09:27:34 -0700 | [diff] [blame] | 92 | c: c, |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 93 | config: cfg, |
Jiri Simsa | 24e87aa | 2014-06-09 09:27:34 -0700 | [diff] [blame] | 94 | secret: secret, |
| 95 | tk: tk, |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | // Start starts the child process, sharing a secret with it and |
| 100 | // setting up a communication channel over which to read its status. |
| 101 | func (p *ParentHandle) Start() error { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 102 | // Make sure that there are no instances of the consts.ExecVersionVariable |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 103 | // already in the environment (which can happen when a subprocess |
| 104 | // creates a subprocess etc) |
| 105 | nenv := make([]string, 0, len(p.c.Env)+1) |
| 106 | for _, e := range p.c.Env { |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 107 | if strings.HasPrefix(e, consts.ExecVersionVariable+"=") { |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 108 | continue |
| 109 | } |
| 110 | nenv = append(nenv, e) |
| 111 | } |
Cosmos Nicolaou | a6fef89 | 2015-02-20 23:09:03 -0800 | [diff] [blame] | 112 | p.c.Env = append(nenv, consts.ExecVersionVariable+"="+version1) |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 113 | |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 114 | // Create anonymous pipe for communicating data between the child |
| 115 | // and the parent. |
Bogdan Caprita | 7f49167 | 2014-11-13 14:51:08 -0800 | [diff] [blame] | 116 | // TODO(caprita): As per ribrdb@, Go's exec does not prune the set |
| 117 | // of file descriptors passed down to the child process, and hence |
| 118 | // a child may get access to the files meant for another child. |
| 119 | // Do we need to ensure only one thread is allowed to create these |
| 120 | // pipes at any time? |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 121 | dataRead, dataWrite, err := os.Pipe() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 122 | if err != nil { |
| 123 | return err |
| 124 | } |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 125 | defer dataRead.Close() |
| 126 | defer dataWrite.Close() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 127 | statusRead, statusWrite, err := os.Pipe() |
| 128 | if err != nil { |
| 129 | return err |
| 130 | } |
| 131 | p.statusRead = statusRead |
| 132 | p.statusWrite = statusWrite |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 133 | // Add the parent-child pipes to cmd.ExtraFiles, offsetting all |
| 134 | // existing file descriptors accordingly. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 135 | extraFiles := make([]*os.File, len(p.c.ExtraFiles)+2) |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 136 | extraFiles[0] = dataRead |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 137 | extraFiles[1] = statusWrite |
| 138 | for i, _ := range p.c.ExtraFiles { |
| 139 | extraFiles[i+2] = p.c.ExtraFiles[i] |
| 140 | } |
| 141 | p.c.ExtraFiles = extraFiles |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 142 | // Start the child process. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 143 | if err := p.c.Start(); err != nil { |
| 144 | p.statusWrite.Close() |
| 145 | p.statusRead.Close() |
| 146 | return err |
| 147 | } |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 148 | // Pass data to the child using a pipe. |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 149 | serializedConfig, err := p.config.Serialize() |
| 150 | if err != nil { |
| 151 | return err |
| 152 | } |
| 153 | if err := encodeString(dataWrite, serializedConfig); err != nil { |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 154 | p.statusWrite.Close() |
| 155 | p.statusRead.Close() |
| 156 | return err |
| 157 | } |
Jiri Simsa | 84059da | 2014-06-02 17:22:05 -0700 | [diff] [blame] | 158 | if err := encodeString(dataWrite, p.secret); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 159 | p.statusWrite.Close() |
| 160 | p.statusRead.Close() |
| 161 | return err |
| 162 | } |
| 163 | return nil |
| 164 | } |
| 165 | |
Bogdan Caprita | 66ca353 | 2015-02-05 21:08:10 -0800 | [diff] [blame] | 166 | // copy is like io.Copy, but it also treats the receipt of the special eofChar |
| 167 | // byte to mean io.EOF. |
| 168 | func copy(w io.Writer, r io.Reader) (err error) { |
| 169 | buf := make([]byte, 1024) |
| 170 | for { |
| 171 | nRead, errRead := r.Read(buf) |
| 172 | if nRead > 0 { |
| 173 | if eofCharIndex := bytes.IndexByte(buf[:nRead], eofChar); eofCharIndex != -1 { |
| 174 | nRead = eofCharIndex |
| 175 | errRead = io.EOF |
| 176 | } |
| 177 | nWrite, errWrite := w.Write(buf[:nRead]) |
| 178 | if errWrite != nil { |
| 179 | err = errWrite |
| 180 | break |
| 181 | } |
| 182 | if nRead != nWrite { |
| 183 | err = io.ErrShortWrite |
| 184 | break |
| 185 | } |
| 186 | } |
| 187 | if errRead == io.EOF { |
| 188 | break |
| 189 | } |
| 190 | if errRead != nil { |
| 191 | err = errRead |
| 192 | break |
| 193 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 194 | } |
Bogdan Caprita | 66ca353 | 2015-02-05 21:08:10 -0800 | [diff] [blame] | 195 | return |
| 196 | } |
| 197 | |
| 198 | func waitForStatus(c chan interface{}, r *os.File) { |
| 199 | var readBytes bytes.Buffer |
| 200 | err := copy(&readBytes, r) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 201 | r.Close() |
Bogdan Caprita | 66ca353 | 2015-02-05 21:08:10 -0800 | [diff] [blame] | 202 | if err != nil { |
| 203 | c <- err |
| 204 | } else { |
| 205 | c <- readBytes.String() |
| 206 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 207 | close(c) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // WaitForReady will wait for the child process to become ready. |
| 211 | func (p *ParentHandle) WaitForReady(timeout time.Duration) error { |
Bogdan Caprita | 66ca353 | 2015-02-05 21:08:10 -0800 | [diff] [blame] | 212 | // An invariant of WaitForReady is that both statusWrite and statusRead |
| 213 | // get closed before WaitForStatus returns (statusRead gets closed by |
| 214 | // waitForStatus). |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 215 | defer p.statusWrite.Close() |
Bogdan Caprita | 66ca353 | 2015-02-05 21:08:10 -0800 | [diff] [blame] | 216 | c := make(chan interface{}, 1) |
| 217 | go waitForStatus(c, p.statusRead) |
| 218 | // TODO(caprita): This can be simplified further by doing the reading |
| 219 | // from the status pipe here, and instead moving the timeout listener to |
| 220 | // a separate goroutine. |
| 221 | select { |
| 222 | case msg := <-c: |
| 223 | switch m := msg.(type) { |
| 224 | case error: |
| 225 | return m |
| 226 | case string: |
| 227 | if strings.HasPrefix(m, readyStatus) { |
| 228 | pid, err := strconv.Atoi(m[len(readyStatus):]) |
Robert Kroeger | b5d6bda | 2015-01-30 16:10:49 -0800 | [diff] [blame] | 229 | if err != nil { |
| 230 | return err |
| 231 | } |
| 232 | p.callbackPid = pid |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 233 | return nil |
| 234 | } |
Bogdan Caprita | 66ca353 | 2015-02-05 21:08:10 -0800 | [diff] [blame] | 235 | if strings.HasPrefix(m, failedStatus) { |
| 236 | return fmt.Errorf("%s", strings.TrimPrefix(m, failedStatus)) |
Cosmos Nicolaou | 1c18c1c | 2014-10-08 16:37:10 -0700 | [diff] [blame] | 237 | } |
Bogdan Caprita | 66ca353 | 2015-02-05 21:08:10 -0800 | [diff] [blame] | 238 | return fmt.Errorf("unrecognised status from subprocess: %q", m) |
| 239 | default: |
| 240 | return fmt.Errorf("unexpected type %T", m) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 241 | } |
Bogdan Caprita | 66ca353 | 2015-02-05 21:08:10 -0800 | [diff] [blame] | 242 | case <-p.tk.After(timeout): |
| 243 | vlog.Errorf("Timed out waiting for child status") |
| 244 | // By writing the special eofChar byte to the pipe, we ensure |
| 245 | // that waitForStatus returns: the copy function treats eofChar |
| 246 | // to indicate end of read input. Note, copy could have |
| 247 | // finished for other reasons already (receipt of eofChar from |
| 248 | // the child process). Note, closing the pipe from the child |
| 249 | // (explicitly or due to crash) would NOT cause copy to read |
| 250 | // io.EOF, since we keep the statusWrite open in the parent. |
| 251 | // Hence, a child crash will eventually trigger this timeout. |
| 252 | p.statusWrite.Write([]byte{eofChar}) |
| 253 | // Before returning, waitForStatus will close r, and then close |
| 254 | // c. Waiting on c ensures that r.Close() in waitForStatus |
| 255 | // already executed. |
| 256 | <-c |
| 257 | return ErrTimeout |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 258 | } |
| 259 | panic("unreachable") |
| 260 | } |
| 261 | |
Bogdan Caprita | 650b162 | 2014-11-21 15:11:05 -0800 | [diff] [blame] | 262 | // wait performs the Wait on the underlying command under lock, and only once |
| 263 | // (subsequent wait calls block until the Wait is finished). It's ok to call |
| 264 | // wait multiple times, and in parallel. The error from the initial Wait is |
| 265 | // cached and returned for all subsequent calls. |
| 266 | func (p *ParentHandle) wait() error { |
| 267 | p.waitLock.Lock() |
| 268 | defer p.waitLock.Unlock() |
| 269 | if p.waitDone { |
| 270 | return p.waitErr |
| 271 | } |
| 272 | p.waitErr = p.c.Wait() |
| 273 | p.waitDone = true |
| 274 | return p.waitErr |
| 275 | } |
| 276 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 277 | // Wait will wait for the child process to terminate of its own accord. |
| 278 | // It returns nil if the process exited cleanly with an exit status of 0, |
| 279 | // any other exit code or error will result in an appropriate error return |
| 280 | func (p *ParentHandle) Wait(timeout time.Duration) error { |
| 281 | c := make(chan error, 1) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 282 | go func() { |
Bogdan Caprita | 650b162 | 2014-11-21 15:11:05 -0800 | [diff] [blame] | 283 | c <- p.wait() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 284 | close(c) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 285 | }() |
| 286 | // If timeout is zero time.After will panic; we handle zero specially |
| 287 | // to mean infinite timeout. |
| 288 | if timeout > 0 { |
| 289 | select { |
| 290 | case <-p.tk.After(timeout): |
| 291 | return ErrTimeout |
| 292 | case err := <-c: |
| 293 | return err |
| 294 | } |
| 295 | } else { |
| 296 | return <-c |
| 297 | } |
| 298 | panic("unreachable") |
| 299 | } |
| 300 | |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 301 | // Pid returns the pid of the child, 0 if the child process doesn't exist |
| 302 | func (p *ParentHandle) Pid() int { |
| 303 | if p.c.Process != nil { |
| 304 | return p.c.Process.Pid |
| 305 | } |
| 306 | return 0 |
| 307 | } |
| 308 | |
Robert Kroeger | b5d6bda | 2015-01-30 16:10:49 -0800 | [diff] [blame] | 309 | // ChildPid returns the pid of a child process as reported by its status |
| 310 | // callback. |
| 311 | func (p *ParentHandle) ChildPid() int { |
| 312 | return p.callbackPid |
| 313 | } |
| 314 | |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 315 | // Exists returns true if the child process exists and can be signal'ed |
| 316 | func (p *ParentHandle) Exists() bool { |
| 317 | if p.c.Process != nil { |
| 318 | return syscall.Kill(p.c.Process.Pid, 0) == nil |
| 319 | } |
| 320 | return false |
| 321 | } |
| 322 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 323 | // Kill kills the child process. |
| 324 | func (p *ParentHandle) Kill() error { |
Robin Thellend | 4490352 | 2015-02-06 12:55:26 -0800 | [diff] [blame] | 325 | if p.c.Process == nil { |
| 326 | return errors.New("no such process") |
| 327 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 328 | return p.c.Process.Kill() |
| 329 | } |
| 330 | |
| 331 | // Signal sends the given signal to the child process. |
| 332 | func (p *ParentHandle) Signal(sig syscall.Signal) error { |
Robin Thellend | 4490352 | 2015-02-06 12:55:26 -0800 | [diff] [blame] | 333 | if p.c.Process == nil { |
| 334 | return errors.New("no such process") |
| 335 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 336 | return syscall.Kill(p.c.Process.Pid, sig) |
| 337 | } |
| 338 | |
| 339 | // Clean will clean up state, including killing the child process. |
| 340 | func (p *ParentHandle) Clean() error { |
| 341 | if err := p.Kill(); err != nil { |
| 342 | return err |
| 343 | } |
Bogdan Caprita | 650b162 | 2014-11-21 15:11:05 -0800 | [diff] [blame] | 344 | return p.wait() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 345 | } |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 346 | |
Jiri Simsa | 84059da | 2014-06-02 17:22:05 -0700 | [diff] [blame] | 347 | func encodeString(w io.Writer, data string) error { |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 348 | l := len(data) |
| 349 | if err := binary.Write(w, binary.BigEndian, int64(l)); err != nil { |
| 350 | return err |
| 351 | } |
| 352 | if n, err := w.Write([]byte(data)); err != nil || n != l { |
| 353 | if err != nil { |
| 354 | return err |
| 355 | } else { |
| 356 | return errors.New("partial write") |
| 357 | } |
| 358 | } |
| 359 | return nil |
| 360 | } |