From 00cf474768576d07a55f1eb3642fe44bebb4e2d8 Mon Sep 17 00:00:00 2001 From: Igor Tsiglyar Date: Tue, 16 May 2023 15:49:35 +0300 Subject: [PATCH] sdjournal: add sd_journal_open_namespace wrapper wrap sd_journal_open_namespace C call to enable reading journal records in non-default namespace fixes #413 --- sdjournal/journal.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/sdjournal/journal.go b/sdjournal/journal.go index 300b485..dbc6c45 100644 --- a/sdjournal/journal.go +++ b/sdjournal/journal.go @@ -39,6 +39,15 @@ package sdjournal // } // // int +// my_sd_journal_open_namespace(void *f, sd_journal **ret, const char *namespace, int flags) +// { +// int (*sd_journal_open_namespace)(sd_journal **, const char *, int); +// +// sd_journal_open_namespace = f; +// return sd_journal_open_namespace(ret, namespace, flags); +// } +// +// int // my_sd_journal_open_directory(void *f, sd_journal **ret, const char *path, int flags) // { // int (*sd_journal_open_directory)(sd_journal **, const char *, int); @@ -438,6 +447,27 @@ func NewJournal() (j *Journal, err error) { return j, nil } +// NewJournal returns a new Journal instance pointing to the local journal in a given namespace +func NewJournalInNamespace(namespace string) (j *Journal, err error) { + j = &Journal{} + + sd_journal_open_namespace, err := getFunction("sd_journal_open_namespace") + if err != nil { + return nil, err + } + + n := C.CString(namespace) + defer C.free(unsafe.Pointer(n)) + + r := C.my_sd_journal_open_namespace(sd_journal_open_namespace, &j.cjournal, n, C.SD_JOURNAL_LOCAL_ONLY) + + if r < 0 { + return nil, fmt.Errorf("failed to open journal in namespace %q: %s", namespace, syscall.Errno(-r).Error()) + } + + return j, nil +} + // NewJournalFromDir returns a new Journal instance pointing to a journal residing // in a given directory. func NewJournalFromDir(path string) (j *Journal, err error) {