daemon_unix.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // +build daemon,!windows
  2. package main
  3. import (
  4. "fmt"
  5. "os"
  6. "os/signal"
  7. "path/filepath"
  8. "syscall"
  9. "github.com/Sirupsen/logrus"
  10. apiserver "github.com/docker/docker/api/server"
  11. "github.com/docker/docker/daemon"
  12. "github.com/docker/docker/libcontainerd"
  13. "github.com/docker/docker/pkg/mflag"
  14. "github.com/docker/docker/pkg/system"
  15. )
  16. const defaultDaemonConfigFile = "/etc/docker/daemon.json"
  17. func setPlatformServerConfig(serverConfig *apiserver.Config, daemonCfg *daemon.Config) *apiserver.Config {
  18. serverConfig.EnableCors = daemonCfg.EnableCors
  19. serverConfig.CorsHeaders = daemonCfg.CorsHeaders
  20. return serverConfig
  21. }
  22. // currentUserIsOwner checks whether the current user is the owner of the given
  23. // file.
  24. func currentUserIsOwner(f string) bool {
  25. if fileInfo, err := system.Stat(f); err == nil && fileInfo != nil {
  26. if int(fileInfo.UID()) == os.Getuid() {
  27. return true
  28. }
  29. }
  30. return false
  31. }
  32. // setDefaultUmask sets the umask to 0022 to avoid problems
  33. // caused by custom umask
  34. func setDefaultUmask() error {
  35. desiredUmask := 0022
  36. syscall.Umask(desiredUmask)
  37. if umask := syscall.Umask(desiredUmask); umask != desiredUmask {
  38. return fmt.Errorf("failed to set umask: expected %#o, got %#o", desiredUmask, umask)
  39. }
  40. return nil
  41. }
  42. func getDaemonConfDir() string {
  43. return "/etc/docker"
  44. }
  45. // setupConfigReloadTrap configures the USR2 signal to reload the configuration.
  46. func setupConfigReloadTrap(configFile string, flags *mflag.FlagSet, reload func(*daemon.Config)) {
  47. c := make(chan os.Signal, 1)
  48. signal.Notify(c, syscall.SIGHUP)
  49. go func() {
  50. for range c {
  51. if err := daemon.ReloadConfiguration(configFile, flags, reload); err != nil {
  52. logrus.Error(err)
  53. }
  54. }
  55. }()
  56. }
  57. func (cli *DaemonCli) getPlatformRemoteOptions() []libcontainerd.RemoteOption {
  58. opts := []libcontainerd.RemoteOption{
  59. libcontainerd.WithDebugLog(cli.Config.Debug),
  60. }
  61. if cli.Config.ContainerdAddr != "" {
  62. opts = append(opts, libcontainerd.WithRemoteAddr(cli.Config.ContainerdAddr))
  63. } else {
  64. opts = append(opts, libcontainerd.WithStartDaemon(true))
  65. }
  66. if daemon.UsingSystemd(cli.Config) {
  67. args := []string{"--systemd-cgroup=true"}
  68. opts = append(opts, libcontainerd.WithRuntimeArgs(args))
  69. }
  70. return opts
  71. }
  72. // getLibcontainerdRoot gets the root directory for libcontainerd/containerd to
  73. // store their state.
  74. func (cli *DaemonCli) getLibcontainerdRoot() string {
  75. return filepath.Join(cli.Config.ExecRoot, "libcontainerd")
  76. }