/
osversion_linux.go
132 lines (117 loc) · 2.74 KB
/
osversion_linux.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 移植来自Delphi
package version
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"unsafe"
)
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
#include <gnu/libc-version.h>
char* GetutsNameRelease() {
struct utsname Name;
if(uname(&Name) == 0) {
char* str = (char*)malloc(_UTSNAME_RELEASE_LENGTH);
strncpy(str, Name.release, _UTSNAME_RELEASE_LENGTH);
return str;
}
return NULL;
}
char* GetutsNameVersion() {
struct utsname Name;
if(uname(&Name) == 0) {
char* str = (char*)malloc(_UTSNAME_VERSION_LENGTH);
strncpy(str, Name.version, _UTSNAME_VERSION_LENGTH);
return str;
}
return NULL;
}
*/
import "C"
func getUTSNameRelease() string {
str := C.GetutsNameRelease()
defer C.free(unsafe.Pointer(str))
return C.GoString(str)
}
func getUTSNameVersion() string {
str := C.GetutsNameVersion()
defer C.free(unsafe.Pointer(str))
return C.GoString(str)
}
func getGNULibCVersion() string {
return C.GoString(C.gnu_get_libc_version())
}
func parseUname() {
str := getUTSNameRelease()
if str != "" {
// Example: '4.8.0-34-generic'
// 4: FMajor
// 8: FMinor,
// 0: FServicePackMajor
// 34: FServicePackMinor
str = strings.Replace(str, "-", ".", -1)
strArr := strings.Split(str, ".")
if len(strArr) >= 1 {
OSVersion.Major, _ = strconv.Atoi(strArr[0])
}
if len(strArr) >= 2 {
OSVersion.Minor, _ = strconv.Atoi(strArr[1])
}
if len(strArr) >= 3 {
OSVersion.ServicePackMajor, _ = strconv.Atoi(strArr[2])
}
if len(strArr) >= 4 {
OSVersion.ServicePackMinor, _ = strconv.Atoi(strArr[3])
}
}
// Example: '#36-Ubuntu SMP Wed Dec 21 17:24:18 UTC 2016'
OSVersion.Name = getUTSNameVersion()
}
const (
osReleaseFileName = "/etc/os-release"
prettyName = "PRETTY_NAME="
sVersionStr = "%s %s (Version %d.%d.%d)"
)
func parseOSRelease() {
f, err := os.Open(osReleaseFileName)
if err != nil {
return
}
defer f.Close()
rd := bufio.NewReader(f)
for {
line, err := rd.ReadString('\n')
if err != nil || err == io.EOF {
break
}
// Example: 'PRETTY_NAME="Ubuntu 16.04.1 LTS"'
if strings.HasPrefix(line, prettyName) {
OSVersion.PrettyName = line[strings.Index(line, "=")+2 : strings.LastIndex(line, "\"")]
break
}
}
}
func parseLibcVersion() {
str := getGNULibCVersion()
items := strings.Split(str, ".")
if len(items) >= 1 {
OSVersion.LibCVersionMajor, _ = strconv.Atoi(items[0])
}
if len(items) >= 2 {
OSVersion.LibCVersionMinor, _ = strconv.Atoi(items[1])
}
}
func initOSVersion() {
OSVersion.Platform = PfLinux
parseUname()
parseOSRelease()
parseLibcVersion()
OSVersion.fmtVerString = fmt.Sprintf(sVersionStr, OSVersion.PrettyName, OSVersion.Name, OSVersion.Major, OSVersion.Minor, OSVersion.ServicePackMajor)
}