Skip to content

Commit d6c05a1

Browse files
authored
Merge pull request coreos#185 from kinvolk/alessandro/sdnotify
daemon: improve SdNotify errors signaling
2 parents d403902 + 0d7fccc commit d6c05a1

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

daemon/sdnotify.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,37 @@
22
package daemon
33

44
import (
5-
"errors"
65
"net"
76
"os"
87
)
98

10-
var SdNotifyNoSocket = errors.New("No socket")
11-
129
// SdNotify sends a message to the init daemon. It is common to ignore the error.
13-
func SdNotify(state string) error {
10+
// It returns one of the following:
11+
// (false, nil) - notification not supported (i.e. NOTIFY_SOCKET is unset)
12+
// (false, err) - notification supported, but failure happened (e.g. error connecting to NOTIFY_SOCKET or while sending data)
13+
// (true, nil) - notification supported, data has been sent
14+
func SdNotify(state string) (sent bool, err error) {
1415
socketAddr := &net.UnixAddr{
1516
Name: os.Getenv("NOTIFY_SOCKET"),
1617
Net: "unixgram",
1718
}
1819

20+
// NOTIFY_SOCKET not set
1921
if socketAddr.Name == "" {
20-
return SdNotifyNoSocket
22+
return false, nil
2123
}
2224

2325
conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
26+
// Error connecting to NOTIFY_SOCKET
2427
if err != nil {
25-
return err
28+
return false, err
2629
}
2730
defer conn.Close()
2831

2932
_, err = conn.Write([]byte(state))
30-
return err
33+
// Error sending the message
34+
if err != nil {
35+
return false, err
36+
}
37+
return true, nil
3138
}

daemon/sdnotify_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2016 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+
15+
package daemon
16+
17+
import (
18+
"io/ioutil"
19+
"net"
20+
"os"
21+
"testing"
22+
)
23+
24+
// TestSdNotify
25+
func TestSdNotify(t *testing.T) {
26+
notificationSupportedDataSent := "Notification supported, data sent"
27+
notificationSupportedFailure := "Notification supported, but failure happened"
28+
notificationNotSupported := "Notification not supported"
29+
30+
testDir, e := ioutil.TempDir("/tmp/", "test-")
31+
if e != nil {
32+
panic(e)
33+
}
34+
defer os.RemoveAll(testDir)
35+
36+
notifySocket := testDir + "/notify-socket.sock"
37+
laddr := net.UnixAddr{
38+
Name: notifySocket,
39+
Net: "unixgram",
40+
}
41+
_, e = net.ListenUnixgram("unixgram", &laddr)
42+
if e != nil {
43+
panic(e)
44+
}
45+
46+
// (true, nil) - notification supported, data has been sent
47+
e = os.Setenv("NOTIFY_SOCKET", notifySocket)
48+
if e != nil {
49+
panic(e)
50+
}
51+
sent, err := SdNotify(notificationSupportedDataSent)
52+
if !sent || err != nil {
53+
t.Errorf("TEST: %s FAILED", notificationSupportedDataSent)
54+
}
55+
56+
// (false, err) - notification supported, but failure happened
57+
e = os.Setenv("NOTIFY_SOCKET", testDir+"/not-exist.sock")
58+
if e != nil {
59+
panic(e)
60+
}
61+
sent, err = SdNotify(notificationSupportedFailure)
62+
if sent && err == nil {
63+
t.Errorf("TEST: %s FAILED", notificationSupportedFailure)
64+
}
65+
66+
// (false, nil) - notification not supported
67+
e = os.Unsetenv("NOTIFY_SOCKET")
68+
if e != nil {
69+
panic(e)
70+
}
71+
sent, err = SdNotify(notificationNotSupported)
72+
if sent || err != nil {
73+
t.Errorf("TEST: %s FAILED", notificationNotSupported)
74+
}
75+
}

test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if [ -z "$GOPATH" ]; then
2727
go get -u github.com/coreos/pkg/dlopen
2828
fi
2929

30-
TESTABLE="activation journal login1 machine1 unit"
30+
TESTABLE="activation daemon journal login1 machine1 unit"
3131
FORMATTABLE="$TESTABLE sdjournal dbus"
3232
if [ -e "/run/systemd/system/" ]; then
3333
TESTABLE="${TESTABLE} sdjournal"

0 commit comments

Comments
 (0)