setns_linux.go 1009 B

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