kernel_version_linux.mx raw

   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
  12  // parsed from the syscall.Uname's 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  }
  41