github.com/uppal0016/docker_new@v0.0.0-20240123060250-1c98be13ac2c/daemon/monitor.go (about)

     1  package daemon
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"runtime"
     8  	"strconv"
     9  
    10  	"github.com/Sirupsen/logrus"
    11  	"github.com/docker/docker/libcontainerd"
    12  	"github.com/docker/docker/runconfig"
    13  )
    14  
    15  // StateChanged updates daemon state changes from containerd
    16  func (daemon *Daemon) StateChanged(id string, e libcontainerd.StateInfo) error {
    17  	c := daemon.containers.Get(id)
    18  	if c == nil {
    19  		return fmt.Errorf("no such container: %s", id)
    20  	}
    21  
    22  	switch e.State {
    23  	case libcontainerd.StateOOM:
    24  		// StateOOM is Linux specific and should never be hit on Windows
    25  		if runtime.GOOS == "windows" {
    26  			return errors.New("Received StateOOM from libcontainerd on Windows. This should never happen.")
    27  		}
    28  		daemon.LogContainerEvent(c, "oom")
    29  	case libcontainerd.StateExit:
    30  		c.Lock()
    31  		defer c.Unlock()
    32  		c.Wait()
    33  		c.Reset(false)
    34  		c.SetStopped(platformConstructExitStatus(e))
    35  		attributes := map[string]string{
    36  			"exitCode": strconv.Itoa(int(e.ExitCode)),
    37  		}
    38  		daemon.LogContainerEventWithAttributes(c, "die", attributes)
    39  		daemon.Cleanup(c)
    40  		// FIXME: here is race condition between two RUN instructions in Dockerfile
    41  		// because they share same runconfig and change image. Must be fixed
    42  		// in builder/builder.go
    43  		if err := c.ToDisk(); err != nil {
    44  			return err
    45  		}
    46  		return daemon.postRunProcessing(c, e)
    47  	case libcontainerd.StateRestart:
    48  		c.Lock()
    49  		defer c.Unlock()
    50  		c.Reset(false)
    51  		c.RestartCount++
    52  		c.SetRestarting(platformConstructExitStatus(e))
    53  		attributes := map[string]string{
    54  			"exitCode": strconv.Itoa(int(e.ExitCode)),
    55  		}
    56  		daemon.LogContainerEventWithAttributes(c, "die", attributes)
    57  		if err := c.ToDisk(); err != nil {
    58  			return err
    59  		}
    60  		return daemon.postRunProcessing(c, e)
    61  	case libcontainerd.StateExitProcess:
    62  		c.Lock()
    63  		defer c.Unlock()
    64  		if execConfig := c.ExecCommands.Get(e.ProcessID); execConfig != nil {
    65  			ec := int(e.ExitCode)
    66  			execConfig.ExitCode = &ec
    67  			execConfig.Running = false
    68  			execConfig.Wait()
    69  			if err := execConfig.CloseStreams(); err != nil {
    70  				logrus.Errorf("%s: %s", c.ID, err)
    71  			}
    72  
    73  			// remove the exec command from the container's store only and not the
    74  			// daemon's store so that the exec command can be inspected.
    75  			c.ExecCommands.Delete(execConfig.ID)
    76  		} else {
    77  			logrus.Warnf("Ignoring StateExitProcess for %v but no exec command found", e)
    78  		}
    79  	case libcontainerd.StateStart, libcontainerd.StateRestore:
    80  		c.SetRunning(int(e.Pid), e.State == libcontainerd.StateStart)
    81  		c.HasBeenManuallyStopped = false
    82  		if err := c.ToDisk(); err != nil {
    83  			c.Reset(false)
    84  			return err
    85  		}
    86  		daemon.LogContainerEvent(c, "start")
    87  	case libcontainerd.StatePause:
    88  		c.Paused = true
    89  		daemon.LogContainerEvent(c, "pause")
    90  	case libcontainerd.StateResume:
    91  		c.Paused = false
    92  		daemon.LogContainerEvent(c, "unpause")
    93  	}
    94  
    95  	return nil
    96  }
    97  
    98  // AttachStreams is called by libcontainerd to connect the stdio.
    99  func (daemon *Daemon) AttachStreams(id string, iop libcontainerd.IOPipe) error {
   100  	var s *runconfig.StreamConfig
   101  	c := daemon.containers.Get(id)
   102  	if c == nil {
   103  		ec, err := daemon.getExecConfig(id)
   104  		if err != nil {
   105  			return fmt.Errorf("no such exec/container: %s", id)
   106  		}
   107  		s = ec.StreamConfig
   108  	} else {
   109  		s = c.StreamConfig
   110  		if err := daemon.StartLogging(c); err != nil {
   111  			c.Reset(false)
   112  			return err
   113  		}
   114  	}
   115  
   116  	if stdin := s.Stdin(); stdin != nil {
   117  		if iop.Stdin != nil {
   118  			go func() {
   119  				io.Copy(iop.Stdin, stdin)
   120  				iop.Stdin.Close()
   121  			}()
   122  		}
   123  	} else {
   124  		if c != nil && !c.Config.Tty {
   125  			// tty is enabled, so dont close containerd's iopipe stdin.
   126  			if iop.Stdin != nil {
   127  				iop.Stdin.Close()
   128  			}
   129  		}
   130  	}
   131  
   132  	copy := func(w io.Writer, r io.Reader) {
   133  		s.Add(1)
   134  		go func() {
   135  			if _, err := io.Copy(w, r); err != nil {
   136  				logrus.Errorf("%v stream copy error: %v", id, err)
   137  			}
   138  			s.Done()
   139  		}()
   140  	}
   141  
   142  	if iop.Stdout != nil {
   143  		copy(s.Stdout(), iop.Stdout)
   144  	}
   145  	if iop.Stderr != nil {
   146  		copy(s.Stderr(), iop.Stderr)
   147  	}
   148  
   149  	return nil
   150  }