github.com/slackhq/nebula@v1.9.0/cmd/nebula/notify_linux.go (about)

     1  package main
     2  
     3  import (
     4  	"net"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/sirupsen/logrus"
     9  )
    10  
    11  // SdNotifyReady tells systemd the service is ready and dependent services can now be started
    12  // https://www.freedesktop.org/software/systemd/man/sd_notify.html
    13  // https://www.freedesktop.org/software/systemd/man/systemd.service.html
    14  const SdNotifyReady = "READY=1"
    15  
    16  func notifyReady(l *logrus.Logger) {
    17  	sockName := os.Getenv("NOTIFY_SOCKET")
    18  	if sockName == "" {
    19  		l.Debugln("NOTIFY_SOCKET systemd env var not set, not sending ready signal")
    20  		return
    21  	}
    22  
    23  	conn, err := net.DialTimeout("unixgram", sockName, time.Second)
    24  	if err != nil {
    25  		l.WithError(err).Error("failed to connect to systemd notification socket")
    26  		return
    27  	}
    28  	defer conn.Close()
    29  
    30  	err = conn.SetWriteDeadline(time.Now().Add(time.Second))
    31  	if err != nil {
    32  		l.WithError(err).Error("failed to set the write deadline for the systemd notification socket")
    33  		return
    34  	}
    35  
    36  	if _, err = conn.Write([]byte(SdNotifyReady)); err != nil {
    37  		l.WithError(err).Error("failed to signal the systemd notification socket")
    38  		return
    39  	}
    40  
    41  	l.Debugln("notified systemd the service is ready")
    42  }