Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package exec |
| 2 | |
| 3 | import ( |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 4 | "encoding/binary" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 5 | "errors" |
Cosmos Nicolaou | 1c18c1c | 2014-10-08 16:37:10 -0700 | [diff] [blame] | 6 | "fmt" |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 7 | "io" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 8 | "os" |
| 9 | "os/exec" |
Cosmos Nicolaou | 1c18c1c | 2014-10-08 16:37:10 -0700 | [diff] [blame] | 10 | "strings" |
Bogdan Caprita | 650b162 | 2014-11-21 15:11:05 -0800 | [diff] [blame] | 11 | "sync" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 12 | "syscall" |
| 13 | "time" |
Cosmos Nicolaou | bfcac5f | 2014-05-22 21:57:35 -0700 | [diff] [blame] | 14 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 15 | "v.io/core/veyron2/vlog" |
Cosmos Nicolaou | 251a4d8 | 2014-09-30 22:28:45 -0700 | [diff] [blame] | 16 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 17 | "v.io/core/veyron/lib/timekeeper" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | var ( |
Cosmos Nicolaou | 1c18c1c | 2014-10-08 16:37:10 -0700 | [diff] [blame] | 21 | ErrAuthTimeout = errors.New("timeout in auth handshake") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 22 | ErrTimeout = errors.New("timeout waiting for child") |
| 23 | ErrSecretTooLarge = errors.New("secret is too large") |
| 24 | ) |
| 25 | |
| 26 | // A ParentHandle is the Parent process' means of managing a single child. |
| 27 | type ParentHandle struct { |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 28 | c *exec.Cmd |
Cosmos Nicolaou | 486d349 | 2014-09-30 22:21:20 -0700 | [diff] [blame] | 29 | config Config |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 30 | secret string |
| 31 | statusRead *os.File |
| 32 | statusWrite *os.File |
| 33 | tk timekeeper.TimeKeeper |
Bogdan Caprita | 650b162 | 2014-11-21 15:11:05 -0800 | [diff] [blame] | 34 | waitDone bool |
| 35 | waitErr error |
| 36 | waitLock sync.Mutex |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // ParentHandleOpt is an option for NewParentHandle. |
| 40 | type ParentHandleOpt interface { |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 41 | // ExecParentHandleOpt is a signature 'dummy' method for the |
| 42 | // interface. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 43 | ExecParentHandleOpt() |
| 44 | } |
| 45 | |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 46 | // ConfigOpt can be used to seed the parent handle with a |
| 47 | // config to be passed to the child. |
| 48 | type ConfigOpt struct { |
Cosmos Nicolaou | 486d349 | 2014-09-30 22:21:20 -0700 | [diff] [blame] | 49 | Config |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 50 | } |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 51 | |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 52 | // ExecParentHandleOpt makes ConfigOpt an instance of |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 53 | // ParentHandleOpt. |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 54 | func (ConfigOpt) ExecParentHandleOpt() {} |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 55 | |
| 56 | // SecretOpt can be used to seed the parent handle with a custom secret. |
| 57 | type SecretOpt string |
| 58 | |
| 59 | // ExecParentHandleOpt makes SecretOpt an instance of ParentHandleOpt. |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 60 | func (SecretOpt) ExecParentHandleOpt() {} |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 61 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 62 | // TimeKeeperOpt can be used to seed the parent handle with a custom timekeeper. |
| 63 | type TimeKeeperOpt struct { |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 64 | timekeeper.TimeKeeper |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // ExecParentHandleOpt makes TimeKeeperOpt an instance of ParentHandleOpt. |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 68 | func (TimeKeeperOpt) ExecParentHandleOpt() {} |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 69 | |
| 70 | // NewParentHandle creates a ParentHandle for the child process represented by |
| 71 | // an instance of exec.Cmd. |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 72 | func NewParentHandle(c *exec.Cmd, opts ...ParentHandleOpt) *ParentHandle { |
Cosmos Nicolaou | 486d349 | 2014-09-30 22:21:20 -0700 | [diff] [blame] | 73 | cfg, secret := NewConfig(), "" |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 74 | tk := timekeeper.RealTime() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 75 | for _, opt := range opts { |
| 76 | switch v := opt.(type) { |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 77 | case ConfigOpt: |
| 78 | cfg = v |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 79 | case SecretOpt: |
| 80 | secret = string(v) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 81 | case TimeKeeperOpt: |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 82 | tk = v |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 83 | default: |
| 84 | vlog.Errorf("Unrecognized parent option: %v", v) |
| 85 | } |
| 86 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 87 | return &ParentHandle{ |
Jiri Simsa | 24e87aa | 2014-06-09 09:27:34 -0700 | [diff] [blame] | 88 | c: c, |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 89 | config: cfg, |
Jiri Simsa | 24e87aa | 2014-06-09 09:27:34 -0700 | [diff] [blame] | 90 | secret: secret, |
| 91 | tk: tk, |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | // Start starts the child process, sharing a secret with it and |
| 96 | // setting up a communication channel over which to read its status. |
| 97 | func (p *ParentHandle) Start() error { |
Cosmos Nicolaou | e5b4150 | 2014-10-29 22:55:09 -0700 | [diff] [blame] | 98 | // Make sure that there are no instances of the VersionVariable |
| 99 | // already in the environment (which can happen when a subprocess |
| 100 | // creates a subprocess etc) |
| 101 | nenv := make([]string, 0, len(p.c.Env)+1) |
| 102 | for _, e := range p.c.Env { |
| 103 | if strings.HasPrefix(e, VersionVariable+"=") { |
| 104 | continue |
| 105 | } |
| 106 | nenv = append(nenv, e) |
| 107 | } |
| 108 | p.c.Env = append(nenv, VersionVariable+"="+version1) |
| 109 | |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 110 | // Create anonymous pipe for communicating data between the child |
| 111 | // and the parent. |
Bogdan Caprita | 7f49167 | 2014-11-13 14:51:08 -0800 | [diff] [blame] | 112 | // TODO(caprita): As per ribrdb@, Go's exec does not prune the set |
| 113 | // of file descriptors passed down to the child process, and hence |
| 114 | // a child may get access to the files meant for another child. |
| 115 | // Do we need to ensure only one thread is allowed to create these |
| 116 | // pipes at any time? |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 117 | dataRead, dataWrite, err := os.Pipe() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 118 | if err != nil { |
| 119 | return err |
| 120 | } |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 121 | defer dataRead.Close() |
| 122 | defer dataWrite.Close() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 123 | statusRead, statusWrite, err := os.Pipe() |
| 124 | if err != nil { |
| 125 | return err |
| 126 | } |
| 127 | p.statusRead = statusRead |
| 128 | p.statusWrite = statusWrite |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 129 | // Add the parent-child pipes to cmd.ExtraFiles, offsetting all |
| 130 | // existing file descriptors accordingly. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 131 | extraFiles := make([]*os.File, len(p.c.ExtraFiles)+2) |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 132 | extraFiles[0] = dataRead |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 133 | extraFiles[1] = statusWrite |
| 134 | for i, _ := range p.c.ExtraFiles { |
| 135 | extraFiles[i+2] = p.c.ExtraFiles[i] |
| 136 | } |
| 137 | p.c.ExtraFiles = extraFiles |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 138 | // Start the child process. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 139 | if err := p.c.Start(); err != nil { |
| 140 | p.statusWrite.Close() |
| 141 | p.statusRead.Close() |
| 142 | return err |
| 143 | } |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 144 | // Pass data to the child using a pipe. |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 145 | serializedConfig, err := p.config.Serialize() |
| 146 | if err != nil { |
| 147 | return err |
| 148 | } |
| 149 | if err := encodeString(dataWrite, serializedConfig); err != nil { |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 150 | p.statusWrite.Close() |
| 151 | p.statusRead.Close() |
| 152 | return err |
| 153 | } |
Jiri Simsa | 84059da | 2014-06-02 17:22:05 -0700 | [diff] [blame] | 154 | if err := encodeString(dataWrite, p.secret); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 155 | p.statusWrite.Close() |
| 156 | p.statusRead.Close() |
| 157 | return err |
| 158 | } |
| 159 | return nil |
| 160 | } |
| 161 | |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 162 | func waitForStatus(c chan string, e chan error, r *os.File) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 163 | buf := make([]byte, 100) |
| 164 | n, err := r.Read(buf) |
| 165 | if err != nil { |
| 166 | e <- err |
| 167 | } else { |
| 168 | c <- string(buf[:n]) |
| 169 | } |
| 170 | r.Close() |
| 171 | close(c) |
| 172 | close(e) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // WaitForReady will wait for the child process to become ready. |
| 176 | func (p *ParentHandle) WaitForReady(timeout time.Duration) error { |
| 177 | defer p.statusWrite.Close() |
| 178 | c := make(chan string, 1) |
| 179 | e := make(chan error, 1) |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 180 | go waitForStatus(c, e, p.statusRead) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 181 | for { |
| 182 | select { |
| 183 | case err := <-e: |
Cosmos Nicolaou | 1c18c1c | 2014-10-08 16:37:10 -0700 | [diff] [blame] | 184 | if err != nil { |
| 185 | return err |
| 186 | } |
| 187 | // waitForStatus has closed the channel, but we may not |
| 188 | // have read the message from it yet. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 189 | case st := <-c: |
| 190 | if st == readyStatus { |
| 191 | return nil |
| 192 | } |
Cosmos Nicolaou | 1c18c1c | 2014-10-08 16:37:10 -0700 | [diff] [blame] | 193 | if strings.HasPrefix(st, failedStatus) { |
| 194 | return fmt.Errorf("%s", strings.TrimPrefix(st, failedStatus)) |
| 195 | } |
| 196 | if len(st) > 0 { |
| 197 | return fmt.Errorf("unrecognised status from subprocess: %q", st) |
| 198 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 199 | case <-p.tk.After(timeout): |
| 200 | // Make sure that the read in waitForStatus |
| 201 | // returns now. |
| 202 | p.statusWrite.Write([]byte("quit")) |
| 203 | return ErrTimeout |
| 204 | } |
| 205 | } |
| 206 | panic("unreachable") |
| 207 | } |
| 208 | |
Bogdan Caprita | 650b162 | 2014-11-21 15:11:05 -0800 | [diff] [blame] | 209 | // wait performs the Wait on the underlying command under lock, and only once |
| 210 | // (subsequent wait calls block until the Wait is finished). It's ok to call |
| 211 | // wait multiple times, and in parallel. The error from the initial Wait is |
| 212 | // cached and returned for all subsequent calls. |
| 213 | func (p *ParentHandle) wait() error { |
| 214 | p.waitLock.Lock() |
| 215 | defer p.waitLock.Unlock() |
| 216 | if p.waitDone { |
| 217 | return p.waitErr |
| 218 | } |
| 219 | p.waitErr = p.c.Wait() |
| 220 | p.waitDone = true |
| 221 | return p.waitErr |
| 222 | } |
| 223 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 224 | // Wait will wait for the child process to terminate of its own accord. |
| 225 | // It returns nil if the process exited cleanly with an exit status of 0, |
| 226 | // any other exit code or error will result in an appropriate error return |
| 227 | func (p *ParentHandle) Wait(timeout time.Duration) error { |
| 228 | c := make(chan error, 1) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 229 | go func() { |
Bogdan Caprita | 650b162 | 2014-11-21 15:11:05 -0800 | [diff] [blame] | 230 | c <- p.wait() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 231 | close(c) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 232 | }() |
| 233 | // If timeout is zero time.After will panic; we handle zero specially |
| 234 | // to mean infinite timeout. |
| 235 | if timeout > 0 { |
| 236 | select { |
| 237 | case <-p.tk.After(timeout): |
| 238 | return ErrTimeout |
| 239 | case err := <-c: |
| 240 | return err |
| 241 | } |
| 242 | } else { |
| 243 | return <-c |
| 244 | } |
| 245 | panic("unreachable") |
| 246 | } |
| 247 | |
Cosmos Nicolaou | ee7abc2 | 2014-05-27 10:50:03 -0700 | [diff] [blame] | 248 | // Pid returns the pid of the child, 0 if the child process doesn't exist |
| 249 | func (p *ParentHandle) Pid() int { |
| 250 | if p.c.Process != nil { |
| 251 | return p.c.Process.Pid |
| 252 | } |
| 253 | return 0 |
| 254 | } |
| 255 | |
| 256 | // Exists returns true if the child process exists and can be signal'ed |
| 257 | func (p *ParentHandle) Exists() bool { |
| 258 | if p.c.Process != nil { |
| 259 | return syscall.Kill(p.c.Process.Pid, 0) == nil |
| 260 | } |
| 261 | return false |
| 262 | } |
| 263 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 264 | // Kill kills the child process. |
| 265 | func (p *ParentHandle) Kill() error { |
| 266 | return p.c.Process.Kill() |
| 267 | } |
| 268 | |
| 269 | // Signal sends the given signal to the child process. |
| 270 | func (p *ParentHandle) Signal(sig syscall.Signal) error { |
| 271 | return syscall.Kill(p.c.Process.Pid, sig) |
| 272 | } |
| 273 | |
| 274 | // Clean will clean up state, including killing the child process. |
| 275 | func (p *ParentHandle) Clean() error { |
| 276 | if err := p.Kill(); err != nil { |
| 277 | return err |
| 278 | } |
Bogdan Caprita | 650b162 | 2014-11-21 15:11:05 -0800 | [diff] [blame] | 279 | return p.wait() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 280 | } |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 281 | |
Jiri Simsa | 84059da | 2014-06-02 17:22:05 -0700 | [diff] [blame] | 282 | func encodeString(w io.Writer, data string) error { |
Jiri Simsa | c199bc1 | 2014-05-30 12:52:24 -0700 | [diff] [blame] | 283 | l := len(data) |
| 284 | if err := binary.Write(w, binary.BigEndian, int64(l)); err != nil { |
| 285 | return err |
| 286 | } |
| 287 | if n, err := w.Write([]byte(data)); err != nil || n != l { |
| 288 | if err != nil { |
| 289 | return err |
| 290 | } else { |
| 291 | return errors.New("partial write") |
| 292 | } |
| 293 | } |
| 294 | return nil |
| 295 | } |