sdnotify.go 2.61 KB
Newer Older
ale's avatar
ale committed
1
// Copyright 2014 Docker, Inc.
ale's avatar
ale committed
2
// Copyright 2015-2018 CoreOS, Inc.
ale's avatar
ale committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
// 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.
//

ale's avatar
ale committed
17 18 19 20 21
// Package daemon provides a Go implementation of the sd_notify protocol.
// It can be used to inform systemd of service start-up completion, watchdog
// events, and other status changes.
//
// https://www.freedesktop.org/software/systemd/man/sd_notify.html#Description
ale's avatar
ale committed
22 23 24 25 26 27 28
package daemon

import (
	"net"
	"os"
)

ale's avatar
ale committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
const (
	// SdNotifyReady tells the service manager that service startup is finished
	// or the service finished loading its configuration.
	SdNotifyReady = "READY=1"

	// SdNotifyStopping tells the service manager that the service is beginning
	// its shutdown.
	SdNotifyStopping = "STOPPING=1"

	// SdNotifyReloading tells the service manager that this service is
	// reloading its configuration. Note that you must call SdNotifyReady when
	// it completed reloading.
	SdNotifyReloading = "RELOADING=1"

	// SdNotifyWatchdog tells the service manager to update the watchdog
	// timestamp for the service.
	SdNotifyWatchdog = "WATCHDOG=1"
)

ale's avatar
ale committed
48 49 50 51 52 53 54 55
// SdNotify sends a message to the init daemon. It is common to ignore the error.
// If `unsetEnvironment` is true, the environment variable `NOTIFY_SOCKET`
// will be unconditionally unset.
//
// It returns one of the following:
// (false, nil) - notification not supported (i.e. NOTIFY_SOCKET is unset)
// (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
ale's avatar
ale committed
56
func SdNotify(unsetEnvironment bool, state string) (bool, error) {
ale's avatar
ale committed
57 58 59 60 61 62 63 64 65 66 67
	socketAddr := &net.UnixAddr{
		Name: os.Getenv("NOTIFY_SOCKET"),
		Net:  "unixgram",
	}

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

	if unsetEnvironment {
ale's avatar
ale committed
68 69 70
		if err := os.Unsetenv("NOTIFY_SOCKET"); err != nil {
			return false, err
		}
ale's avatar
ale committed
71 72 73 74 75 76 77 78 79
	}

	conn, err := net.DialUnix(socketAddr.Net, nil, socketAddr)
	// Error connecting to NOTIFY_SOCKET
	if err != nil {
		return false, err
	}
	defer conn.Close()

ale's avatar
ale committed
80
	if _, err = conn.Write([]byte(state)); err != nil {
ale's avatar
ale committed
81 82 83 84
		return false, err
	}
	return true, nil
}