github.com/iDigitalFlame/xmt@v0.5.4/cmd/y_linux_crypt.go (about)

     1  //go:build !windows && !js && crypt
     2  // +build !windows,!js,crypt
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package cmd
    21  
    22  import (
    23  	"os"
    24  	"sort"
    25  	"strconv"
    26  
    27  	"github.com/iDigitalFlame/xmt/util/crypt"
    28  )
    29  
    30  // Processes attempts to gather the current running Processes and returns them
    31  // as a slice of ProcessInfo structs, otherwise any errors are returned.
    32  func Processes() ([]ProcessInfo, error) {
    33  	f, err := os.OpenFile(crypt.Get(11), 0, 0) // /proc/
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	l, err := f.Readdir(0)
    38  	if f.Close(); err != nil {
    39  		return nil, err
    40  	}
    41  	if len(l) == 0 {
    42  		return nil, nil
    43  	}
    44  	var (
    45  		v uint64
    46  		p uint32
    47  		n string
    48  		r = make(processList, 0, len(l)/2)
    49  	)
    50  	for i := range l {
    51  		if !l[i].IsDir() {
    52  			continue
    53  		}
    54  		if n = l[i].Name(); len(n) < 1 || n[0] < 48 || n[0] > 57 {
    55  			continue
    56  		}
    57  		if v, err = strconv.ParseUint(n, 10, 32); err != nil {
    58  			continue
    59  		}
    60  		n, p = readProcStats(
    61  			crypt.Get(11) + // /proc/
    62  				n +
    63  				crypt.Get(12), // /status
    64  		)
    65  		if len(n) == 0 {
    66  			continue
    67  		}
    68  		r = append(r, ProcessInfo{
    69  			PID:  uint32(v),
    70  			PPID: p,
    71  			Name: n,
    72  			User: getProcUser(crypt.Get(11) + n), // /proc/
    73  		})
    74  	}
    75  	sort.Sort(r)
    76  	return r, nil
    77  }