mirror of
https://github.com/fleetdm/fleet
synced 2026-05-06 06:48:54 +00:00
For https://github.com/fleetdm/fleet/issues/9943 This will help us avoid issues like this where the log message never worked right: https://github.com/fleetdm/fleet/pull/28296#discussion_r2047505191 Most of the changes are no-op type changes like removing unneeded typecast or disabling gosec on reviewed lines of code # Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) - [x] Added/updated automated tests - [x] A detailed QA plan exists on the associated ticket (if it isn't there, work with the product group's QA engineer to add it) - [x] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [x] Make sure fleetd is compatible with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/fleetd-development-and-release-strategy.md)). - [x] Orbit runs on macOS, Linux and Windows. Check if the orbit feature/bugfix should only apply to one platform (`runtime.GOOS`). - [x] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. - [x] Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).
85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
package platform
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
gopsutil_process "github.com/shirou/gopsutil/v3/process"
|
|
)
|
|
|
|
var (
|
|
ErrProcessNotFound = errors.New("process not found")
|
|
ErrComChannelNotFound = errors.New("comm channel not found")
|
|
)
|
|
|
|
type UUIDSource string
|
|
|
|
const (
|
|
UUIDSourceInvalid = "UUID_Source_Invalid"
|
|
UUIDSourceWMI = "UUID_Source_WMI"
|
|
UUIDSourceHardware = "UUID_Source_Hardware"
|
|
)
|
|
|
|
// getProcessesByName returns all the running processes with the given prefix in their name.
|
|
func getProcessesByName(namePrefix string) ([]*gopsutil_process.Process, error) {
|
|
if namePrefix == "" {
|
|
return nil, errors.New("process name prefix should not be empty")
|
|
}
|
|
|
|
processes, err := gopsutil_process.Processes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var foundProcesses []*gopsutil_process.Process
|
|
for _, process := range processes {
|
|
processName, err := process.Name()
|
|
if err != nil {
|
|
// No need to print errors here as this method might file for system processes
|
|
continue
|
|
}
|
|
|
|
if strings.HasPrefix(processName, namePrefix) {
|
|
foundProcesses = append(foundProcesses, process)
|
|
}
|
|
}
|
|
|
|
return foundProcesses, nil
|
|
}
|
|
|
|
// Process holds basic information of a process.
|
|
type Process struct {
|
|
// Name is the name of the process.
|
|
Name string
|
|
// PID is the process identifier.
|
|
PID int32
|
|
}
|
|
|
|
// KillAllProcessByName kills all the running processes with the given prefix in their name.
|
|
// It returns the processes that were killed. It returns `nil, nil` if there were no processes
|
|
// running with such name prefix.
|
|
func KillAllProcessByName(namePrefix string) ([]Process, error) {
|
|
if namePrefix == "" {
|
|
return nil, errors.New("process name prefix should not be empty")
|
|
}
|
|
|
|
foundProcesses, err := getProcessesByName(namePrefix)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get processes by name: %w", err)
|
|
}
|
|
|
|
var killedProcesses []Process
|
|
for _, foundProcess := range foundProcesses {
|
|
processName, _ := foundProcess.Name()
|
|
if err := foundProcess.Kill(); err != nil {
|
|
return nil, fmt.Errorf("kill process %d: %w", foundProcess.Pid, err)
|
|
}
|
|
killedProcesses = append(killedProcesses, Process{
|
|
Name: processName,
|
|
PID: foundProcess.Pid,
|
|
})
|
|
}
|
|
|
|
return killedProcesses, nil
|
|
}
|