File tree 2 files changed +38
-2
lines changed
2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change 25
25
package sdjournal
26
26
27
27
/*
28
- #cgo pkg-config: libsystemd-journal
28
+ #cgo pkg-config: libsystemd
29
29
#include <systemd/sd-journal.h>
30
30
#include <stdlib.h>
31
31
#include <syslog.h>
32
32
*/
33
33
import "C"
34
34
import (
35
35
"fmt"
36
+ "path/filepath"
36
37
"sync"
37
38
"time"
38
39
"unsafe"
@@ -86,6 +87,27 @@ func NewJournal() (*Journal, error) {
86
87
return j , nil
87
88
}
88
89
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
+
89
111
// Close closes a journal opened with NewJournal.
90
112
func (j * Journal ) Close () error {
91
113
j .mu .Lock ()
Original file line number Diff line number Diff line change @@ -60,7 +60,9 @@ import "C"
60
60
import (
61
61
"errors"
62
62
"fmt"
63
+ "io/ioutil"
63
64
"os"
65
+ "strings"
64
66
"syscall"
65
67
"unsafe"
66
68
)
@@ -244,7 +246,7 @@ func CurrentUnitName() (unit string, err error) {
244
246
}
245
247
246
248
// 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
248
250
// checks whether /run/systemd/system/ exists and is a directory.
249
251
// http://www.freedesktop.org/software/systemd/man/sd_booted.html
250
252
func IsRunningSystemd () bool {
@@ -254,3 +256,15 @@ func IsRunningSystemd() bool {
254
256
}
255
257
return fi .IsDir ()
256
258
}
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
+ }
You can’t perform that action at this time.
0 commit comments