Skip to content

Commit

Permalink
Merge pull request #354 from hudeng-go/master-new
Browse files Browse the repository at this point in the history
sdjournal: add GetBootID support
  • Loading branch information
Luca Bruno committed Mar 3, 2021
2 parents 9e0bb8c + 342e132 commit 9a6dca3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
48 changes: 48 additions & 0 deletions sdjournal/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,24 @@ package sdjournal
// return sd_journal_get_catalog(j, ret);
// }
//
// int
// my_sd_id128_get_boot(void *f, sd_id128_t *boot_id)
// {
// int(*sd_id128_get_boot)(sd_id128_t *);
//
// sd_id128_get_boot = f;
// return sd_id128_get_boot(boot_id);
// }
//
// char *
// my_sd_id128_to_string(void *f, sd_id128_t boot_id, char s[_SD_ARRAY_STATIC SD_ID128_STRING_MAX])
// {
// char *(*sd_id128_to_string)(sd_id128_t, char *);
//
// sd_id128_to_string = f;
// return sd_id128_to_string(boot_id, s);
// }
//
import "C"
import (
"bytes"
Expand Down Expand Up @@ -1118,3 +1136,33 @@ func (j *Journal) GetCatalog() (string, error) {

return catalog, nil
}

// GetBootID get systemd boot id
func (j *Journal) GetBootID() (string, error) {
sd_id128_get_boot, err := getFunction("sd_id128_get_boot")
if err != nil {
return "", err
}

var boot_id C.sd_id128_t
r := C.my_sd_id128_get_boot(sd_id128_get_boot, &boot_id)
if r < 0 {
return "", fmt.Errorf("failed to get boot id: %s", syscall.Errno(-r).Error())
}

sd_id128_to_string, err := getFunction("sd_id128_to_string")
if err != nil {
return "", err
}

c := (*C.char)(C.malloc(33))
defer C.free(unsafe.Pointer(c))
C.my_sd_id128_to_string(sd_id128_to_string, boot_id, c)

bootID := C.GoString(c)
if len(bootID) <= 0 {
return "", fmt.Errorf("get boot id %s is not valid", bootID)
}

return bootID, nil
}
21 changes: 21 additions & 0 deletions sdjournal/journal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,27 @@ func TestJournalGetCatalog(t *testing.T) {
}
}

func TestJournalGetBootID(t *testing.T) {
j, err := NewJournal()
if err != nil {
t.Fatal(err)
}

defer j.Close()

bootID, err := j.GetBootID()

if err != nil {
t.Fatalf("Failed to get bootID : %s", err)
}

if len(bootID) <= 0 {
t.Fatalf("Get bootID: %s is Null", bootID)
}

fmt.Printf("Test GetBootID: %s", bootID)
}

func contains(s []string, v string) bool {
for _, entry := range s {
if entry == v {
Expand Down

0 comments on commit 9a6dca3

Please sign in to comment.