github.com/rentongzhang/docker@v1.8.2-rc1/api/server/server_linux.go (about)

     1  // +build linux
     2  
     3  package server
     4  
     5  import (
     6  	"fmt"
     7  	"net"
     8  	"net/http"
     9  	"strconv"
    10  
    11  	"github.com/Sirupsen/logrus"
    12  	"github.com/docker/docker/daemon"
    13  	"github.com/docker/docker/pkg/sockets"
    14  	"github.com/docker/docker/pkg/systemd"
    15  	"github.com/docker/docker/pkg/version"
    16  	"github.com/docker/docker/runconfig"
    17  	"github.com/docker/libnetwork/portallocator"
    18  )
    19  
    20  const (
    21  	// See http://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/tree/kernel/sched/sched.h?id=8cd9234c64c584432f6992fe944ca9e46ca8ea76#n269
    22  	linuxMinCpuShares = 2
    23  	linuxMaxCpuShares = 262144
    24  )
    25  
    26  // newServer sets up the required serverClosers and does protocol specific checking.
    27  func (s *Server) newServer(proto, addr string) ([]serverCloser, error) {
    28  	var (
    29  		err error
    30  		ls  []net.Listener
    31  	)
    32  	switch proto {
    33  	case "fd":
    34  		ls, err = systemd.ListenFD(addr)
    35  		if err != nil {
    36  			return nil, err
    37  		}
    38  		// We don't want to start serving on these sockets until the
    39  		// daemon is initialized and installed. Otherwise required handlers
    40  		// won't be ready.
    41  		<-s.start
    42  	case "tcp":
    43  		l, err := s.initTcpSocket(addr)
    44  		if err != nil {
    45  			return nil, err
    46  		}
    47  		ls = append(ls, l)
    48  	case "unix":
    49  		l, err := sockets.NewUnixSocket(addr, s.cfg.SocketGroup, s.start)
    50  		if err != nil {
    51  			return nil, err
    52  		}
    53  		ls = append(ls, l)
    54  	default:
    55  		return nil, fmt.Errorf("Invalid protocol format: %q", proto)
    56  	}
    57  	var res []serverCloser
    58  	for _, l := range ls {
    59  		res = append(res, &HttpServer{
    60  			&http.Server{
    61  				Addr:    addr,
    62  				Handler: s.router,
    63  			},
    64  			l,
    65  		})
    66  	}
    67  	return res, nil
    68  }
    69  
    70  func (s *Server) AcceptConnections(d *daemon.Daemon) {
    71  	// Tell the init daemon we are accepting requests
    72  	s.daemon = d
    73  	s.registerSubRouter()
    74  	go systemd.SdNotify("READY=1")
    75  	// close the lock so the listeners start accepting connections
    76  	select {
    77  	case <-s.start:
    78  	default:
    79  		close(s.start)
    80  	}
    81  }
    82  
    83  func allocateDaemonPort(addr string) error {
    84  	host, port, err := net.SplitHostPort(addr)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	intPort, err := strconv.Atoi(port)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	var hostIPs []net.IP
    95  	if parsedIP := net.ParseIP(host); parsedIP != nil {
    96  		hostIPs = append(hostIPs, parsedIP)
    97  	} else if hostIPs, err = net.LookupIP(host); err != nil {
    98  		return fmt.Errorf("failed to lookup %s address in host specification", host)
    99  	}
   100  
   101  	pa := portallocator.Get()
   102  	for _, hostIP := range hostIPs {
   103  		if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
   104  			return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
   105  		}
   106  	}
   107  	return nil
   108  }
   109  
   110  func adjustCpuShares(version version.Version, hostConfig *runconfig.HostConfig) {
   111  	if version.LessThan("1.19") {
   112  		if hostConfig != nil && hostConfig.CpuShares > 0 {
   113  			// Handle unsupported CpuShares
   114  			if hostConfig.CpuShares < linuxMinCpuShares {
   115  				logrus.Warnf("Changing requested CpuShares of %d to minimum allowed of %d", hostConfig.CpuShares, linuxMinCpuShares)
   116  				hostConfig.CpuShares = linuxMinCpuShares
   117  			} else if hostConfig.CpuShares > linuxMaxCpuShares {
   118  				logrus.Warnf("Changing requested CpuShares of %d to maximum allowed of %d", hostConfig.CpuShares, linuxMaxCpuShares)
   119  				hostConfig.CpuShares = linuxMaxCpuShares
   120  			}
   121  		}
   122  	}
   123  }