forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dmesg.go
62 lines (53 loc) · 1.25 KB
/
dmesg.go
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
61
62
// Copyright 2012-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Read the system log.
//
// Synopsis:
// dmesg [-clear|-read-clear]
//
// Options:
// -clear: clear the log
// -read-clear: clear the log after printing
package main
import (
"flag"
"log"
"os"
"syscall"
"unsafe"
)
const (
_SYSLOG_ACTION_READ = 2
_SYSLOG_ACTION_READ_ALL = 3
_SYSLOG_ACTION_READ_CLEAR = 4
_SYSLOG_ACTION_CLEAR = 5
)
var (
clear bool
readClear bool
)
func init() {
flag.BoolVar(&clear, "clear", false, "Clear the log")
flag.BoolVar(&readClear, "read-clear", false, "Clear the log after printing")
flag.BoolVar(&readClear, "c", false, "Clear the log after printing")
}
func main() {
flag.Parse()
if clear && readClear {
log.Fatalf("cannot specify both -clear and -read-clear")
}
level := uintptr(_SYSLOG_ACTION_READ_ALL)
if clear {
level = _SYSLOG_ACTION_CLEAR
}
if readClear {
level = _SYSLOG_ACTION_READ_CLEAR
}
b := make([]byte, 256*1024)
amt, _, err := syscall.Syscall(syscall.SYS_SYSLOG, level, uintptr(unsafe.Pointer(&b[0])), uintptr(len(b)))
if err != 0 {
log.Fatalf("syslog failed: %v", err)
}
os.Stdout.Write(b[:amt])
}