From 22b5d0b61995f07c89a7b6ad472024aa494b9a3c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 22 Jul 2016 15:18:08 -0400 Subject: ListenFds: Detect file names from LISTEN_FDNAMES. --- sd_daemon/listen_fds.go | 22 ++++++++++++++++------ 1 file 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 -- cgit v1.1-4-g5e80