fleet/orbit/pkg/platform/platform_notwindows.go
Marcos Oviedo ec3f49881f
8009 fleet desktop icon duplication (#8017)
* Adding a new synchronization mechanism between fleet-desktop app and Orbit service. Improved windows service teardown to ensure that fleet-desktop does not get force killed without getting signaled. Improved windows process enumeration to avoid unnecessary delays during windows service start and windows service teardown. Updating windows service to reflect service teardown extra time due to synchronization.
2022-10-13 10:58:37 -03:00

78 lines
2 KiB
Go

//go:build !windows
// +build !windows
package platform
import (
"errors"
"fmt"
"os"
"strings"
"github.com/fleetdm/fleet/v4/orbit/pkg/constant"
gopsutil_process "github.com/shirou/gopsutil/v3/process"
)
// 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 {
return fmt.Errorf("chmod executable directory: %w", err)
}
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 {
return fmt.Errorf("chmod executable: %w", err)
}
return nil
}
// 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")
}
if err := KillProcessByName(constant.DesktopAppExecName); err != nil && !errors.Is(err, ErrProcessNotFound) {
return fmt.Errorf("There was an error kill target process %s: %w", processName, err)
}
return nil
}
// GetProcessByName gets a single process object by its name
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
}