Debian Codegraph @

systemd/sdnotify.go

raw symbols SCIP
1 // +build !windows,!plan9
2
3 package systemd
4
5 import (
6 "errors"
7 "net"
8 "os"
9 "sync"
10 )
11
12 // Error returned if no systemd notify protocol socket can be found.
13 //
14 // This is an indication that the service is not running under systemd or
15 // Type=notify is not set in the systemd unit file.
16 var ErrNoSocket = errors.New("No socket")
17
18 // sdNotifySocket
19 var sdNotifyMutex sync.Mutex
20 var sdNotifySocket *net.UnixConn
21 var sdNotifyInited bool
22
23 // Send sends a message to the init daemon. It is common to ignore the error.
24 //
25 // This function differs from that in github.com/coreos/go-systemd/daemon in
26 // that that code closes the socket after each call, and so won't work in a
27 // chroot. This function keeps the socket open; so long as it is called at
28 // least once before chrooting, it can continue to be used.
29 //
30 // May return ErrNoSocket.
31 func NotifySend(state string) error {
32 sdNotifyMutex.Lock()
33 defer sdNotifyMutex.Unlock()
34
35 if !sdNotifyInited {
36 sdNotifyInited = true
37
38 socketAddr := &net.UnixAddr{
39 Name: os.Getenv("NOTIFY_SOCKET"),
40 Net: "unixgram",
41 }
42
43 if socketAddr.Name == "" {
44 return ErrNoSocket
45 }
46
47 conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
48 if err != nil {
49 return err
50 }
51
52 sdNotifySocket = conn
53 }
54
55 if sdNotifySocket == nil {
56 return ErrNoSocket
57 }
58
59 _, err := sdNotifySocket.Write([]byte(state))
60 return err
61 }
62
63 // © 2015 CoreOS, Inc. Apache 2.0 License