Skip to content

Commit 716ff80

Browse files
committed
Merge pull request #130 from jonboulle/chrissnell-custom-journal-directory
*: add util.GetMachineID+journal.NewJournalFromDir
2 parents a831f36 + 7f0723f commit 716ff80

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

sdjournal/journal.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
package sdjournal
2626

2727
/*
28-
#cgo pkg-config: libsystemd-journal
28+
#cgo pkg-config: libsystemd
2929
#include <systemd/sd-journal.h>
3030
#include <stdlib.h>
3131
#include <syslog.h>
3232
*/
3333
import "C"
3434
import (
3535
"fmt"
36+
"path/filepath"
3637
"sync"
3738
"time"
3839
"unsafe"
@@ -86,6 +87,27 @@ func NewJournal() (*Journal, error) {
8687
return j, nil
8788
}
8889

90+
// NewJournalFromDir returns a new Journal instance pointing to a journal residing
91+
// in a given directory. The supplied path may be relative or absolute; if
92+
// relative, it will be converted to an absolute path before being opened.
93+
func NewJournalFromDir(path string) (*Journal, error) {
94+
path, err := filepath.Abs(path)
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
p := C.CString(path)
100+
defer C.free(unsafe.Pointer(p))
101+
102+
j := &Journal{}
103+
err := C.sd_journal_open_directory(&j.cjournal, p, 0)
104+
if err < 0 {
105+
return nil, fmt.Errorf("failed to open journal in directory %v: %v", path, err)
106+
}
107+
108+
return j, nil
109+
}
110+
89111
// Close closes a journal opened with NewJournal.
90112
func (j *Journal) Close() error {
91113
j.mu.Lock()

util/util.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ import "C"
6060
import (
6161
"errors"
6262
"fmt"
63+
"io/ioutil"
6364
"os"
65+
"strings"
6466
"syscall"
6567
"unsafe"
6668
)
@@ -244,7 +246,7 @@ func CurrentUnitName() (unit string, err error) {
244246
}
245247

246248
// IsRunningSystemd checks whether the host was booted with systemd as its init
247-
// system. This functions similar to systemd's `sd_booted(3)`: internally, it
249+
// system. This functions similarly to systemd's `sd_booted(3)`: internally, it
248250
// checks whether /run/systemd/system/ exists and is a directory.
249251
// http://www.freedesktop.org/software/systemd/man/sd_booted.html
250252
func IsRunningSystemd() bool {
@@ -254,3 +256,15 @@ func IsRunningSystemd() bool {
254256
}
255257
return fi.IsDir()
256258
}
259+
260+
// GetMachineID returns a host's 128-bit machine ID as a string. This functions
261+
// similarly to systemd's `sd_id128_get_machine`: internally, it simply reads
262+
// the contents of /etc/machine-id
263+
// http://www.freedesktop.org/software/systemd/man/sd_id128_get_machine.html
264+
func GetMachineID() (string, error) {
265+
machineID, err := ioutil.ReadFile("/etc/machine-id")
266+
if err != nil {
267+
return "", fmt.Errorf("failed to read /etc/machine-id: %v", err)
268+
}
269+
return strings.TrimSpace(string(machineID)), nil
270+
}

0 commit comments

Comments
 (0)