Go bindings to systemd socket activation, journal, D-Bus, and unit files

go-systemd

godoc minimum golang 1.12

Go bindings to systemd. The project has several packages:

  • activation - for writing and using socket activation from Go
  • daemon - for notifying systemd of service status changes
  • dbus - for starting/stopping/inspecting running services and units
  • journal - for writing to systemd's logging service, journald
  • sdjournal - for reading from journald by wrapping its C API
  • login1 - for integration with the systemd logind API
  • machine1 - for registering machines/containers with systemd
  • unit - for (de)serialization and comparison of unit files

Socket Activation

An example HTTP server using socket activation can be quickly set up by following this README on a Linux machine running systemd:

https://github.com/coreos/go-systemd/tree/main/examples/activation/httpserver

systemd Service Notification

The daemon package is an implementation of the sd_notify protocol. It can be used to inform systemd of service start-up completion, watchdog events, and other status changes.

D-Bus

The dbus package connects to the systemd D-Bus API and lets you start, stop and introspect systemd units. API documentation is available online.

Debugging

Create /etc/dbus-1/system-local.conf that looks like this:

<!DOCTYPE busconfig PUBLIC
"-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
    <policy user="root">
        <allow eavesdrop="true"/>
        <allow eavesdrop="true" send_destination="*"/>
    </policy>
</busconfig>

Journal

Writing to the Journal

Using the pure-Go journal package you can submit journal entries directly to systemd's journal, taking advantage of features like indexed key/value pairs for each log entry.

Reading from the Journal

The sdjournal package provides read access to the journal by wrapping around journald's native C API; consequently it requires cgo and the journal headers to be available.

logind

The login1 package provides functions to integrate with the systemd logind API.

machined

The machine1 package allows interaction with the systemd machined D-Bus API.

Units

The unit package provides various functions for working with systemd unit files.

Owner
CoreOS
Key components to secure, simplify and automate your container infrastructure
CoreOS
Comments
  • daemon: improve SdNotify errors signaling

    daemon: improve SdNotify errors signaling

    This patch makes SdNotify return value compliant to the sd_notify() documentation. See sd_notify(3) man page.

    Fixes: https://github.com/coreos/go-systemd/issues/184

  • dbus: Fix API break from godbus

    dbus: Fix API break from godbus

    Something in godbus seems to have broken API, thus breaking builds including go get. This patches fixes those changes. I don't know any background about this change, so if anyone has information, I'd sure like to know! Thanks

  • sdjournal: dlopen libsystemd instead of linking against it

    sdjournal: dlopen libsystemd instead of linking against it

    This allows not having to link users of the library to libsystemd, detect at runtime if it exists or not and react accordingly.

    Also, refactor dlopen code.

  • sdjournal: add GetEntry method to retrieve all fields

    sdjournal: add GetEntry method to retrieve all fields

    Implements the same semantics as the JOURNAL_FOREACH_DATA_RETVAL macro in journal-internal.h and produces a *JournalEntry with every field with its corresponding data in a map[string]string. Also it includes address fields like realtime (__REALTIME_TIMESTAMP), monotonic (__MONOTONIC_TIMESTAMP and cursor (__CURSOR).

    It mimics more or less journalctl -o json.

    Fixes #142 Supersedes #136

  • dbus: add support for new ListUnits* methods

    dbus: add support for new ListUnits* methods

    …d support)

    is a part of this change: https://github.com/endocode/systemd/commit/5b4452a643d924015ed25972d071ad8802aad29c and this pull request: https://github.com/coreos/fleet/pull/1564

  • Add go module support

    Add go module support

    This PR adds a go.mod and go.sum to the project which contains the latest dependencies. It would be awesome if we could tag a release according to semver after this PR got merged, WDYT? :innocent:

  • activation: close files after creating listeners

    activation: close files after creating listeners

    This is an adaptation of #215 to ensure tests don't fail and extend the behaviour to PacketConns(). A bit of test coverage is lost, but just testing if variable environments are not unset in the simple case doesn't seem worth adding more code.

    Fix #215

  • journal: use a connection-less socket

    journal: use a connection-less socket

    When a non temporary error is detected while sending a message, the connection should be closed to attempt a reconnection, otherwise it won't be possible to send new messages to journald anymore.

  • sdjournal: fix return values type

    sdjournal: fix return values type

    fix inconsistency in return types for journal functions: NextSkip, Previous, PreviousSkip. Currently they will return (uint64, error), however according to sd-journal.h it should be (int, error)

    Journal.Next returns (int, error) Journal.Previous returns (uint64, error)

    https://github.com/systemd/systemd/blob/master/src/systemd/sd-journal.h#L98

  • dbus: add PropPids to set initial pids of transient scope units

    dbus: add PropPids to set initial pids of transient scope units

    Should there also be a usage example on how to create a transient scope unit instead of a transient service unit?

    Feel free to use https://github.com/mgit-at/systemd-runas/blob/master/main.go as a example on how to use the call with transient scopes.

  • journal: use a connection-less socket

    journal: use a connection-less socket

    This gets rid of the connection ownership problem by using a non-connected Unix datagram socket and an explicit destination to send each message. It does not impact the public API, but it requires a minimum-go version bump due to https://go-review.googlesource.com/c/go/+/45872 (golang 1.10+).

    Fixes https://github.com/coreos/go-systemd/issues/5 Fixes https://github.com/coreos/go-systemd/issues/233 Fixes https://github.com/coreos/go-systemd/issues/275


    Note: as 1.11 got released, all upstream supported versions of the Go runtime have a fixed version of WriteMsgUnix that this PR uses. However, this means that the journal package won't work on older/buggy runtimes (<= 1.9).

  • sdjournal: please add support for journal namespaces

    sdjournal: please add support for journal namespaces

    I am using sdjournal indirectly with the filebeat journald input, and was wondering if reading from a non-default journald namespace is supported directly. I know that I can point filebeat to the namespace's systemd.journal file residing in /var/lib/journal/machine-id.namespace, but this requires knowledge of the machine-id for each individual server. I solved the problem for our system by symlinking the machine-id.namespace directory to a machine-independent path, but this still feels hacked together.

    Looking at the code it doesn't seem like, so I just wanted to confirm.

    Thanks a lot and all the best!

  • Socket activation logic lacks error checking

    Socket activation logic lacks error checking

    In https://github.com/coreos/go-systemd/blob/f5f8d59a116b1ca501dd87cdb28f6ce89ccf58dc/activation/files_unix.go#L66 the return value of os.NewFile should be checked before files is changed. It can fail if the environment variables don't match the file descriptor table, e.g. LISTEN_FDS is much larger than the number of open files. The current logic would force consumers of the interface to check for this condition or run into mysterious panics in a completely unrelated code. If os.NewFile fails, just returning nil might be reasonable, even if it means close-on-exec is set on some file descriptors.

  • Module for reading service credentials

    Module for reading service credentials

    I would like a module that can read credentials when the current program is running as a systemd service.

    This should be fairly simple and a small amount of code. I'm willing to send a PR at some point.

  • bug(dbus): New[User|System]ConnectionContext doesn't respect incoming context.Context

    bug(dbus): New[User|System]ConnectionContext doesn't respect incoming context.Context

    Hi Community, I a recent event, I just discovered that the two functions to establish dbus connection doesn't handle context deadline event

    func TestGoSystemd(t *testing.T) {
    	ctx, _ := context.WithTimeout(context.Background(), 0)
    	_, err := dbus.NewSystemConnectionContext(ctx)
    	if err != nil {
    		t.Fatal(err)
    	}
    }
    

    this throws:

    === RUN   TestGoSystemd
        dbus_test.go:13: read unix @->/run/dbus/system_bus_socket: use of closed network connection
    --- FAIL: TestGoSystemd (0.00s)
    

    Same for

    ctx, _ := context.WithTimeout(context.Background(), 0)
    _, err := dbus.NewUserConnectionContext(ctx)
    // : read unix @->/run/user/1000/bus: use of closed network connection
    

    And when integrated with large software, it makes debugging a lot harder. I think here it's best to use standard context.DeadlineExceeded error when such a scenario occurs. Thank you!

Git extension for versioning large files

Git Large File Storage Git LFS is a command line extension and specification for managing large files with Git. The client is written in Go, with pre-

Jan 5, 2023
JuiceFS is a distributed POSIX file system built on top of Redis and S3.
JuiceFS is a distributed POSIX file system built on top of Redis and S3.

JuiceFS is a high-performance POSIX file system released under GNU Affero General Public License v3.0. It is specially optimized for the cloud-native

Jan 1, 2023
Go implementation of systemd Journal's native API for logging

journald Package journald offers Go implementation of systemd Journal's native API for logging. Key features are: based on a connection-less socket wo

Dec 23, 2022
Keygen SDK for Go. Integrate license activation and automatic updates for Go binaries.

Keygen Go SDK Package keygen allows Go programs to license and remotely update themselves using the keygen.sh service. Usage keygen.Validate(fingerpri

Dec 18, 2022
Simple Relay between a Unix socket and a TCP socket, and vice versa.
Simple Relay between a Unix socket and a TCP socket, and vice versa.

Simple TCP <-> Unix Relay simpletcpunixrelay is a program which exposes a TCP endpoint as a Unix socket and vice versa. Usecase Let's say you are runn

Nov 23, 2022
Glue - Robust Go and Javascript Socket Library (Alternative to Socket.io)

Glue - Robust Go and Javascript Socket Library Glue is a real-time bidirectional socket library. It is a clean, robust and efficient alternative to so

Nov 25, 2022
Glue - Robust Go and Javascript Socket Library (Alternative to Socket.io)

Glue - Robust Go and Javascript Socket Library Glue is a real-time bidirectional socket library. It is a clean, robust and efficient alternative to so

Nov 25, 2022
go-socket.io is library an implementation of Socket.IO in Golang

go-socket.io is library an implementation of Socket.IO in Golang, which is a realtime application framework.

Jan 5, 2023
GOWS is GoLang web-socket module Provides you with ease of handling web socket connections with a few lines

GOWS GOWS is GoLang web-socket module Provides you with ease of handling web socket connections with a few lines, it supports multi-connection on one

Apr 4, 2022
📔 Journal helps you manage multiple journals with ease from the comfort of your terminal, web browser or API client.
📔 Journal helps you manage multiple journals with ease from the comfort of your terminal, web browser or API client.

Journal helps you manage multiple journals with ease from the comfort of your terminal, web browser or API client. You can import/export journals as horcruxes and set simple customizations for layout, theme, and keybindings.

Sep 14, 2022
Go bindings for D-Bus

Documentation Look at the API on GoPkgDoc. Installation goinstall github.com/norisatir/go-dbus Usage Methods Methods is obtained with meth, err := co

Oct 20, 2019
Native Go bindings for D-Bus

dbus dbus is a simple library that implements native Go client bindings for the D-Bus message bus system. Features Complete native implementation of t

Dec 30, 2022
Native Go bindings for D-Bus

go.dbus go.dbus is a simple library that implements native Go client bindings for the D-Bus message bus system. Features Complete native implementatio

Nov 20, 2022
Split multiple Kubernetes files into smaller files with ease. Split multi-YAML files into individual files.

Split multiple Kubernetes files into smaller files with ease. Split multi-YAML files into individual files.

Dec 29, 2022
Split multiple Kubernetes files into smaller files with ease. Split multi-YAML files into individual files.

kubectl-slice: split Kubernetes YAMLs into files kubectl-slice is a neat tool that allows you to split a single multi-YAML Kubernetes manifest into mu

Jan 3, 2023
🔊Minimalist message bus implementation for internal communication

?? Bus Bus is a minimalist event/message bus implementation for internal communication. It is heavily inspired from my event_bus package for Elixir la

Jan 3, 2023
Go simple async message bus
Go simple async message bus

?? message-bus Go simple async message bus. ?? ABOUT Contributors: Rafał Lorenz Want to contribute ? Feel free to send pull requests! Have problems, b

Dec 29, 2022
A lightweight transactional message bus on top of RabbitMQ

grabbit A lightweight transactional message bus on top of RabbitMQ supporting: Supported Messaging Styles One Way (Fire and forget) Publish/Subscribe

Dec 22, 2022
Gin best practices, gin development scaffolding, too late to explain, get on the bus.

Table of Contents generated with DocToc gin_scaffold 现在开始 文件分层 log / redis / mysql / http.client 常用方法 swagger文档生成 gin_scaffold Gin best practices, gin

Dec 27, 2022
Easy to use distributed event bus similar to Kafka
Easy to use distributed event bus similar to Kafka

chukcha Easy to use distributed event bus similar to Kafka. The event bus is designed to be used as a persistent intermediate storage buffer for any k

Dec 30, 2022