summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-07-22 15:18:08 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-07-22 15:18:08 -0400
commit22b5d0b61995f07c89a7b6ad472024aa494b9a3c (patch)
tree79d23337169164c047c3797d84c1513ec8cf8132
parent89efdfbee5f9a22f9dd1083f7a383daba54d4f12 (diff)
ListenFds: Detect file names from LISTEN_FDNAMES.
-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