github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/agent/systemd/notify.go (about)

     1  package systemd
     2  
     3  import (
     4  	"errors"
     5  	"net"
     6  	"os"
     7  )
     8  
     9  const (
    10  	// magic values for systemd
    11  	// from https://www.freedesktop.org/software/systemd/man/sd_notify.html#Description
    12  
    13  	Ready     = "READY=1"
    14  	Reloading = "RELOADING=1"
    15  	Stopping  = "STOPPING=1"
    16  )
    17  
    18  var NotifyNoSocket = errors.New("No socket")
    19  
    20  // Notifier provides a method to send a message to systemd.
    21  type Notifier struct{}
    22  
    23  // Notify sends a message to the init daemon. It is common to ignore the error.
    24  func (n *Notifier) Notify(state string) error {
    25  	addr := &net.UnixAddr{
    26  		Name: os.Getenv("NOTIFY_SOCKET"),
    27  		Net:  "unixgram",
    28  	}
    29  
    30  	if addr.Name == "" {
    31  		return NotifyNoSocket
    32  	}
    33  
    34  	conn, err := net.DialUnix(addr.Net, nil, addr)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	defer conn.Close()
    39  
    40  	_, err = conn.Write([]byte(state))
    41  	return err
    42  }