Skip to content

Commit a15e088

Browse files
committed
Minor refactoring
1 parent d65a642 commit a15e088

2 files changed

Lines changed: 24 additions & 49 deletions

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Go Report Card](https://goreportcard.com/badge/github.com/marselester/systemd)](https://goreportcard.com/report/github.com/marselester/systemd)
55

66
This package provides an access to systemd via D-Bus
7-
to list services with a low overhead for a caller.
7+
to list services (think `systemctl list-units`) with a low overhead for a caller.
88
If you find the API too limiting or missing some of functionality,
99
perhaps https://github.com/coreos/go-systemd might suit you better.
1010

@@ -40,6 +40,13 @@ $ go run ./cmd/units -svc
4040
0 pk-debconf-helper.service inactive
4141
```
4242

43+
You can get the same results with `dbus-send`.
44+
45+
```sh
46+
$ dbus-send --system --print-reply --dest=org.freedesktop.systemd1 /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager.ListUnits
47+
$ dbus-send --system --print-reply --dest=org.freedesktop.systemd1 /org/freedesktop/systemd1/unit/dbus_2eservice org.freedesktop.DBus.Properties.Get string:'org.freedesktop.systemd1.Service' string:'MainPID'
48+
```
49+
4350
## Testing
4451

4552
Run tests and linters.

header.go

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,16 @@ const (
192192
// i.e., the body must be 0-length.
193193
// This header field is controlled by the message sender.
194194
fieldSignature
195-
// fieldUnixFDs is the number of Unix file descriptors that accompany the message.
196-
// If omitted, it is assumed that no Unix file descriptors accompany the message.
197-
// The actual file descriptors need to be transferred via platform specific mechanism out-of-band.
198-
// They must be sent at the same time as part of the message itself.
199-
// They may not be sent before the first byte of the message itself is transferred
200-
// or after the last byte of the message itself.
201-
// This header field is controlled by the message sender.
202-
fieldUnixFDs
195+
)
196+
197+
// D-Bus types,
198+
// see https://dbus.freedesktop.org/doc/dbus-specification.html#id-1.3.8.
199+
const (
200+
typeByte = 'y'
201+
typeUint32 = 'u'
202+
typeString = 's'
203+
typeObjectPath = 'o'
204+
typeSignature = 'g'
203205
)
204206

205207
// headerField represents a header field.
@@ -220,34 +222,6 @@ type headerField struct {
220222
Code byte
221223
}
222224

223-
func (f *headerField) String() string {
224-
var name string
225-
switch f.Code {
226-
case fieldPath:
227-
name = "PATH"
228-
case fieldInterface:
229-
name = "INTERFACE"
230-
case fieldMember:
231-
name = "MEMBER"
232-
case fieldErrorName:
233-
name = "ERROR_NAME"
234-
case fieldReplySerial:
235-
name = "REPLY_SERIAL"
236-
case fieldDestination:
237-
name = "DESTINATION"
238-
case fieldSender:
239-
name = "SENDER"
240-
case fieldSignature:
241-
name = "SIGNATURE"
242-
case fieldUnixFDs:
243-
name = "UNIX_FDS"
244-
default:
245-
name = "INVALID"
246-
}
247-
248-
return name
249-
}
250-
251225
// decodeHeaderField decodes a header field.
252226
func decodeHeaderField(d *decoder, conv *stringConverter) (f headerField, err error) {
253227
// Since "(yv)" struct is being decoded, an alignment must be discarded.
@@ -280,20 +254,17 @@ func decodeHeaderField(d *decoder, conv *stringConverter) (f headerField, err er
280254
s []byte
281255
)
282256
switch sign[0] {
283-
// UINT32 type.
284-
case 'u':
257+
case typeUint32:
285258
if u, err = d.Uint32(); err != nil {
286259
return
287260
}
288261
f.U = uint64(u)
289-
// STRING, OBJECT_PATH types.
290-
case 's', 'o':
262+
case typeString, typeObjectPath:
291263
if s, err = d.String(); err != nil {
292264
return
293265
}
294266
f.S = conv.String(s)
295-
// SIGNATURE type.
296-
case 'g':
267+
case typeSignature:
297268
if s, err = d.Signature(); err != nil {
298269
return
299270
}
@@ -367,14 +338,11 @@ func encodeHeaderField(e *encoder, f headerField) error {
367338
e.Signature(f.Signature)
368339

369340
switch f.Signature[0] {
370-
// UINT32 type.
371-
case 'u':
341+
case typeUint32:
372342
e.Uint32(uint32(f.U))
373-
// STRING, OBJECT_PATH types.
374-
case 's', 'o':
343+
case typeString, typeObjectPath:
375344
e.String(f.S)
376-
// SIGNATURE type.
377-
case 'g':
345+
case typeSignature:
378346
e.Signature(f.S)
379347
default:
380348
return fmt.Errorf("unknown type: %s", f.Signature)

0 commit comments

Comments
 (0)