k3s/vendor/github.com/Microsoft/hcsshim/process.go

99 lines
2.8 KiB
Go
Raw Normal View History

2019-01-12 04:58:27 +00:00
package hcsshim
import (
2019-09-30 23:25:17 +00:00
"context"
2019-01-12 04:58:27 +00:00
"io"
2019-09-30 23:25:17 +00:00
"sync"
2019-01-12 04:58:27 +00:00
"time"
"github.com/Microsoft/hcsshim/internal/hcs"
)
// ContainerError is an error encountered in HCS
type process struct {
2019-09-30 23:25:17 +00:00
p *hcs.Process
waitOnce sync.Once
waitCh chan struct{}
waitErr error
2019-01-12 04:58:27 +00:00
}
// Pid returns the process ID of the process within the container.
func (process *process) Pid() int {
return process.p.Pid()
}
// Kill signals the process to terminate but does not wait for it to finish terminating.
func (process *process) Kill() error {
2019-09-30 23:25:17 +00:00
found, err := process.p.Kill(context.Background())
if err != nil {
return convertProcessError(err, process)
}
if !found {
return &ProcessError{Process: process, Err: ErrElementNotFound, Operation: "hcsshim::Process::Kill"}
}
return nil
2019-01-12 04:58:27 +00:00
}
// Wait waits for the process to exit.
func (process *process) Wait() error {
return convertProcessError(process.p.Wait(), process)
}
// WaitTimeout waits for the process to exit or the duration to elapse. It returns
// false if timeout occurs.
func (process *process) WaitTimeout(timeout time.Duration) error {
2019-09-30 23:25:17 +00:00
process.waitOnce.Do(func() {
process.waitCh = make(chan struct{})
go func() {
process.waitErr = process.Wait()
close(process.waitCh)
}()
})
t := time.NewTimer(timeout)
defer t.Stop()
select {
case <-t.C:
return &ProcessError{Process: process, Err: ErrTimeout, Operation: "hcsshim::Process::Wait"}
case <-process.waitCh:
return process.waitErr
}
2019-01-12 04:58:27 +00:00
}
// ExitCode returns the exit code of the process. The process must have
// already terminated.
func (process *process) ExitCode() (int, error) {
code, err := process.p.ExitCode()
if err != nil {
err = convertProcessError(err, process)
}
return code, err
}
// ResizeConsole resizes the console of the process.
func (process *process) ResizeConsole(width, height uint16) error {
2019-09-30 23:25:17 +00:00
return convertProcessError(process.p.ResizeConsole(context.Background(), width, height), process)
2019-01-12 04:58:27 +00:00
}
// Stdio returns the stdin, stdout, and stderr pipes, respectively. Closing
// these pipes does not close the underlying pipes; it should be possible to
// call this multiple times to get multiple interfaces.
func (process *process) Stdio() (io.WriteCloser, io.ReadCloser, io.ReadCloser, error) {
2019-09-30 23:25:17 +00:00
stdin, stdout, stderr, err := process.p.StdioLegacy()
2019-01-12 04:58:27 +00:00
if err != nil {
err = convertProcessError(err, process)
}
return stdin, stdout, stderr, err
}
// CloseStdin closes the write side of the stdin pipe so that the process is
// notified on the read side that there is no more data in stdin.
func (process *process) CloseStdin() error {
2019-09-30 23:25:17 +00:00
return convertProcessError(process.p.CloseStdin(context.Background()), process)
2019-01-12 04:58:27 +00:00
}
// Close cleans up any state associated with the process but does not kill
// or wait on it.
func (process *process) Close() error {
return convertProcessError(process.p.Close(), process)
}