github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/agent/host/unix.go (about)

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