utils.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package namespaces
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/pkg/libcontainer"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. )
  10. func addEnvIfNotSet(container *libcontainer.Container, key, value string) {
  11. jv := fmt.Sprintf("%s=%s", key, value)
  12. if len(container.Command.Env) == 0 {
  13. container.Command.Env = []string{jv}
  14. return
  15. }
  16. for _, v := range container.Command.Env {
  17. parts := strings.Split(v, "=")
  18. if parts[0] == key {
  19. return
  20. }
  21. }
  22. container.Command.Env = append(container.Command.Env, jv)
  23. }
  24. // getNsFd returns the fd for a specific pid and namespace option
  25. func getNsFd(pid int, ns string) (uintptr, error) {
  26. nspath := filepath.Join("/proc", strconv.Itoa(pid), "ns", ns)
  27. // OpenFile adds closOnExec
  28. f, err := os.OpenFile(nspath, os.O_RDONLY, 0666)
  29. if err != nil {
  30. return 0, err
  31. }
  32. return f.Fd(), nil
  33. }
  34. // setupEnvironment adds additional environment variables to the container's
  35. // Command such as USER, LOGNAME, container, and TERM
  36. func setupEnvironment(container *libcontainer.Container) {
  37. addEnvIfNotSet(container, "container", "docker")
  38. // TODO: check if pty
  39. addEnvIfNotSet(container, "TERM", "xterm")
  40. // TODO: get username from container
  41. addEnvIfNotSet(container, "USER", "root")
  42. addEnvIfNotSet(container, "LOGNAME", "root")
  43. }