2021-09-07 16:33:40 +00:00
|
|
|
//go:build !windows
|
|
|
|
// +build !windows
|
2021-04-17 00:26:30 +00:00
|
|
|
|
|
|
|
package platform
|
|
|
|
|
|
|
|
import (
|
2022-10-13 13:58:37 +00:00
|
|
|
"errors"
|
2021-11-22 14:13:26 +00:00
|
|
|
"fmt"
|
2021-04-17 00:26:30 +00:00
|
|
|
"os"
|
2022-10-13 13:58:37 +00:00
|
|
|
"strings"
|
2021-04-17 00:26:30 +00:00
|
|
|
|
2021-08-11 14:02:22 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/orbit/pkg/constant"
|
2022-10-13 13:58:37 +00:00
|
|
|
gopsutil_process "github.com/shirou/gopsutil/v3/process"
|
2021-04-17 00:26:30 +00:00
|
|
|
)
|
|
|
|
|
2023-01-26 21:51:24 +00:00
|
|
|
// ChmodRestrictFile sets the appropriate permissions on a file so it can not be read by everyone
|
|
|
|
// On POSIX this is a normal chmod call.
|
|
|
|
func ChmodRestrictFile(path string) error {
|
|
|
|
if err := os.Chmod(path, constant.DefaultFileMode); err != nil {
|
|
|
|
return fmt.Errorf("chmod restrict file: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-17 00:26:30 +00:00
|
|
|
// ChmodExecutableDirectory sets the appropriate permissions on an executable
|
|
|
|
// file. On POSIX this is a normal chmod call.
|
|
|
|
func ChmodExecutableDirectory(path string) error {
|
|
|
|
if err := os.Chmod(path, constant.DefaultDirMode); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("chmod executable directory: %w", err)
|
2021-04-17 00:26:30 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChmodExecutable sets the appropriate permissions on the parent directory of
|
|
|
|
// an executable file. On POSIX this is a regular chmod call.
|
|
|
|
func ChmodExecutable(path string) error {
|
|
|
|
if err := os.Chmod(path, constant.DefaultExecutableMode); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("chmod executable: %w", err)
|
2021-04-17 00:26:30 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-13 13:58:37 +00:00
|
|
|
|
|
|
|
// SignalProcessBeforeTerminate just force terminate the target process
|
|
|
|
// Signaling the child process before termination is not supported on non-windows OSes
|
|
|
|
func SignalProcessBeforeTerminate(processName string) error {
|
|
|
|
if processName == "" {
|
|
|
|
return errors.New("processName should not be empty")
|
|
|
|
}
|
|
|
|
|
2024-01-29 19:44:50 +00:00
|
|
|
if err := killProcessByName(constant.DesktopAppExecName); err != nil && !errors.Is(err, ErrProcessNotFound) {
|
2022-10-13 13:58:37 +00:00
|
|
|
return fmt.Errorf("There was an error kill target process %s: %w", processName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-29 21:52:55 +00:00
|
|
|
// GetProcessByName gets a single running process object by its name.
|
|
|
|
// Returns ErrProcessNotFound if the process was not found running.
|
2022-10-13 13:58:37 +00:00
|
|
|
func GetProcessByName(name string) (*gopsutil_process.Process, error) {
|
|
|
|
if name == "" {
|
|
|
|
return nil, errors.New("process name should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
processes, err := gopsutil_process.Processes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var foundProcess *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, name) {
|
|
|
|
foundProcess = process
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if foundProcess == nil {
|
|
|
|
return nil, ErrProcessNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return foundProcess, nil
|
|
|
|
}
|
2022-12-13 21:04:49 +00:00
|
|
|
|
|
|
|
func GetSMBiosUUID() (string, UUIDSource, error) {
|
|
|
|
return "", UUIDSourceInvalid, errors.New("not implemented.")
|
|
|
|
}
|
2023-03-08 17:49:03 +00:00
|
|
|
|
|
|
|
// RunUpdateQuirks is a no-op on non-windows platforms
|
|
|
|
func PreUpdateQuirks() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInvalidReparsePoint is a no-op on non-windows platforms
|
|
|
|
func IsInvalidReparsePoint(err error) bool {
|
|
|
|
return false
|
|
|
|
}
|