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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
//go:build linux
// Package journal provides a journalctl-backed file reader.
package journal
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"syscall"
"time"
"github.com/mimecast/dtail/internal/io/fs"
"github.com/mimecast/dtail/internal/io/line"
"github.com/mimecast/dtail/internal/io/pool"
"github.com/mimecast/dtail/internal/lcontext"
"github.com/mimecast/dtail/internal/regex"
)
const (
defaultSourceID = "journal"
journalctlCommand = "journalctl"
maxScannerTokenSize = 1024 * 1024
processTerminateGrace = 200 * time.Millisecond
)
var errStopReading = errors.New("stop journal reading")
// ErrJournalctlNotFound reports that journalctl could not be found on PATH.
var ErrJournalctlNotFound = errors.New("journalctl not found")
// Reader reads journal entries by executing journalctl.
type Reader struct {
journalctlPath string
args []string
sourceID string
serverMessages chan<- string
follow bool
}
var _ fs.FileReader = (*Reader)(nil)
// NewReader returns a journalctl-backed file reader.
func NewReader(args []string, sourceID string, follow bool, serverMessages chan<- string) (*Reader, error) {
journalctlPath, err := exec.LookPath(journalctlCommand)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrJournalctlNotFound, err)
}
if sourceID == "" {
sourceID = defaultSourceID
}
copiedArgs := append([]string(nil), args...)
return &Reader{
journalctlPath: journalctlPath,
args: copiedArgs,
sourceID: sourceID,
serverMessages: serverMessages,
follow: follow,
}, nil
}
// StartWithProcessor reads journalctl stdout and sends matching lines to processor.
func (r *Reader) StartWithProcessor(ctx context.Context, ltx lcontext.LContext,
processor line.Processor, re regex.Regex) error {
return r.runWithProcessor(ctx, ltx, processor, re)
}
// StartWithProcessorOptimized reads journalctl stdout and sends matching lines to processor.
func (r *Reader) StartWithProcessorOptimized(ctx context.Context, ltx lcontext.LContext,
processor line.Processor, re regex.Regex) error {
return r.runWithProcessor(ctx, ltx, processor, re)
}
// FilePath returns a stable journalctl command description.
func (r *Reader) FilePath() string {
if len(r.args) == 0 {
return journalctlCommand
}
return journalctlCommand + " " + strings.Join(r.args, " ")
}
// Retry reports whether journalctl should be restarted after it exits.
func (r *Reader) Retry() bool {
return r.follow
}
func (r *Reader) runWithProcessor(ctx context.Context, ltx lcontext.LContext,
processor line.Processor, re regex.Regex) error {
sink := processorSink{processor: processor}
// In follow mode r.run blocks until journalctl is stopped, so a batching
// processor (the NetworkWriter, which buffers up to 64KB before
// sending) would hold live lines in its buffer and the client would never
// see interactive output. Flush after every scanned line while following so
// journal follow output reaches the client promptly — the same latency
// guarantee the file follow path gets from its per-read-chunk flush. A
// non-follow snapshot read keeps the batching benefit and flushes once at
// the end below.
var flushLine func() error
if r.follow {
flushLine = processor.Flush
}
err := r.run(ctx, ltx, sink, re, flushLine)
if flushErr := processor.Flush(); flushErr != nil && err == nil {
err = flushErr
}
return err
}
func (r *Reader) run(ctx context.Context, ltx lcontext.LContext, sink journalSink,
re regex.Regex, flushLine func() error) error {
cmd := r.command(ctx)
stdout, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("open journalctl stdout: %w", err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
return fmt.Errorf("open journalctl stderr: %w", err)
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("start journalctl: %w", err)
}
stderrDone := make(chan error, 1)
go func() {
stderrDone <- r.forwardStderr(ctx, stderr)
}()
filter := newJournalFilter(ltx, sink, re, r.sourceID)
scanErr := r.scanStdout(ctx, stdout, filter, flushLine)
waitErr := waitForJournalctl(cmd, scanErr != nil)
stderrErr := <-stderrDone
filter.Close()
if ctx.Err() != nil || errors.Is(scanErr, errStopReading) {
return nil
}
if scanErr != nil {
return scanErr
}
if stderrErr != nil {
return stderrErr
}
if waitErr != nil {
return fmt.Errorf("journalctl failed: %w", waitErr)
}
return nil
}
func (r *Reader) command(ctx context.Context) *exec.Cmd {
args := r.commandArgs()
cmd := exec.CommandContext(ctx, r.journalctlPath, args...)
cmd.Cancel = func() error {
terminateProcess(cmd.Process)
return nil
}
cmd.WaitDelay = processTerminateGrace
return cmd
}
func (r *Reader) commandArgs() []string {
args := append([]string(nil), r.args...)
if r.follow {
args = append(args, "-f", "-n", "0")
}
return args
}
func terminateProcess(process *os.Process) {
if process == nil {
return
}
_ = process.Signal(syscall.SIGTERM)
}
func killProcess(process *os.Process) {
if process == nil {
return
}
_ = process.Kill()
}
func waitForJournalctl(cmd *exec.Cmd, earlyStop bool) error {
if !earlyStop {
return cmd.Wait()
}
terminateProcess(cmd.Process)
waitDone := make(chan error, 1)
go func() {
waitDone <- cmd.Wait()
}()
timer := time.NewTimer(processTerminateGrace)
defer timer.Stop()
select {
case err := <-waitDone:
return err
case <-timer.C:
killProcess(cmd.Process)
return <-waitDone
}
}
func (r *Reader) scanStdout(ctx context.Context, stdout io.Reader, filter *journalFilter,
flushLine func() error) error {
scanner := bufio.NewScanner(stdout)
bufPtr := pool.GetScannerBuffer()
defer pool.PutScannerBuffer(bufPtr)
scanner.Buffer(*bufPtr, maxScannerTokenSize)
scanner.Split(scanLinesPreserveEndings)
for scanner.Scan() {
select {
case <-ctx.Done():
return nil
default:
}
lineBuf := pool.BytesBuffer.Get().(*bytes.Buffer)
lineBuf.Write(scanner.Bytes())
if err := filter.Process(ctx, lineBuf); err != nil {
return err
}
// In follow mode, push any buffered line straight to the client so
// batching never delays live output. flushLine is nil for non-follow
// reads (they batch and flush once at the end) and for the immediate
// channel sink.
if flushLine != nil {
if err := flushLine(); err != nil {
return err
}
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("scan journalctl stdout: %w", err)
}
return nil
}
func (r *Reader) forwardStderr(ctx context.Context, stderr io.Reader) error {
scanner := bufio.NewScanner(stderr)
bufPtr := pool.GetScannerBuffer()
defer pool.PutScannerBuffer(bufPtr)
scanner.Buffer(*bufPtr, maxScannerTokenSize)
for scanner.Scan() {
if !r.sendServerMessage(ctx, fmt.Sprintf("journalctl stderr: %s\n", scanner.Text())) {
return nil
}
}
if err := scanner.Err(); err != nil {
if errors.Is(err, os.ErrClosed) {
return nil
}
return fmt.Errorf("scan journalctl stderr: %w", err)
}
return nil
}
func (r *Reader) sendServerMessage(ctx context.Context, message string) bool {
if r.serverMessages == nil {
return true
}
select {
case r.serverMessages <- message:
return true
case <-ctx.Done():
return false
}
}
func scanLinesPreserveEndings(data []byte, atEOF bool) (int, []byte, error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, '\n'); i >= 0 {
return i + 1, data[:i+1], nil
}
if atEOF {
return len(data), nil, nil
}
return 0, nil, nil
}
|