github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/helper/users/lookup_unix.go (about)

     1  //go:build unix
     2  
     3  package users
     4  
     5  import (
     6  	"fmt"
     7  	"os/user"
     8  	"strconv"
     9  )
    10  
    11  var (
    12  	// nobody is a cached copy of the nobody user, which is going to be looked-up
    13  	// frequently and is unlikely to be modified on the underlying system.
    14  	nobody user.User
    15  
    16  	// nobodyUID is a cached copy of the int value of the nobody user's UID.
    17  	nobodyUID uint32
    18  
    19  	// nobodyGID int is a cached copy of the int value of the nobody users's GID.
    20  	nobodyGID uint32
    21  )
    22  
    23  // Nobody returns User data for the "nobody" user on the system, bypassing the
    24  // locking / file read / NSS lookup.
    25  func Nobody() user.User {
    26  	return nobody
    27  }
    28  
    29  // NobodyIDs returns the integer UID and GID of the nobody user.
    30  func NobodyIDs() (uint32, uint32) {
    31  	return nobodyUID, nobodyGID
    32  }
    33  
    34  func init() {
    35  	u, err := Lookup("nobody")
    36  	if err != nil {
    37  		panic(fmt.Sprintf("failed to lookup nobody user: %v", err))
    38  	}
    39  	nobody = *u
    40  
    41  	uid, err := strconv.Atoi(u.Uid)
    42  	if err != nil {
    43  		panic(fmt.Sprintf("failed to parse nobody UID: %v", err))
    44  	}
    45  
    46  	gid, err := strconv.Atoi(u.Gid)
    47  	if err != nil {
    48  		panic(fmt.Sprintf("failed to parse nobody GID: %v", err))
    49  	}
    50  
    51  	nobodyUID, nobodyGID = uint32(uid), uint32(gid)
    52  }