Skip to content

Commit

Permalink
check env
Browse files Browse the repository at this point in the history
  • Loading branch information
aloababa committed May 24, 2019
1 parent a1443e6 commit 4e10603
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion activation/activation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package activation

import (
"errors"
"fmt"
"log"
"net"
Expand All @@ -20,7 +21,7 @@ var listeners []net.Listener
func init() {
var err error
if listeners, err = activationListeners(); err != nil {
log.Println("warning: failed to init activation listeners:", err)
log.Println("warning: systemd socket activation disabled")
}
}

Expand All @@ -43,6 +44,9 @@ func Listen(addr string) (net.Listener, error) {
}

func activationListeners() ([]net.Listener, error) {
if err := checkEnv(); err != nil {
return nil, err
}
listenPid, err := strconv.Atoi(os.Getenv("LISTEN_PID"))
if err != nil {
return nil, fmt.Errorf("failed to parse LISTEN_PID as int: %v", err)
Expand Down Expand Up @@ -88,3 +92,17 @@ func activationListeners() ([]net.Listener, error) {
}
return listeners, nil
}

func checkEnv() error {
_, ok := os.LookupEnv("LISTEN_PID")
if !ok {
return errors.New("LISTEN_PID not set")
}
if _, ok = os.LookupEnv("LISTEN_FDS"); !ok {
return errors.New("LISTEN_FDS not set")
}
if _, ok = os.LookupEnv("LISTEN_FDNAMES"); !ok {
return errors.New("LISTEN_FDNAMES not set")
}
return nil
}

0 comments on commit 4e10603

Please sign in to comment.