-
Notifications
You must be signed in to change notification settings - Fork 48
Add uname package #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kolyshkin
wants to merge
1
commit into
moby:main
Choose a base branch
from
kolyshkin:add-uname
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add uname package #204
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| Copyright 2009 The Go Authors. | ||
|
|
||
| Redistribution and use in source and binary forms, with or without | ||
| modification, are permitted provided that the following conditions are | ||
| met: | ||
|
|
||
| * Redistributions of source code must retain the above copyright | ||
| notice, this list of conditions and the following disclaimer. | ||
| * Redistributions in binary form must reproduce the above | ||
| copyright notice, this list of conditions and the following disclaimer | ||
| in the documentation and/or other materials provided with the | ||
| distribution. | ||
| * Neither the name of Google LLC nor the names of its | ||
| contributors may be used to endorse or promote products derived from | ||
| this software without specific prior written permission. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module github.com/moby/sys/uname | ||
|
|
||
| go 1.18 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // SPDX-FileCopyrightText: 2025 The moby/sys Authors. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Package uname provides a simple way to check the kernel version. | ||
| // Currently it only supports Linux. | ||
| package uname | ||
|
|
||
| // KernelVersion returns major and minor kernel version numbers | ||
| // parsed from the [syscall.Uname] Release field, or (0, 0) if | ||
| // the version can't be obtained or parsed. | ||
| func KernelVersion() (major, minor int) { | ||
| return kernelVersion() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // SPDX-FileCopyrightText: 2025 The Go Authors | ||
| // SPDX-FileCopyrightText: 2025 The moby/sys Authors. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Copyright 2025 The Go Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE.BSD file. | ||
|
|
||
| package uname | ||
|
|
||
| // KernelVersionGE checks if the running kernel version | ||
| // is greater than or equal to the provided version. | ||
| func KernelVersionGE(x, y int) bool { | ||
| xx, yy := KernelVersion() | ||
|
|
||
| return xx > x || (xx == x && yy >= y) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // SPDX-FileCopyrightText: 2025 The Go Authors | ||
| // SPDX-FileCopyrightText: 2025 The moby/sys Authors. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Copyright 2025 The Go Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE.BSD file. | ||
|
|
||
| package uname_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/moby/sys/uname" | ||
| ) | ||
|
|
||
| func TestKernelVersionGE(t *testing.T) { | ||
| major, minor := uname.KernelVersion() | ||
| t.Logf("Running on kernel %d.%d", major, minor) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| x, y int | ||
| want bool | ||
| }{ | ||
| { | ||
| name: "current version equals itself", | ||
| x: major, | ||
| y: minor, | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "older major version", | ||
| x: major - 1, | ||
| y: 0, | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "same major, older minor version", | ||
| x: major, | ||
| y: minor - 1, | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "newer major version", | ||
| x: major + 1, | ||
| y: 0, | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "same major, newer minor version", | ||
| x: major, | ||
| y: minor + 1, | ||
| want: false, | ||
| }, | ||
| { | ||
| name: "min version (0.0)", | ||
| x: 0, | ||
| y: 0, | ||
| want: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := uname.KernelVersionGE(tt.x, tt.y) | ||
| if got != tt.want { | ||
| t.Errorf("KernelVersionGE(%d, %d): got %v, want %v", tt.x, tt.y, got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // SPDX-FileCopyrightText: 2022 The Go Authors | ||
| // SPDX-FileCopyrightText: 2025 The moby/sys Authors. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Copyright 2022 The Go Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE.BSD file. | ||
|
|
||
| package uname | ||
|
|
||
| import ( | ||
| "syscall" | ||
| ) | ||
|
|
||
| func kernelVersion() (major, minor int) { | ||
| var uname syscall.Utsname | ||
| if err := syscall.Uname(&uname); err != nil { | ||
| return | ||
| } | ||
|
|
||
| var ( | ||
| values [2]int | ||
| value, vi int | ||
| ) | ||
| for _, c := range uname.Release { | ||
| if '0' <= c && c <= '9' { | ||
| value = (value * 10) + int(c-'0') | ||
| } else { | ||
| // Note that we're assuming N.N.N here. | ||
| // If we see anything else, we are likely to mis-parse it. | ||
| values[vi] = value | ||
| vi++ | ||
| if vi >= len(values) { | ||
| break | ||
| } | ||
| value = 0 | ||
| } | ||
| } | ||
|
|
||
| return values[0], values[1] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // SPDX-FileCopyrightText: 2022 The Go Authors | ||
| // SPDX-FileCopyrightText: 2025 The moby/sys Authors. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Copyright 2022 The Go Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE.BSD file. | ||
|
|
||
| //go:build !linux | ||
|
|
||
| package uname | ||
|
|
||
| func kernelVersion() (major int, minor int) { | ||
| return 0, 0 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // SPDX-FileCopyrightText: 2025 The moby/sys Authors. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package uname_test | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/moby/sys/uname" | ||
| ) | ||
|
|
||
| func TestKernelVersion(t *testing.T) { | ||
| x, y := uname.KernelVersion() | ||
| t.Logf("KernelVersion: %d.%d (GOOS: %s)", x, y, runtime.GOOS) | ||
| switch runtime.GOOS { | ||
| case "linux": | ||
| // Go requires Linux >= 2.x. | ||
| if x < 2 { | ||
| t.Errorf("want major >= 2, got %d", x) | ||
| } | ||
| // Sanity check. | ||
| if y < 0 { | ||
| t.Errorf("want minor >= 0, got %d", y) | ||
| } | ||
| default: | ||
| if x != 0 || y != 0 { | ||
| t.Fatalf("want 0.0, got %d.%d", x, y) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how much changes we want to make to the forked files, but if we're open to making changes, then I usually like calling non-exported functions when used internally, and have a single exported "implementation" (which can be backed by a non-exported platform-specific one); it can make it more clear / discoverable what code is used, and what not., and having a single exported function gives a good place to maintain the GoDoc.
So along these lines;
The platform-agnostic file would have the exported
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done