Skip to content

Commit

Permalink
daemon: SdNotify: add support for abstract unix sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
arianvp committed Aug 23, 2024
1 parent 7d375ec commit 3e1c173
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
23 changes: 18 additions & 5 deletions daemon/sdnotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,29 @@ const (
// (false, err) - notification supported, but failure happened (e.g. error connecting to NOTIFY_SOCKET or while sending data)
// (true, nil) - notification supported, data has been sent
func SdNotify(unsetEnvironment bool, state string) (bool, error) {
socketAddr := &net.UnixAddr{
Name: os.Getenv("NOTIFY_SOCKET"),
Net: "unixgram",
}
notifySocket := os.Getenv("NOTIFY_SOCKET")

// NOTIFY_SOCKET not set
if socketAddr.Name == "" {
if notifySocket == "" {
return false, nil
}

// socket type not supported. We only support unix domain sockets
// but NOTIFY_SOCKET can also use vsock
if notifySocket[0] != '@' && notifySocket[0] != '/' {
return false, nil
}

// abstract unix socket. Start with a 0-byte
if notifySocket[0] == '@' {
notifySocket = "\x00" + notifySocket[1:]
}

socketAddr := &net.UnixAddr{
Name: notifySocket,
Net: "unixgram",
}

if unsetEnvironment {
if err := os.Unsetenv("NOTIFY_SOCKET"); err != nil {
return false, err
Expand Down
12 changes: 12 additions & 0 deletions daemon/sdnotify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func TestSdNotify(t *testing.T) {
panic(e)
}

abstractLAddr := net.UnixAddr{
Name: "\x00" + notifySocket,
}
_, e = net.ListenUnixgram("unixgram", &abstractLAddr)
if e != nil {
panic(e)
}

tests := []struct {
unsetEnv bool
envSocket string
Expand All @@ -50,10 +58,14 @@ func TestSdNotify(t *testing.T) {
}{
// (true, nil) - notification supported, data has been sent
{false, notifySocket, true, false},
{false, "@" + notifySocket, true, false},

// (false, err) - notification supported, but failure happened
{true, testDir + "/missing.sock", false, true},
// (false, nil) - notification not supported
{true, "", false, false},

// (true, nil) - notification supported, data has been sent
}

for i, tt := range tests {
Expand Down

0 comments on commit 3e1c173

Please sign in to comment.