github.com/xyproto/orbiton/v2@v2.65.12-0.20240516144430-e10a419274ec/process.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  )
     8  
     9  // parentIsMan checks if the parent process is an executable named "man".
    10  func parentIsMan() bool {
    11  	parentPath, err := getProcPath(os.Getppid(), "exe")
    12  	if err != nil {
    13  		return false
    14  	}
    15  	baseName := filepath.Base(parentPath)
    16  	return baseName == "man"
    17  }
    18  
    19  // parentCommand returns the command line of the parent process or an empty string if an error occurs.
    20  func parentCommand() string {
    21  	commandString, err := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", os.Getppid()))
    22  	if err != nil {
    23  		return ""
    24  	}
    25  	return string(commandString)
    26  }
    27  
    28  // getProcPath resolves and returns the specified path (e.g., "exe", "cwd") for the process identified by pid.
    29  // It returns an error if the path cannot be resolved.
    30  func getProcPath(pid int, suffix string) (string, error) {
    31  	path, err := os.Readlink(fmt.Sprintf("/proc/%d/%s", pid, suffix))
    32  	if err != nil {
    33  		return "", err
    34  	}
    35  	return path, nil
    36  }