github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/abi/linux/utsname.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package linux
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  )
    21  
    22  const (
    23  	// UTSLen is the maximum length of strings contained in fields of
    24  	// UtsName.
    25  	UTSLen = 64
    26  )
    27  
    28  // UtsName represents struct utsname, the struct returned by uname(2).
    29  //
    30  // +marshal
    31  type UtsName struct {
    32  	Sysname    [UTSLen + 1]byte
    33  	Nodename   [UTSLen + 1]byte
    34  	Release    [UTSLen + 1]byte
    35  	Version    [UTSLen + 1]byte
    36  	Machine    [UTSLen + 1]byte
    37  	Domainname [UTSLen + 1]byte
    38  }
    39  
    40  // utsNameString converts a UtsName entry to a string without NULs.
    41  func utsNameString(s [UTSLen + 1]byte) string {
    42  	// The NUL bytes will remain even in a cast to string. We must
    43  	// explicitly strip them.
    44  	return string(bytes.TrimRight(s[:], "\x00"))
    45  }
    46  
    47  func (u UtsName) String() string {
    48  	return fmt.Sprintf("{Sysname: %s, Nodename: %s, Release: %s, Version: %s, Machine: %s, Domainname: %s}",
    49  		utsNameString(u.Sysname), utsNameString(u.Nodename), utsNameString(u.Release),
    50  		utsNameString(u.Version), utsNameString(u.Machine), utsNameString(u.Domainname))
    51  }