util.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package util contains utility functions related to systemd that applications
  15. // can use to check things like whether systemd is running. Note that some of
  16. // these functions attempt to manually load systemd libraries at runtime rather
  17. // than linking against them.
  18. package util
  19. import (
  20. "fmt"
  21. "io/ioutil"
  22. "os"
  23. "strings"
  24. )
  25. var (
  26. ErrNoCGO = fmt.Errorf("go-systemd built with CGO disabled")
  27. )
  28. // GetRunningSlice attempts to retrieve the name of the systemd slice in which
  29. // the current process is running.
  30. // This function is a wrapper around the libsystemd C library; if it cannot be
  31. // opened, an error is returned.
  32. func GetRunningSlice() (string, error) {
  33. return getRunningSlice()
  34. }
  35. // RunningFromSystemService tries to detect whether the current process has
  36. // been invoked from a system service. The condition for this is whether the
  37. // process is _not_ a user process. User processes are those running in session
  38. // scopes or under per-user `systemd --user` instances.
  39. //
  40. // To avoid false positives on systems without `pam_systemd` (which is
  41. // responsible for creating user sessions), this function also uses a heuristic
  42. // to detect whether it's being invoked from a session leader process. This is
  43. // the case if the current process is executed directly from a service file
  44. // (e.g. with `ExecStart=/this/cmd`). Note that this heuristic will fail if the
  45. // command is instead launched in a subshell or similar so that it is not
  46. // session leader (e.g. `ExecStart=/bin/bash -c "/this/cmd"`)
  47. //
  48. // This function is a wrapper around the libsystemd C library; if this is
  49. // unable to successfully open a handle to the library for any reason (e.g. it
  50. // cannot be found), an error will be returned.
  51. func RunningFromSystemService() (bool, error) {
  52. return runningFromSystemService()
  53. }
  54. // CurrentUnitName attempts to retrieve the name of the systemd system unit
  55. // from which the calling process has been invoked. It wraps the systemd
  56. // `sd_pid_get_unit` call, with the same caveat: for processes not part of a
  57. // systemd system unit, this function will return an error.
  58. func CurrentUnitName() (string, error) {
  59. return currentUnitName()
  60. }
  61. // IsRunningSystemd checks whether the host was booted with systemd as its init
  62. // system. This functions similarly to systemd's `sd_booted(3)`: internally, it
  63. // checks whether /run/systemd/system/ exists and is a directory.
  64. // http://www.freedesktop.org/software/systemd/man/sd_booted.html
  65. func IsRunningSystemd() bool {
  66. fi, err := os.Lstat("/run/systemd/system")
  67. if err != nil {
  68. return false
  69. }
  70. return fi.IsDir()
  71. }
  72. // GetMachineID returns a host's 128-bit machine ID as a string. This functions
  73. // similarly to systemd's `sd_id128_get_machine`: internally, it simply reads
  74. // the contents of /etc/machine-id
  75. // http://www.freedesktop.org/software/systemd/man/sd_id128_get_machine.html
  76. func GetMachineID() (string, error) {
  77. machineID, err := ioutil.ReadFile("/etc/machine-id")
  78. if err != nil {
  79. return "", fmt.Errorf("failed to read /etc/machine-id: %v", err)
  80. }
  81. return strings.TrimSpace(string(machineID)), nil
  82. }