This commit is contained in:
2026-02-19 10:07:43 +00:00
parent 007438e372
commit 6e637ecf77
1763 changed files with 60820 additions and 279516 deletions

View File

@@ -1,37 +0,0 @@
# CHANGELOG
## v1.3.2
- [Improvement]: updated dependencies, test with Go 1.20.
# v1.3.1
- [Improvement]: bump package netlink to pull in big endian architecture fixes.
# v1.3.0
**This is the first release of package genetlink that only supports Go 1.18+.
Users on older versions of Go must use v1.2.0.**
- [Improvement]: drop support for older versions of Go so we can begin using
modern versions of `x/sys` and other dependencies.
## v1.2.0
**This is the last release of package genetlink that supports Go 1.17 and
below.**
- [Improvement]: pruned Go module dependencies via package `netlink` v1.6.0 and
removing tool version pins.
## v1.1.0
**This is the first release of package genetlink that only supports Go 1.12+.
Users on older versions must use v1.0.0.**
- [Improvement]: modernization of various parts of the code and documentation in
prep for future work.
## v1.0.0
- Initial stable commit.

View File

@@ -1,9 +0,0 @@
# MIT License
Copyright (C) 2016-2022 Matt Layher
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,24 +0,0 @@
# genetlink [![Test Status](https://github.com/mdlayher/genetlink/workflows/Linux%20Test/badge.svg)](https://github.com/mdlayher/genetlink/actions) [![Go Reference](https://pkg.go.dev/badge/github.com/mdlayher/genetlink.svg)](https://pkg.go.dev/github.com/mdlayher/genetlink) [![Go Report Card](https://goreportcard.com/badge/github.com/mdlayher/genetlink)](https://goreportcard.com/report/github.com/mdlayher/genetlink)
Package `genetlink` implements generic netlink interactions and data types.
MIT Licensed.
For more information about how netlink and generic netlink work,
check out my blog series on [Linux, Netlink, and Go](https://mdlayher.com/blog/linux-netlink-and-go-part-1-netlink/).
If you have any questions or you'd like some guidance, please join us on
[Gophers Slack](https://invite.slack.golangbridge.org) in the `#networking`
channel!
## Stability
See the [CHANGELOG](./CHANGELOG.md) file for a description of changes between
releases.
This package has a stable v1 API and any future breaking changes will prompt
the release of a new major version. Features and bug fixes will continue to
occur in the v1.x.x series.
This package only supports the two most recent major versions of Go, mirroring
Go's own release policy. Older versions of Go may lack critical features and bug
fixes which are necessary for this package to function correctly.

View File

@@ -1,220 +0,0 @@
package genetlink
import (
"syscall"
"time"
"github.com/mdlayher/netlink"
"golang.org/x/net/bpf"
)
// Protocol is the netlink protocol constant used to specify generic netlink.
const Protocol = 0x10 // unix.NETLINK_GENERIC
// A Conn is a generic netlink connection. A Conn can be used to send and
// receive generic netlink messages to and from netlink.
//
// A Conn is safe for concurrent use, but to avoid contention in
// high-throughput applications, the caller should almost certainly create a
// pool of Conns and distribute them among workers.
type Conn struct {
// Operating system-specific netlink connection.
c *netlink.Conn
}
// Dial dials a generic netlink connection. Config specifies optional
// configuration for the underlying netlink connection. If config is
// nil, a default configuration will be used.
func Dial(config *netlink.Config) (*Conn, error) {
c, err := netlink.Dial(Protocol, config)
if err != nil {
return nil, err
}
return NewConn(c), nil
}
// NewConn creates a Conn that wraps an existing *netlink.Conn for
// generic netlink communications.
//
// NewConn is primarily useful for tests. Most applications should use
// Dial instead.
func NewConn(c *netlink.Conn) *Conn {
return &Conn{c: c}
}
// Close closes the connection and unblocks any pending read operations.
func (c *Conn) Close() error {
return c.c.Close()
}
// GetFamily retrieves a generic netlink family with the specified name.
//
// If the family does not exist, the error value can be checked using
// `errors.Is(err, os.ErrNotExist)`.
func (c *Conn) GetFamily(name string) (Family, error) {
return c.getFamily(name)
}
// ListFamilies retrieves all registered generic netlink families.
func (c *Conn) ListFamilies() ([]Family, error) {
return c.listFamilies()
}
// JoinGroup joins a netlink multicast group by its ID.
func (c *Conn) JoinGroup(group uint32) error {
return c.c.JoinGroup(group)
}
// LeaveGroup leaves a netlink multicast group by its ID.
func (c *Conn) LeaveGroup(group uint32) error {
return c.c.LeaveGroup(group)
}
// SetBPF attaches an assembled BPF program to a Conn.
func (c *Conn) SetBPF(filter []bpf.RawInstruction) error {
return c.c.SetBPF(filter)
}
// RemoveBPF removes a BPF filter from a Conn.
func (c *Conn) RemoveBPF() error {
return c.c.RemoveBPF()
}
// SetOption enables or disables a netlink socket option for the Conn.
func (c *Conn) SetOption(option netlink.ConnOption, enable bool) error {
return c.c.SetOption(option, enable)
}
// SetReadBuffer sets the size of the operating system's receive buffer
// associated with the Conn.
func (c *Conn) SetReadBuffer(bytes int) error {
return c.c.SetReadBuffer(bytes)
}
// SetWriteBuffer sets the size of the operating system's transmit buffer
// associated with the Conn.
func (c *Conn) SetWriteBuffer(bytes int) error {
return c.c.SetWriteBuffer(bytes)
}
// SyscallConn returns a raw network connection. This implements the
// syscall.Conn interface.
//
// SyscallConn is intended for advanced use cases, such as getting and setting
// arbitrary socket options using the netlink socket's file descriptor.
//
// Once invoked, it is the caller's responsibility to ensure that operations
// performed using Conn and the syscall.RawConn do not conflict with
// each other.
func (c *Conn) SyscallConn() (syscall.RawConn, error) {
return c.c.SyscallConn()
}
// SetDeadline sets the read and write deadlines associated with the connection.
func (c *Conn) SetDeadline(t time.Time) error {
return c.c.SetDeadline(t)
}
// SetReadDeadline sets the read deadline associated with the connection.
func (c *Conn) SetReadDeadline(t time.Time) error {
return c.c.SetReadDeadline(t)
}
// SetWriteDeadline sets the write deadline associated with the connection.
func (c *Conn) SetWriteDeadline(t time.Time) error {
return c.c.SetWriteDeadline(t)
}
// Send sends a single Message to netlink, wrapping it in a netlink.Message
// using the specified generic netlink family and flags. On success, Send
// returns a copy of the netlink.Message with all parameters populated, for
// later validation.
func (c *Conn) Send(m Message, family uint16, flags netlink.HeaderFlags) (netlink.Message, error) {
nm, err := packMessage(m, family, flags)
if err != nil {
return netlink.Message{}, err
}
reqnm, err := c.c.Send(nm)
if err != nil {
return netlink.Message{}, err
}
return reqnm, nil
}
// Receive receives one or more Messages from netlink. The netlink.Messages
// used to wrap each Message are available for later validation.
func (c *Conn) Receive() ([]Message, []netlink.Message, error) {
msgs, err := c.c.Receive()
if err != nil {
return nil, nil, err
}
gmsgs, err := unpackMessages(msgs)
if err != nil {
return nil, nil, err
}
return gmsgs, msgs, nil
}
// Execute sends a single Message to netlink using Send, receives one or more
// replies using Receive, and then checks the validity of the replies against
// the request using netlink.Validate.
//
// Execute acquires a lock for the duration of the function call which blocks
// concurrent calls to Send and Receive, in order to ensure consistency between
// generic netlink request/reply messages.
//
// See the documentation of Send, Receive, and netlink.Validate for details
// about each function.
func (c *Conn) Execute(m Message, family uint16, flags netlink.HeaderFlags) ([]Message, error) {
nm, err := packMessage(m, family, flags)
if err != nil {
return nil, err
}
// Locking behavior handled by netlink.Conn.Execute.
msgs, err := c.c.Execute(nm)
if err != nil {
return nil, err
}
return unpackMessages(msgs)
}
// packMessage packs a generic netlink Message into a netlink.Message with the
// appropriate generic netlink family and netlink flags.
func packMessage(m Message, family uint16, flags netlink.HeaderFlags) (netlink.Message, error) {
nm := netlink.Message{
Header: netlink.Header{
Type: netlink.HeaderType(family),
Flags: flags,
},
}
mb, err := m.MarshalBinary()
if err != nil {
return netlink.Message{}, err
}
nm.Data = mb
return nm, nil
}
// unpackMessages unpacks generic netlink Messages from a slice of netlink.Messages.
func unpackMessages(msgs []netlink.Message) ([]Message, error) {
gmsgs := make([]Message, 0, len(msgs))
for _, nm := range msgs {
var gm Message
if err := (&gm).UnmarshalBinary(nm.Data); err != nil {
return nil, err
}
gmsgs = append(gmsgs, gm)
}
return gmsgs, nil
}

View File

@@ -1,6 +0,0 @@
// Package genetlink implements generic netlink interactions and data types.
//
// If you have any questions or you'd like some guidance, please join us on
// Gophers Slack (https://invite.slack.golangbridge.org) in the #networking
// channel!
package genetlink

View File

@@ -1,17 +0,0 @@
package genetlink
// A Family is a generic netlink family.
type Family struct {
ID uint16
Version uint8
Name string
Groups []MulticastGroup
}
// A MulticastGroup is a generic netlink multicast group, which can be joined
// for notifications from generic netlink families when specific events take
// place.
type MulticastGroup struct {
ID uint32
Name string
}

View File

@@ -1,150 +0,0 @@
//go:build linux
// +build linux
package genetlink
import (
"errors"
"fmt"
"math"
"github.com/mdlayher/netlink"
"github.com/mdlayher/netlink/nlenc"
"golang.org/x/sys/unix"
)
// errInvalidFamilyVersion is returned when a family's version is greater
// than an 8-bit integer.
var errInvalidFamilyVersion = errors.New("invalid family version attribute")
// getFamily retrieves a generic netlink family with the specified name.
func (c *Conn) getFamily(name string) (Family, error) {
b, err := netlink.MarshalAttributes([]netlink.Attribute{{
Type: unix.CTRL_ATTR_FAMILY_NAME,
Data: nlenc.Bytes(name),
}})
if err != nil {
return Family{}, err
}
req := Message{
Header: Header{
Command: unix.CTRL_CMD_GETFAMILY,
// TODO(mdlayher): grab nlctrl version?
Version: 1,
},
Data: b,
}
msgs, err := c.Execute(req, unix.GENL_ID_CTRL, netlink.Request)
if err != nil {
return Family{}, err
}
// TODO(mdlayher): consider interpreting generic netlink header values
families, err := buildFamilies(msgs)
if err != nil {
return Family{}, err
}
if len(families) != 1 {
// If this were to ever happen, netlink must be in a state where
// its answers cannot be trusted
panic(fmt.Sprintf("netlink returned multiple families for name: %q", name))
}
return families[0], nil
}
// listFamilies retrieves all registered generic netlink families.
func (c *Conn) listFamilies() ([]Family, error) {
req := Message{
Header: Header{
Command: unix.CTRL_CMD_GETFAMILY,
// TODO(mdlayher): grab nlctrl version?
Version: 1,
},
}
msgs, err := c.Execute(req, unix.GENL_ID_CTRL, netlink.Request|netlink.Dump)
if err != nil {
return nil, err
}
return buildFamilies(msgs)
}
// buildFamilies builds a slice of Families by parsing attributes from the
// input Messages.
func buildFamilies(msgs []Message) ([]Family, error) {
families := make([]Family, 0, len(msgs))
for _, m := range msgs {
f, err := parseFamily(m.Data)
if err != nil {
return nil, err
}
families = append(families, f)
}
return families, nil
}
// parseFamily decodes netlink attributes into a Family.
func parseFamily(b []byte) (Family, error) {
ad, err := netlink.NewAttributeDecoder(b)
if err != nil {
return Family{}, err
}
var f Family
for ad.Next() {
switch ad.Type() {
case unix.CTRL_ATTR_FAMILY_ID:
f.ID = ad.Uint16()
case unix.CTRL_ATTR_FAMILY_NAME:
f.Name = ad.String()
case unix.CTRL_ATTR_VERSION:
v := ad.Uint32()
if v > math.MaxUint8 {
return Family{}, errInvalidFamilyVersion
}
f.Version = uint8(v)
case unix.CTRL_ATTR_MCAST_GROUPS:
ad.Nested(parseMulticastGroups(&f.Groups))
}
}
if err := ad.Err(); err != nil {
return Family{}, err
}
return f, nil
}
// parseMulticastGroups parses an array of multicast group nested attributes
// into a slice of MulticastGroups.
func parseMulticastGroups(groups *[]MulticastGroup) func(*netlink.AttributeDecoder) error {
return func(ad *netlink.AttributeDecoder) error {
*groups = make([]MulticastGroup, 0, ad.Len())
for ad.Next() {
ad.Nested(func(nad *netlink.AttributeDecoder) error {
var g MulticastGroup
for nad.Next() {
switch nad.Type() {
case unix.CTRL_ATTR_MCAST_GRP_NAME:
g.Name = nad.String()
case unix.CTRL_ATTR_MCAST_GRP_ID:
g.ID = nad.Uint32()
}
}
*groups = append(*groups, g)
return nil
})
}
return nil
}
}

View File

@@ -1,24 +0,0 @@
//go:build !linux
// +build !linux
package genetlink
import (
"fmt"
"runtime"
)
// errUnimplemented is returned by all functions on platforms that
// cannot make use of generic netlink.
var errUnimplemented = fmt.Errorf("generic netlink not implemented on %s/%s",
runtime.GOOS, runtime.GOARCH)
// getFamily always returns an error.
func (c *Conn) getFamily(name string) (Family, error) {
return Family{}, errUnimplemented
}
// listFamilies always returns an error.
func (c *Conn) listFamilies() ([]Family, error) {
return nil, errUnimplemented
}

View File

@@ -1,21 +0,0 @@
//go:build gofuzz
// +build gofuzz
package genetlink
func Fuzz(data []byte) int {
return fuzzMessage(data)
}
func fuzzMessage(data []byte) int {
var m Message
if err := (&m).UnmarshalBinary(data); err != nil {
return 0
}
if _, err := m.MarshalBinary(); err != nil {
panic(err)
}
return 1
}

View File

@@ -1,61 +0,0 @@
package genetlink
import "errors"
// errInvalidMessage is returned when a Message is malformed.
var errInvalidMessage = errors.New("generic netlink message is invalid or too short")
// A Header is a generic netlink header. A Header is sent and received with
// each generic netlink message to indicate metadata regarding a Message.
type Header struct {
// Command specifies a command to issue to netlink.
Command uint8
// Version specifies the version of a command to use.
Version uint8
}
// headerLen is the length of a Header.
const headerLen = 4 // unix.GENL_HDRLEN
// A Message is a generic netlink message. It contains a Header and an
// arbitrary byte payload, which may be decoded using information from the
// Header.
//
// Data is encoded using the native endianness of the host system. Use
// the netlink.AttributeDecoder and netlink.AttributeEncoder types to decode
// and encode data.
type Message struct {
Header Header
Data []byte
}
// MarshalBinary marshals a Message into a byte slice.
func (m Message) MarshalBinary() ([]byte, error) {
b := make([]byte, headerLen)
b[0] = m.Header.Command
b[1] = m.Header.Version
// b[2] and b[3] are padding bytes and set to zero
return append(b, m.Data...), nil
}
// UnmarshalBinary unmarshals the contents of a byte slice into a Message.
func (m *Message) UnmarshalBinary(b []byte) error {
if len(b) < headerLen {
return errInvalidMessage
}
// Don't allow reserved pad bytes to be set
if b[2] != 0 || b[3] != 0 {
return errInvalidMessage
}
m.Header.Command = b[0]
m.Header.Version = b[1]
m.Data = b[4:]
return nil
}

View File

@@ -1,8 +0,0 @@
//go:build plan9 || windows
// +build plan9 windows
package nltest
func isSyscallError(_ error) bool {
return false
}

View File

@@ -1,11 +0,0 @@
//go:build !plan9 && !windows
// +build !plan9,!windows
package nltest
import "golang.org/x/sys/unix"
func isSyscallError(err error) bool {
_, ok := err.(unix.Errno)
return ok
}

View File

@@ -1,207 +0,0 @@
// Package nltest provides utilities for netlink testing.
package nltest
import (
"fmt"
"io"
"os"
"github.com/mdlayher/netlink"
"github.com/mdlayher/netlink/nlenc"
)
// PID is the netlink header PID value assigned by nltest.
const PID = 1
// MustMarshalAttributes marshals a slice of netlink.Attributes to their binary
// format, but panics if any errors occur.
func MustMarshalAttributes(attrs []netlink.Attribute) []byte {
b, err := netlink.MarshalAttributes(attrs)
if err != nil {
panic(fmt.Sprintf("failed to marshal attributes to binary: %v", err))
}
return b
}
// Multipart sends a slice of netlink.Messages to the caller as a
// netlink multi-part message. If less than two messages are present,
// the messages are not altered.
func Multipart(msgs []netlink.Message) ([]netlink.Message, error) {
if len(msgs) < 2 {
return msgs, nil
}
for i := range msgs {
// Last message has header type "done" in addition to multi-part flag.
if i == len(msgs)-1 {
msgs[i].Header.Type = netlink.Done
}
msgs[i].Header.Flags |= netlink.Multi
}
return msgs, nil
}
// Error returns a netlink error to the caller with the specified error
// number, in the body of the specified request message.
func Error(number int, reqs []netlink.Message) ([]netlink.Message, error) {
req := reqs[0]
req.Header.Length += 4
req.Header.Type = netlink.Error
errno := -1 * int32(number)
req.Data = append(nlenc.Int32Bytes(errno), req.Data...)
return []netlink.Message{req}, nil
}
// A Func is a function that can be used to test netlink.Conn interactions.
// The function can choose to return zero or more netlink messages, or an
// error if needed.
//
// For a netlink request/response interaction, a request req is populated by
// netlink.Conn.Send and passed to the function.
//
// For multicast interactions, an empty request req is passed to the function
// when netlink.Conn.Receive is called.
//
// If a Func returns an error, the error will be returned as-is to the caller.
// If no messages and io.EOF are returned, no messages and no error will be
// returned to the caller, simulating a multi-part message with no data.
type Func func(req []netlink.Message) ([]netlink.Message, error)
// Dial sets up a netlink.Conn for testing using the specified Func. All requests
// sent from the connection will be passed to the Func. The connection should be
// closed as usual when it is no longer needed.
func Dial(fn Func) *netlink.Conn {
sock := &socket{
fn: fn,
}
return netlink.NewConn(sock, PID)
}
// CheckRequest returns a Func that verifies that each message in an incoming
// request has the specified netlink header type and flags in the same slice
// position index, and then passes the request through to fn.
//
// The length of the types and flags slices must match the number of requests
// passed to the returned Func, or CheckRequest will panic.
//
// As an example:
// - types[0] and flags[0] will be checked against reqs[0]
// - types[1] and flags[1] will be checked against reqs[1]
// - ... and so on
//
// If an element of types or flags is set to the zero value, that check will
// be skipped for the request message that occurs at the same index.
//
// As an example, if types[0] is 0 and reqs[0].Header.Type is 1, the check will
// succeed because types[0] was not specified.
func CheckRequest(types []netlink.HeaderType, flags []netlink.HeaderFlags, fn Func) Func {
if len(types) != len(flags) {
panicf("nltest: CheckRequest called with mismatched types and flags slice lengths: %d != %d",
len(types), len(flags))
}
return func(req []netlink.Message) ([]netlink.Message, error) {
if len(types) != len(req) {
panicf("nltest: CheckRequest function invoked types/flags and request message slice lengths: %d != %d",
len(types), len(req))
}
for i := range req {
if want, got := types[i], req[i].Header.Type; types[i] != 0 && want != got {
return nil, fmt.Errorf("nltest: unexpected netlink header type: %s, want: %s", got, want)
}
if want, got := flags[i], req[i].Header.Flags; flags[i] != 0 && want != got {
return nil, fmt.Errorf("nltest: unexpected netlink header flags: %s, want: %s", got, want)
}
}
return fn(req)
}
}
// A socket is a netlink.Socket used for testing.
type socket struct {
fn Func
msgs []netlink.Message
err error
}
func (c *socket) Close() error { return nil }
func (c *socket) SendMessages(messages []netlink.Message) error {
msgs, err := c.fn(messages)
c.msgs = append(c.msgs, msgs...)
c.err = err
return nil
}
func (c *socket) Send(m netlink.Message) error {
c.msgs, c.err = c.fn([]netlink.Message{m})
return nil
}
func (c *socket) Receive() ([]netlink.Message, error) {
// No messages set by Send means that we are emulating a
// multicast response or an error occurred.
if len(c.msgs) == 0 {
switch c.err {
case nil:
// No error, simulate multicast, but also return EOF to simulate
// no replies if needed.
msgs, err := c.fn(nil)
if err == io.EOF {
err = nil
}
return msgs, err
case io.EOF:
// EOF, simulate no replies in multi-part message.
return nil, nil
}
// If the error is a system call error, wrap it in os.NewSyscallError
// to simulate what the Linux netlink.Conn does.
if isSyscallError(c.err) {
return nil, os.NewSyscallError("recvmsg", c.err)
}
// Some generic error occurred and should be passed to the caller.
return nil, c.err
}
// Detect multi-part messages.
var multi bool
for _, m := range c.msgs {
if m.Header.Flags&netlink.Multi != 0 && m.Header.Type != netlink.Done {
multi = true
}
}
// When a multi-part message is detected, return all messages except for the
// final "multi-part done", so that a second call to Receive from netlink.Conn
// will drain that message.
if multi {
last := c.msgs[len(c.msgs)-1]
ret := c.msgs[:len(c.msgs)-1]
c.msgs = []netlink.Message{last}
return ret, c.err
}
msgs, err := c.msgs, c.err
c.msgs, c.err = nil, nil
return msgs, err
}
func panicf(format string, a ...interface{}) {
panic(fmt.Sprintf(format, a...))
}

View File

@@ -1,9 +0,0 @@
# MIT License
Copyright (C) 2020-2022 Matt Layher
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,4 +0,0 @@
# sdnotify [![Test Status](https://github.com/mdlayher/sdnotify/workflows/Test/badge.svg)](https://github.com/mdlayher/sdnotify/actions) [![Go Reference](https://pkg.go.dev/badge/github.com/mdlayher/sdnotify.svg)](https://pkg.go.dev/github.com/mdlayher/sdnotify) [![Go Report Card](https://goreportcard.com/badge/github.com/mdlayher/sdnotify)](https://goreportcard.com/report/github.com/mdlayher/sdnotify)
Package `sdnotify` implements systemd readiness notifications as described in
https://www.freedesktop.org/software/systemd/man/sd_notify.html. MIT Licensed.

View File

@@ -1,96 +0,0 @@
// Package sdnotify implements systemd readiness notifications as described in
// https://www.freedesktop.org/software/systemd/man/sd_notify.html.
package sdnotify
import (
"fmt"
"io"
"net"
"os"
"strings"
)
// Socket is the predefined systemd notification socket environment variable.
const Socket = "NOTIFY_SOCKET"
// Common notification values. For a description of each, see:
// https://www.freedesktop.org/software/systemd/man/sd_notify.html#Description.
const (
Ready = "READY=1"
Reloading = "RELOADING=1"
Stopping = "STOPPING=1"
)
// Statusf creates a formatted STATUS notification with the input format string
// and values.
func Statusf(format string, v ...interface{}) string {
return fmt.Sprintf("STATUS=%s", fmt.Sprintf(format, v...))
}
// A Notifier can notify systemd of service status and readiness. Any methods
// called on a nil Notifier will result in a no-op, allowing graceful
// functionality degradation when a Go program is not running under systemd
// supervision.
type Notifier struct{ wc io.WriteCloser }
// New creates a Notifier which sends notifications to the UNIX socket specified
// by the NOTIFY_SOCKET environment variable. See Open for more details.
func New() (*Notifier, error) {
s := os.Getenv(Socket)
if s == "" {
// Don't bother stat'ing an empty socket, just return now.
return nil, os.ErrNotExist
}
return Open(s)
}
// Open creates a Notifier which sends notifications to the UNIX socket
// specified by sock.
//
// If sock does not exist or is unset (meaning the service is not running under
// systemd supervision, or is not using systemd unit Type=notify), Open will
// return an error which can be checked with 'errors.Is(err, os.ErrNotExist)'.
// Calling any of the resulting nil Notifier's methods will result in a no-op.
func Open(sock string) (*Notifier, error) {
// Don't stat Linux abstract namespace sockets, as would be created with a
// net.ListenPacket with no path.
if !strings.HasPrefix(sock, "@") {
if _, err := os.Stat(sock); err != nil {
return nil, fmt.Errorf("failed to stat notify socket: %w", err)
}
}
c, err := net.Dial("unixgram", sock)
if err != nil {
return nil, err
}
return &Notifier{wc: c}, nil
}
// Notify sends zero or more notifications to systemd. See the package constants
// for a list of common notifications or use the Statusf function to create a
// STATUS notification.
//
// For advanced use cases, see:
// https://www.freedesktop.org/software/systemd/man/sd_notify.html#Description.
//
// If n is nil or no strings are specified, Notify is a no-op.
func (n *Notifier) Notify(s ...string) error {
if n == nil || len(s) == 0 {
return nil
}
_, err := io.WriteString(n.wc, strings.Join(s, "\n"))
return err
}
// Close closes the Notifier's socket. If n is nil, Close is a no-op.
func (n *Notifier) Close() error {
if n == nil {
return nil
}
return n.wc.Close()
}