Skip to content

Commit 9a6dca3

Browse files
author
Luca Bruno
authored
Merge pull request coreos#354 from hudeng-go/master-new
sdjournal: add GetBootID support
2 parents 9e0bb8c + 342e132 commit 9a6dca3

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

sdjournal/journal.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,24 @@ package sdjournal
300300
// return sd_journal_get_catalog(j, ret);
301301
// }
302302
//
303+
// int
304+
// my_sd_id128_get_boot(void *f, sd_id128_t *boot_id)
305+
// {
306+
// int(*sd_id128_get_boot)(sd_id128_t *);
307+
//
308+
// sd_id128_get_boot = f;
309+
// return sd_id128_get_boot(boot_id);
310+
// }
311+
//
312+
// char *
313+
// my_sd_id128_to_string(void *f, sd_id128_t boot_id, char s[_SD_ARRAY_STATIC SD_ID128_STRING_MAX])
314+
// {
315+
// char *(*sd_id128_to_string)(sd_id128_t, char *);
316+
//
317+
// sd_id128_to_string = f;
318+
// return sd_id128_to_string(boot_id, s);
319+
// }
320+
//
303321
import "C"
304322
import (
305323
"bytes"
@@ -1118,3 +1136,33 @@ func (j *Journal) GetCatalog() (string, error) {
11181136

11191137
return catalog, nil
11201138
}
1139+
1140+
// GetBootID get systemd boot id
1141+
func (j *Journal) GetBootID() (string, error) {
1142+
sd_id128_get_boot, err := getFunction("sd_id128_get_boot")
1143+
if err != nil {
1144+
return "", err
1145+
}
1146+
1147+
var boot_id C.sd_id128_t
1148+
r := C.my_sd_id128_get_boot(sd_id128_get_boot, &boot_id)
1149+
if r < 0 {
1150+
return "", fmt.Errorf("failed to get boot id: %s", syscall.Errno(-r).Error())
1151+
}
1152+
1153+
sd_id128_to_string, err := getFunction("sd_id128_to_string")
1154+
if err != nil {
1155+
return "", err
1156+
}
1157+
1158+
c := (*C.char)(C.malloc(33))
1159+
defer C.free(unsafe.Pointer(c))
1160+
C.my_sd_id128_to_string(sd_id128_to_string, boot_id, c)
1161+
1162+
bootID := C.GoString(c)
1163+
if len(bootID) <= 0 {
1164+
return "", fmt.Errorf("get boot id %s is not valid", bootID)
1165+
}
1166+
1167+
return bootID, nil
1168+
}

sdjournal/journal_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,27 @@ func TestJournalGetCatalog(t *testing.T) {
470470
}
471471
}
472472

473+
func TestJournalGetBootID(t *testing.T) {
474+
j, err := NewJournal()
475+
if err != nil {
476+
t.Fatal(err)
477+
}
478+
479+
defer j.Close()
480+
481+
bootID, err := j.GetBootID()
482+
483+
if err != nil {
484+
t.Fatalf("Failed to get bootID : %s", err)
485+
}
486+
487+
if len(bootID) <= 0 {
488+
t.Fatalf("Get bootID: %s is Null", bootID)
489+
}
490+
491+
fmt.Printf("Test GetBootID: %s", bootID)
492+
}
493+
473494
func contains(s []string, v string) bool {
474495
for _, entry := range s {
475496
if entry == v {

0 commit comments

Comments
 (0)