github.com/hernad/nomad@v1.6.112/command/agent/host/unix.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  //go:build !windows
     5  // +build !windows
     6  
     7  package host
     8  
     9  import (
    10  	"strings"
    11  	"syscall"
    12  
    13  	"golang.org/x/sys/unix"
    14  )
    15  
    16  // uname returns the syscall like `uname -a`
    17  func uname() string {
    18  	u := &unix.Utsname{}
    19  	err := unix.Uname(u)
    20  	if err != nil {
    21  		return err.Error()
    22  	}
    23  
    24  	uname := strings.Join([]string{
    25  		nullStr(u.Machine[:]),
    26  		nullStr(u.Nodename[:]),
    27  		nullStr(u.Release[:]),
    28  		nullStr(u.Sysname[:]),
    29  		nullStr(u.Version[:]),
    30  	}, " ")
    31  
    32  	return uname
    33  }
    34  
    35  func etcHosts() string {
    36  	return slurp("/etc/hosts")
    37  }
    38  
    39  func resolvConf() string {
    40  	return slurp("/etc/resolv.conf")
    41  }
    42  
    43  func nullStr(bs []byte) string {
    44  	// find the null byte
    45  	var i int
    46  	var b byte
    47  	for i, b = range bs {
    48  		if b == 0 {
    49  			break
    50  		}
    51  	}
    52  
    53  	return string(bs[:i])
    54  }
    55  
    56  type df struct {
    57  	s *syscall.Statfs_t
    58  }
    59  
    60  func makeDf(path string) (*df, error) {
    61  	var s syscall.Statfs_t
    62  	err := syscall.Statfs(path, &s)
    63  	return &df{s: &s}, err
    64  }
    65  
    66  func (d *df) total() uint64 {
    67  	return d.s.Blocks * uint64(d.s.Bsize)
    68  }
    69  
    70  func (d *df) available() uint64 {
    71  	return d.s.Bavail * uint64(d.s.Bsize)
    72  }