summaryrefslogtreecommitdiff
path: root/sd_daemon/notify.go
blob: 1d72cc3db579816884915d7aa8499078f23f24f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2013-2015 Docker, Inc.
// Copyright 2014 CoreOS, Inc.
// Copyright 2015-2016 Luke Shumaker
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sd_daemon

import (
	"bytes"
	"net"
	"os"

	"golang.org/x/sys/unix"
)

// Notify sends a message from process pid to the service manager
// about state changes. If pid <= 0, or if the current process does
// not have priveleges to send messages on behalf of other processes,
// then the message is simply sent from the current process.
//
// If unsetEnv is true, then (regardless of whether the function call
// itself succeeds or not) it will unset the environmental variable
// NOTIFY_SOCKET, which will cause further calls to this function to
// fail.
//
// The state parameter should countain a newline-separated list of
// variable assignments.  See the documentation for sd_notify(3) for
// well-known variable assignments.
// https://www.freedesktop.org/software/systemd/man/sd_notify.html
//
// It is possible to include a set of file descriptors with the
// message.  This is useful for keeping files open across restarts, as
// it enables the service manager to pass those files to the new
// process when it is restarted (see ListenFds).  Note: The service
// manager will only actually store the file descriptors if you
// include "FDSTORE=1" in the state (again, see sd_notify(3) for
// well-known variable assignments).
//
// If the service manager is not listening for notifications from this
// process tree (or this has already been called with unsetEnv=true),
// then ErrDisabled is returned.  If the service manager appears to be
// listening, but there is an error sending the message, then that
// error is returned.  It is generally recommended that you ignore the
// return value: if there is an error, this is function no-op; meaning
// that by calling the function but ignoring the return value, you can
// easily support both service managers that support these
// notifications and those that do not.
func Notify(pid int, unsetEnv bool, state string, files []*os.File) error {
	if unsetEnv {
		defer func() { _ = os.Unsetenv("NOTIFY_SOCKET") }()
	}

	socketAddr := &net.UnixAddr{
		Name: os.Getenv("NOTIFY_SOCKET"),
		Net:  "unixgram",
	}

	if socketAddr.Name == "" {
		return ErrDisabled
	}

	conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
	if err != nil {
		return err
	}
	defer func() { _ = conn.Close() }()

	var cmsgs [][]byte

	if len(files) > 0 {
		fds := make([]int, len(files))
		for i := range files {
			fds[i] = int(files[i].Fd())
		}
		cmsg := unix.UnixRights(fds...)
		cmsgs = append(cmsgs, cmsg)
	}

	havePid := pid > 0 && pid != os.Getpid()
	if havePid {
		cmsg := unix.UnixCredentials(&unix.Ucred{
			Pid: int32(pid),
			Uid: uint32(os.Getuid()),
			Gid: uint32(os.Getgid()),
		})
		cmsgs = append(cmsgs, cmsg)
	}

	// If the 2nd argument is empty, this is equivalent to .Write([]byte(state))
	_, _, err = conn.WriteMsgUnix([]byte(state), bytes.Join(cmsgs, nil), nil)

	if err != nil && havePid {
		// Maybe it failed because we don't have privileges to
		// spoof our pid, retry without spoofing the pid.
		//
		// I'm not too happy that does this silently without
		// notifying the user, but that's what
		// sd_pid_notify_with_fds does.
		cmsgs = cmsgs[:len(cmsgs)-1]
		_, _, err = conn.WriteMsgUnix([]byte(state), bytes.Join(cmsgs, nil), nil)
	}

	return err
}