| 156 | } |
| 157 | |
| 158 | func (d *Process) kill(sig syscall.Signal) error { |
| 159 | if d.cmd.OSProcess() == nil { |
| 160 | return xerrors.Errorf("cmd has not been started") |
| 161 | } |
| 162 | |
| 163 | atomic.StoreInt64(d.userKilled, 1) |
| 164 | |
| 165 | pid := d.cmd.OSProcess().Pid |
| 166 | err := d.cmd.OSProcess().Signal(sig) |
| 167 | if err != nil { |
| 168 | return xerrors.Errorf("kill proc: %w", err) |
| 169 | } |
| 170 | |
| 171 | ticker := time.NewTicker(time.Millisecond * 10) |
| 172 | defer ticker.Stop() |
| 173 | |
| 174 | fs := xunix.GetFS(d.ctx) |
| 175 | |
| 176 | for { |
| 177 | // Try to find the process in the procfs. If we can't find |
| 178 | // it, it means the process has exited. It's also possible that |
| 179 | // we find the same PID but the cmd is different indicating the PID |
| 180 | // has been reused. |
| 181 | exited, err := isProcExited(fs, pid, d.binName) |
| 182 | if err != nil { |
| 183 | return xerrors.Errorf("is proc cmd: %w", err) |
| 184 | } |
| 185 | |
| 186 | if exited { |
| 187 | return nil |
| 188 | } |
| 189 | |
| 190 | select { |
| 191 | case <-d.ctx.Done(): |
| 192 | return d.ctx.Err() |
| 193 | case <-ticker.C: |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // isProcExited checks if the provided PID has exited. It does this |
| 199 | // by attempting to read its entry in /proc/<pid>. If it can't find the |