mirror of
https://github.com/rancher/os.git
synced 2025-09-04 08:14:21 +00:00
Update vendor
This commit is contained in:
54
vendor/github.com/docker/containerd/subreaper/exec/copy.go
generated
vendored
Normal file
54
vendor/github.com/docker/containerd/subreaper/exec/copy.go
generated
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Below is largely a copy from go's os/exec/exec.go
|
||||
|
||||
// Run starts the specified command and waits for it to complete.
|
||||
//
|
||||
// The returned error is nil if the command runs, has no problems
|
||||
// copying stdin, stdout, and stderr, and exits with a zero exit
|
||||
// status.
|
||||
//
|
||||
// If the command fails to run or doesn't complete successfully, the
|
||||
// error is of type *ExitError. Other error types may be
|
||||
// returned for I/O problems.
|
||||
func (c *Cmd) Run() error {
|
||||
if err := c.Start(); err != nil {
|
||||
return translateError(err)
|
||||
}
|
||||
return translateError(c.Wait())
|
||||
}
|
||||
|
||||
// Output runs the command and returns its standard output.
|
||||
// Any returned error will usually be of type *ExitError.
|
||||
// If c.Stderr was nil, Output populates ExitError.Stderr.
|
||||
func (c *Cmd) Output() ([]byte, error) {
|
||||
if c.Stdout != nil {
|
||||
return nil, errors.New("exec: Stdout already set")
|
||||
}
|
||||
var stdout bytes.Buffer
|
||||
c.Stdout = &stdout
|
||||
|
||||
err := c.Run()
|
||||
return stdout.Bytes(), err
|
||||
}
|
||||
|
||||
// CombinedOutput runs the command and returns its combined standard
|
||||
// output and standard error.
|
||||
func (c *Cmd) CombinedOutput() ([]byte, error) {
|
||||
if c.Stdout != nil {
|
||||
return nil, errors.New("exec: Stdout already set")
|
||||
}
|
||||
if c.Stderr != nil {
|
||||
return nil, errors.New("exec: Stderr already set")
|
||||
}
|
||||
var b bytes.Buffer
|
||||
c.Stdout = &b
|
||||
c.Stderr = &b
|
||||
err := c.Run()
|
||||
return b.Bytes(), err
|
||||
}
|
81
vendor/github.com/docker/containerd/subreaper/exec/wrapper.go
generated
vendored
Normal file
81
vendor/github.com/docker/containerd/subreaper/exec/wrapper.go
generated
vendored
Normal file
@@ -0,0 +1,81 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
osExec "os/exec"
|
||||
"strconv"
|
||||
|
||||
"github.com/docker/containerd/subreaper"
|
||||
)
|
||||
|
||||
var ErrNotFound = osExec.ErrNotFound
|
||||
|
||||
type Cmd struct {
|
||||
osExec.Cmd
|
||||
err error
|
||||
sub *subreaper.Subscription
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
Name string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *Error) Error() string {
|
||||
return "exec: " + strconv.Quote(e.Name) + ": " + e.Err.Error()
|
||||
}
|
||||
|
||||
type ExitCodeError struct {
|
||||
Code int
|
||||
}
|
||||
|
||||
func (e ExitCodeError) Error() string {
|
||||
return fmt.Sprintf("Non-zero exit code: %d", e.Code)
|
||||
}
|
||||
|
||||
func LookPath(file string) (string, error) {
|
||||
v, err := osExec.LookPath(file)
|
||||
return v, translateError(err)
|
||||
}
|
||||
|
||||
func Command(name string, args ...string) *Cmd {
|
||||
return &Cmd{
|
||||
Cmd: *osExec.Command(name, args...),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cmd) Start() error {
|
||||
c.sub = subreaper.Subscribe()
|
||||
err := c.Cmd.Start()
|
||||
if err != nil {
|
||||
subreaper.Unsubscribe(c.sub)
|
||||
c.sub = nil
|
||||
c.err = translateError(err)
|
||||
return c.err
|
||||
}
|
||||
|
||||
c.sub.SetPid(c.Cmd.Process.Pid)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cmd) Wait() error {
|
||||
if c.sub == nil {
|
||||
return c.err
|
||||
}
|
||||
exitCode := c.sub.Wait()
|
||||
if exitCode == 0 {
|
||||
return nil
|
||||
}
|
||||
return ExitCodeError{Code: exitCode}
|
||||
}
|
||||
|
||||
func translateError(err error) error {
|
||||
switch v := err.(type) {
|
||||
case *osExec.Error:
|
||||
return &Error{
|
||||
Name: v.Name,
|
||||
Err: v.Err,
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
102
vendor/github.com/docker/containerd/subreaper/reaper.go
generated
vendored
Normal file
102
vendor/github.com/docker/containerd/subreaper/reaper.go
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
package subreaper
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/docker/containerd/osutils"
|
||||
)
|
||||
|
||||
var (
|
||||
subscriptions = map[int]*Subscription{}
|
||||
subLock = sync.Mutex{}
|
||||
counter = 0
|
||||
once = sync.Once{}
|
||||
)
|
||||
|
||||
type Subscription struct {
|
||||
id int
|
||||
exit osutils.Exit
|
||||
c chan osutils.Exit
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func (s *Subscription) SetPid(pid int) {
|
||||
go func() {
|
||||
for exit := range s.c {
|
||||
if exit.Pid == pid {
|
||||
s.exit = exit
|
||||
s.wg.Done()
|
||||
Unsubscribe(s)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (s *Subscription) Wait() int {
|
||||
s.wg.Wait()
|
||||
return s.exit.Status
|
||||
}
|
||||
|
||||
func Subscribe() *Subscription {
|
||||
subLock.Lock()
|
||||
defer subLock.Unlock()
|
||||
|
||||
Start()
|
||||
|
||||
counter++
|
||||
s := &Subscription{
|
||||
id: counter,
|
||||
c: make(chan osutils.Exit, 1024),
|
||||
}
|
||||
s.wg.Add(1)
|
||||
subscriptions[s.id] = s
|
||||
return s
|
||||
}
|
||||
|
||||
func Unsubscribe(sub *Subscription) {
|
||||
subLock.Lock()
|
||||
defer subLock.Unlock()
|
||||
|
||||
delete(subscriptions, sub.id)
|
||||
}
|
||||
|
||||
func Start() error {
|
||||
var err error
|
||||
once.Do(func() {
|
||||
err = osutils.SetSubreaper(1)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
s := make(chan os.Signal, 2048)
|
||||
signal.Notify(s, syscall.SIGCHLD)
|
||||
go childReaper(s)
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func childReaper(s chan os.Signal) {
|
||||
for range s {
|
||||
exits, err := osutils.Reap()
|
||||
if err == nil {
|
||||
notify(exits)
|
||||
} else {
|
||||
logrus.WithField("error", err).Warn("containerd: reap child processes")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func notify(exits []osutils.Exit) {
|
||||
subLock.Lock()
|
||||
for _, exit := range exits {
|
||||
for _, sub := range subscriptions {
|
||||
sub.c <- exit
|
||||
}
|
||||
}
|
||||
subLock.Unlock()
|
||||
}
|
Reference in New Issue
Block a user