aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/host/machine_info.go
blob: 7078c3175dddaa79dcb70929218133c4b7336325 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2020 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package host

import (
	"bytes"
	"fmt"
	"os"
	"strings"
)

func CollectMachineInfo() ([]byte, error) {
	buf := new(bytes.Buffer)
	for _, pair := range machineInfoFuncs {
		pos0 := buf.Len()
		fmt.Fprintf(buf, "[%s]\n", pair.name)
		pos1 := buf.Len()
		err := pair.fn(buf)
		if err != nil {
			if !os.IsNotExist(err) {
				return nil, err
			}
		}
		if buf.Len() == pos1 {
			buf.Truncate(pos0)
			continue
		}
		fmt.Fprintf(buf, "\n%v\n\n", strings.Repeat("-", 80))
	}
	return buf.Bytes(), nil
}

func CollectModulesInfo() ([]KernelModule, error) {
	if machineModulesInfo == nil {
		return nil, nil
	}
	return machineModulesInfo()
}

func CollectGlobsInfo(globs map[string]bool) (map[string][]string, error) {
	if machineGlobsInfo == nil {
		return nil, nil
	}
	return machineGlobsInfo(globs)
}

var machineInfoFuncs []machineInfoFunc
var machineModulesInfo func() ([]KernelModule, error)
var machineGlobsInfo func(map[string]bool) (map[string][]string, error)

type machineInfoFunc struct {
	name string
	fn   func(*bytes.Buffer) error
}

type KernelModule struct {
	Name string
	Addr uint64
}