Files
vHost/backend/internal/isolation/pureftp_process_linux.go
T

64 lines
1.3 KiB
Go

//go:build linux
package isolation
import (
"context"
"errors"
"os"
"path/filepath"
"strconv"
"syscall"
"time"
)
func pureFTPDaemonPIDs(binaryPath string) ([]int, error) {
expected, err := filepath.EvalSymlinks(binaryPath)
if err != nil {
return nil, err
}
entries, err := os.ReadDir("/proc")
if err != nil {
return nil, err
}
pids := make([]int, 0, 2)
for _, entry := range entries {
pid, parseErr := strconv.Atoi(entry.Name())
if parseErr != nil || pid <= 1 || pid == os.Getpid() {
continue
}
executable, readErr := os.Readlink("/proc/" + entry.Name() + "/exe")
if readErr == nil && filepath.Clean(executable) == filepath.Clean(expected) {
pids = append(pids, pid)
}
}
return pids, nil
}
func terminatePureFTPDaemons(ctx context.Context, binaryPath string) error {
pids, err := pureFTPDaemonPIDs(binaryPath)
if err != nil {
return err
}
for _, pid := range pids {
_ = syscall.Kill(pid, syscall.SIGTERM)
}
if len(pids) > 0 {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(750 * time.Millisecond):
}
}
pids, err = pureFTPDaemonPIDs(binaryPath)
if err != nil {
return err
}
for _, pid := range pids {
if killErr := syscall.Kill(pid, syscall.SIGKILL); killErr != nil && !errors.Is(killErr, syscall.ESRCH) {
return killErr
}
}
return nil
}