github.com/apptainer/singularity@v3.1.1+incompatible/internal/pkg/runtime/engines/oci/monitor.go (about)

     1  // Copyright (c) 2018, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package oci
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  	"syscall"
    12  )
    13  
    14  // MonitorContainer monitors a container
    15  func (engine *EngineOperations) MonitorContainer(pid int, signals chan os.Signal) (syscall.WaitStatus, error) {
    16  	var status syscall.WaitStatus
    17  
    18  	for {
    19  		s := <-signals
    20  		switch s {
    21  		case syscall.SIGCHLD:
    22  			if wpid, err := syscall.Wait4(pid, &status, syscall.WNOHANG, nil); err != nil {
    23  				return status, fmt.Errorf("error while waiting child: %s", err)
    24  			} else if wpid != pid {
    25  				continue
    26  			}
    27  			return status, nil
    28  		default:
    29  			if err := syscall.Kill(pid, s.(syscall.Signal)); err != nil {
    30  				return status, fmt.Errorf("interrupted by signal %s", s.String())
    31  			}
    32  		}
    33  	}
    34  }