Skip to content

Commit d5623bf

Browse files
authored
Merge pull request #410 from WGH-/env-journal-stream
journal: add StderrIsJournalStream function
2 parents 43ee42e + 4ca6222 commit d5623bf

File tree

7 files changed

+302
-2
lines changed

7 files changed

+302
-2
lines changed

.github/workflows/containers.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
runs-on: ubuntu-latest
2222
strategy:
2323
matrix:
24-
baseimage: ['debian:stretch', 'ubuntu:16.04', 'ubuntu:18.04']
24+
baseimage: ['debian:stretch', 'ubuntu:18.04', 'ubuntu:20.04']
2525
steps:
2626
- run: sudo apt-get -qq update
2727
- name: Install libsystemd-dev
@@ -38,7 +38,7 @@ jobs:
3838
- name: Pull base image - ${{ matrix.baseimage }}
3939
run: docker pull ${{ matrix.baseimage }}
4040
- name: Install packages for ${{ matrix.baseimage }}
41-
run: docker run --privileged -e GOPATH=${GOPATH} --cidfile=/tmp/cidfile ${{ matrix.baseimage }} /bin/bash -c "apt-get update && apt-get install -y sudo build-essential git golang dbus libsystemd-dev libpam-systemd systemd-container"
41+
run: docker run --privileged -e GOPATH=${GOPATH} --cidfile=/tmp/cidfile ${{ matrix.baseimage }} /bin/bash -c "export DEBIAN_FRONTEND=noninteractive; apt-get update && apt-get install -y sudo build-essential git golang dbus libsystemd-dev libpam-systemd systemd-container"
4242
- name: Persist base container
4343
run: docker commit `cat /tmp/cidfile` go-systemd/container-tests
4444
- run: rm -f /tmp/cidfile

examples/journal/main.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2022 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License
14+
15+
package main
16+
17+
import (
18+
"fmt"
19+
"os"
20+
21+
"github.com/coreos/go-systemd/v22/journal"
22+
)
23+
24+
func main() {
25+
ok, err := journal.StderrIsJournalStream()
26+
if err != nil {
27+
panic(err)
28+
}
29+
30+
if ok {
31+
// use journal native protocol
32+
journal.Send("this is a message logged through the native protocol", journal.PriInfo, nil)
33+
} else {
34+
// use stderr
35+
fmt.Fprintln(os.Stderr, "this is a message logged through stderr")
36+
}
37+
}

examples/journal/run.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
go build
6+
7+
echo "Running directly"
8+
./journal
9+
10+
echo "Running through systemd"
11+
unit_name="run-$(systemd-id128 new)"
12+
systemd-run -u "$unit_name" --user --wait --quiet ./journal
13+
journalctl --user -u "$unit_name"

journal/journal_unix.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,58 @@ func Enabled() bool {
6969
return true
7070
}
7171

72+
// StderrIsJournalStream returns whether the process stderr is connected
73+
// to the Journal's stream transport.
74+
//
75+
// This can be used for automatic protocol upgrading described in [Journal Native Protocol].
76+
//
77+
// Returns true if JOURNAL_STREAM environment variable is present,
78+
// and stderr's device and inode numbers match it.
79+
//
80+
// Error is returned if unexpected error occurs: e.g. if JOURNAL_STREAM environment variable
81+
// is present, but malformed, fstat syscall fails, etc.
82+
//
83+
// [Journal Native Protocol]: https://systemd.io/JOURNAL_NATIVE_PROTOCOL/#automatic-protocol-upgrading
84+
func StderrIsJournalStream() (bool, error) {
85+
return fdIsJournalStream(syscall.Stderr)
86+
}
87+
88+
// StdoutIsJournalStream returns whether the process stdout is connected
89+
// to the Journal's stream transport.
90+
//
91+
// Returns true if JOURNAL_STREAM environment variable is present,
92+
// and stdout's device and inode numbers match it.
93+
//
94+
// Error is returned if unexpected error occurs: e.g. if JOURNAL_STREAM environment variable
95+
// is present, but malformed, fstat syscall fails, etc.
96+
//
97+
// Most users should probably use [StderrIsJournalStream].
98+
func StdoutIsJournalStream() (bool, error) {
99+
return fdIsJournalStream(syscall.Stdout)
100+
}
101+
102+
func fdIsJournalStream(fd int) (bool, error) {
103+
journalStream := os.Getenv("JOURNAL_STREAM")
104+
if journalStream == "" {
105+
return false, nil
106+
}
107+
108+
var expectedStat syscall.Stat_t
109+
_, err := fmt.Sscanf(journalStream, "%d:%d", &expectedStat.Dev, &expectedStat.Ino)
110+
if err != nil {
111+
return false, fmt.Errorf("failed to parse JOURNAL_STREAM=%q: %v", journalStream, err)
112+
}
113+
114+
var stat syscall.Stat_t
115+
err = syscall.Fstat(fd, &stat)
116+
if err != nil {
117+
return false, err
118+
}
119+
120+
match := stat.Dev == expectedStat.Dev && stat.Ino == expectedStat.Ino
121+
return match, nil
122+
}
123+
72124
// Send a message to the local systemd journal. vars is a map of journald
73125
// fields to values. Fields must be composed of uppercase letters, numbers,
74126
// and underscores, but must not start with an underscore. Within these

journal/journal_unix_test.go

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
// Copyright 2022 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build !windows
16+
// +build !windows
17+
18+
package journal_test
19+
20+
import (
21+
"fmt"
22+
"os"
23+
"os/exec"
24+
"syscall"
25+
"testing"
26+
27+
"github.com/coreos/go-systemd/v22/journal"
28+
)
29+
30+
func TestJournalStreamParsing(t *testing.T) {
31+
if _, ok := os.LookupEnv("JOURNAL_STREAM"); ok {
32+
t.Fatal("unset JOURNAL_STREAM before running this test")
33+
}
34+
35+
t.Run("Missing", func(t *testing.T) {
36+
ok, err := journal.StderrIsJournalStream()
37+
if err != nil {
38+
t.Fatal(err)
39+
}
40+
if ok {
41+
t.Error("stderr shouldn't be connected to journal stream")
42+
}
43+
})
44+
t.Run("Present", func(t *testing.T) {
45+
f, stat := getUnixStreamSocket(t)
46+
defer f.Close()
47+
os.Setenv("JOURNAL_STREAM", fmt.Sprintf("%d:%d", stat.Dev, stat.Ino))
48+
defer os.Unsetenv("JOURNAL_STREAM")
49+
replaceStderr(int(f.Fd()), func() {
50+
ok, err := journal.StderrIsJournalStream()
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
if !ok {
55+
t.Error("stderr should've been connected to journal stream")
56+
}
57+
})
58+
})
59+
t.Run("NotMatching", func(t *testing.T) {
60+
f, stat := getUnixStreamSocket(t)
61+
defer f.Close()
62+
os.Setenv("JOURNAL_STREAM", fmt.Sprintf("%d:%d", stat.Dev+1, stat.Ino))
63+
defer os.Unsetenv("JOURNAL_STREAM")
64+
replaceStderr(int(f.Fd()), func() {
65+
ok, err := journal.StderrIsJournalStream()
66+
if err != nil {
67+
t.Fatal(err)
68+
}
69+
if ok {
70+
t.Error("stderr shouldn't be connected to journal stream")
71+
}
72+
})
73+
})
74+
t.Run("Malformed", func(t *testing.T) {
75+
f, stat := getUnixStreamSocket(t)
76+
defer f.Close()
77+
os.Setenv("JOURNAL_STREAM", fmt.Sprintf("%d-%d", stat.Dev, stat.Ino))
78+
defer os.Unsetenv("JOURNAL_STREAM")
79+
replaceStderr(int(f.Fd()), func() {
80+
_, err := journal.StderrIsJournalStream()
81+
if err == nil {
82+
t.Fatal("JOURNAL_STREAM is malformed, but no error returned")
83+
}
84+
})
85+
})
86+
}
87+
88+
func TestStderrIsJournalStream(t *testing.T) {
89+
const (
90+
message = "TEST_MESSAGE"
91+
)
92+
93+
userOrSystem := "--user"
94+
if os.Getuid() == 0 {
95+
userOrSystem = "--system"
96+
}
97+
98+
if _, ok := os.LookupEnv("JOURNAL_STREAM"); !ok {
99+
// Re-execute this test under systemd (see the else branch),
100+
// and observe its exit code.
101+
args := []string{
102+
"systemd-run",
103+
userOrSystem,
104+
"--wait",
105+
"--quiet",
106+
"--",
107+
os.Args[0],
108+
"-test.run=TestStderrIsJournalStream",
109+
"-test.count=1", // inhibit caching
110+
}
111+
112+
cmd := exec.Command(args[0], args[1:]...)
113+
cmd.Stderr = os.Stderr
114+
if err := cmd.Run(); err != nil {
115+
t.Fatal(err)
116+
}
117+
} else {
118+
ok, err := journal.StderrIsJournalStream()
119+
if err != nil {
120+
t.Fatal(err)
121+
}
122+
if !ok {
123+
t.Fatal("StderrIsJournalStream should've returned true")
124+
}
125+
126+
err = journal.Send(message, journal.PriInfo, nil)
127+
if err != nil {
128+
t.Fatal(err)
129+
}
130+
}
131+
}
132+
133+
func ExampleStderrIsJournalStream() {
134+
// NOTE: this is just an example. Production code
135+
// will likely use this to setup a logging library
136+
// to write messages to either journal or stderr.
137+
ok, err := journal.StderrIsJournalStream()
138+
if err != nil {
139+
panic(err)
140+
}
141+
142+
if ok {
143+
// use journal native protocol
144+
journal.Send("this is a message logged through the native protocol", journal.PriInfo, nil)
145+
} else {
146+
// use stderr
147+
fmt.Fprintln(os.Stderr, "this is a message logged through stderr")
148+
}
149+
}
150+
151+
func replaceStderr(fd int, cb func()) {
152+
savedStderr, err := syscall.Dup(syscall.Stderr)
153+
if err != nil {
154+
panic(err)
155+
}
156+
defer syscall.Close(savedStderr)
157+
err = syscall.Dup2(fd, syscall.Stderr)
158+
if err != nil {
159+
panic(err)
160+
}
161+
defer func() {
162+
err := syscall.Dup2(savedStderr, syscall.Stderr)
163+
if err != nil {
164+
panic(err)
165+
}
166+
}()
167+
cb()
168+
}
169+
170+
// getUnixStreamSocket returns a unix stream socket obtained with
171+
// socketpair(2), and its fstat result. Only one end of the socket pair
172+
// is returned, and the other end is closed immediately: we don't need
173+
// it for our purposes.
174+
func getUnixStreamSocket(t *testing.T) (*os.File, *syscall.Stat_t) {
175+
fds, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
176+
if err != nil {
177+
t.Fatal(os.NewSyscallError("socketpair", err))
178+
}
179+
// we don't need the remote end for our tests
180+
syscall.Close(fds[1])
181+
182+
file := os.NewFile(uintptr(fds[0]), "unix-stream")
183+
stat, err := file.Stat()
184+
if err != nil {
185+
t.Fatal(err)
186+
}
187+
return file, stat.Sys().(*syscall.Stat_t)
188+
}

journal/journal_windows.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ func Enabled() bool {
3333
func Send(message string, priority Priority, vars map[string]string) error {
3434
return errors.New("could not initialize socket to journald")
3535
}
36+
37+
func StderrIsJournalStream() (bool, error) {
38+
return false, nil
39+
}
40+
41+
func StdoutIsJournalStream() (bool, error) {
42+
return false, nil
43+
}

scripts/ci-runner.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ function build_tests {
2323
echo " - examples/${ex}"
2424
go build -o ./test_bins/${ex}.example ./examples/activation/${ex}.go
2525
done
26+
# just to make sure it's buildable
27+
go build -o ./test_bins/journal ./examples/journal/
2628
}
2729

2830
function run_tests {

0 commit comments

Comments
 (0)