github.com/xuyutom/docker@v1.6.0/pkg/systemd/listendfd.go (about)

     1  package systemd
     2  
     3  import (
     4  	"errors"
     5  	"net"
     6  	"strconv"
     7  
     8  	"github.com/coreos/go-systemd/activation"
     9  )
    10  
    11  // ListenFD returns the specified socket activated files as a slice of
    12  // net.Listeners or all of the activated files if "*" is given.
    13  func ListenFD(addr string) ([]net.Listener, error) {
    14  	// socket activation
    15  	listeners, err := activation.Listeners(false)
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  
    20  	if listeners == nil || len(listeners) == 0 {
    21  		return nil, errors.New("No sockets found")
    22  	}
    23  
    24  	// default to all fds just like unix:// and tcp://
    25  	if addr == "" {
    26  		addr = "*"
    27  	}
    28  
    29  	fdNum, _ := strconv.Atoi(addr)
    30  	fdOffset := fdNum - 3
    31  	if (addr != "*") && (len(listeners) < int(fdOffset)+1) {
    32  		return nil, errors.New("Too few socket activated files passed in")
    33  	}
    34  
    35  	if addr == "*" {
    36  		return listeners, nil
    37  	}
    38  
    39  	return []net.Listener{listeners[fdOffset]}, nil
    40  }