sdnotify.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package feature
  4. import (
  5. "runtime"
  6. "tailscale.com/feature/buildfeatures"
  7. )
  8. // HookSystemdReady sends a readiness to systemd. This will unblock service
  9. // dependents from starting.
  10. var HookSystemdReady Hook[func()]
  11. // HookSystemdStatus holds a func that will send a single line status update to
  12. // systemd so that information shows up in systemctl output.
  13. var HookSystemdStatus Hook[func(format string, args ...any)]
  14. // SystemdStatus sends a single line status update to systemd so that
  15. // information shows up in systemctl output.
  16. //
  17. // It does nothing on non-Linux systems or if the binary was built without
  18. // the sdnotify feature.
  19. func SystemdStatus(format string, args ...any) {
  20. if !CanSystemdStatus { // mid-stack inlining DCE
  21. return
  22. }
  23. if f, ok := HookSystemdStatus.GetOk(); ok {
  24. f(format, args...)
  25. }
  26. }
  27. // CanSystemdStatus reports whether the current build has systemd notifications
  28. // linked in.
  29. //
  30. // It's effectively the same as HookSystemdStatus.IsSet(), but a constant for
  31. // dead code elimination reasons.
  32. const CanSystemdStatus = runtime.GOOS == "linux" && buildfeatures.HasSDNotify