-
Notifications
You must be signed in to change notification settings - Fork 0
/
journal_def.go
554 lines (478 loc) · 14.7 KB
/
journal_def.go
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package main
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/klauspost/compress/zstd"
"github.com/pierrec/lz4"
"github.com/ulikunitz/xz"
)
// This implementation base on https://systemd.io/JOURNAL_FILE_FORMAT/
const (
// Definitions for Header incompatible_flags
HEADER_INCOMPATIBLE_COMPRESSED_XZ = 1 << 0
HEADER_INCOMPATIBLE_COMPRESSED_LZ4 = 1 << 1
HEADER_INCOMPATIBLE_KEYED_HASH = 1 << 2
HEADER_INCOMPATIBLE_COMPRESSED_ZSTD = 1 << 3
HEADER_INCOMPATIBLE_COMPACT = 1 << 4
// Definitions for Header compatible_flags
HEADER_COMPATIBLE_SEALED = 1 << 0
HEADER_COMPATIBLE_TAIL_ENTRY_BOOT_ID = 1 << 1
// Definitions for Header states
STATE_OFFLINE = 0
STATE_ONLINE = 1
STATE_ARCHIVED = 2
// Definitions for ObjectHeader flags
OBJECT_COMPRESSED_XZ = 1 << 0
OBJECT_COMPRESSED_LZ4 = 1 << 1
OBJECT_COMPRESSED_ZSTD = 1 << 2
// Maximum size for Header
// uint8_t -> 1
// le32_t -> 4
// sd_id128_t -> 16*1
// le64_t -> 8
HEADER_MAX_SIZE = 16*1 + 2*4 + 4*16*1 + 24*8
TAG_LENGTH = 256 / 8
// Definitions for object types
OBJECT_UNUSED = 0
OBJECT_DATA = 1
OBJECT_FIELD = 2
OBJECT_ENTRY = 3
OBJECT_DATA_HASH_TABLE = 4
OBJECT_FIELD_HASH_TABLE = 5
OBJECT_ENTRY_ARRAY = 6
OBJECT_TAG = 7
// ObjectHeader size
// uint8_t -> 1
// le64_t -> 8
OBJECT_HEADER_SIZE = 8*1 + 1*8
ATTRIBUTE_CURSOR = "__CURSOR"
ATTRIBUTE_REALTIME_TIMESTAMP = "__REALTIME_TIMESTAMP"
ATTRIBUTE_MONOTONIC_TIMESTAMP = "__MONOTONIC_TIMESTAMP"
PRIORITY_EMERGENCY = "emerg"
PRIORITY_ALERT = "alert"
PRIORITY_CRITICAL = "crit"
PRIORITY_ERROR = "err"
PRIORITY_WARNING = "warning"
PRIORITY_NOTICE = "notice"
PRIORITY_INFO = "info"
PRIORITY_DEBUG = "debug"
PRIORITY_
)
var PRIORITIES = [8]string{
PRIORITY_EMERGENCY,
PRIORITY_ALERT,
PRIORITY_CRITICAL,
PRIORITY_ERROR,
PRIORITY_WARNING,
PRIORITY_NOTICE,
PRIORITY_INFO,
PRIORITY_DEBUG,
}
func priorityID(priority string) (int, error) {
for id, p := range PRIORITIES {
if p == priority {
return id, nil
}
}
return 0, fmt.Errorf("unknown priority name: %s", priority)
}
// le32 converts [4]byte to uint32
func le32(data [4]byte) uint32 {
return binary.LittleEndian.Uint32(data[:])
}
// le64 converts [8]byte to uint32
func le64(data [8]byte) uint64 {
return binary.LittleEndian.Uint64(data[:])
}
// Definition of Header type
// rel: https://systemd.io/JOURNAL_FILE_FORMAT/#header
type Header struct {
signature [8]uint8 // uint8_t[8] "LPKSHHRH"
compatible_flags uint32 // le32_t
incompatible_flags uint32 // le32_t
state uint8 // uint8_t
reserved [7]uint8 // uint8_t[7]
file_id [16]byte // sd_id128_t
machine_id [16]byte // sd_id128_t
tail_entry_boot_id [16]byte // sd_id128_t
seqnum_id [16]byte // sd_id128_t
header_size uint64 // le64_t
arena_size uint64 // le64_t
data_hash_table_offset uint64 // le64_t
data_hash_table_size uint64 // le64_t
field_hash_table_offset uint64 // le64_t
field_hash_table_size uint64 // le64_t
tail_object_offset uint64 // le64_t
n_objects uint64 // le64_t
n_entries uint64 // le64_t
tail_entry_seqnum uint64 // le64_t
head_entry_seqnum uint64 // le64_t
entry_array_offset uint64 // le64_t
head_entry_realtime uint64 // le64_t
tail_entry_realtime uint64 // le64_t
tail_entry_monotonic uint64 // le64_t
// /* Added in 187 */
n_data uint64 // le64_t
n_fields uint64 // le64_t
// /* Added in 189 */
n_tags uint64 // le64_t
n_entry_arrays uint64 // le64_t
// /* Added in 246 */
data_hash_chain_depth uint64 // le64_t
field_hash_chain_depth uint64 // le64_t
// /* Added in 252 */
tail_entry_array_offset uint64 // le64_t
tail_entry_array_n_entries uint64 // le64_t
// /* Added in 254 */
tail_entry_offset uint64 // le64_t
}
// newHeader creates Header out of byte array
func newHeader(data []byte) (*Header, error) {
if len(data) < 208 {
return nil, fmt.Errorf("not enough data (%d) to read the header", len(data))
}
hu := Header{
signature: ([8]byte)(data[0:8]),
compatible_flags: le32(([4]byte)(data[8:12])),
incompatible_flags: le32(([4]byte)(data[12:16])),
state: data[16],
reserved: ([7]byte)(data[17:24]),
file_id: ([16]byte)(data[24:40]),
machine_id: ([16]byte)(data[40:56]),
tail_entry_boot_id: ([16]byte)(data[56:72]),
seqnum_id: ([16]byte)(data[72:88]),
header_size: le64(([8]byte)(data[88:96])),
arena_size: le64(([8]byte)(data[96:104])),
data_hash_table_offset: le64(([8]byte)(data[104:112])),
data_hash_table_size: le64(([8]byte)(data[112:120])),
field_hash_table_offset: le64(([8]byte)(data[120:128])),
field_hash_table_size: le64(([8]byte)(data[128:136])),
tail_object_offset: le64(([8]byte)(data[136:144])),
n_objects: le64(([8]byte)(data[144:152])),
n_entries: le64(([8]byte)(data[152:160])),
tail_entry_seqnum: le64(([8]byte)(data[160:168])),
head_entry_seqnum: le64(([8]byte)(data[168:176])),
entry_array_offset: le64(([8]byte)(data[176:184])),
head_entry_realtime: le64(([8]byte)(data[184:192])),
tail_entry_realtime: le64(([8]byte)(data[192:200])),
tail_entry_monotonic: le64(([8]byte)(data[200:208])),
}
// Added in 187
if hu.header_size > 208 {
if len(data) < 224 {
return nil, fmt.Errorf("not enough data (%d) to read the header", len(data))
}
hu.n_data = le64(([8]byte)(data[208:216]))
hu.n_fields = le64(([8]byte)(data[216:224]))
}
// Added in 189
if hu.header_size > 224 {
if len(data) < 240 {
return nil, fmt.Errorf("not enough data (%d) to read the header", len(data))
}
hu.n_tags = le64(([8]byte)(data[224:232]))
hu.n_entry_arrays = le64(([8]byte)(data[232:240]))
}
// Added in 246
if hu.header_size > 240 {
if len(data) < 256 {
return nil, fmt.Errorf("not enough data (%d) to read the header", len(data))
}
hu.data_hash_chain_depth = le64(([8]byte)(data[240:248]))
hu.field_hash_chain_depth = le64(([8]byte)(data[248:256]))
}
// Added in 252
if hu.header_size > 256 {
if len(data) < 272 {
return nil, fmt.Errorf("not enough data (%d) to read the header", len(data))
}
hu.tail_entry_array_offset = le64(([8]byte)(data[256:264]))
hu.tail_entry_array_n_entries = le64(([8]byte)(data[264:272]))
}
// Added in 254
if hu.header_size > 272 {
if len(data) < HEADER_MAX_SIZE {
return nil, fmt.Errorf("not enough data (%d) to read the header", len(data))
}
hu.tail_entry_offset = le64(([8]byte)(data[272:280]))
}
return &hu, nil
}
// isCompact returns true if HEADER_INCOMPATIBLE_COMPACT flag is enable
func (hu Header) isCompact() bool {
return hu.incompatible_flags&HEADER_INCOMPATIBLE_COMPACT > 0
}
// Definition of ObjectHeader type
// rel: https://systemd.io/JOURNAL_FILE_FORMAT/#objects
type ObjectHeader struct {
objectType uint8 // uint8_t
flags uint8 // uint8_t
reserved [6]uint8 // uint8_t[6]
size uint64 // le64_t
payload []uint8 // uint8_t[]
}
// newObjectHeader creates ObjectHeader out of byte array
// !!!It doesn't set the payload. Use setPayload to set it!!!
func newObjectHeader(data []byte) (*ObjectHeader, error) {
if len(data) < 16 {
return nil, fmt.Errorf("not enough data (%d) to read the object header", len(data))
}
oh := ObjectHeader{
objectType: data[0],
flags: data[1],
reserved: ([6]byte)(data[2:8]),
size: le64(([8]byte)(data[8:16])),
}
return &oh, nil
}
// setPayload sets payload for ObjectHeader
func (oh *ObjectHeader) setPayload(payload []byte) {
oh.payload = payload
}
// payloadSize computes payload size as diff of declared size and header size
func (oh *ObjectHeader) payloadSize() int {
return int(oh.size) - OBJECT_HEADER_SIZE
}
// EntryArray returns EntryArray object out of the ObjectHeader object
func (oh *ObjectHeader) EntryArray(incompatible_compact bool) *EntryArray {
do := EntryArray{
ObjectHeader: oh,
next_entry_array_offset: le64(([8]byte)(oh.payload[0:8])),
}
regularItems := []uint64{}
compactItems := []uint32{}
ptr := 8
if incompatible_compact {
for {
if ptr == len(oh.payload) {
break
}
compactItems = append(compactItems, le32(([4]byte)(oh.payload[ptr:ptr+4])))
ptr += 4
}
do.countItems = len(compactItems)
} else {
for {
if ptr == len(oh.payload) {
break
}
regularItems = append(regularItems, le64(([8]byte)(oh.payload[ptr:ptr+8])))
ptr += 8
}
do.countItems = len(regularItems)
}
do.compactItems = compactItems
do.regularItems = regularItems
return &do
}
// Entry returns Entry object out of the ObjectHeader object
func (oh *ObjectHeader) Entry(incompatible_compact bool) *Entry {
eo := Entry{
ObjectHeader: oh,
seqnum: le64(([8]byte)(oh.payload[0:8])),
realtime: le64(([8]byte)(oh.payload[8:16])),
monotonic: le64(([8]byte)(oh.payload[16:24])),
boot_id: ([16]byte)(oh.payload[24:40]),
xor_hash: le64(([8]byte)(oh.payload[40:48])),
}
regularItems := []regularEntryItem{}
compactItems := []compactEntryItem{}
ptr := 48
if incompatible_compact {
for {
if ptr == len(oh.payload) {
break
}
compactItems = append(compactItems, compactEntryItem{
object_offset: le32(([4]byte)(oh.payload[ptr : ptr+4])),
})
ptr += 4
}
} else {
for {
if ptr == len(oh.payload) {
break
}
regularItems = append(regularItems, regularEntryItem{
object_offset: le64(([8]byte)(oh.payload[ptr : ptr+8])),
hash: le64(([8]byte)(oh.payload[ptr+8 : ptr+16])),
})
ptr += 16
}
}
eo.itemsRegular = regularItems
eo.itemsCompact = compactItems
return &eo
}
// Data returns Data object out of the ObjectHeader object
func (oh *ObjectHeader) Data(incompatibleCompact bool) *Data {
do := Data{
ObjectHeader: oh,
hash: le64(([8]byte)(oh.payload[0:8])),
next_hash_offset: le64(([8]byte)(oh.payload[8:16])),
next_field_offset: le64(([8]byte)(oh.payload[16:24])),
entry_offset: le64(([8]byte)(oh.payload[24:32])),
entry_array_offset: le64(([8]byte)(oh.payload[32:40])),
n_entries: le64(([8]byte)(oh.payload[40:48])),
}
if incompatibleCompact {
do.tail_entry_array_offset = le32(([4]byte)(oh.payload[48:52]))
do.tail_entry_array_n_entries = le32(([4]byte)(oh.payload[52:56]))
do.payload = oh.payload[56:]
} else {
do.payload = oh.payload[48:]
}
return &do
}
// DataHashTable returns HashTable object out of the ObjectHeader object
func (oh *ObjectHeader) DataHashTable() *HashTable {
hashItems := []HashItem{}
ht := HashTable{
ObjectHeader: oh,
}
ptr := 0
for {
if ptr == len(oh.payload) {
break
}
hashItems = append(hashItems, HashItem{
head_hash_offset: le64(([8]byte)(oh.payload[ptr : ptr+8])),
tail_hash_offset: le64(([8]byte)(oh.payload[ptr+8 : ptr+16])),
})
ptr += 16
}
ht.items = hashItems
return &ht
}
// definition of Data type
// rel: https://systemd.io/JOURNAL_FILE_FORMAT/#data-objects
type Data struct {
*ObjectHeader
hash uint64 // le64_t
next_hash_offset uint64 // le64_t
next_field_offset uint64 // le64_t
entry_offset uint64 // le64_t /* the first array entry we store inline */
entry_array_offset uint64 // le64_t
n_entries uint64 // le64_t
// If HEADER_INCOMPATIBLE_COMPACT is set
tail_entry_array_offset uint32 // le32_t
tail_entry_array_n_entries uint32 // le32_t
payload []uint8 // uint8_t[]
}
// getPayloadKeyValue returns payload as key and value strings
// it handles compressed payload
func (so Data) getPayloadKeyValue() (string, string, error) {
var payload []uint8
switch true {
case so.flags&OBJECT_COMPRESSED_XZ > 0:
// decompress xz payload
r, err := xz.NewReader(bytes.NewReader(so.payload))
if err != nil {
return "", "", err
}
_, err = r.Read(payload)
if err != nil {
return "", "", err
}
case so.flags&OBJECT_COMPRESSED_LZ4 > 0:
// decompress lz4 payload
r := lz4.NewReader(bytes.NewReader(so.payload))
_, err := r.Read(payload)
if err != nil {
return "", "", err
}
case so.flags&OBJECT_COMPRESSED_ZSTD > 0:
// decompress zstd payload
r, err := zstd.NewReader(bytes.NewReader([]byte{}))
if err != nil {
return "", "", err
}
payload, err = r.DecodeAll(so.payload, nil)
if err != nil {
return "", "", err
}
default:
payload = so.payload
}
// Split payload by first `=`
for i := 0; ; i++ {
if payload[i] == '=' {
return string(payload[:i]), string(payload[i+1:]), nil
}
}
}
// definition of helper structure for regular items
type regularEntryItem struct {
object_offset uint64 // le64_t
hash uint64 // le64_t
}
// definition of helper structure for compact items
type compactEntryItem struct {
object_offset uint32 // le32_t
}
// definition of Entry type
// rel: https://systemd.io/JOURNAL_FILE_FORMAT/#entry-objects
type Entry struct {
*ObjectHeader
seqnum uint64 // le64_t
realtime uint64 // le64_t
monotonic uint64 // le64_t
boot_id [16]byte // sd_id128_t
xor_hash uint64 // le64_t
// for not HEADER_INCOMPATIBLE_COMPACT
itemsRegular []regularEntryItem
// for HEADER_INCOMPATIBLE_COMPACT
itemsCompact []compactEntryItem
}
// items returns EntryArray items in format of regular items
func (eo *Entry) items() []regularEntryItem {
if len(eo.itemsRegular) > 0 {
return eo.itemsRegular
}
items := []regularEntryItem{}
for _, item := range eo.itemsCompact {
items = append(items, regularEntryItem{
object_offset: uint64(item.object_offset),
})
}
return items
}
// definition of HashItem type
// rel: https://systemd.io/JOURNAL_FILE_FORMAT/#hash-table-objects
type HashItem struct {
head_hash_offset uint64 // le64_t
tail_hash_offset uint64 // le64_t
}
// definition of DataHashTable type
// rel: https://systemd.io/JOURNAL_FILE_FORMAT/#hash-table-objects
type HashTable struct {
*ObjectHeader
items []HashItem // HashItem[]
}
// definition of Entry Array type
// rel: https://systemd.io/JOURNAL_FILE_FORMAT/#entry-array-objects
type EntryArray struct {
*ObjectHeader
next_entry_array_offset uint64 // le64_t
regularItems []uint64 // le64_t[] // if not HEADER_INCOMPATIBLE_COMPACT
compactItems []uint32 // le32_t[] // for HEADER_INCOMPATIBLE_COMPACT
countItems int
}
// items returns EntryArray items in format of regularItems
func (eao *EntryArray) items() []uint64 {
if len(eao.regularItems) > 0 {
eao.countItems = len(eao.regularItems)
return eao.regularItems
}
items := []uint64{}
for _, item := range eao.compactItems {
items = append(items, uint64(item))
}
eao.countItems = len(eao.compactItems)
return items
}
// Log is just a key-value map
type Log struct {
attributes map[string]string
}