github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/runc/libcontainer/system/setns_linux.go (about)

     1  package system
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"syscall"
     7  )
     8  
     9  // Via http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=7b21fddd087678a70ad64afc0f632e0f1071b092
    10  //
    11  // We need different setns values for the different platforms and arch
    12  // We are declaring the macro here because the SETNS syscall does not exist in th stdlib
    13  var setNsMap = map[string]uintptr{
    14  	"linux/386":     346,
    15  	"linux/arm64":   268,
    16  	"linux/amd64":   308,
    17  	"linux/arm":     375,
    18  	"linux/ppc":     350,
    19  	"linux/ppc64":   350,
    20  	"linux/ppc64le": 350,
    21  	"linux/s390x":   339,
    22  }
    23  
    24  var sysSetns = setNsMap[fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)]
    25  
    26  func SysSetns() uint32 {
    27  	return uint32(sysSetns)
    28  }
    29  
    30  func Setns(fd uintptr, flags uintptr) error {
    31  	ns, exists := setNsMap[fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)]
    32  	if !exists {
    33  		return fmt.Errorf("unsupported platform %s/%s", runtime.GOOS, runtime.GOARCH)
    34  	}
    35  	_, _, err := syscall.RawSyscall(ns, fd, flags, 0)
    36  	if err != 0 {
    37  		return err
    38  	}
    39  	return nil
    40  }