github.com/hashicorp/go-plugin@v1.6.0/internal/cmdrunner/notes_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 cmdrunner
     8  
     9  import (
    10  	"debug/elf"
    11  	"debug/macho"
    12  	"debug/pe"
    13  	"fmt"
    14  	"os"
    15  	"os/user"
    16  	"runtime"
    17  	"strconv"
    18  	"syscall"
    19  )
    20  
    21  // additionalNotesAboutCommand tries to get additional information about a command that might help diagnose
    22  // why it won't run correctly. It runs as a best effort only.
    23  func additionalNotesAboutCommand(path string) string {
    24  	notes := ""
    25  	stat, err := os.Stat(path)
    26  	if err != nil {
    27  		return notes
    28  	}
    29  
    30  	notes += "\nAdditional notes about plugin:\n"
    31  	notes += fmt.Sprintf("  Path: %s\n", path)
    32  	notes += fmt.Sprintf("  Mode: %s\n", stat.Mode())
    33  	statT, ok := stat.Sys().(*syscall.Stat_t)
    34  	if ok {
    35  		currentUsername := "?"
    36  		if u, err := user.LookupId(strconv.FormatUint(uint64(os.Getuid()), 10)); err == nil {
    37  			currentUsername = u.Username
    38  		}
    39  		currentGroup := "?"
    40  		if g, err := user.LookupGroupId(strconv.FormatUint(uint64(os.Getgid()), 10)); err == nil {
    41  			currentGroup = g.Name
    42  		}
    43  		username := "?"
    44  		if u, err := user.LookupId(strconv.FormatUint(uint64(statT.Uid), 10)); err == nil {
    45  			username = u.Username
    46  		}
    47  		group := "?"
    48  		if g, err := user.LookupGroupId(strconv.FormatUint(uint64(statT.Gid), 10)); err == nil {
    49  			group = g.Name
    50  		}
    51  		notes += fmt.Sprintf("  Owner: %d [%s] (current: %d [%s])\n", statT.Uid, username, os.Getuid(), currentUsername)
    52  		notes += fmt.Sprintf("  Group: %d [%s] (current: %d [%s])\n", statT.Gid, group, os.Getgid(), currentGroup)
    53  	}
    54  
    55  	if elfFile, err := elf.Open(path); err == nil {
    56  		defer elfFile.Close()
    57  		notes += fmt.Sprintf("  ELF architecture: %s (current architecture: %s)\n", elfFile.Machine, runtime.GOARCH)
    58  	} else if machoFile, err := macho.Open(path); err == nil {
    59  		defer machoFile.Close()
    60  		notes += fmt.Sprintf("  MachO architecture: %s (current architecture: %s)\n", machoFile.Cpu, runtime.GOARCH)
    61  	} else if peFile, err := pe.Open(path); err == nil {
    62  		defer peFile.Close()
    63  		machine, ok := peTypes[peFile.Machine]
    64  		if !ok {
    65  			machine = "unknown"
    66  		}
    67  		notes += fmt.Sprintf("  PE architecture: %s (current architecture: %s)\n", machine, runtime.GOARCH)
    68  	}
    69  	return notes
    70  }