Skip to content

Commit 03a554f

Browse files
committed
uname: add
This is a tiny package to check the Linux kernel version. It is taken from the golang sources (src/internal/syscall/unix), with the irrelevant parts, including FreeBSD and Solaris implementations, removed. The code is covered by the "BSD 3-clause" license, which is compatible with Apache license. The history of the code before the fork can be seen here (oldest first): - https://go-review.googlesource.com/c/go/+/424896 - https://go-review.googlesource.com/c/go/+/427675 - https://go-review.googlesource.com/c/go/+/427676 This is aimed to replace the relevant containerd package (github.com/containerd/containerd/pkg/kernelversion) and its forks. Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 9dc3a90 commit 03a554f

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

uname/LICENSE.BSD

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2009 The Go Authors.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
* Neither the name of Google LLC nor the names of its
14+
contributors may be used to endorse or promote products derived from
15+
this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

uname/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/moby/sys/uname
2+
3+
go 1.18

uname/kernel_version_linux.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2022 The Go 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.BSD file.
4+
5+
package uname
6+
7+
import (
8+
"syscall"
9+
)
10+
11+
// KernelVersion returns major and minor kernel version numbers
12+
// parsed from the [syscall.Uname] Release field, or (0, 0) if
13+
// the version can't be obtained or parsed.
14+
func KernelVersion() (major, minor int) {
15+
var uname syscall.Utsname
16+
if err := syscall.Uname(&uname); err != nil {
17+
return
18+
}
19+
20+
var (
21+
values [2]int
22+
value, vi int
23+
)
24+
for _, c := range uname.Release {
25+
if '0' <= c && c <= '9' {
26+
value = (value * 10) + int(c-'0')
27+
} else {
28+
// Note that we're assuming N.N.N here.
29+
// If we see anything else, we are likely to mis-parse it.
30+
values[vi] = value
31+
vi++
32+
if vi >= len(values) {
33+
break
34+
}
35+
value = 0
36+
}
37+
}
38+
39+
return values[0], values[1]
40+
}

uname/kernel_version_other.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2022 The Go 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.BSD file.
4+
5+
//go:build !linux
6+
7+
package uname
8+
9+
func KernelVersion() (major int, minor int) {
10+
return 0, 0
11+
}

0 commit comments

Comments
 (0)