Source file src/internal/syscall/unix/kernel_version_linux.go

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

View as plain text