2014-01-04 02:06:34 +00:00
|
|
|
package systemd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
2014-01-21 20:43:57 +00:00
|
|
|
"github.com/dotcloud/docker/pkg/systemd/activation"
|
2014-01-04 02:06:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ListenFD returns the specified socket activated files as a slice of
|
|
|
|
// net.Listeners or all of the activated files if "*" is given.
|
|
|
|
func ListenFD(addr string) ([]net.Listener, error) {
|
2014-01-17 22:07:06 +00:00
|
|
|
// socket activation
|
|
|
|
listeners, err := activation.Listeners(false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if listeners == nil || len(listeners) == 0 {
|
2014-01-04 02:06:34 +00:00
|
|
|
return nil, errors.New("No sockets found")
|
|
|
|
}
|
|
|
|
|
2014-01-04 02:07:43 +00:00
|
|
|
// default to all fds just like unix:// and tcp://
|
|
|
|
if addr == "" {
|
|
|
|
addr = "*"
|
|
|
|
}
|
|
|
|
|
2014-01-04 02:06:34 +00:00
|
|
|
fdNum, _ := strconv.Atoi(addr)
|
|
|
|
fdOffset := fdNum - 3
|
2014-01-17 22:07:06 +00:00
|
|
|
if (addr != "*") && (len(listeners) < int(fdOffset)+1) {
|
2014-01-04 02:06:34 +00:00
|
|
|
return nil, errors.New("Too few socket activated files passed in")
|
|
|
|
}
|
|
|
|
|
|
|
|
if addr == "*" {
|
|
|
|
return listeners, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return []net.Listener{listeners[fdOffset]}, nil
|
|
|
|
}
|