github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/status/log_send_desktop.go (about)

     1  // Copyright 2019 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  //go:build !ios && !android
     5  // +build !ios,!android
     6  
     7  package status
     8  
     9  import (
    10  	"fmt"
    11  	"os"
    12  	"os/exec"
    13  	"regexp"
    14  	"runtime"
    15  	"time"
    16  
    17  	context "golang.org/x/net/context"
    18  
    19  	ps "github.com/keybase/go-ps"
    20  )
    21  
    22  func keybaseProcessList() string {
    23  	ret := ""
    24  	osinfo, err := getOSInfo()
    25  	if err == nil {
    26  		ret += osinfo + "\n\n"
    27  	} else {
    28  		ret += fmt.Sprintf("could not get OS info for platform %s: %s\n\n", runtime.GOOS, err)
    29  	}
    30  
    31  	processes, err := pgrep(keybaseProcessRegexp)
    32  	if err != nil {
    33  		return fmt.Sprintf("error getting processes: %s", err)
    34  	}
    35  	for _, process := range processes {
    36  		path, err := process.Path()
    37  		if err != nil {
    38  			path = "unable to get process path"
    39  		}
    40  		ret += fmt.Sprintf("%s (%+v)\n", path, process)
    41  	}
    42  	return ret
    43  }
    44  
    45  func getOSInfo() (string, error) {
    46  	switch runtime.GOOS {
    47  	case "linux":
    48  		osinfo, err := os.ReadFile("/etc/os-release")
    49  		return string(osinfo), err
    50  	case "darwin": // no ios
    51  		ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    52  		defer cancel()
    53  		osinfo, err := exec.CommandContext(ctx, "/usr/bin/sw_vers").CombinedOutput()
    54  		return string(osinfo), err
    55  	case "windows":
    56  		ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    57  		defer cancel()
    58  		osinfo, err := exec.CommandContext(ctx, "cmd", "/c", "ver").CombinedOutput()
    59  		return string(osinfo), err
    60  	default:
    61  		return "", fmt.Errorf("no OS info for platform %s", runtime.GOOS)
    62  	}
    63  }
    64  
    65  var keybaseProcessRegexp = regexp.MustCompile(`(?i:kbfs|keybase|upd.exe)`)
    66  
    67  func pgrep(matcher *regexp.Regexp) ([]ps.Process, error) {
    68  	processes, err := ps.Processes()
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	var filteredProcesses []ps.Process
    73  	for _, process := range processes {
    74  		if matcher.MatchString(process.Executable()) {
    75  			filteredProcesses = append(filteredProcesses, process)
    76  		}
    77  	}
    78  	return filteredProcesses, nil
    79  }