Skip to content

Commit 2d44d43

Browse files
committed
sdjournal: wrap return values with syscall.Errno
Systemd functions return negative errno-like errors, wrap those with syscall.Errno() so we can get descriptive error messages instead of numbers.
1 parent 17b531a commit 2d44d43

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

sdjournal/journal.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ import (
196196
"path/filepath"
197197
"strings"
198198
"sync"
199+
"syscall"
199200
"time"
200201
"unsafe"
201202

@@ -291,7 +292,7 @@ func NewJournal() (*Journal, error) {
291292
r := C.my_sd_journal_open(sd_journal_open, &j.cjournal, C.SD_JOURNAL_LOCAL_ONLY)
292293

293294
if r < 0 {
294-
return nil, fmt.Errorf("failed to open journal: %d", r)
295+
return nil, fmt.Errorf("failed to open journal: %d", syscall.Errno(-r))
295296
}
296297

297298
return j, nil
@@ -323,7 +324,7 @@ func NewJournalFromDir(path string) (*Journal, error) {
323324

324325
r := C.my_sd_journal_open_directory(sd_journal_open_directory, &j.cjournal, p, 0)
325326
if r < 0 {
326-
return nil, fmt.Errorf("failed to open journal in directory %q: %d", path, r)
327+
return nil, fmt.Errorf("failed to open journal in directory %q: %d", path, syscall.Errno(-r))
327328
}
328329

329330
return j, nil
@@ -358,7 +359,7 @@ func (j *Journal) AddMatch(match string) error {
358359
j.mu.Unlock()
359360

360361
if r < 0 {
361-
return fmt.Errorf("failed to add match: %d", r)
362+
return fmt.Errorf("failed to add match: %d", syscall.Errno(-r))
362363
}
363364

364365
return nil
@@ -376,7 +377,7 @@ func (j *Journal) AddDisjunction() error {
376377
j.mu.Unlock()
377378

378379
if r < 0 {
379-
return fmt.Errorf("failed to add a disjunction in the match list: %d", r)
380+
return fmt.Errorf("failed to add a disjunction in the match list: %d", syscall.Errno(-r))
380381
}
381382

382383
return nil
@@ -394,7 +395,7 @@ func (j *Journal) AddConjunction() error {
394395
j.mu.Unlock()
395396

396397
if r < 0 {
397-
return fmt.Errorf("failed to add a conjunction in the match list: %d", r)
398+
return fmt.Errorf("failed to add a conjunction in the match list: %d", syscall.Errno(-r))
398399
}
399400

400401
return nil
@@ -424,7 +425,7 @@ func (j *Journal) Next() (int, error) {
424425
j.mu.Unlock()
425426

426427
if r < 0 {
427-
return int(r), fmt.Errorf("failed to iterate journal: %d", r)
428+
return int(r), fmt.Errorf("failed to iterate journal: %d", syscall.Errno(-r))
428429
}
429430

430431
return int(r), nil
@@ -443,7 +444,7 @@ func (j *Journal) NextSkip(skip uint64) (uint64, error) {
443444
j.mu.Unlock()
444445

445446
if r < 0 {
446-
return uint64(r), fmt.Errorf("failed to iterate journal: %d", r)
447+
return uint64(r), fmt.Errorf("failed to iterate journal: %d", syscall.Errno(-r))
447448
}
448449

449450
return uint64(r), nil
@@ -461,7 +462,7 @@ func (j *Journal) Previous() (uint64, error) {
461462
j.mu.Unlock()
462463

463464
if r < 0 {
464-
return uint64(r), fmt.Errorf("failed to iterate journal: %d", r)
465+
return uint64(r), fmt.Errorf("failed to iterate journal: %d", syscall.Errno(-r))
465466
}
466467

467468
return uint64(r), nil
@@ -480,7 +481,7 @@ func (j *Journal) PreviousSkip(skip uint64) (uint64, error) {
480481
j.mu.Unlock()
481482

482483
if r < 0 {
483-
return uint64(r), fmt.Errorf("failed to iterate journal: %d", r)
484+
return uint64(r), fmt.Errorf("failed to iterate journal: %d", syscall.Errno(-r))
484485
}
485486

486487
return uint64(r), nil
@@ -505,7 +506,7 @@ func (j *Journal) GetData(field string) (string, error) {
505506
j.mu.Unlock()
506507

507508
if r < 0 {
508-
return "", fmt.Errorf("failed to read message: %d", r)
509+
return "", fmt.Errorf("failed to read message: %d", syscall.Errno(-r))
509510
}
510511

511512
msg := C.GoStringN((*C.char)(d), C.int(l))
@@ -538,7 +539,7 @@ func (j *Journal) SetDataThreshold(threshold uint64) error {
538539
j.mu.Unlock()
539540

540541
if r < 0 {
541-
return fmt.Errorf("failed to set data threshold: %d", r)
542+
return fmt.Errorf("failed to set data threshold: %d", syscall.Errno(-r))
542543
}
543544

544545
return nil
@@ -559,7 +560,7 @@ func (j *Journal) GetRealtimeUsec() (uint64, error) {
559560
j.mu.Unlock()
560561

561562
if r < 0 {
562-
return 0, fmt.Errorf("error getting timestamp for entry: %d", r)
563+
return 0, fmt.Errorf("error getting timestamp for entry: %d", syscall.Errno(-r))
563564
}
564565

565566
return uint64(usec), nil
@@ -578,7 +579,7 @@ func (j *Journal) SeekTail() error {
578579
j.mu.Unlock()
579580

580581
if r < 0 {
581-
return fmt.Errorf("failed to seek to tail of journal: %d", r)
582+
return fmt.Errorf("failed to seek to tail of journal: %d", syscall.Errno(-r))
582583
}
583584

584585
return nil
@@ -597,7 +598,7 @@ func (j *Journal) SeekRealtimeUsec(usec uint64) error {
597598
j.mu.Unlock()
598599

599600
if r < 0 {
600-
return fmt.Errorf("failed to seek to %d: %d", usec, r)
601+
return fmt.Errorf("failed to seek to %d: %d", usec, syscall.Errno(-r))
601602
}
602603

603604
return nil
@@ -644,7 +645,7 @@ func (j *Journal) GetUsage() (uint64, error) {
644645
j.mu.Unlock()
645646

646647
if r < 0 {
647-
return 0, fmt.Errorf("failed to get journal disk space usage: %d", r)
648+
return 0, fmt.Errorf("failed to get journal disk space usage: %d", syscall.Errno(-r))
648649
}
649650

650651
return uint64(out), nil

0 commit comments

Comments
 (0)