Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

activation: add support for listeners with names #251

Merged
merged 1 commit into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion activation/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package activation
import (
"os"
"strconv"
"strings"
"syscall"
)

Expand All @@ -30,6 +31,7 @@ func Files(unsetEnv bool) []*os.File {
if unsetEnv {
defer os.Unsetenv("LISTEN_PID")
defer os.Unsetenv("LISTEN_FDS")
defer os.Unsetenv("LISTEN_FDNAMES")
}

pid, err := strconv.Atoi(os.Getenv("LISTEN_PID"))
Expand All @@ -42,10 +44,17 @@ func Files(unsetEnv bool) []*os.File {
return nil
}

names := strings.Split(os.Getenv("LISTEN_FDNAMES"), ":")

files := make([]*os.File, 0, nfds)
for fd := listenFdsStart; fd < listenFdsStart+nfds; fd++ {
syscall.CloseOnExec(fd)
files = append(files, os.NewFile(uintptr(fd), "LISTEN_FD_"+strconv.Itoa(fd)))
name := "LISTEN_FD_" + strconv.Itoa(fd)
offset := fd - listenFdsStart
if offset < len(names) && len(names[offset]) > 0 {
name = names[offset]
}
files = append(files, os.NewFile(uintptr(fd), name))
}

return files
Expand Down
6 changes: 3 additions & 3 deletions activation/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ func TestActivation(t *testing.T) {
}

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "FIX_LISTEN_PID=1")
cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "LISTEN_FDNAMES=fd1", "FIX_LISTEN_PID=1")

err := cmd.Run()
if err != nil {
t.Fatalf(err.Error())
}

correctStringWritten(t, r1, "Hello world")
correctStringWritten(t, r2, "Goodbye world")
correctStringWritten(t, r1, "Hello world: fd1")
correctStringWritten(t, r2, "Goodbye world: LISTEN_FD_4")
}

func TestActivationNoFix(t *testing.T) {
Expand Down
44 changes: 44 additions & 0 deletions activation/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ func Listeners(unsetEnv bool) ([]net.Listener, error) {
return listeners, nil
}

// ListenersWithNames maps a listener name to a set of net.Listener instances.
func ListenersWithNames(unsetEnv bool) (map[string][]net.Listener, error) {
files := Files(unsetEnv)
listeners := map[string][]net.Listener{}

for _, f := range files {
if pc, err := net.FileListener(f); err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would benefit from the same conditional Close we are discussing at #250.

current, ok := listeners[f.Name()]
if !ok {
listeners[f.Name()] = []net.Listener{pc}
} else {
listeners[f.Name()] = append(current, pc)
}
if unsetEnv {
f.Close()
}
}
}
return listeners, nil
}

// TLSListeners returns a slice containing a net.listener for each matching TCP socket type
// passed to this process.
// It uses default Listeners func and forces TCP sockets handlers to use TLS based on tlsConfig.
Expand All @@ -58,3 +79,26 @@ func TLSListeners(unsetEnv bool, tlsConfig *tls.Config) ([]net.Listener, error)

return listeners, err
}

// TLSListenersWithNames maps a listener name to a net.Listener with
// the associated TLS configuration.
func TLSListenersWithNames(unsetEnv bool, tlsConfig *tls.Config) (map[string][]net.Listener, error) {
listeners, err := ListenersWithNames(unsetEnv)

if listeners == nil || err != nil {
return nil, err
}

if tlsConfig != nil && err == nil {
for _, ll := range listeners {
// Activate TLS only for TCP sockets
for i, l := range ll {
if l.Addr().Network() == "tcp" {
ll[i] = tls.NewListener(l, tlsConfig)
}
}
}
}

return listeners, err
}
8 changes: 4 additions & 4 deletions activation/listeners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ func TestListeners(t *testing.T) {
r2.Write([]byte("Hi"))

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "FIX_LISTEN_PID=1")
cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "LISTEN_FDNAMES=fd1:fd2", "FIX_LISTEN_PID=1")

out, err := cmd.Output()
out, err := cmd.CombinedOutput()
if err != nil {
println(string(out))
t.Fatalf(err.Error())
}

correctStringWrittenNet(t, r1, "Hello world")
correctStringWrittenNet(t, r2, "Goodbye world")
correctStringWrittenNet(t, r1, "Hello world: fd1")
correctStringWrittenNet(t, r2, "Goodbye world: fd2")
}
2 changes: 1 addition & 1 deletion activation/packetconns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestPacketConns(t *testing.T) {
r2.Write([]byte("Hi"))

cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "FIX_LISTEN_PID=1")
cmd.Env = append(cmd.Env, "LISTEN_FDS=2", "LISTEN_FDNAMES=fd1:fd2", "FIX_LISTEN_PID=1")

out, err := cmd.CombinedOutput()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions examples/activation/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ func main() {
panic("No files")
}

if os.Getenv("LISTEN_PID") == "" || os.Getenv("LISTEN_FDS") == "" {
if os.Getenv("LISTEN_PID") == "" || os.Getenv("LISTEN_FDS") == "" || os.Getenv("LISTEN_FDNAMES") == "" {
panic("Should not unset envs")
}

files = activation.Files(true)

if os.Getenv("LISTEN_PID") != "" || os.Getenv("LISTEN_FDS") != "" {
if os.Getenv("LISTEN_PID") != "" || os.Getenv("LISTEN_FDS") != "" || os.Getenv("LISTEN_FDNAMES") != "" {
panic("Can not unset envs")
}

// Write out the expected strings to the two pipes
files[0].Write([]byte("Hello world"))
files[1].Write([]byte("Goodbye world"))
files[0].Write([]byte("Hello world: " + files[0].Name()))
files[1].Write([]byte("Goodbye world: " + files[1].Name()))

return
}
14 changes: 7 additions & 7 deletions examples/activation/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ func main() {
panic("No listeners")
}

if os.Getenv("LISTEN_PID") == "" || os.Getenv("LISTEN_FDS") == "" {
if os.Getenv("LISTEN_PID") == "" || os.Getenv("LISTEN_FDS") == "" || os.Getenv("LISTEN_FDNAMES") == "" {
panic("Should not unset envs")
}

listeners, err := activation.Listeners(true)
listenersWithNames, err := activation.ListenersWithNames(true)
if err != nil {
panic(err)
}

if os.Getenv("LISTEN_PID") != "" || os.Getenv("LISTEN_FDS") != "" {
if os.Getenv("LISTEN_PID") != "" || os.Getenv("LISTEN_FDS") != "" || os.Getenv("LISTEN_FDNAMES") != "" {
panic("Can not unset envs")
}

c0, _ := listeners[0].Accept()
c1, _ := listeners[1].Accept()
c0, _ := listenersWithNames["fd1"][0].Accept()
c1, _ := listenersWithNames["fd2"][0].Accept()

// Write out the expected strings to the two pipes
c0.Write([]byte("Hello world"))
c1.Write([]byte("Goodbye world"))
c0.Write([]byte("Hello world: fd1"))
c1.Write([]byte("Goodbye world: fd2"))

return
}
4 changes: 2 additions & 2 deletions examples/activation/udpconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
panic("No packetConns")
}

if os.Getenv("LISTEN_PID") == "" || os.Getenv("LISTEN_FDS") == "" {
if os.Getenv("LISTEN_PID") == "" || os.Getenv("LISTEN_FDS") == "" || os.Getenv("LISTEN_FDNAMES") == "" {
panic("Should not unset envs")
}

Expand All @@ -52,7 +52,7 @@ func main() {
panic(err)
}

if os.Getenv("LISTEN_PID") != "" || os.Getenv("LISTEN_FDS") != "" {
if os.Getenv("LISTEN_PID") != "" || os.Getenv("LISTEN_FDS") != "" || os.Getenv("LISTEN_FDNAMES") != "" {
panic("Can not unset envs")
}

Expand Down