sdnotify.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2014 Docker, Inc.
  2. // Copyright 2015-2018 CoreOS, Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // Package daemon provides a Go implementation of the sd_notify protocol.
  17. // It can be used to inform systemd of service start-up completion, watchdog
  18. // events, and other status changes.
  19. //
  20. // https://www.freedesktop.org/software/systemd/man/sd_notify.html#Description
  21. package daemon
  22. import (
  23. "net"
  24. "os"
  25. )
  26. const (
  27. // SdNotifyReady tells the service manager that service startup is finished
  28. // or the service finished loading its configuration.
  29. SdNotifyReady = "READY=1"
  30. // SdNotifyStopping tells the service manager that the service is beginning
  31. // its shutdown.
  32. SdNotifyStopping = "STOPPING=1"
  33. // SdNotifyReloading tells the service manager that this service is
  34. // reloading its configuration. Note that you must call SdNotifyReady when
  35. // it completed reloading.
  36. SdNotifyReloading = "RELOADING=1"
  37. // SdNotifyWatchdog tells the service manager to update the watchdog
  38. // timestamp for the service.
  39. SdNotifyWatchdog = "WATCHDOG=1"
  40. )
  41. // SdNotify sends a message to the init daemon. It is common to ignore the error.
  42. // If `unsetEnvironment` is true, the environment variable `NOTIFY_SOCKET`
  43. // will be unconditionally unset.
  44. //
  45. // It returns one of the following:
  46. // (false, nil) - notification not supported (i.e. NOTIFY_SOCKET is unset)
  47. // (false, err) - notification supported, but failure happened (e.g. error connecting to NOTIFY_SOCKET or while sending data)
  48. // (true, nil) - notification supported, data has been sent
  49. func SdNotify(unsetEnvironment bool, state string) (bool, error) {
  50. socketAddr := &net.UnixAddr{
  51. Name: os.Getenv("NOTIFY_SOCKET"),
  52. Net: "unixgram",
  53. }
  54. // NOTIFY_SOCKET not set
  55. if socketAddr.Name == "" {
  56. return false, nil
  57. }
  58. if unsetEnvironment {
  59. if err := os.Unsetenv("NOTIFY_SOCKET"); err != nil {
  60. return false, err
  61. }
  62. }
  63. conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
  64. // Error connecting to NOTIFY_SOCKET
  65. if err != nil {
  66. return false, err
  67. }
  68. defer conn.Close()
  69. if _, err = conn.Write([]byte(state)); err != nil {
  70. return false, err
  71. }
  72. return true, nil
  73. }