summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sd_daemon/listen_fds.go22
1 files changed, 16 insertions, 6 deletions
diff --git a/sd_daemon/listen_fds.go b/sd_daemon/listen_fds.go
index e6a81ca..51433d9 100644
--- a/sd_daemon/listen_fds.go
+++ b/sd_daemon/listen_fds.go
@@ -1,5 +1,5 @@
// Copyright 2015 CoreOS, Inc.
-// Copyright 2015 Luke Shumaker
+// Copyright 2015, 2016 Luke Shumaker
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@ package sd
import (
"os"
"strconv"
+ "strings"
"syscall"
)
@@ -36,8 +37,11 @@ import "C"
// In the case of an error, this function returns nil.
func ListenFds(unsetEnv bool) []*os.File {
if unsetEnv {
- defer func() { _ = os.Unsetenv("LISTEN_PID") }()
- defer func() { _ = os.Unsetenv("LISTEN_FDS") }()
+ defer func() {
+ _ = os.Unsetenv("LISTEN_PID")
+ _ = os.Unsetenv("LISTEN_FDS")
+ _ = os.Unsetenv("LISTEN_FDNAMES")
+ }()
}
pid, err := strconv.Atoi(os.Getenv("LISTEN_PID"))
@@ -46,14 +50,20 @@ func ListenFds(unsetEnv bool) []*os.File {
}
nfds, err := strconv.Atoi(os.Getenv("LISTEN_FDS"))
- if err != nil || nfds == 0 {
+ if err != nil || nfds < 1 {
return nil
}
+ names := strings.Split(os.Getenv("LISTEN_FDNAMES"), ":")
+
files := make([]*os.File, 0, nfds)
- for fd := C.SD_LISTEN_FDS_START; fd < C.SD_LISTEN_FDS_START+nfds; fd++ {
+ for i = 0; i < nfds; i++ {
syscall.CloseOnExec(fd)
- files = append(files, os.NewFile(uintptr(fd), "LISTEN_FD_"+strconv.Itoa(fd)))
+ name = "unknown"
+ if i < len(names) {
+ name = names[i]
+ }
+ files = append(files, os.NewFile(uintptr(i+C.SD_LISTEN_FDS_START), name))
}
return files