sd_notify.go 957 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2021 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package cli // import "miniflux.app/cli"
  5. import (
  6. "net"
  7. "os"
  8. )
  9. const (
  10. // sdNotifyReady tells the service manager that service startup is
  11. // finished, or the service finished loading its configuration.
  12. sdNotifyReady = "READY=1"
  13. )
  14. // sdNotify sends a message to systemd using the sd_notify protocol.
  15. // See https://www.freedesktop.org/software/systemd/man/sd_notify.html.
  16. func sdNotify(state string) error {
  17. addr := &net.UnixAddr{
  18. Net: "unixgram",
  19. Name: os.Getenv("NOTIFY_SOCKET"),
  20. }
  21. if addr.Name == "" {
  22. // We're not running under systemd (NOTIFY_SOCKET has not set).
  23. return nil
  24. }
  25. conn, err := net.DialUnix(addr.Net, nil, addr)
  26. if err != nil {
  27. return err
  28. }
  29. defer conn.Close()
  30. if _, err = conn.Write([]byte(state)); err != nil {
  31. return err
  32. }
  33. return nil
  34. }