...

Source file src/github.com/opencontainers/runc/libcontainer/system/linux.go

Documentation: github.com/opencontainers/runc/libcontainer/system

     1  //go:build linux
     2  // +build linux
     3  
     4  package system
     5  
     6  import (
     7  	"os"
     8  	"os/exec"
     9  	"unsafe"
    10  
    11  	"golang.org/x/sys/unix"
    12  )
    13  
    14  type ParentDeathSignal int
    15  
    16  func (p ParentDeathSignal) Restore() error {
    17  	if p == 0 {
    18  		return nil
    19  	}
    20  	current, err := GetParentDeathSignal()
    21  	if err != nil {
    22  		return err
    23  	}
    24  	if p == current {
    25  		return nil
    26  	}
    27  	return p.Set()
    28  }
    29  
    30  func (p ParentDeathSignal) Set() error {
    31  	return SetParentDeathSignal(uintptr(p))
    32  }
    33  
    34  func Execv(cmd string, args []string, env []string) error {
    35  	name, err := exec.LookPath(cmd)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	return Exec(name, args, env)
    41  }
    42  
    43  func Exec(cmd string, args []string, env []string) error {
    44  	for {
    45  		err := unix.Exec(cmd, args, env)
    46  		if err != unix.EINTR { //nolint:errorlint // unix errors are bare
    47  			return &os.PathError{Op: "exec", Path: cmd, Err: err}
    48  		}
    49  	}
    50  }
    51  
    52  func SetParentDeathSignal(sig uintptr) error {
    53  	if err := unix.Prctl(unix.PR_SET_PDEATHSIG, sig, 0, 0, 0); err != nil {
    54  		return err
    55  	}
    56  	return nil
    57  }
    58  
    59  func GetParentDeathSignal() (ParentDeathSignal, error) {
    60  	var sig int
    61  	if err := unix.Prctl(unix.PR_GET_PDEATHSIG, uintptr(unsafe.Pointer(&sig)), 0, 0, 0); err != nil {
    62  		return -1, err
    63  	}
    64  	return ParentDeathSignal(sig), nil
    65  }
    66  
    67  func SetKeepCaps() error {
    68  	if err := unix.Prctl(unix.PR_SET_KEEPCAPS, 1, 0, 0, 0); err != nil {
    69  		return err
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func ClearKeepCaps() error {
    76  	if err := unix.Prctl(unix.PR_SET_KEEPCAPS, 0, 0, 0, 0); err != nil {
    77  		return err
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  func Setctty() error {
    84  	if err := unix.IoctlSetInt(0, unix.TIOCSCTTY, 0); err != nil {
    85  		return err
    86  	}
    87  	return nil
    88  }
    89  
    90  // SetSubreaper sets the value i as the subreaper setting for the calling process
    91  func SetSubreaper(i int) error {
    92  	return unix.Prctl(unix.PR_SET_CHILD_SUBREAPER, uintptr(i), 0, 0, 0)
    93  }
    94  
    95  // GetSubreaper returns the subreaper setting for the calling process
    96  func GetSubreaper() (int, error) {
    97  	var i uintptr
    98  
    99  	if err := unix.Prctl(unix.PR_GET_CHILD_SUBREAPER, uintptr(unsafe.Pointer(&i)), 0, 0, 0); err != nil {
   100  		return -1, err
   101  	}
   102  
   103  	return int(i), nil
   104  }
   105  

View as plain text