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