github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/pkg/checker/dmesg.go (about)

     1  // Copyright 2017-2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package checker
     6  
     7  import (
     8  	"strings"
     9  	"syscall"
    10  	"unsafe"
    11  )
    12  
    13  // shamelessly copied from u-root/cmds/dmesg
    14  const (
    15  	_SYSLOG_ACTION_READ_ALL = 3
    16  )
    17  
    18  func getDmesg() (string, error) {
    19  	level := uintptr(_SYSLOG_ACTION_READ_ALL)
    20  	b := make([]byte, 256*1024)
    21  	n, _, err := syscall.Syscall(syscall.SYS_SYSLOG, level, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)))
    22  	if err != 0 {
    23  		return "", err
    24  	}
    25  	return string(b[:n]), nil
    26  }
    27  
    28  func grep(b, pattern string) []string {
    29  	lines := strings.Split(b, "\n")
    30  	ret := make([]string, 0)
    31  	for _, line := range lines {
    32  		if strings.Contains(line, pattern) {
    33  			ret = append(ret, line)
    34  		}
    35  	}
    36  	return ret
    37  }