Skip to content

Commit 3e1c173

Browse files
committed
daemon: SdNotify: add support for abstract unix sockets
As per https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html#
1 parent 7d375ec commit 3e1c173

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

daemon/sdnotify.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,29 @@ const (
5454
// (false, err) - notification supported, but failure happened (e.g. error connecting to NOTIFY_SOCKET or while sending data)
5555
// (true, nil) - notification supported, data has been sent
5656
func SdNotify(unsetEnvironment bool, state string) (bool, error) {
57-
socketAddr := &net.UnixAddr{
58-
Name: os.Getenv("NOTIFY_SOCKET"),
59-
Net: "unixgram",
60-
}
57+
notifySocket := os.Getenv("NOTIFY_SOCKET")
6158

6259
// NOTIFY_SOCKET not set
63-
if socketAddr.Name == "" {
60+
if notifySocket == "" {
61+
return false, nil
62+
}
63+
64+
// socket type not supported. We only support unix domain sockets
65+
// but NOTIFY_SOCKET can also use vsock
66+
if notifySocket[0] != '@' && notifySocket[0] != '/' {
6467
return false, nil
6568
}
6669

70+
// abstract unix socket. Start with a 0-byte
71+
if notifySocket[0] == '@' {
72+
notifySocket = "\x00" + notifySocket[1:]
73+
}
74+
75+
socketAddr := &net.UnixAddr{
76+
Name: notifySocket,
77+
Net: "unixgram",
78+
}
79+
6780
if unsetEnvironment {
6881
if err := os.Unsetenv("NOTIFY_SOCKET"); err != nil {
6982
return false, err

daemon/sdnotify_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ func TestSdNotify(t *testing.T) {
4141
panic(e)
4242
}
4343

44+
abstractLAddr := net.UnixAddr{
45+
Name: "\x00" + notifySocket,
46+
}
47+
_, e = net.ListenUnixgram("unixgram", &abstractLAddr)
48+
if e != nil {
49+
panic(e)
50+
}
51+
4452
tests := []struct {
4553
unsetEnv bool
4654
envSocket string
@@ -50,10 +58,14 @@ func TestSdNotify(t *testing.T) {
5058
}{
5159
// (true, nil) - notification supported, data has been sent
5260
{false, notifySocket, true, false},
61+
{false, "@" + notifySocket, true, false},
62+
5363
// (false, err) - notification supported, but failure happened
5464
{true, testDir + "/missing.sock", false, true},
5565
// (false, nil) - notification not supported
5666
{true, "", false, false},
67+
68+
// (true, nil) - notification supported, data has been sent
5769
}
5870

5971
for i, tt := range tests {

0 commit comments

Comments
 (0)