dbus.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright 2013 CoreOS Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Integration with the systemd D-Bus API. See http://www.freedesktop.org/wiki/Software/systemd/dbus/
  14. package dbus
  15. import (
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "github.com/godbus/dbus"
  22. )
  23. const (
  24. alpha = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`
  25. num = `0123456789`
  26. alphanum = alpha + num
  27. signalBuffer = 100
  28. )
  29. // needsEscape checks whether a byte in a potential dbus ObjectPath needs to be escaped
  30. func needsEscape(i int, b byte) bool {
  31. // Escape everything that is not a-z-A-Z-0-9
  32. // Also escape 0-9 if it's the first character
  33. return strings.IndexByte(alphanum, b) == -1 ||
  34. (i == 0 && strings.IndexByte(num, b) != -1)
  35. }
  36. // PathBusEscape sanitizes a constituent string of a dbus ObjectPath using the
  37. // rules that systemd uses for serializing special characters.
  38. func PathBusEscape(path string) string {
  39. // Special case the empty string
  40. if len(path) == 0 {
  41. return "_"
  42. }
  43. n := []byte{}
  44. for i := 0; i < len(path); i++ {
  45. c := path[i]
  46. if needsEscape(i, c) {
  47. e := fmt.Sprintf("_%x", c)
  48. n = append(n, []byte(e)...)
  49. } else {
  50. n = append(n, c)
  51. }
  52. }
  53. return string(n)
  54. }
  55. // Conn is a connection to systemd's dbus endpoint.
  56. type Conn struct {
  57. sysconn *dbus.Conn
  58. sysobj *dbus.Object
  59. jobListener struct {
  60. jobs map[dbus.ObjectPath]chan string
  61. sync.Mutex
  62. }
  63. subscriber struct {
  64. updateCh chan<- *SubStateUpdate
  65. errCh chan<- error
  66. sync.Mutex
  67. ignore map[dbus.ObjectPath]int64
  68. cleanIgnore int64
  69. }
  70. dispatch map[string]func(dbus.Signal)
  71. }
  72. // New() establishes a connection to the system bus and authenticates.
  73. func New() (*Conn, error) {
  74. c := new(Conn)
  75. if err := c.initConnection(); err != nil {
  76. return nil, err
  77. }
  78. c.initJobs()
  79. return c, nil
  80. }
  81. func (c *Conn) initConnection() error {
  82. var err error
  83. c.sysconn, err = dbus.SystemBusPrivate()
  84. if err != nil {
  85. return err
  86. }
  87. // Only use EXTERNAL method, and hardcode the uid (not username)
  88. // to avoid a username lookup (which requires a dynamically linked
  89. // libc)
  90. methods := []dbus.Auth{dbus.AuthExternal(strconv.Itoa(os.Getuid()))}
  91. err = c.sysconn.Auth(methods)
  92. if err != nil {
  93. c.sysconn.Close()
  94. return err
  95. }
  96. err = c.sysconn.Hello()
  97. if err != nil {
  98. c.sysconn.Close()
  99. return err
  100. }
  101. c.sysobj = c.sysconn.Object("org.freedesktop.systemd1", dbus.ObjectPath("/org/freedesktop/systemd1"))
  102. // Setup the listeners on jobs so that we can get completions
  103. c.sysconn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0,
  104. "type='signal', interface='org.freedesktop.systemd1.Manager', member='JobRemoved'")
  105. c.initSubscription()
  106. c.initDispatch()
  107. return nil
  108. }